Add --version and --help to ldb and sst_dump (#6951)

Summary:
as title
Pull Request resolved: https://github.com/facebook/rocksdb/pull/6951

Test Plan: tests included + manual

Reviewed By: ajkr

Differential Revision: D21918540

Pulled By: pdillinger

fbshipit-source-id: 79d4991f2a831214fc7e477a839ec19dbbace6c5
This commit is contained in:
Peter Dillinger 2020-06-09 10:01:12 -07:00 committed by Facebook GitHub Bot
parent 6a8ddd374d
commit edf74d1cb1
7 changed files with 62 additions and 17 deletions

View File

@ -11,7 +11,7 @@ namespace ROCKSDB_NAMESPACE {
class SSTDumpTool {
public:
int Run(int argc, char** argv, Options options = Options());
int Run(int argc, char const* const* argv, Options options = Options());
};
} // namespace ROCKSDB_NAMESPACE

View File

@ -78,7 +78,7 @@ class LDBCommand {
SelectCommand);
static LDBCommand* InitFromCmdLineArgs(
int argc, char** argv, const Options& options,
int argc, char const* const* argv, const Options& options,
const LDBOptions& ldb_options,
const std::vector<ColumnFamilyDescriptor>* column_families);
@ -269,11 +269,13 @@ class LDBCommand {
class LDBCommandRunner {
public:
static void PrintHelp(const LDBOptions& ldb_options, const char* exec_name);
static void PrintHelp(const LDBOptions& ldb_options, const char* exec_name,
bool to_stderr = true);
// Returns the status code to return. 0 is no error.
static int RunCommand(
int argc, char** argv, Options options, const LDBOptions& ldb_options,
int argc, char const* const* argv, Options options,
const LDBOptions& ldb_options,
const std::vector<ColumnFamilyDescriptor>* column_families);
};

View File

@ -97,7 +97,7 @@ void DumpSstFile(Options options, std::string filename, bool output_hex,
};
LDBCommand* LDBCommand::InitFromCmdLineArgs(
int argc, char** argv, const Options& options,
int argc, char const* const* argv, const Options& options,
const LDBOptions& ldb_options,
const std::vector<ColumnFamilyDescriptor>* column_families) {
std::vector<std::string> args;

View File

@ -41,6 +41,18 @@ class LdbCmdTest : public testing::Test {
std::shared_ptr<Env> env_guard_;
};
TEST_F(LdbCmdTest, HelpAndVersion) {
Options o;
o.env = TryLoadCustomOrDefaultEnv();
LDBOptions lo;
static const char* help[] = {"./ldb", "--help"};
ASSERT_EQ(0, LDBCommandRunner::RunCommand(2, help, o, lo, nullptr));
static const char* version[] = {"./ldb", "--version"};
ASSERT_EQ(0, LDBCommandRunner::RunCommand(2, version, o, lo, nullptr));
static const char* bad[] = {"./ldb", "--not_an_option"};
ASSERT_NE(0, LDBCommandRunner::RunCommand(2, bad, o, lo, nullptr));
}
TEST_F(LdbCmdTest, HexToString) {
// map input to expected outputs.
// odd number of "hex" half bytes doesn't make sense

View File

@ -13,7 +13,7 @@ namespace ROCKSDB_NAMESPACE {
LDBOptions::LDBOptions() {}
void LDBCommandRunner::PrintHelp(const LDBOptions& ldb_options,
const char* /*exec_name*/) {
const char* /*exec_name*/, bool to_stderr) {
std::string ret;
ret.append(ldb_options.print_help_header);
@ -99,22 +99,32 @@ void LDBCommandRunner::PrintHelp(const LDBOptions& ldb_options,
WriteExternalSstFilesCommand::Help(ret);
IngestExternalSstFilesCommand::Help(ret);
fprintf(stderr, "%s\n", ret.c_str());
fprintf(to_stderr ? stderr : stdout, "%s\n", ret.c_str());
}
int LDBCommandRunner::RunCommand(
int argc, char** argv, Options options, const LDBOptions& ldb_options,
int argc, char const* const* argv, Options options,
const LDBOptions& ldb_options,
const std::vector<ColumnFamilyDescriptor>* column_families) {
if (argc <= 2) {
PrintHelp(ldb_options, argv[0]);
return 1;
if (std::string(argv[1]) == "--version") {
printf("ldb from RocksDB %d.%d.%d\n", ROCKSDB_MAJOR, ROCKSDB_MINOR,
ROCKSDB_PATCH);
return 0;
} else if (std::string(argv[1]) == "--help") {
PrintHelp(ldb_options, argv[0], /*to_stderr*/ false);
return 0;
} else {
PrintHelp(ldb_options, argv[0], /*to_stderr*/ true);
return 1;
}
}
LDBCommand* cmdObj = LDBCommand::InitFromCmdLineArgs(
argc, argv, options, ldb_options, column_families);
if (cmdObj == nullptr) {
fprintf(stderr, "Unknown command\n");
PrintHelp(ldb_options, argv[0]);
PrintHelp(ldb_options, argv[0], /*to_stderr*/ true);
return 1;
}

View File

@ -129,6 +129,20 @@ class SSTDumpToolTest : public testing::Test {
}
};
TEST_F(SSTDumpToolTest, HelpAndVersion) {
Options opts;
opts.env = env();
ROCKSDB_NAMESPACE::SSTDumpTool tool;
static const char* help[] = {"./sst_dump", "--help"};
ASSERT_TRUE(!tool.Run(2, help, opts));
static const char* version[] = {"./sst_dump", "--version"};
ASSERT_TRUE(!tool.Run(2, version, opts));
static const char* bad[] = {"./sst_dump", "--not_an_option"};
ASSERT_TRUE(tool.Run(2, bad, opts));
}
TEST_F(SSTDumpToolTest, EmptyFilter) {
Options opts;
opts.env = env();

View File

@ -481,9 +481,9 @@ Status SstFileDumper::ReadTableProperties(
namespace {
void print_help() {
void print_help(bool to_stderr) {
fprintf(
stderr,
to_stderr ? stderr : stdout,
R"(sst_dump --file=<data_dir_OR_sst_file> [--command=check|scan|raw|recompress|identify]
--file=<data_dir_OR_sst_file>
Path to SST file or directory containing SST files
@ -572,7 +572,7 @@ bool ParseIntArg(const char* arg, const std::string arg_name,
}
} // namespace
int SSTDumpTool::Run(int argc, char** argv, Options options) {
int SSTDumpTool::Run(int argc, char const* const* argv, Options options) {
const char* env_uri = nullptr;
const char* dir_or_file = nullptr;
uint64_t read_num = std::numeric_limits<uint64_t>::max();
@ -696,10 +696,17 @@ int SSTDumpTool::Run(int argc, char** argv, Options options) {
"compression_level_to must be numeric", &tmp_val)) {
has_compression_level_to = true;
compress_level_to = static_cast<int>(tmp_val);
} else if (strcmp(argv[i], "--help") == 0) {
print_help(/*to_stderr*/ false);
return 0;
} else if (strcmp(argv[i], "--version") == 0) {
printf("sst_dump from RocksDB %d.%d.%d\n", ROCKSDB_MAJOR, ROCKSDB_MINOR,
ROCKSDB_PATCH);
return 0;
} else {
fprintf(stderr, "Unrecognized argument '%s'\n\n", argv[i]);
print_help();
exit(1);
print_help(/*to_stderr*/ true);
return 1;
}
}
@ -730,7 +737,7 @@ int SSTDumpTool::Run(int argc, char** argv, Options options) {
if (dir_or_file == nullptr) {
fprintf(stderr, "file or directory must be specified.\n\n");
print_help();
print_help(/*to_stderr*/ true);
exit(1);
}