Skip to content

Commit

Permalink
Merge branch 'unstable' into feature/etc
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexStocks committed Aug 9, 2024
2 parents 0a511c0 + a72cd92 commit ace2213
Show file tree
Hide file tree
Showing 25 changed files with 429 additions and 374 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/base_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ BaseCmd* BaseCmdGroup::GetSubCmd(const std::string& cmdName) {
bool BaseCmdGroup::DoInitial(PClient* client) {
client->SetSubCmdName(client->argv_[1]);
if (!subCmds_.contains(client->SubCmdName())) {
client->SetRes(CmdRes::kSyntaxErr, client->argv_[0] + " unknown subcommand for '" + client->SubCmdName() + "'");
client->SetRes(CmdRes::kErrOther, client->argv_[0] + " unknown subcommand for '" + client->SubCmdName() + "'");
return false;
}
return true;
Expand Down
2 changes: 2 additions & 0 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class CmdRes {
kInvalidDB,
kInconsistentHashTag,
kErrOther,
kUnknownCmd,
kUnknownSubCmd,
KIncrByOverFlow,
kInvalidCursor,
kWrongLeader,
Expand Down
2 changes: 1 addition & 1 deletion src/cmd_keys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ bool TypeCmd::DoInitial(PClient* client) {
}

void TypeCmd::DoCmd(PClient* client) {
storage::DataType type;
storage::DataType type = storage::DataType::kNones;
rocksdb::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->GetType(client->Key(), type);
if (s.ok()) {
client->AppendContent("+" + std::string(storage::DataTypeToString(type)));
Expand Down
8 changes: 3 additions & 5 deletions src/cmd_kv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -323,20 +323,18 @@ void BitOpCmd::DoCmd(PClient* client) {
PString res;
storage::BitOpType op = storage::kBitOpDefault;

if (keys.size() == 1) {
if (!keys.empty()) {
if (pstd::StringEqualCaseInsensitive(client->argv_[1], "or")) {
err = kPErrorOK;
op = storage::kBitOpOr;
}
} else if (keys.size() >= 2) {
if (pstd::StringEqualCaseInsensitive(client->argv_[1], "xor")) {
} else if (pstd::StringEqualCaseInsensitive(client->argv_[1], "xor")) {
err = kPErrorOK;
op = storage::kBitOpXor;
} else if (pstd::StringEqualCaseInsensitive(client->argv_[1], "and")) {
err = kPErrorOK;
op = storage::kBitOpAnd;
} else if (pstd::StringEqualCaseInsensitive(client->argv_[1], "not")) {
if (client->argv_.size() == 4) {
if (keys.size() == 1) {
err = kPErrorOK;
op = storage::kBitOpNot;
}
Expand Down
7 changes: 4 additions & 3 deletions src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "cmd_raft.h"
#include "cmd_set.h"
#include "cmd_zset.h"
#include "pstd_string.h"

namespace pikiwidb {

Expand Down Expand Up @@ -190,16 +191,16 @@ std::pair<BaseCmd*, CmdRes::CmdRet> CmdTableManager::GetCommand(const std::strin
auto cmd = cmds_->find(cmdName);

if (cmd == cmds_->end()) {
return std::pair(nullptr, CmdRes::kSyntaxErr);
return std::pair(nullptr, CmdRes::kUnknownCmd);
}

if (cmd->second->HasSubCommand()) {
if (client->argv_.size() < 2) {
return std::pair(nullptr, CmdRes::kInvalidParameter);
}
return std::pair(cmd->second->GetSubCmd(client->argv_[1]), CmdRes::kSyntaxErr);
return std::pair(cmd->second->GetSubCmd(pstd::StringToLower(client->argv_[1])), CmdRes::kUnknownSubCmd);
}
return std::pair(cmd->second.get(), CmdRes::kSyntaxErr);
return std::pair(cmd->second.get(), CmdRes::kOK);
}

bool CmdTableManager::CmdExist(const std::string& cmd) const {
Expand Down
8 changes: 5 additions & 3 deletions src/cmd_thread_pool_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ void CmdWorkThreadPoolWorker::Work() {
auto [cmdPtr, ret] = cmd_table_manager_.GetCommand(task->CmdName(), task->Client().get());

if (!cmdPtr) {
if (ret == CmdRes::kInvalidParameter) {
task->Client()->SetRes(CmdRes::kInvalidParameter);
if (ret == CmdRes::kUnknownCmd) {
task->Client()->SetRes(CmdRes::kErrOther, "unknown command '" + task->CmdName() + "'");
} else if (ret == CmdRes::kUnknownSubCmd) {
task->Client()->SetRes(CmdRes::kErrOther, "unknown sub command '" + task->Client().get()->argv_[1] + "'");
} else {
task->Client()->SetRes(CmdRes::kSyntaxErr, "unknown command '" + task->CmdName() + "'");
task->Client()->SetRes(CmdRes::kInvalidParameter);
}
g_pikiwidb->PushWriteTask(task->Client());
continue;
Expand Down
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
Loading

0 comments on commit ace2213

Please sign in to comment.