Skip to content

Commit

Permalink
feat: add admin flushdb cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
super-Pan66 committed Dec 5, 2023
1 parent 5a0c26c commit 0b6c64e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const std::string kCmdNameDiscard = "discard";

// admin
const std::string kCmdNameConfig = "config";
const std::string kCmdNameFlushdb = "flushdb";

const std::string kCmdNameAppend = "append";
const std::string kCmdNameGetset = "getset";
Expand Down
13 changes: 13 additions & 0 deletions src/cmd_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include "cmd_admin.h"
#include "store.h"

namespace pikiwidb {

Expand All @@ -27,4 +28,16 @@ bool CmdConfigSet::DoInitial(PClient* client) { return true; }

void CmdConfigSet::DoCmd(PClient* client) { client->AppendString("config cmd in development"); }

FlushdbCmd::FlushdbCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, CmdFlagsAdmin | CmdFlagsWrite, AclCategoryWrite | AclCategoryAdmin) {}

bool FlushdbCmd::DoInitial(PClient* client) { return true; }

void FlushdbCmd::DoCmd(PClient* client) {
PSTORE.dirty_ += PSTORE.DBSize();
PSTORE.ClearCurrentDB();
Propagate(PSTORE.GetDB(), std::vector<PString>{"flushdb"});
client->SetRes(CmdRes::kOk);
}

} // namespace pikiwidb
11 changes: 11 additions & 0 deletions src/cmd_admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,15 @@ class CmdConfigSet : public BaseCmd {
void DoCmd(PClient* client) override;
};

class FlushdbCmd : public BaseCmd {
public:
FlushdbCmd(const std::string& name, int16_t arity);

protected:
bool DoInitial(PClient* client) override;

private:
void DoCmd(PClient* client) override;
};

} // namespace pikiwidb
3 changes: 3 additions & 0 deletions src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ void CmdTableManager::InitCmdTable() {

cmds_->insert(std::make_pair(kCmdNameConfig, std::move(configPtr)));

std::unique_ptr<BaseCmd> flushdbPtr = std::make_unique<FlushdbCmd>(kCmdNameFlushdb, 1);
cmds_->insert(std::make_pair(kCmdNameFlushdb, std::move(flushdbPtr)));

// keyspace
std::unique_ptr<BaseCmd> delPtr = std::make_unique<DelCmd>(kCmdNameDel, -2);
cmds_->insert(std::make_pair(kCmdNameDel, std::move(delPtr)));
Expand Down

0 comments on commit 0b6c64e

Please sign in to comment.