From ed5d6baebf59264f6abac916b7ca18ffa1a2c71a Mon Sep 17 00:00:00 2001 From: super-Pan66 <54765080+super-Pan66@users.noreply.github.com> Date: Sun, 3 Dec 2023 20:46:25 +0800 Subject: [PATCH] feat:add key del cmd (#62) * resolve conflicts * resolve conflicts * variable init * fix merge wrong * resolve conflits * resolve conflict * code format * create keyspace cmd file * add del cmd * code format * code format * delete useless files * delete useless files --- src/base_cmd.h | 3 +++ src/cmd_keys.cc | 29 +++++++++++++++++++++++++++++ src/cmd_keys.h | 25 +++++++++++++++++++++++++ src/cmd_kv.cc | 2 +- src/cmd_table_manager.cc | 5 +++++ 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/cmd_keys.cc create mode 100644 src/cmd_keys.h diff --git a/src/base_cmd.h b/src/base_cmd.h index 80c36c9e4..f2eea5e77 100644 --- a/src/base_cmd.h +++ b/src/base_cmd.h @@ -21,6 +21,9 @@ namespace pikiwidb { // command definition +// key cmd +const std::string kCmdNameDel = "del"; + // string cmd const std::string kCmdNameSet = "set"; const std::string kCmdNameGet = "get"; diff --git a/src/cmd_keys.cc b/src/cmd_keys.cc new file mode 100644 index 000000000..73c75c5da --- /dev/null +++ b/src/cmd_keys.cc @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023-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 "cmd_keys.h" +#include "store.h" + +namespace pikiwidb { + +DelCmd::DelCmd(const std::string& name, int16_t arity) + : BaseCmd(name, arity, CmdFlagsWrite, AclCategoryWrite | AclCategoryKeyspace) {} + +bool DelCmd::DoInitial(PClient* client) { + client->SetKey(client->argv_[1]); + return true; +} + +void DelCmd::DoCmd(PClient* client) { + if (PSTORE.DeleteKey(client->Key())) { + PSTORE.ClearExpire(client->Key()); + client->AppendInteger(1); + } else { + client->AppendInteger(0); + } +} +} // namespace pikiwidb \ No newline at end of file diff --git a/src/cmd_keys.h b/src/cmd_keys.h new file mode 100644 index 000000000..7428c5d3f --- /dev/null +++ b/src/cmd_keys.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023-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. + */ + +#pragma once + +#include "base_cmd.h" + +namespace pikiwidb { + +class DelCmd : public BaseCmd { + public: + DelCmd(const std::string& name, int16_t arity); + + protected: + bool DoInitial(PClient* client) override; + + private: + void DoCmd(PClient* client) override; +}; + +} // namespace pikiwidb diff --git a/src/cmd_kv.cc b/src/cmd_kv.cc index f04c378fc..724e55525 100644 --- a/src/cmd_kv.cc +++ b/src/cmd_kv.cc @@ -456,4 +456,4 @@ void SetnxCmd::DoCmd(PClient* client) { } } -} // namespace pikiwidb +} // namespace pikiwidb \ No newline at end of file diff --git a/src/cmd_table_manager.cc b/src/cmd_table_manager.cc index 1bc0dd8fa..736f068a6 100644 --- a/src/cmd_table_manager.cc +++ b/src/cmd_table_manager.cc @@ -8,6 +8,7 @@ #include "cmd_table_manager.h" #include #include "cmd_admin.h" +#include "cmd_keys.h" #include "cmd_kv.h" namespace pikiwidb { @@ -27,6 +28,10 @@ void CmdTableManager::InitCmdTable() { cmds_->insert(std::make_pair(kCmdNameConfig, std::move(configPtr))); + // keyspace + std::unique_ptr delPtr = std::make_unique(kCmdNameDel, -2); + cmds_->insert(std::make_pair(kCmdNameDel, std::move(delPtr))); + // kv std::unique_ptr getPtr = std::make_unique(kCmdNameGet, 2); cmds_->insert(std::make_pair(kCmdNameGet, std::move(getPtr)));