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);