Skip to content

Commit

Permalink
fix: add basic.tcl, fixed some bugs (#403)
Browse files Browse the repository at this point in the history
* fix Rename and Renamenx bug

* add basic.tcl
---------

Co-authored-by: liuyuecai <liuyuecai@360.cn>
  • Loading branch information
luky116 and liuyuecai authored Aug 8, 2024
1 parent 694e225 commit 124b41e
Show file tree
Hide file tree
Showing 9 changed files with 340 additions and 284 deletions.
82 changes: 47 additions & 35 deletions src/storage/src/redis_hashes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1150,21 +1150,27 @@ Status Redis::HashesRename(const Slice& key, Redis* new_inst, const Slice& newke
BaseMetaKey base_meta_key(key);
BaseMetaKey base_meta_newkey(newkey);
s = db_->Get(default_read_options_, handles_[kMetaCF], base_meta_key.Encode(), &meta_value);
if (s.ok()) {
if (IsStale(meta_value)) {
return Status::NotFound();
}
// copy a new hash with newkey
ParsedHashesMetaValue parsed_hashes_meta_value(&meta_value);
statistic = parsed_hashes_meta_value.Count();
s = new_inst->GetDB()->Put(default_write_options_, handles_[kMetaCF], base_meta_newkey.Encode(), meta_value);
new_inst->UpdateSpecificKeyStatistics(DataType::kHashes, newkey.ToString(), statistic);

// HashesDel key
parsed_hashes_meta_value.InitialMetaValue();
s = db_->Put(default_write_options_, handles_[kMetaCF], base_meta_key.Encode(), meta_value);
UpdateSpecificKeyStatistics(DataType::kHashes, key.ToString(), statistic);
if (!s.ok() || !ExpectedMetaValue(DataType::kHashes, meta_value)) {
return s;
}
if (key == newkey) {
return Status::OK();
}

if (IsStale(meta_value)) {
return Status::NotFound();
}
// copy a new hash with newkey
ParsedHashesMetaValue parsed_hashes_meta_value(&meta_value);
statistic = parsed_hashes_meta_value.Count();
s = new_inst->GetDB()->Put(default_write_options_, handles_[kMetaCF], base_meta_newkey.Encode(), meta_value);
new_inst->UpdateSpecificKeyStatistics(DataType::kHashes, newkey.ToString(), statistic);

// HashesDel key
parsed_hashes_meta_value.InitialMetaValue();
s = db_->Put(default_write_options_, handles_[kMetaCF], base_meta_key.Encode(), meta_value);
UpdateSpecificKeyStatistics(DataType::kHashes, key.ToString(), statistic);

return s;
}

Expand All @@ -1178,30 +1184,36 @@ Status Redis::HashesRenamenx(const Slice& key, Redis* new_inst, const Slice& new
BaseMetaKey base_meta_key(key);
BaseMetaKey base_meta_newkey(newkey);
s = db_->Get(default_read_options_, handles_[kMetaCF], base_meta_key.Encode(), &meta_value);
if (!s.ok() || !ExpectedMetaValue(DataType::kHashes, meta_value)) {
return s;
}
if (key == newkey) {
return Status::Corruption();
}

if (IsStale(meta_value)) {
return Status::NotFound();
}
ParsedHashesMetaValue parsed_hashes_meta_value(&meta_value);
// check if newkey exists.
std::string new_meta_value;
s = new_inst->GetDB()->Get(default_read_options_, handles_[kMetaCF], base_meta_newkey.Encode(), &new_meta_value);
if (s.ok()) {
if (IsStale(meta_value)) {
return Status::NotFound();
if (!IsStale(new_meta_value)) {
return Status::Corruption(); // newkey already exists.
}
ParsedHashesMetaValue parsed_hashes_meta_value(&meta_value);
// check if newkey exists.
std::string new_meta_value;
s = new_inst->GetDB()->Get(default_read_options_, handles_[kMetaCF], base_meta_newkey.Encode(), &new_meta_value);
if (s.ok()) {
if (!IsStale(new_meta_value)) {
return Status::Corruption(); // newkey already exists.
}
}
ParsedHashesMetaValue parsed_hashes_new_meta_value(&new_meta_value);
// copy a new hash with newkey
statistic = parsed_hashes_meta_value.Count();
s = new_inst->GetDB()->Put(default_write_options_, handles_[kMetaCF], base_meta_newkey.Encode(), meta_value);
new_inst->UpdateSpecificKeyStatistics(DataType::kHashes, newkey.ToString(), statistic);

// HashesDel key
parsed_hashes_meta_value.InitialMetaValue();
s = db_->Put(default_write_options_, handles_[kMetaCF], base_meta_key.Encode(), meta_value);
UpdateSpecificKeyStatistics(DataType::kHashes, key.ToString(), statistic);
}
ParsedHashesMetaValue parsed_hashes_new_meta_value(&new_meta_value);
// copy a new hash with newkey
statistic = parsed_hashes_meta_value.Count();
s = new_inst->GetDB()->Put(default_write_options_, handles_[kMetaCF], base_meta_newkey.Encode(), meta_value);
new_inst->UpdateSpecificKeyStatistics(DataType::kHashes, newkey.ToString(), statistic);

// HashesDel key
parsed_hashes_meta_value.InitialMetaValue();
s = db_->Put(default_write_options_, handles_[kMetaCF], base_meta_key.Encode(), meta_value);
UpdateSpecificKeyStatistics(DataType::kHashes, key.ToString(), statistic);

return s;
}

Expand Down
80 changes: 46 additions & 34 deletions src/storage/src/redis_lists.cc
Original file line number Diff line number Diff line change
Expand Up @@ -981,21 +981,27 @@ Status Redis::ListsRename(const Slice& key, Redis* new_inst, const Slice& newkey
BaseMetaKey base_meta_key(key);
BaseMetaKey base_meta_newkey(newkey);
Status s = db_->Get(default_read_options_, handles_[kMetaCF], base_meta_key.Encode(), &meta_value);
if (s.ok()) {
if (IsStale(meta_value)) {
return Status::NotFound();
}
// copy a new list with newkey
ParsedListsMetaValue parsed_lists_meta_value(&meta_value);
statistic = parsed_lists_meta_value.Count();
s = new_inst->GetDB()->Put(default_write_options_, handles_[kMetaCF], base_meta_newkey.Encode(), meta_value);
new_inst->UpdateSpecificKeyStatistics(DataType::kLists, newkey.ToString(), statistic);
if (!s.ok() || !ExpectedMetaValue(DataType::kLists, meta_value)) {
return s;
}
if (key == newkey) {
return Status::OK();
}

// ListsDel key
parsed_lists_meta_value.InitialMetaValue();
s = db_->Put(default_write_options_, handles_[kMetaCF], base_meta_key.Encode(), meta_value);
UpdateSpecificKeyStatistics(DataType::kLists, key.ToString(), statistic);
if (IsStale(meta_value)) {
return Status::NotFound();
}
// copy a new list with newkey
ParsedListsMetaValue parsed_lists_meta_value(&meta_value);
statistic = parsed_lists_meta_value.Count();
s = new_inst->GetDB()->Put(default_write_options_, handles_[kMetaCF], base_meta_newkey.Encode(), meta_value);
new_inst->UpdateSpecificKeyStatistics(DataType::kLists, newkey.ToString(), statistic);

// ListsDel key
parsed_lists_meta_value.InitialMetaValue();
s = db_->Put(default_write_options_, handles_[kMetaCF], base_meta_key.Encode(), meta_value);
UpdateSpecificKeyStatistics(DataType::kLists, key.ToString(), statistic);

return s;
}

Expand All @@ -1008,30 +1014,36 @@ Status Redis::ListsRenamenx(const Slice& key, Redis* new_inst, const Slice& newk
BaseMetaKey base_meta_key(key);
BaseMetaKey base_meta_newkey(newkey);
Status s = db_->Get(default_read_options_, handles_[kMetaCF], base_meta_key.Encode(), &meta_value);
if (!s.ok() || !ExpectedMetaValue(DataType::kLists, meta_value)) {
return s;
}
if (key == newkey) {
return Status::Corruption();
}

if (IsStale(meta_value)) {
return Status::NotFound();
}
// check if newkey exists.
std::string new_meta_value;
ParsedListsMetaValue parsed_lists_meta_value(&meta_value);
s = new_inst->GetDB()->Get(default_read_options_, handles_[kMetaCF], base_meta_newkey.Encode(), &new_meta_value);
if (s.ok()) {
if (IsStale(meta_value)) {
return Status::NotFound();
}
// check if newkey exists.
std::string new_meta_value;
ParsedListsMetaValue parsed_lists_meta_value(&meta_value);
s = new_inst->GetDB()->Get(default_read_options_, handles_[kMetaCF], base_meta_newkey.Encode(), &new_meta_value);
if (s.ok()) {
if (IsStale(new_meta_value)) {
return Status::Corruption(); // newkey already exists.
}
if (IsStale(new_meta_value)) {
return Status::Corruption(); // newkey already exists.
}
ParsedSetsMetaValue parsed_lists_new_meta_value(&new_meta_value);
// copy a new list with newkey
statistic = parsed_lists_meta_value.Count();
s = new_inst->GetDB()->Put(default_write_options_, handles_[kMetaCF], base_meta_newkey.Encode(), meta_value);
new_inst->UpdateSpecificKeyStatistics(DataType::kLists, newkey.ToString(), statistic);

// ListsDel key
parsed_lists_meta_value.InitialMetaValue();
s = db_->Put(default_write_options_, handles_[kMetaCF], base_meta_key.Encode(), meta_value);
UpdateSpecificKeyStatistics(DataType::kLists, key.ToString(), statistic);
}
ParsedSetsMetaValue parsed_lists_new_meta_value(&new_meta_value);
// copy a new list with newkey
statistic = parsed_lists_meta_value.Count();
s = new_inst->GetDB()->Put(default_write_options_, handles_[kMetaCF], base_meta_newkey.Encode(), meta_value);
new_inst->UpdateSpecificKeyStatistics(DataType::kLists, newkey.ToString(), statistic);

// ListsDel key
parsed_lists_meta_value.InitialMetaValue();
s = db_->Put(default_write_options_, handles_[kMetaCF], base_meta_key.Encode(), meta_value);
UpdateSpecificKeyStatistics(DataType::kLists, key.ToString(), statistic);

return s;
}

Expand Down
91 changes: 53 additions & 38 deletions src/storage/src/redis_sets.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1244,23 +1244,32 @@ Status Redis::SetsRename(const Slice& key, Redis* new_inst, const Slice& newkey)
BaseMetaKey base_meta_key(key);
BaseMetaKey base_meta_newkey(newkey);
rocksdb::Status s = db_->Get(default_read_options_, handles_[kMetaCF], base_meta_key.Encode(), &meta_value);
if (s.ok()) {
ParsedSetsMetaValue parsed_sets_meta_value(&meta_value);
if (parsed_sets_meta_value.IsStale()) {
return rocksdb::Status::NotFound("Stale");
} else if (parsed_sets_meta_value.Count() == 0) {
return rocksdb::Status::NotFound();
}
// copy a new set with newkey
statistic = parsed_sets_meta_value.Count();
s = new_inst->GetDB()->Put(default_write_options_, handles_[kMetaCF], base_meta_newkey.Encode(), meta_value);
new_inst->UpdateSpecificKeyStatistics(DataType::kSets, newkey.ToString(), statistic);
if (!s.ok()) {
return s;
}
if (!s.ok() || !ExpectedMetaValue(DataType::kSets, meta_value)) {
return s;
}
if (key == newkey) {
return Status::OK();
}

// SetsDel key
parsed_sets_meta_value.InitialMetaValue();
s = db_->Put(default_write_options_, handles_[kMetaCF], base_meta_key.Encode(), meta_value);
UpdateSpecificKeyStatistics(DataType::kSets, key.ToString(), statistic);
ParsedSetsMetaValue parsed_sets_meta_value(&meta_value);
if (parsed_sets_meta_value.IsStale()) {
return rocksdb::Status::NotFound("Stale");
} else if (parsed_sets_meta_value.Count() == 0) {
return rocksdb::Status::NotFound();
}
// copy a new set with newkey
statistic = parsed_sets_meta_value.Count();
s = new_inst->GetDB()->Put(default_write_options_, handles_[kMetaCF], base_meta_newkey.Encode(), meta_value);
new_inst->UpdateSpecificKeyStatistics(DataType::kSets, newkey.ToString(), statistic);

// SetsDel key
parsed_sets_meta_value.InitialMetaValue();
s = db_->Put(default_write_options_, handles_[kMetaCF], base_meta_key.Encode(), meta_value);
UpdateSpecificKeyStatistics(DataType::kSets, key.ToString(), statistic);

return s;
}

Expand All @@ -1273,33 +1282,39 @@ Status Redis::SetsRenamenx(const Slice& key, Redis* new_inst, const Slice& newke
BaseMetaKey base_meta_key(key);
BaseMetaKey base_meta_newkey(newkey);
rocksdb::Status s = db_->Get(default_read_options_, handles_[kMetaCF], base_meta_key.Encode(), &meta_value);
if (!s.ok() || !ExpectedMetaValue(DataType::kSets, meta_value)) {
return s;
}
if (key == newkey) {
return Status::Corruption();
}

ParsedSetsMetaValue parsed_sets_meta_value(&meta_value);
if (parsed_sets_meta_value.IsStale()) {
return rocksdb::Status::NotFound("Stale");
} else if (parsed_sets_meta_value.Count() == 0) {
return rocksdb::Status::NotFound();
}
// check if newkey exists.
std::string new_meta_value;
s = new_inst->GetDB()->Get(default_read_options_, handles_[kMetaCF], base_meta_newkey.Encode(), &new_meta_value);
if (s.ok()) {
ParsedSetsMetaValue parsed_sets_meta_value(&meta_value);
if (parsed_sets_meta_value.IsStale()) {
return rocksdb::Status::NotFound("Stale");
} else if (parsed_sets_meta_value.Count() == 0) {
return rocksdb::Status::NotFound();
}
// check if newkey exists.
std::string new_meta_value;
s = new_inst->GetDB()->Get(default_read_options_, handles_[kMetaCF], base_meta_newkey.Encode(), &new_meta_value);
if (s.ok()) {
ParsedSetsMetaValue parsed_sets_new_meta_value(&new_meta_value);
if (!parsed_sets_new_meta_value.IsStale() && parsed_sets_new_meta_value.Count() != 0) {
return Status::Corruption(); // newkey already exists.
}
ParsedSetsMetaValue parsed_sets_new_meta_value(&new_meta_value);
if (!parsed_sets_new_meta_value.IsStale() && parsed_sets_new_meta_value.Count() != 0) {
return Status::Corruption(); // newkey already exists.
}
}

// copy a new set with newkey
statistic = parsed_sets_meta_value.Count();
s = new_inst->GetDB()->Put(default_write_options_, handles_[kMetaCF], base_meta_newkey.Encode(), meta_value);
new_inst->UpdateSpecificKeyStatistics(DataType::kSets, newkey.ToString(), statistic);
// copy a new set with newkey
statistic = parsed_sets_meta_value.Count();
s = new_inst->GetDB()->Put(default_write_options_, handles_[kMetaCF], base_meta_newkey.Encode(), meta_value);
new_inst->UpdateSpecificKeyStatistics(DataType::kSets, newkey.ToString(), statistic);

// SetsDel key
parsed_sets_meta_value.InitialMetaValue();
s = db_->Put(default_write_options_, handles_[kMetaCF], base_meta_key.Encode(), meta_value);
UpdateSpecificKeyStatistics(DataType::kSets, key.ToString(), statistic);

// SetsDel key
parsed_sets_meta_value.InitialMetaValue();
s = db_->Put(default_write_options_, handles_[kMetaCF], base_meta_key.Encode(), meta_value);
UpdateSpecificKeyStatistics(DataType::kSets, key.ToString(), statistic);
}
return s;
}

Expand Down
Loading

0 comments on commit 124b41e

Please sign in to comment.