Skip to content

Commit

Permalink
feat: add Slice pstd::GetFixed (#1425)
Browse files Browse the repository at this point in the history
  • Loading branch information
4kangjc authored May 4, 2023
1 parent e3d4236 commit fb39239
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
14 changes: 8 additions & 6 deletions include/pika_binlog_transverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
#include <iostream>
#include <vector>

/*
* ***********************************************Type First Binlog Item
* Format*********************************************** | <Type> | <Create Time> | <Term Id> | <Binlog Logic Id> |
* <File Num> | <Offset> | <Content Length> | <Content> | 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

Expand Down
18 changes: 11 additions & 7 deletions src/pika_binlog_transverter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -98,11 +98,15 @@ bool PikaBinlogTransverter::BinlogDecode(BinlogType type, const std::string& bin
}

/*
* *************************************************Type First Binlog Item
* Format************************************************** | <Type> | <Create Time> | <Term Id> | <Binlog Logic Id>
* | <File Num> | <Offset> | <Content Length> | <Content> | | 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;
Expand Down Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions src/pstd/include/pstd_coding.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit fb39239

Please sign in to comment.