-
Notifications
You must be signed in to change notification settings - Fork 63
/
cmd_kv.cc
51 lines (42 loc) · 1.42 KB
/
cmd_kv.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* 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_kv.h"
#include "store.h"
namespace pikiwidb {
GetCmd::GetCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, CmdFlagsReadonly, AclCategoryRead | AclCategoryString) {}
bool GetCmd::DoInitial(CmdContext& ctx) {
ctx.key_ = ctx.argv_[1];
return true;
}
void GetCmd::DoCmd(CmdContext& ctx) {
PObject* value;
PError err = PSTORE.GetValueByType(ctx.key_, value, PType_string);
if (err != PError_ok) {
if (err == PError_notExist) {
ctx.AppendString("");
} else {
ctx.SetRes(CmdRes::kSyntaxErr, "get key error");
}
return;
}
auto str = GetDecodedString(value);
std::string reply(str->c_str(), str->size());
ctx.AppendString(reply);
}
SetCmd::SetCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, CmdFlagsWrite, AclCategoryWrite | AclCategoryString) {}
bool SetCmd::DoInitial(CmdContext& ctx) {
ctx.key_ = ctx.argv_[1];
return true;
}
void SetCmd::DoCmd(CmdContext& ctx) {
PSTORE.ClearExpire(ctx.argv_[1]); // clear key's old ttl
PSTORE.SetValue(ctx.argv_[1], PObject::CreateString(ctx.argv_[2]));
ctx.SetRes(CmdRes::kOk);
}
} // namespace pikiwidb