Skip to content

Commit

Permalink
支持json优化
Browse files Browse the repository at this point in the history
  • Loading branch information
tatezhang committed Jun 15, 2017
1 parent ae9f403 commit a6530c0
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/client/pebble_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ Stat* PebbleClient::GetStat() {
return NULL;
}

int32_t PebbleClient::MakeCoroutine(const cxx::function<void()>& routine, bool start_immediately) {
int32_t PebbleClient::MakeCoroutine(const cxx::function<void()>& routine) {
if (!m_coroutine_schedule) {
PLOG_ERROR("coroutine schedule is null");
return -1;
Expand All @@ -436,7 +436,7 @@ int32_t PebbleClient::MakeCoroutine(const cxx::function<void()>& routine, bool s
}

task->Init(routine);
int64_t coid = task->Start(start_immediately);
int64_t coid = task->Start(true);

return coid < 0 ? -1 : 0;
}
Expand Down
3 changes: 1 addition & 2 deletions src/client/pebble_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,9 @@ class PebbleClient {

/// @brief 创建一个协程,并决定是否立即执行
/// @param routine 协程执行入口函数
/// @param start_immediately 默认立即执行,设置为false时等下一次update时执行
/// @return 0 成功
/// @return <0 失败
int32_t MakeCoroutine(const cxx::function<void()>& routine, bool start_immediately = true);
int32_t MakeCoroutine(const cxx::function<void()>& routine);

private:
int32_t ProcessMessage();
Expand Down
60 changes: 30 additions & 30 deletions src/framework/dr/protocol/rapidjson_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,19 @@ class TraverseContext {

virtual ~TraverseContext() {}

virtual const Value &next() = 0;
virtual const RawValue &next() = 0;

virtual bool has_more() = 0;
};


class ObjectTraverseContext : public TraverseContext {
public:
ObjectTraverseContext(Value::ConstMemberIterator itr, Value::ConstMemberIterator end) : itr_(itr), end_(end), value_(false) {}
ObjectTraverseContext(const Value &v) : itr_(v.MemberBegin()), end_(v.MemberEnd()), value_(false) {}
ObjectTraverseContext(RawValue::ConstMemberIterator itr, RawValue::ConstMemberIterator end) : itr_(itr), end_(end), value_(false) {}
ObjectTraverseContext(const RawValue &v) : itr_(v.MemberBegin()), end_(v.MemberEnd()), value_(false) {}

const Value &next() {
const Value & ret = value_ ? itr_++->value : itr_->name;
const RawValue &next() {
const RawValue & ret = value_ ? itr_++->value : itr_->name;
value_ = !value_;
return ret;
}
Expand All @@ -151,20 +151,20 @@ class ObjectTraverseContext : public TraverseContext {
}

private:
Value::ConstMemberIterator itr_;
RawValue::ConstMemberIterator itr_;

Value::ConstMemberIterator end_;
RawValue::ConstMemberIterator end_;

bool value_;
};


class ArrayTraverseContext : public TraverseContext {
public:
ArrayTraverseContext(Value::ConstValueIterator itr, Value::ConstValueIterator end) : itr_(itr), end_(end) {}
ArrayTraverseContext(const Value &v) : itr_(v.Begin()), end_(v.End()) {}
ArrayTraverseContext(RawValue::ConstValueIterator itr, RawValue::ConstValueIterator end) : itr_(itr), end_(end) {}
ArrayTraverseContext(const RawValue &v) : itr_(v.Begin()), end_(v.End()) {}

const Value &next() {
const RawValue &next() {
return *(itr_++);
}

Expand All @@ -173,16 +173,16 @@ class ArrayTraverseContext : public TraverseContext {
}

private:
Value::ConstValueIterator itr_;
RawValue::ConstValueIterator itr_;

Value::ConstValueIterator end_;
RawValue::ConstValueIterator end_;
};

class ValueTraverseContext : public TraverseContext {
public:
ValueTraverseContext(const Value &value) : value_(value), consumed_(false) {}
ValueTraverseContext(const RawValue &value) : value_(value), consumed_(false) {}

const Value &next() {
const RawValue &next() {
consumed_ = true;
return value_;
}
Expand All @@ -192,7 +192,7 @@ class ValueTraverseContext : public TraverseContext {
}

private:
const Value &value_;
const RawValue &value_;

bool consumed_;
};
Expand Down Expand Up @@ -346,7 +346,7 @@ uint32_t TRAPIDJSONProtocol::writeBinary(const std::string& str) {
return write_to_transport();
}

inline const Value &TRAPIDJSONProtocol::get_value(uint32_t &readed, bool no_parse) {
inline const RawValue &TRAPIDJSONProtocol::get_value(uint32_t &readed, bool no_parse) {
if (contexts_stack_.size() > 0) {
if (contexts_stack_.top()->has_more()) {
readed = 0;
Expand Down Expand Up @@ -374,14 +374,14 @@ uint32_t TRAPIDJSONProtocol::readMessageBegin(std::string& name,
}

uint32_t readed = 0;
const Value &root = get_value(readed);
const RawValue &root = get_value(readed);

if (!root.IsArray()) {
throw TProtocolException(TProtocolException::INVALID_DATA,
"Expected Array for a message.");
}

Value::ConstValueIterator itr = root.Begin();
RawValue::ConstValueIterator itr = root.Begin();

if (!(*itr).IsUint() || (*itr).GetUint() != kThriftVersion1) {
throw TProtocolException(TProtocolException::BAD_VERSION,
Expand Down Expand Up @@ -427,7 +427,7 @@ uint32_t TRAPIDJSONProtocol::readMessageEnd() {

uint32_t TRAPIDJSONProtocol::readStructBegin(std::string& name) {
uint32_t readed = 0;
const Value &obj = get_value(readed);
const RawValue &obj = get_value(readed);
if (!obj.IsObject()) {
throw TProtocolException(TProtocolException::INVALID_DATA,
"Expected Object for a struct.");
Expand All @@ -450,7 +450,7 @@ uint32_t TRAPIDJSONProtocol::readFieldBegin(std::string& name,
if (!contexts_stack_.top()->has_more()) {
fieldType = pebble::dr::protocol::T_STOP;
} else {
const Value &field_name = get_value(readed, true);
const RawValue &field_name = get_value(readed, true);
result += readed;
if (!field_name.IsString()) {
throw TProtocolException(TProtocolException::INVALID_DATA,
Expand All @@ -472,7 +472,7 @@ uint32_t TRAPIDJSONProtocol::readMapBegin(TType& keyType,
uint32_t& size) {
uint32_t result = 0;

const Value& map = get_value(result);
const RawValue& map = get_value(result);
if (!map.IsArray()) {
throw TProtocolException(TProtocolException::INVALID_DATA,
"Expected Array for map.");
Expand All @@ -492,7 +492,7 @@ uint32_t TRAPIDJSONProtocol::readMapEnd() {
uint32_t TRAPIDJSONProtocol::readListBegin(TType& elemType,
uint32_t& size) {
uint32_t result = 0;
const Value &list = get_value(result);
const RawValue &list = get_value(result);
if (!list.IsArray()) {
throw TProtocolException(TProtocolException::INVALID_DATA,
"invalid list format.");
Expand Down Expand Up @@ -523,7 +523,7 @@ uint32_t TRAPIDJSONProtocol::readSetEnd() {

uint32_t TRAPIDJSONProtocol::readBool(bool& value) {
uint32_t readed = 0;
const Value &data = get_value(readed);
const RawValue &data = get_value(readed);
if (!data.IsBool()) {
throw TProtocolException(TProtocolException::INVALID_DATA,
"Expected Bool.");
Expand All @@ -536,7 +536,7 @@ uint32_t TRAPIDJSONProtocol::readBool(bool& value) {
// as a text type instead of an integer type
uint32_t TRAPIDJSONProtocol::readByte(int8_t& byte) {
uint32_t readed = 0;
const Value &data = get_value(readed);
const RawValue &data = get_value(readed);
if (!data.IsInt()) {
throw TProtocolException(TProtocolException::INVALID_DATA,
"Expected Int.");
Expand All @@ -547,7 +547,7 @@ uint32_t TRAPIDJSONProtocol::readByte(int8_t& byte) {

uint32_t TRAPIDJSONProtocol::readI16(int16_t& i16) {
uint32_t readed = 0;
const Value &data = get_value(readed);
const RawValue &data = get_value(readed);
if (!data.IsInt()) {
throw TProtocolException(TProtocolException::INVALID_DATA,
"Expected Int.");
Expand All @@ -558,7 +558,7 @@ uint32_t TRAPIDJSONProtocol::readI16(int16_t& i16) {

uint32_t TRAPIDJSONProtocol::readI32(int32_t& i32) {
uint32_t readed = 0;
const Value &data = get_value(readed);
const RawValue &data = get_value(readed);
if (!data.IsInt()) {
throw TProtocolException(TProtocolException::INVALID_DATA,
"Expected Int.");
Expand All @@ -569,7 +569,7 @@ uint32_t TRAPIDJSONProtocol::readI32(int32_t& i32) {

uint32_t TRAPIDJSONProtocol::readI64(int64_t& i64) {
uint32_t readed = 0;
const Value &data = get_value(readed);
const RawValue &data = get_value(readed);
if (!data.IsInt64()) {
throw TProtocolException(TProtocolException::INVALID_DATA,
"Expected Int64.");
Expand All @@ -580,7 +580,7 @@ uint32_t TRAPIDJSONProtocol::readI64(int64_t& i64) {

uint32_t TRAPIDJSONProtocol::readDouble(double& dub) {
uint32_t readed = 0;
const Value &data = get_value(readed);
const RawValue &data = get_value(readed);
if (!data.IsDouble()) {
throw TProtocolException(TProtocolException::INVALID_DATA,
"Expected Double.");
Expand All @@ -591,7 +591,7 @@ uint32_t TRAPIDJSONProtocol::readDouble(double& dub) {

uint32_t TRAPIDJSONProtocol::readString(std::string &str) {
uint32_t readed = 0;
const Value &data = get_value(readed);
const RawValue &data = get_value(readed);
if (!data.IsString()) {
throw TProtocolException(TProtocolException::INVALID_DATA,
"Expected String.");
Expand All @@ -602,7 +602,7 @@ uint32_t TRAPIDJSONProtocol::readString(std::string &str) {

uint32_t TRAPIDJSONProtocol::readBinary(std::string &str) {
uint32_t result = 0;
const Value &tmp = get_value(result);
const RawValue &tmp = get_value(result);
if (!tmp.IsString()) {
throw TProtocolException(TProtocolException::INVALID_DATA,
"Expected String.");
Expand Down
9 changes: 7 additions & 2 deletions src/framework/dr/protocol/rapidjson_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@
#define PEBBLE_DR_PROTOCOL_RAPIDJSONPROTOCOL_H

#include "framework/dr/protocol/virtual_protocol.h"
#define RAPIDJSON_ASSERT(x) if (x) {}
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/document.h"


#include <stack>

namespace pebble { namespace dr { namespace protocol {

class TraverseContext;

typedef rapidjson::GenericValue<rapidjson::ASCII<> > RawValue;


struct LookaheadStream {
typedef uint8_t Ch;

Expand Down Expand Up @@ -231,7 +236,7 @@ class TRAPIDJSONProtocol : public TVirtualProtocol<TRAPIDJSONProtocol> {

rapidjson::Writer<rapidjson::StringBuffer> writer_;

rapidjson::Document document_;
rapidjson::GenericDocument<rapidjson::ASCII<> > document_;

LookaheadStream lookahead_;

Expand All @@ -245,7 +250,7 @@ class TRAPIDJSONProtocol : public TVirtualProtocol<TRAPIDJSONProtocol> {
return sb_size;
}

inline const rapidjson::Value &get_value(uint32_t &len, bool no_parse = false);
inline const RawValue &get_value(uint32_t &len, bool no_parse = false);
};

/**
Expand Down
4 changes: 2 additions & 2 deletions src/server/pebble_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ int32_t PebbleServer::InitControlService() {
return 0;
}

int32_t PebbleServer::MakeCoroutine(const cxx::function<void()>& routine, bool start_immediately) {
int32_t PebbleServer::MakeCoroutine(const cxx::function<void()>& routine) {
if (!m_coroutine_schedule) {
PLOG_ERROR("coroutine schedule is null");
return -1;
Expand All @@ -1037,7 +1037,7 @@ int32_t PebbleServer::MakeCoroutine(const cxx::function<void()>& routine, bool s
}

task->Init(routine);
int64_t coid = task->Start(start_immediately);
int64_t coid = task->Start(true);

return coid < 0 ? -1 : 0;
}
Expand Down
3 changes: 1 addition & 2 deletions src/server/pebble_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,9 @@ class PebbleServer {

/// @brief 创建一个协程,并决定是否立即执行
/// @param routine 协程执行入口函数
/// @param start_immediately 默认立即执行,设置为false时等下一次update时执行
/// @return 0 成功
/// @return <0 失败
int32_t MakeCoroutine(const cxx::function<void()>& routine, bool start_immediately = true);
int32_t MakeCoroutine(const cxx::function<void()>& routine);

/// @brief 注册控制命令
/// @param cmd 用户自定义命令
Expand Down

0 comments on commit a6530c0

Please sign in to comment.