-
Notifications
You must be signed in to change notification settings - Fork 63
/
cmd_table_manager.cc
62 lines (48 loc) · 1.9 KB
/
cmd_table_manager.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
52
53
54
55
56
57
58
59
60
61
62
/*
* 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_table_manager.h"
#include <memory>
#include "cmd_admin.h"
#include "cmd_kv.h"
namespace pikiwidb {
CmdTableManager::CmdTableManager() {
cmds_ = std::make_unique<CmdTable>();
cmds_->reserve(300);
}
void CmdTableManager::InitCmdTable() {
std::unique_lock wl(mutex_);
// admin
auto configPtr = std::make_unique<CmdConfig>(kCmdNameConfig, -2);
configPtr->AddSubCmd(std::make_unique<CmdConfigGet>("get", -3));
configPtr->AddSubCmd(std::make_unique<CmdConfigSet>("set", -4));
cmds_->insert(std::make_pair(kCmdNameConfig, std::move(configPtr)));
// kv
std::unique_ptr<BaseCmd> getPtr = std::make_unique<GetCmd>(kCmdNameGet, 2);
cmds_->insert(std::make_pair(kCmdNameGet, std::move(getPtr)));
std::unique_ptr<BaseCmd> setPtr = std::make_unique<SetCmd>(kCmdNameSet, -3);
cmds_->insert(std::make_pair(kCmdNameSet, std::move(setPtr)));
}
std::pair<BaseCmd*, CmdRes::CmdRet> CmdTableManager::GetCommand(const std::string& cmdName, CmdContext& ctx) {
std::shared_lock rl(mutex_);
auto cmd = cmds_->find(cmdName);
if (cmd == cmds_->end()) {
return std::pair(nullptr, CmdRes::kSyntaxErr);
}
if (cmd->second->HasSubCommand()) {
if (ctx.argv_.size() < 2) {
return std::pair(nullptr, CmdRes::kInvalidParameter);
}
return std::pair(cmd->second->GetSubCmd(ctx.argv_[1]), CmdRes::kSyntaxErr);
}
return std::pair(cmd->second.get(), CmdRes::kSyntaxErr);
}
bool CmdTableManager::CmdExist(const std::string& cmd) const {
std::shared_lock rl(mutex_);
return cmds_->find(cmd) != cmds_->end();
}
uint32_t CmdTableManager::GetCmdId() { return ++cmdId_; }
} // namespace pikiwidb