Skip to content

Commit f5296e5

Browse files
authored
fix: list rename cmd (#165)
2 parents 10fa247 + 4068391 commit f5296e5

File tree

2 files changed

+69
-15
lines changed

2 files changed

+69
-15
lines changed

src/net/net_options.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@ class NetOptions {
1414
NetOptions() = default;
1515
~NetOptions() = default;
1616

17-
void SetThreadNum(int8_t threadNum) { threadNum_ = threadNum; }
17+
void SetThreadNum(int8_t number) { thread_num_ = number; }
1818

19-
int8_t GetThreadNum() const { return threadNum_; }
19+
int8_t GetThreadNum() const { return thread_num_; }
2020

21-
void SetRwSeparation(bool rwSeparation = true) { rwSeparation_ = rwSeparation; }
21+
void SetRwSeparation(bool spearation = true) { rw_separation_ = spearation; }
2222

23-
bool GetRwSeparation() const { return rwSeparation_; }
23+
bool GetRwSeparation() const { return rw_separation_; }
2424

25-
void SetOpTcpKeepAlive(uint32_t tcpKeepAlive) { tcpKeepAlive_ = tcpKeepAlive; }
25+
void SetOpTcpKeepAlive(uint32_t timeout) { tcp_keepalive_timeout_ = timeout; }
2626

27-
uint32_t GetOpTcpKeepAlive() const { return tcpKeepAlive_; }
27+
uint32_t GetOpTcpKeepAlive() const { return tcp_keepalive_timeout_; }
2828

2929
private:
30-
bool rwSeparation_ = true; // Whether to separate read and write
30+
bool rw_separation_ = true; // Whether to separate read and write
3131

32-
int8_t threadNum_ = 1; // The number of threads
32+
int8_t thread_num_ = 1; // The number of threads
3333

34-
uint32_t tcpKeepAlive_ = 300; // The timeout of the keepalive connection in seconds
34+
uint32_t tcp_keepalive_timeout_ = 300; // The timeout of the keepalive connection in seconds
3535
};
3636

3737
} // namespace net

src/storage/src/redis_lists.cc

+60-6
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,7 @@ Status Redis::RPushx(const Slice& key, const std::vector<std::string>& values, u
973973
}
974974

975975
Status Redis::ListsRename(const Slice& key, Redis* new_inst, const Slice& newkey) {
976+
auto batch = Batch::CreateBatch(this);
976977
std::string meta_value;
977978
uint32_t statistic = 0;
978979
const std::vector<std::string> keys = {key.ToString(), newkey.ToString()};
@@ -994,18 +995,45 @@ Status Redis::ListsRename(const Slice& key, Redis* new_inst, const Slice& newkey
994995
// copy a new list with newkey
995996
ParsedListsMetaValue parsed_lists_meta_value(&meta_value);
996997
statistic = parsed_lists_meta_value.Count();
997-
s = new_inst->GetDB()->Put(default_write_options_, handles_[kMetaCF], base_meta_newkey.Encode(), meta_value);
998+
999+
// todo if value is too many, will slow to rename
1000+
uint32_t version = parsed_lists_meta_value.Version();
1001+
uint64_t index = parsed_lists_meta_value.LeftIndex() + 1;
1002+
uint64_t right_index = parsed_lists_meta_value.RightIndex() - 1;
1003+
ListsDataKey base_lists_data_key(key, version, index);
1004+
std::vector<std::string> list_nodes;
1005+
rocksdb::Iterator* iter = db_->NewIterator(default_read_options_, handles_[kListsDataCF]);
1006+
uint64_t current_index = index;
1007+
for (iter->Seek(base_lists_data_key.Encode()); iter->Valid() && current_index <= right_index;
1008+
iter->Next(), current_index++) {
1009+
ParsedBaseDataValue parsed_value(iter->value());
1010+
list_nodes.push_back(parsed_value.UserValue().ToString());
1011+
}
1012+
delete iter;
1013+
1014+
// write new data value
1015+
current_index = index;
1016+
for (const auto& node : list_nodes) {
1017+
ListsDataKey new_lists_data_key(newkey, version, current_index++);
1018+
BaseDataValue n_val(node);
1019+
batch->Put(kListsDataCF, new_lists_data_key.Encode(), n_val.Encode());
1020+
}
1021+
// write new meta_key
1022+
batch->Put(kMetaCF, base_meta_newkey.Encode(), meta_value);
9981023
new_inst->UpdateSpecificKeyStatistics(DataType::kLists, newkey.ToString(), statistic);
9991024

10001025
// ListsDel key
10011026
parsed_lists_meta_value.InitialMetaValue();
10021027
s = db_->Put(default_write_options_, handles_[kMetaCF], base_meta_key.Encode(), meta_value);
1028+
batch->Delete(kListsDataCF, base_meta_key.Encode());
10031029
UpdateSpecificKeyStatistics(DataType::kLists, key.ToString(), statistic);
10041030

1005-
return s;
1031+
return batch->Commit();
10061032
}
10071033

10081034
Status Redis::ListsRenamenx(const Slice& key, Redis* new_inst, const Slice& newkey) {
1035+
auto batch = Batch::CreateBatch(this);
1036+
10091037
std::string meta_value;
10101038
uint32_t statistic = 0;
10111039
const std::vector<std::string> keys = {key.ToString(), newkey.ToString()};
@@ -1029,22 +1057,48 @@ Status Redis::ListsRenamenx(const Slice& key, Redis* new_inst, const Slice& newk
10291057
ParsedListsMetaValue parsed_lists_meta_value(&meta_value);
10301058
s = new_inst->GetDB()->Get(default_read_options_, handles_[kMetaCF], base_meta_newkey.Encode(), &new_meta_value);
10311059
if (s.ok()) {
1032-
if (IsStale(new_meta_value)) {
1060+
ParsedListsMetaValue parsed_lists_new_meta_value(new_meta_value);
1061+
if (parsed_lists_new_meta_value.Count() != 0 || parsed_lists_new_meta_value.IsStale()) {
10331062
return Status::Corruption(); // newkey already exists.
10341063
}
10351064
}
1036-
ParsedSetsMetaValue parsed_lists_new_meta_value(&new_meta_value);
1065+
// ParsedListsMetaValue parsed_lists_new_meta_value(&new_meta_value);
10371066
// copy a new list with newkey
10381067
statistic = parsed_lists_meta_value.Count();
1039-
s = new_inst->GetDB()->Put(default_write_options_, handles_[kMetaCF], base_meta_newkey.Encode(), meta_value);
1068+
1069+
// todo if value is too many, will slow to rename
1070+
uint32_t version = parsed_lists_meta_value.Version();
1071+
uint64_t index = parsed_lists_meta_value.LeftIndex() + 1;
1072+
uint64_t right_index = parsed_lists_meta_value.RightIndex() - 1;
1073+
ListsDataKey base_lists_data_key(key, version, index);
1074+
std::vector<std::string> list_nodes;
1075+
rocksdb::Iterator* iter = db_->NewIterator(default_read_options_, handles_[kListsDataCF]);
1076+
uint64_t current_index = index;
1077+
for (iter->Seek(base_lists_data_key.Encode()); iter->Valid() && current_index <= right_index;
1078+
iter->Next(), current_index++) {
1079+
ParsedBaseDataValue parsed_value(iter->value());
1080+
list_nodes.push_back(parsed_value.UserValue().ToString());
1081+
}
1082+
delete iter;
1083+
1084+
// write new data value
1085+
current_index = index;
1086+
for (const auto& node : list_nodes) {
1087+
ListsDataKey new_lists_data_key(newkey, version, current_index++);
1088+
BaseDataValue n_val(node);
1089+
batch->Put(kListsDataCF, new_lists_data_key.Encode(), n_val.Encode());
1090+
}
1091+
// write new meta_key
1092+
batch->Put(kMetaCF, base_meta_newkey.Encode(), meta_value);
10401093
new_inst->UpdateSpecificKeyStatistics(DataType::kLists, newkey.ToString(), statistic);
10411094

10421095
// ListsDel key
10431096
parsed_lists_meta_value.InitialMetaValue();
10441097
s = db_->Put(default_write_options_, handles_[kMetaCF], base_meta_key.Encode(), meta_value);
1098+
batch->Delete(kListsDataCF, base_meta_key.Encode());
10451099
UpdateSpecificKeyStatistics(DataType::kLists, key.ToString(), statistic);
10461100

1047-
return s;
1101+
return batch->Commit();
10481102
}
10491103

10501104
void Redis::ScanLists() {

0 commit comments

Comments
 (0)