Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance ldb_cmd_tool to enable user pass in customized cfds #12261

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions tools/ldb_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ LDBCommand* LDBCommand::InitFromCmdLineArgs(
LDBCommand* 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,
const std::function<LDBCommand*(const ParsedParams&)>& selector) {
// --x=y command line arguments are added as x->y map entries in
// parsed_params.option_map.
Expand Down Expand Up @@ -200,6 +200,7 @@ LDBCommand* LDBCommand::InitFromCmdLineArgs(
if (command) {
command->SetDBOptions(options);
command->SetLDBOptions(ldb_options);
command->SetColumnFamilies(column_families);
}
return command;
}
Expand Down Expand Up @@ -930,10 +931,12 @@ void LDBCommand::OverrideBaseCFOptions(ColumnFamilyOptions* cf_opts) {
// Second, overrides the options according to the CLI arguments and the
// specific subcommand being run.
void LDBCommand::PrepareOptions() {
std::vector<ColumnFamilyDescriptor> column_families_from_options;

if (!create_if_missing_ && try_load_options_) {
config_options_.env = options_.env;
Status s = LoadLatestOptions(config_options_, db_path_, &options_,
&column_families_);
&column_families_from_options);
if (!s.ok() && !s.IsNotFound()) {
// Option file exists but load option file error.
std::string current_version = std::to_string(ROCKSDB_MAJOR) + "." +
Expand All @@ -959,7 +962,7 @@ void LDBCommand::PrepareOptions() {
}

// If merge operator is not set, set a string append operator.
for (auto& cf_entry : column_families_) {
for (auto& cf_entry : column_families_from_options) {
if (!cf_entry.options.merge_operator) {
cf_entry.options.merge_operator =
MergeOperators::CreateStringAppendOperator(':');
Expand All @@ -977,22 +980,29 @@ void LDBCommand::PrepareOptions() {
}

if (column_families_.empty()) {
// Reads the MANIFEST to figure out what column families exist. In this
// case, the option overrides from the CLI argument/specific subcommand
// apply to all column families.
std::vector<std::string> cf_list;
Status st = DB::ListColumnFamilies(options_, db_path_, &cf_list);
// It is possible the DB doesn't exist yet, for "create if not
// existing" case. The failure is ignored here. We rely on DB::Open()
// to give us the correct error message for problem with opening
// existing DB.
if (st.ok() && cf_list.size() > 1) {
// Ignore single column family DB.
for (const auto& cf_name : cf_list) {
column_families_.emplace_back(cf_name, options_);
// column_families not set. Either set it from MANIFEST or OPTIONS file.
if (column_families_from_options.empty()) {
// Reads the MANIFEST to figure out what column families exist. In this
// case, the option overrides from the CLI argument/specific subcommand
// apply to all column families.
std::vector<std::string> cf_list;
Status st = DB::ListColumnFamilies(options_, db_path_, &cf_list);
// It is possible the DB doesn't exist yet, for "create if not
// existing" case. The failure is ignored here. We rely on DB::Open()
// to give us the correct error message for problem with opening
// existing DB.
if (st.ok() && cf_list.size() > 1) {
// Ignore single column family DB.
for (const auto& cf_name : cf_list) {
column_families_.emplace_back(cf_name, options_);
}
}
} else {
SetColumnFamilies(&column_families_from_options);
}
} else {
}

if (!column_families_from_options.empty()) {
// We got column families from the OPTIONS file. In this case, the option
// overrides from the CLI argument/specific subcommand only apply to the
// column family specified by `--column_family_name`.
Expand Down
5 changes: 3 additions & 2 deletions tools/ldb_cmd_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,8 @@ TEST_F(LdbCmdTest, CustomComparator) {
std::string dbname = test::PerThreadDBPath(env, "ldb_cmd_test");
DB* db = nullptr;

std::vector<ColumnFamilyDescriptor> cfds = {{kDefaultColumnFamilyName, opts}};
std::vector<ColumnFamilyDescriptor> cfds = {
{kDefaultColumnFamilyName, opts}, {"cf1", opts}, {"cf2", opts}};
std::vector<ColumnFamilyHandle*> handles;
ASSERT_OK(DestroyDB(dbname, opts));
ASSERT_OK(DB::Open(opts, dbname, cfds, &handles, &db));
Expand All @@ -1250,7 +1251,7 @@ TEST_F(LdbCmdTest, CustomComparator) {
char* argv[] = {arg1, const_cast<char*>(arg2.c_str()), arg3, arg4};

ASSERT_EQ(0,
LDBCommandRunner::RunCommand(4, argv, opts, LDBOptions(), nullptr));
LDBCommandRunner::RunCommand(4, argv, opts, LDBOptions(), &cfds));
}

} // namespace ROCKSDB_NAMESPACE
Expand Down
Loading