forked from apache/kvrocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch_extractor.cc
232 lines (220 loc) · 8.79 KB
/
batch_extractor.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "batch_extractor.h"
#include <rocksdb/write_batch.h>
#include <glog/logging.h>
#include "redis_bitmap.h"
#include "redis_slot.h"
#include "redis_reply.h"
void WriteBatchExtractor::LogData(const rocksdb::Slice &blob) {
log_data_.Decode(blob);
}
rocksdb::Status WriteBatchExtractor::PutCF(uint32_t column_family_id, const Slice &key,
const Slice &value) {
if (column_family_id == kColumnFamilyIDZSetScore) {
return rocksdb::Status::OK();
}
std::string ns, user_key, sub_key;
std::vector<std::string> command_args;
if (column_family_id == kColumnFamilyIDMetadata) {
ExtractNamespaceKey(key, &ns, &user_key, is_slotid_encoded_);
if (slot_ >= 0) {
if (static_cast<uint16_t>(slot_) != GetSlotNumFromKey(user_key)) return rocksdb::Status::OK();
}
Metadata metadata(kRedisNone);
metadata.Decode(value.ToString());
if (metadata.Type() == kRedisString) {
command_args = {"SET", user_key, value.ToString().substr(5, value.size() - 5)};
resp_commands_[ns].emplace_back(Redis::Command2RESP(command_args));
if (metadata.expire > 0) {
command_args = {"EXPIREAT", user_key, std::to_string(metadata.expire)};
resp_commands_[ns].emplace_back(Redis::Command2RESP(command_args));
}
} else if (metadata.expire > 0) {
auto args = log_data_.GetArguments();
if (args->size() > 0) {
RedisCommand cmd = static_cast<RedisCommand >(std::stoi((*args)[0]));
if (cmd == kRedisCmdExpire) {
command_args = {"EXPIREAT", user_key, std::to_string(metadata.expire)};
resp_commands_[ns].emplace_back(Redis::Command2RESP(command_args));
}
}
}
return rocksdb::Status::OK();
}
if (column_family_id == kColumnFamilyIDDefault) {
InternalKey ikey(key, is_slotid_encoded_);
user_key = ikey.GetKey().ToString();
if (slot_ >= 0) {
if (static_cast<uint16_t>(slot_) != GetSlotNumFromKey(user_key)) return rocksdb::Status::OK();
}
sub_key = ikey.GetSubKey().ToString();
ns = ikey.GetNamespace().ToString();
switch (log_data_.GetRedisType()) {
case kRedisHash:command_args = {"HSET", user_key, sub_key, value.ToString()};
break;
case kRedisList: {
auto args = log_data_.GetArguments();
if (args->size() < 1) {
LOG(ERROR) << "Fail to parse write_batch in putcf type list : args error ,should at least contain cmd";
return rocksdb::Status::OK();
}
RedisCommand cmd = static_cast<RedisCommand >(std::stoi((*args)[0]));
switch (cmd) {
case kRedisCmdLSet:
if (args->size() < 2) {
LOG(ERROR) << "Fail to parse write_batch in putcf cmd lset : args error ,should contain lset index";
return rocksdb::Status::OK();
}
command_args = {"LSET", user_key, (*args)[1], value.ToString()};
break;
case kRedisCmdLInsert:
if (first_seen_) {
if (args->size() < 4) {
LOG(ERROR)
<< "Fail to parse write_batch in putcf cmd linsert : args error, should contain before pivot value";
return rocksdb::Status::OK();
}
command_args = {"LINSERT", user_key, (*args)[1] == "1" ? "before" : "after", (*args)[2], (*args)[3]};
first_seen_ = false;
}
break;
case kRedisCmdLRem:
// lrem will be parsed in deletecf, so ignore this putcf
break;
default:command_args = {cmd == kRedisCmdLPush ? "LPUSH" : "RPUSH", user_key, value.ToString()};
}
break;
}
case kRedisSet:command_args = {"SADD", user_key, sub_key};
break;
case kRedisZSet: {
double score = DecodeDouble(value.data());
command_args = {"ZADD", user_key, std::to_string(score), sub_key};
break;
}
case kRedisBitmap: {
auto args = log_data_.GetArguments();
if (args->size() < 1) {
LOG(ERROR) << "Fail to parse write_batch in putcf type bitmap : args error ,should at least contain cmd";
return rocksdb::Status::OK();
}
RedisCommand cmd = static_cast<RedisCommand >(std::stoi((*args)[0]));
switch (cmd) {
case kRedisCmdSetBit: {
if (args->size() < 2) {
LOG(ERROR) << "Fail to parse write_batch in putcf cmd setbit : args error ,should contain setbit offset";
return rocksdb::Status::OK();
}
bool bit_value = Redis::Bitmap::GetBitFromValueAndOffset(value.ToString(), std::stoi((*args)[1]));
command_args = {"SETBIT", user_key, (*args)[1], bit_value ? "1" : "0"};
break;
}
case kRedisCmdBitOp:
if (first_seen_) {
if (args->size() < 4) {
LOG(ERROR)
<< "Fail to parse write_batch in putcf cmd bitop : args error, should at least contain srckey";
return rocksdb::Status::OK();
}
command_args = {"BITOP", (*args)[1], user_key};
command_args.insert(command_args.end(), args->begin() + 2, args->end());
first_seen_ = false;
}
break;
default:
LOG(ERROR) << "Fail to parse write_batch in putcf type bitmap : cmd error";
return rocksdb::Status::OK();
}
break;
}
case kRedisSortedint: {
if (!to_redis_) {
command_args = {"SIADD", user_key, std::to_string(DecodeFixed64(sub_key.data()))};
}
break;
}
default: break;
}
}
if (!command_args.empty()) {
resp_commands_[ns].emplace_back(Redis::Command2RESP(command_args));
}
return rocksdb::Status::OK();
}
rocksdb::Status WriteBatchExtractor::DeleteCF(uint32_t column_family_id, const Slice &key) {
if (column_family_id == kColumnFamilyIDZSetScore) {
return rocksdb::Status::OK();
}
std::string ns, user_key, sub_key;
std::vector<std::string> command_args;
if (column_family_id == kColumnFamilyIDMetadata) {
ExtractNamespaceKey(key, &ns, &user_key, is_slotid_encoded_);
if (slot_ >= 0) {
if (static_cast<uint16_t>(slot_) != GetSlotNumFromKey(user_key)) return rocksdb::Status::OK();
}
command_args = {"DEL", user_key};
} else if (column_family_id == kColumnFamilyIDDefault) {
InternalKey ikey(key, is_slotid_encoded_);
user_key = ikey.GetKey().ToString();
if (slot_ >= 0) {
if (static_cast<uint16_t>(slot_) != GetSlotNumFromKey(user_key)) return rocksdb::Status::OK();
}
sub_key = ikey.GetSubKey().ToString();
ns = ikey.GetNamespace().ToString();
switch (log_data_.GetRedisType()) {
case kRedisHash: command_args = {"HDEL", user_key, sub_key};
break;
case kRedisSet: command_args = {"SREM", user_key, sub_key};
break;
case kRedisZSet: command_args = {"ZREM", user_key, sub_key};
break;
case kRedisList: {
auto args = log_data_.GetArguments();
if (args->size() < 1) {
LOG(ERROR) << "Fail to parse write_batch in DeleteCF type list : args error ,should contain cmd";
return rocksdb::Status::OK();
}
RedisCommand cmd = static_cast<RedisCommand >(std::stoi((*args)[0]));
switch (cmd) {
case kRedisCmdLTrim:
if (first_seen_) {
if (args->size() < 3) {
LOG(ERROR) << "Fail to parse write_batch in DeleteCF cmd ltrim : args error ,should contain start,stop";
return rocksdb::Status::OK();
}
command_args = {"LTRIM", user_key, (*args)[1], (*args)[2]};
first_seen_ = false;
}
break;
case kRedisCmdLRem:
if (first_seen_) {
if (args->size() < 3) {
LOG(ERROR) << "Fail to parse write_batch in DeleteCF cmd lrem : args error ,should contain count,value";
return rocksdb::Status::OK();
}
command_args = {"LREM", user_key, (*args)[1], (*args)[2]};
first_seen_ = false;
}
break;
default:command_args = {cmd == kRedisCmdLPop ? "LPOP" : "RPOP", user_key};
}
break;
}
case kRedisSortedint: {
if (!to_redis_) {
command_args = {"SIREM", user_key, std::to_string(DecodeFixed64(sub_key.data()))};
}
break;
}
default: break;
}
}
if (!command_args.empty()) {
resp_commands_[ns].emplace_back(Redis::Command2RESP(command_args));
}
return rocksdb::Status::OK();
}
rocksdb::Status WriteBatchExtractor::DeleteRangeCF(uint32_t column_family_id,
const Slice& begin_key, const Slice& end_key) {
// Do nothing about DeleteRange operations
return rocksdb::Status::OK();
}