forked from OpenAtomFoundation/pikiwidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpika_binlog_transverter.cc
149 lines (132 loc) · 5.13 KB
/
pika_binlog_transverter.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) 2018-present, Qihoo, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
#include "include/pika_binlog_transverter.h"
uint32_t BinlogItem::exec_time() const {
return exec_time_;
}
uint32_t BinlogItem::server_id() const {
return server_id_;
}
uint64_t BinlogItem::logic_id() const {
return logic_id_;
}
uint32_t BinlogItem::filenum() const {
return filenum_;
}
uint64_t BinlogItem::offset() const {
return offset_;
}
std::string BinlogItem::content() const {
return content_;
}
void BinlogItem::set_exec_time(uint32_t exec_time) {
exec_time_ = exec_time;
}
void BinlogItem::set_server_id(uint32_t server_id) {
server_id_ = server_id;
}
void BinlogItem::set_logic_id(uint64_t logic_id) {
logic_id_ = logic_id;
}
void BinlogItem::set_filenum(uint32_t filenum) {
filenum_ = filenum;
}
void BinlogItem::set_offset(uint64_t offset) {
offset_ = offset;
}
std::string BinlogItem::ToString() const {
std::string str;
str.append("exec_time: " + std::to_string(exec_time_));
str.append(",server_id: " + std::to_string(server_id_));
str.append(",logic_id: " + std::to_string(logic_id_));
str.append(",filenum: " + std::to_string(filenum_));
str.append(",offset: " + std::to_string(offset_));
str.append("\ncontent: ");
for (size_t idx = 0; idx < content_.size(); ++idx) {
if (content_[idx] == '\n') {
str.append("\\n");
} else if (content_[idx] == '\r') {
str.append("\\r");
} else {
str.append(1, content_[idx]);
}
}
str.append("\n");
return str;
}
std::string PikaBinlogTransverter::BinlogEncode(BinlogType type,
uint32_t exec_time,
uint32_t server_id,
uint64_t logic_id,
uint32_t filenum,
uint64_t offset,
const std::string& content,
const std::vector<std::string>& extends) {
std::string binlog;
slash::PutFixed16(&binlog, type);
slash::PutFixed32(&binlog, exec_time);
slash::PutFixed32(&binlog, server_id);
slash::PutFixed64(&binlog, logic_id);
slash::PutFixed32(&binlog, filenum);
slash::PutFixed64(&binlog, offset);
uint32_t content_length = content.size();
slash::PutFixed32(&binlog, content_length);
binlog.append(content);
return binlog;
}
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;
slash::GetFixed16(&binlog_str, &binlog_type);
if (binlog_type != type) {
LOG(ERROR) << "Binlog Item type error, expect type:" << type << " actualy type: " << binlog_type;
return false;
}
slash::GetFixed32(&binlog_str, &binlog_item->exec_time_);
slash::GetFixed32(&binlog_str, &binlog_item->server_id_);
slash::GetFixed64(&binlog_str, &binlog_item->logic_id_);
slash::GetFixed32(&binlog_str, &binlog_item->filenum_);
slash::GetFixed64(&binlog_str, &binlog_item->offset_);
slash::GetFixed32(&binlog_str, &content_length);
if (binlog_str.size() == content_length) {
binlog_item->content_.assign(binlog_str.data(), content_length);
} else {
LOG(ERROR) << "Binlog Item get content error, expect length:" << content_length << " left length:" << binlog_str.size();
return false;
}
return true;
}
bool PikaBinlogTransverter::BinlogItemWithoutContentDecode(BinlogType type,
const std::string& binlog,
BinlogItem* binlog_item) {
uint16_t binlog_type = 0;
std::string binlog_str = binlog;
slash::GetFixed16(&binlog_str, &binlog_type);
if (binlog_type != type) {
LOG(ERROR) << "Binlog Item type error, expect type:" << type << " actualy type: " << binlog_type;
return false;
}
slash::GetFixed32(&binlog_str, &binlog_item->exec_time_);
slash::GetFixed32(&binlog_str, &binlog_item->server_id_);
slash::GetFixed64(&binlog_str, &binlog_item->logic_id_);
slash::GetFixed32(&binlog_str, &binlog_item->filenum_);
slash::GetFixed64(&binlog_str, &binlog_item->offset_);
return true;
}
bool PikaBinlogTransverter::BinlogHeaderDecode(BinlogType type,
const std::string& header,
BinlogHeader* binlog_header) {
std::string header_str = header;
slash::GetFixed16(&header_str, &(binlog_header->header_type_));
slash::GetFixed32(&header_str, &(binlog_header->item_length_));
if (binlog_header->header_type_ != kTypeAuth && binlog_header->header_type_ != kTypeBinlog) {
LOG(ERROR) << "Unrecognizable Type: " << binlog_header->header_type_ << " identify binlog type error";
return false;
}
return true;
}