[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
main
Arun Sharma 9 years ago
parent aab91b8d8f
commit 5c06e0814c
  1. 14
      include/rocksdb/utilities/ldb_cmd.h
  2. 14
      tools/ldb_cmd.cc
  3. 4
      tools/reduce_levels_test.cc

@ -53,10 +53,12 @@ class LDBCommand {
static const std::string ARG_CREATE_IF_MISSING;
static const std::string ARG_NO_VALUE;
template <typename Selector>
static LDBCommand* InitFromCmdLineArgs(
const std::vector<std::string>& args, const Options& options,
const LDBOptions& ldb_options,
const std::vector<ColumnFamilyDescriptor>* column_families);
const std::vector<ColumnFamilyDescriptor>* column_families,
Selector selector = SelectCommand);
static LDBCommand* InitFromCmdLineArgs(
int argc, char** argv, const Options& options,
@ -105,6 +107,11 @@ class LDBCommand {
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:
LDBCommandExecuteResult exec_state_;
std::string db_path_;
@ -223,11 +230,6 @@ class LDBCommand {
* Otherwise an exception is thrown.
*/
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 {

@ -86,7 +86,8 @@ LDBCommand* LDBCommand::InitFromCmdLineArgs(
for (int i = 1; i < argc; 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.
* Returns nullptr if the command-line cannot be parsed.
*/
template <typename Selector>
LDBCommand* LDBCommand::InitFromCmdLineArgs(
const vector<string>& args, const Options& 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.
map<string, string> option_map;
@ -137,12 +140,7 @@ LDBCommand* LDBCommand::InitFromCmdLineArgs(
string cmd = cmdTokens[0];
vector<string> cmdParams(cmdTokens.begin()+1, cmdTokens.end());
LDBCommand* command = LDBCommand::SelectCommand(
cmd,
cmdParams,
option_map,
flags
);
LDBCommand* command = selector(cmd, cmdParams, option_map, flags);
if (command) {
command->SetDBOptions(options);

@ -93,8 +93,8 @@ Status ReduceLevelTest::OpenDB(bool create_if_missing, int num_levels) {
bool ReduceLevelTest::ReduceLevels(int target_level) {
std::vector<std::string> args = rocksdb::ReduceDBLevelsCommand::PrepareArgs(
dbname_, target_level, false);
LDBCommand* level_reducer =
LDBCommand::InitFromCmdLineArgs(args, Options(), LDBOptions(), nullptr);
LDBCommand* level_reducer = LDBCommand::InitFromCmdLineArgs(
args, Options(), LDBOptions(), nullptr, LDBCommand::SelectCommand);
level_reducer->Run();
bool is_succeed = level_reducer->GetExecuteState().IsSucceed();
delete level_reducer;

Loading…
Cancel
Save