Skip to content

Commit

Permalink
feat:add key del cmd (#62)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
super-Pan66 authored Dec 3, 2023
1 parent 171844d commit ed5d6ba
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
29 changes: 29 additions & 0 deletions src/cmd_keys.cc
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions src/cmd_keys.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/cmd_kv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -456,4 +456,4 @@ void SetnxCmd::DoCmd(PClient* client) {
}
}

} // namespace pikiwidb
} // namespace pikiwidb
5 changes: 5 additions & 0 deletions src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "cmd_table_manager.h"
#include <memory>
#include "cmd_admin.h"
#include "cmd_keys.h"
#include "cmd_kv.h"

namespace pikiwidb {
Expand All @@ -27,6 +28,10 @@ void CmdTableManager::InitCmdTable() {

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

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

// kv
std::unique_ptr<BaseCmd> getPtr = std::make_unique<GetCmd>(kCmdNameGet, 2);
cmds_->insert(std::make_pair(kCmdNameGet, std::move(getPtr)));
Expand Down

0 comments on commit ed5d6ba

Please sign in to comment.