[ldb] Templatize the Selector
Summary: So a customized ldb tool can pass it's own Selector. Such a selector is expected to call LDBCommand::SelectCommand and then add some of its own customized commands Test Plan: make ldb Reviewers: sdong, IslamAbdelRahman Reviewed By: IslamAbdelRahman Subscribers: andrewkr, dhruba Differential Revision: https://reviews.facebook.net/D57249
This commit is contained in:
parent
aab91b8d8f
commit
5c06e0814c
|
@ -53,10 +53,12 @@ class LDBCommand {
|
||||||
static const std::string ARG_CREATE_IF_MISSING;
|
static const std::string ARG_CREATE_IF_MISSING;
|
||||||
static const std::string ARG_NO_VALUE;
|
static const std::string ARG_NO_VALUE;
|
||||||
|
|
||||||
|
template <typename Selector>
|
||||||
static LDBCommand* InitFromCmdLineArgs(
|
static LDBCommand* InitFromCmdLineArgs(
|
||||||
const std::vector<std::string>& args, const Options& options,
|
const std::vector<std::string>& args, const Options& options,
|
||||||
const LDBOptions& ldb_options,
|
const LDBOptions& ldb_options,
|
||||||
const std::vector<ColumnFamilyDescriptor>* column_families);
|
const std::vector<ColumnFamilyDescriptor>* column_families,
|
||||||
|
Selector selector = SelectCommand);
|
||||||
|
|
||||||
static LDBCommand* InitFromCmdLineArgs(
|
static LDBCommand* InitFromCmdLineArgs(
|
||||||
int argc, char** argv, const Options& options,
|
int argc, char** argv, const Options& options,
|
||||||
|
@ -105,6 +107,11 @@ class LDBCommand {
|
||||||
|
|
||||||
static const char* DELIM;
|
static const char* DELIM;
|
||||||
|
|
||||||
|
static LDBCommand* SelectCommand(
|
||||||
|
const std::string& cmd, const std::vector<std::string>& cmdParams,
|
||||||
|
const std::map<std::string, std::string>& option_map,
|
||||||
|
const std::vector<std::string>& flags);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
LDBCommandExecuteResult exec_state_;
|
LDBCommandExecuteResult exec_state_;
|
||||||
std::string db_path_;
|
std::string db_path_;
|
||||||
|
@ -223,11 +230,6 @@ class LDBCommand {
|
||||||
* Otherwise an exception is thrown.
|
* Otherwise an exception is thrown.
|
||||||
*/
|
*/
|
||||||
bool StringToBool(std::string val);
|
bool StringToBool(std::string val);
|
||||||
|
|
||||||
static LDBCommand* SelectCommand(
|
|
||||||
const std::string& cmd, const std::vector<std::string>& cmdParams,
|
|
||||||
const std::map<std::string, std::string>& option_map,
|
|
||||||
const std::vector<std::string>& flags);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class LDBCommandRunner {
|
class LDBCommandRunner {
|
||||||
|
|
|
@ -86,7 +86,8 @@ LDBCommand* LDBCommand::InitFromCmdLineArgs(
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
args.push_back(argv[i]);
|
args.push_back(argv[i]);
|
||||||
}
|
}
|
||||||
return InitFromCmdLineArgs(args, options, ldb_options, column_families);
|
return InitFromCmdLineArgs(args, options, ldb_options, column_families,
|
||||||
|
SelectCommand);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -99,10 +100,12 @@ LDBCommand* LDBCommand::InitFromCmdLineArgs(
|
||||||
* Command name is not included in args.
|
* Command name is not included in args.
|
||||||
* Returns nullptr if the command-line cannot be parsed.
|
* Returns nullptr if the command-line cannot be parsed.
|
||||||
*/
|
*/
|
||||||
|
template <typename Selector>
|
||||||
LDBCommand* LDBCommand::InitFromCmdLineArgs(
|
LDBCommand* LDBCommand::InitFromCmdLineArgs(
|
||||||
const vector<string>& args, const Options& options,
|
const vector<string>& args, const Options& options,
|
||||||
const LDBOptions& ldb_options,
|
const LDBOptions& ldb_options,
|
||||||
const std::vector<ColumnFamilyDescriptor>* column_families) {
|
const std::vector<ColumnFamilyDescriptor>* column_families,
|
||||||
|
Selector selector) {
|
||||||
// --x=y command line arguments are added as x->y map entries.
|
// --x=y command line arguments are added as x->y map entries.
|
||||||
map<string, string> option_map;
|
map<string, string> option_map;
|
||||||
|
|
||||||
|
@ -137,12 +140,7 @@ LDBCommand* LDBCommand::InitFromCmdLineArgs(
|
||||||
|
|
||||||
string cmd = cmdTokens[0];
|
string cmd = cmdTokens[0];
|
||||||
vector<string> cmdParams(cmdTokens.begin()+1, cmdTokens.end());
|
vector<string> cmdParams(cmdTokens.begin()+1, cmdTokens.end());
|
||||||
LDBCommand* command = LDBCommand::SelectCommand(
|
LDBCommand* command = selector(cmd, cmdParams, option_map, flags);
|
||||||
cmd,
|
|
||||||
cmdParams,
|
|
||||||
option_map,
|
|
||||||
flags
|
|
||||||
);
|
|
||||||
|
|
||||||
if (command) {
|
if (command) {
|
||||||
command->SetDBOptions(options);
|
command->SetDBOptions(options);
|
||||||
|
|
|
@ -93,8 +93,8 @@ Status ReduceLevelTest::OpenDB(bool create_if_missing, int num_levels) {
|
||||||
bool ReduceLevelTest::ReduceLevels(int target_level) {
|
bool ReduceLevelTest::ReduceLevels(int target_level) {
|
||||||
std::vector<std::string> args = rocksdb::ReduceDBLevelsCommand::PrepareArgs(
|
std::vector<std::string> args = rocksdb::ReduceDBLevelsCommand::PrepareArgs(
|
||||||
dbname_, target_level, false);
|
dbname_, target_level, false);
|
||||||
LDBCommand* level_reducer =
|
LDBCommand* level_reducer = LDBCommand::InitFromCmdLineArgs(
|
||||||
LDBCommand::InitFromCmdLineArgs(args, Options(), LDBOptions(), nullptr);
|
args, Options(), LDBOptions(), nullptr, LDBCommand::SelectCommand);
|
||||||
level_reducer->Run();
|
level_reducer->Run();
|
||||||
bool is_succeed = level_reducer->GetExecuteState().IsSucceed();
|
bool is_succeed = level_reducer->GetExecuteState().IsSucceed();
|
||||||
delete level_reducer;
|
delete level_reducer;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user