2012-09-24 23:53:13 +00:00
|
|
|
// Copyright (c) 2012 Facebook. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2012-10-31 18:47:18 +00:00
|
|
|
#include "util/ldb_cmd.h"
|
2012-09-24 23:53:13 +00:00
|
|
|
|
2012-10-31 18:47:18 +00:00
|
|
|
namespace leveldb {
|
2012-09-24 23:53:13 +00:00
|
|
|
|
2012-10-31 18:47:18 +00:00
|
|
|
class LDBCommandRunner {
|
|
|
|
public:
|
2012-09-24 23:53:13 +00:00
|
|
|
|
2012-10-31 18:47:18 +00:00
|
|
|
static void PrintHelp(const char* exec_name) {
|
|
|
|
std::string ret;
|
|
|
|
ret.append("--- compact ----:\n");
|
|
|
|
ret.append(exec_name);
|
|
|
|
ret.append(" compact ");
|
|
|
|
Compactor::Help(ret);
|
2012-09-26 15:06:19 +00:00
|
|
|
|
2012-10-31 18:47:18 +00:00
|
|
|
ret.append("\n--- dump ----:\n");
|
|
|
|
ret.append(exec_name);
|
|
|
|
ret.append(" dump ");
|
|
|
|
DBDumper::Help(ret);
|
2012-09-26 15:06:19 +00:00
|
|
|
|
2012-10-31 18:47:18 +00:00
|
|
|
ret.append("\n---reduce_levels ----:\n");
|
|
|
|
ret.append(exec_name);
|
|
|
|
ret.append(" reduce_levels ");
|
|
|
|
ReduceDBLevels::Help(ret);
|
2012-09-26 15:06:19 +00:00
|
|
|
|
2012-10-31 18:47:18 +00:00
|
|
|
fprintf(stderr, "%s\n", ret.c_str());
|
2012-09-24 23:53:13 +00:00
|
|
|
}
|
|
|
|
|
2012-10-31 18:47:18 +00:00
|
|
|
static void RunCommand(int argc, char** argv) {
|
|
|
|
if (argc <= 2) {
|
|
|
|
PrintHelp(argv[0]);
|
|
|
|
exit(1);
|
2012-09-24 23:53:13 +00:00
|
|
|
}
|
2012-10-31 18:47:18 +00:00
|
|
|
const char* cmd = argv[1];
|
|
|
|
std::string db_name;
|
|
|
|
std::vector<std::string> args;
|
|
|
|
for (int i = 2; i < argc; i++) {
|
|
|
|
if (strncmp(argv[i], "--db=", strlen("--db=")) == 0) {
|
|
|
|
db_name = argv[i] + strlen("--db=");
|
2012-09-24 23:53:13 +00:00
|
|
|
} else {
|
2012-10-31 18:47:18 +00:00
|
|
|
args.push_back(argv[i]);
|
2012-09-24 23:53:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-31 18:47:18 +00:00
|
|
|
LDBCommand* cmdObj = NULL;
|
|
|
|
if (strncmp(cmd, "compact", strlen("compact")) == 0) {
|
|
|
|
// run compactor
|
|
|
|
cmdObj = new Compactor(db_name, args);
|
|
|
|
} else if (strncmp(cmd, "dump", strlen("dump")) == 0) {
|
|
|
|
// run dump
|
|
|
|
cmdObj = new DBDumper(db_name, args);
|
|
|
|
} else if (strncmp(cmd, "reduce_levels", strlen("reduce_levels")) == 0) {
|
|
|
|
// reduce db levels
|
|
|
|
cmdObj = new ReduceDBLevels(db_name, args);
|
2012-09-26 15:06:19 +00:00
|
|
|
} else {
|
2012-10-31 18:47:18 +00:00
|
|
|
fprintf(stderr, "Unknown command: %s\n", cmd);
|
|
|
|
PrintHelp(argv[0]);
|
2012-09-26 15:06:19 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-10-31 18:47:18 +00:00
|
|
|
cmdObj->Run();
|
|
|
|
LDBCommandExecuteResult ret = cmdObj->GetExecuteState();
|
|
|
|
fprintf(stderr, "%s\n", ret.ToString().c_str());
|
|
|
|
delete cmdObj;
|
2012-09-26 15:06:19 +00:00
|
|
|
}
|
2012-10-31 18:47:18 +00:00
|
|
|
};
|
2012-09-26 15:06:19 +00:00
|
|
|
|
2012-10-31 18:47:18 +00:00
|
|
|
}
|
2012-09-26 15:06:19 +00:00
|
|
|
|
2012-10-31 18:47:18 +00:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
leveldb::LDBCommandRunner::RunCommand(argc, argv);
|
2012-09-26 15:06:19 +00:00
|
|
|
}
|