diff --git a/include/pika_binlog_transverter.h b/include/pika_binlog_transverter.h index d6a0de5ec1..2231914a9e 100644 --- a/include/pika_binlog_transverter.h +++ b/include/pika_binlog_transverter.h @@ -11,12 +11,14 @@ #include #include -/* - * ***********************************************Type First Binlog Item - * Format*********************************************** | | | | | - * | | | | 2 Bytes 4 Bytes 4 Bytes 8 Bytes 4 - * Bytes 8 Bytes 4 Bytes content length Bytes - * +/******************* Type First Binlog Item Format ****************** + * +-----------------------------------------------------------------+ + * | Type (2 bytes) | Create Time (4 bytes) | Term Id (4 bytes) | + * |-----------------------------------------------------------------| + * | Logic Id (8 bytes) | File Num (4 bytes) | Offset (8 bytes) | + * |-----------------------------------------------------------------| + * | Content Length (4 bytes) | Content (content length bytes) | + * +-----------------------------------------------------------------+ */ #define BINLOG_ENCODE_LEN 34 diff --git a/src/pika_binlog_transverter.cc b/src/pika_binlog_transverter.cc index e6bc5b4129..90e800eb5e 100644 --- a/src/pika_binlog_transverter.cc +++ b/src/pika_binlog_transverter.cc @@ -75,7 +75,7 @@ std::string PikaBinlogTransverter::BinlogEncode(BinlogType type, uint32_t exec_t bool PikaBinlogTransverter::BinlogDecode(BinlogType type, const std::string& binlog, BinlogItem* binlog_item) { uint16_t binlog_type = 0; uint32_t content_length = 0; - std::string binlog_str = binlog; + Slice binlog_str = binlog; pstd::GetFixed16(&binlog_str, &binlog_type); if (binlog_type != type) { LOG(ERROR) << "Binlog Item type error, expect type:" << type << " actualy type: " << binlog_type; @@ -98,11 +98,15 @@ bool PikaBinlogTransverter::BinlogDecode(BinlogType type, const std::string& bin } /* - * *************************************************Type First Binlog Item - * Format************************************************** | | | | - * | | | | | | 2 Bytes | 4 Bytes | 4 Bytes | 8 - * Bytes | 4 Bytes | 8 Bytes | 4 Bytes | content length Bytes | - * |---------------------------------------------- 34 Bytes -----------------------------------------------| +/******************* Type First Binlog Item Format ****************** + * +-----------------------------------------------------------------+ + * | Type (2 bytes) | Create Time (4 bytes) | Term Id (4 bytes) | + * |-----------------------------------------------------------------| + * | Logic Id (8 bytes) | File Num (4 bytes) | Offset (8 bytes) | + * |-----------------------------------------------------------------| + * | Content Length (4 bytes) | Content (content length bytes) | + * +-----------------------------------------------------------------+ + * |------------------------ 34 Bytes -------------------------------| * * content: *2\r\n$7\r\npadding\r\n$00001\r\n***\r\n * length of *** -> total_len - PADDING_BINLOG_PROTOCOL_SIZE - SPACE_STROE_PARAMETER_LENGTH; @@ -154,7 +158,7 @@ std::string PikaBinlogTransverter::ConstructPaddingBinlog(BinlogType type, uint3 bool PikaBinlogTransverter::BinlogItemWithoutContentDecode(BinlogType type, const std::string& binlog, BinlogItem* binlog_item) { uint16_t binlog_type = 0; - std::string binlog_str = binlog; + Slice binlog_str = binlog; pstd::GetFixed16(&binlog_str, &binlog_type); if (binlog_type != type) { LOG(ERROR) << "Binlog Item type error, expect type:" << type << " actualy type: " << binlog_type; diff --git a/src/pstd/include/pstd_coding.h b/src/pstd/include/pstd_coding.h index 53304e682e..0b786091ec 100644 --- a/src/pstd/include/pstd_coding.h +++ b/src/pstd/include/pstd_coding.h @@ -31,6 +31,13 @@ extern void GetFixed32(std::string* dst, uint32_t* value); extern void GetFixed64(std::string* dst, uint64_t* value); extern bool GetVarint32(std::string* input, uint32_t* value); extern bool GetVarint64(std::string* input, uint64_t* value); + +extern void GetFixed16(Slice* dst, uint16_t* value); +extern void GetFixed32(Slice* dst, uint32_t* value); +extern void GetFixed64(Slice* dst, uint64_t* value); +extern bool GetVarint32(Slice* input, uint32_t* value); +extern bool GetVarint64(Slice* input, uint64_t* value); + extern const char* GetLengthPrefixedSlice(const char* p, const char* limit, Slice* result); extern bool GetLengthPrefixedSlice(Slice* input, Slice* result); extern bool GetLengthPrefixedString(std::string* input, std::string* result); @@ -82,20 +89,41 @@ inline uint64_t DecodeFixed64(const char* ptr) { } inline void GetFixed16(std::string* dst, uint16_t* value) { + if (!dst || !value) return; *value = DecodeFixed16(dst->data()); dst->erase(0, sizeof(uint16_t)); } inline void GetFixed32(std::string* dst, uint32_t* value) { + if (!dst || !value) return; *value = DecodeFixed32(dst->data()); dst->erase(0, sizeof(uint32_t)); } inline void GetFixed64(std::string* dst, uint64_t* value) { + if (!dst || !value) return; *value = DecodeFixed64(dst->data()); dst->erase(0, sizeof(uint64_t)); } +inline void GetFixed16(Slice* dst, uint16_t* value) { + if (!dst || !value) return; + *value = DecodeFixed16(dst->data()); + dst->remove_prefix(sizeof(uint16_t) / sizeof(char)); +} + +inline void GetFixed32(Slice* dst, uint32_t* value) { + if (!dst || !value) return; + *value = DecodeFixed32(dst->data()); + dst->remove_prefix(sizeof(uint32_t) / sizeof(char)); +} + +inline void GetFixed64(Slice* dst, uint64_t* value) { + if (!dst || !value) return; + *value = DecodeFixed64(dst->data()); + dst->remove_prefix(sizeof(uint64_t) / sizeof(char)); +} + // Internal routine for use by fallback path of GetVarint32Ptr extern const char* GetVarint32PtrFallback(const char* p, const char* limit, uint32_t* value); inline const char* GetVarint32Ptr(const char* p, const char* limit, uint32_t* value) {