From 5c2f13fb14540f8b57337120811bf755e132c6fb Mon Sep 17 00:00:00 2001 From: Aaron Gao Date: Thu, 27 Jun 2019 11:08:45 -0700 Subject: [PATCH] add create_column_family and drop_column_family cmd to ldb tool (#5503) Summary: `create_column_family` cmd already exists but was somehow missed in the help message. also add `drop_column_family` cmd which can drop a cf without opening db. Pull Request resolved: https://github.com/facebook/rocksdb/pull/5503 Test Plan: Updated existing ldb_test.py to test deleting a column family. Differential Revision: D16018414 Pulled By: lightmark fbshipit-source-id: 1fc33680b742104fea86b10efc8499f79e722301 --- tools/ldb_cmd.cc | 42 ++++++++++++++++++++++++++++++++++++++++++ tools/ldb_cmd_impl.h | 17 +++++++++++++++++ tools/ldb_test.py | 2 ++ tools/ldb_tool.cc | 2 ++ 4 files changed, 63 insertions(+) diff --git a/tools/ldb_cmd.cc b/tools/ldb_cmd.cc index 49489173c..a1507b188 100644 --- a/tools/ldb_cmd.cc +++ b/tools/ldb_cmd.cc @@ -223,6 +223,10 @@ LDBCommand* LDBCommand::SelectCommand(const ParsedParams& parsed_params) { return new CreateColumnFamilyCommand(parsed_params.cmd_params, parsed_params.option_map, parsed_params.flags); + } else if (parsed_params.cmd == DropColumnFamilyCommand::Name()) { + return new DropColumnFamilyCommand(parsed_params.cmd_params, + parsed_params.option_map, + parsed_params.flags); } else if (parsed_params.cmd == DBFileDumperCommand::Name()) { return new DBFileDumperCommand(parsed_params.cmd_params, parsed_params.option_map, @@ -1125,6 +1129,44 @@ void CreateColumnFamilyCommand::DoCommand() { CloseDB(); } +void DropColumnFamilyCommand::Help(std::string& ret) { + ret.append(" "); + ret.append(DropColumnFamilyCommand::Name()); + ret.append(" --db= "); + ret.append("\n"); +} + +DropColumnFamilyCommand::DropColumnFamilyCommand( + const std::vector& params, + const std::map& options, + const std::vector& flags) + : LDBCommand(options, flags, true, {ARG_DB}) { + if (params.size() != 1) { + exec_state_ = LDBCommandExecuteResult::Failed( + "The name of column family to drop must be specified"); + } else { + cf_name_to_drop_ = params[0]; + } +} + +void DropColumnFamilyCommand::DoCommand() { + auto iter = cf_handles_.find(cf_name_to_drop_); + if (iter == cf_handles_.end()) { + exec_state_ = LDBCommandExecuteResult::Failed( + "Column family: " + cf_name_to_drop_ + " doesn't exist in db."); + return; + } + ColumnFamilyHandle* cf_handle_to_drop = iter->second; + Status st = db_->DropColumnFamily(cf_handle_to_drop); + if (st.ok()) { + fprintf(stdout, "OK\n"); + } else { + exec_state_ = LDBCommandExecuteResult::Failed( + "Fail to drop column family: " + st.ToString()); + } + CloseDB(); +} + // ---------------------------------------------------------------------------- namespace { diff --git a/tools/ldb_cmd_impl.h b/tools/ldb_cmd_impl.h index 868c81f44..23bafe682 100644 --- a/tools/ldb_cmd_impl.h +++ b/tools/ldb_cmd_impl.h @@ -205,6 +205,23 @@ class CreateColumnFamilyCommand : public LDBCommand { std::string new_cf_name_; }; +class DropColumnFamilyCommand : public LDBCommand { + public: + static std::string Name() { return "drop_column_family"; } + + DropColumnFamilyCommand(const std::vector& params, + const std::map& options, + const std::vector& flags); + + static void Help(std::string& ret); + virtual void DoCommand() override; + + virtual bool NoDBOpen() override { return false; } + + private: + std::string cf_name_to_drop_; +}; + class ReduceDBLevelsCommand : public LDBCommand { public: static std::string Name() { return "reduce_levels"; } diff --git a/tools/ldb_test.py b/tools/ldb_test.py index e64e76ee7..26167ee83 100644 --- a/tools/ldb_test.py +++ b/tools/ldb_test.py @@ -553,8 +553,10 @@ class LDBTestCase(unittest.TestCase): "1") self.assertRunOK("get cf3_1 --column_family=three", "3") + self.assertRunOK("drop_column_family three", "OK") # non-existing column family. self.assertRunFAIL("get cf3_1 --column_family=four") + self.assertRunFAIL("drop_column_family four") def testIngestExternalSst(self): print "Running testIngestExternalSst..." diff --git a/tools/ldb_tool.cc b/tools/ldb_tool.cc index fe307eab7..2813f6c6e 100644 --- a/tools/ldb_tool.cc +++ b/tools/ldb_tool.cc @@ -82,6 +82,8 @@ void LDBCommandRunner::PrintHelp(const LDBOptions& ldb_options, DBLoaderCommand::Help(ret); ManifestDumpCommand::Help(ret); ListColumnFamiliesCommand::Help(ret); + CreateColumnFamilyCommand::Help(ret); + DropColumnFamilyCommand::Help(ret); DBFileDumperCommand::Help(ret); InternalDumpCommand::Help(ret); RepairCommand::Help(ret);