diff --git a/tools/ldb_cmd.cc b/tools/ldb_cmd.cc index 9d09b512e..9b8195dea 100644 --- a/tools/ldb_cmd.cc +++ b/tools/ldb_cmd.cc @@ -21,6 +21,7 @@ #include "rocksdb/cache.h" #include "rocksdb/table_properties.h" #include "rocksdb/utilities/backupable_db.h" +#include "rocksdb/utilities/checkpoint.h" #include "rocksdb/utilities/object_registry.h" #include "rocksdb/write_batch.h" #include "rocksdb/write_buffer_manager.h" @@ -222,6 +223,10 @@ LDBCommand* LDBCommand::SelectCommand(const ParsedParams& parsed_params) { return new CheckConsistencyCommand(parsed_params.cmd_params, parsed_params.option_map, parsed_params.flags); + } else if (parsed_params.cmd == CheckPointCommand::Name()) { + return new CheckPointCommand(parsed_params.cmd_params, + parsed_params.option_map, + parsed_params.flags); } else if (parsed_params.cmd == RepairCommand::Name()) { return new RepairCommand(parsed_params.cmd_params, parsed_params.option_map, parsed_params.flags); @@ -2564,6 +2569,47 @@ void CheckConsistencyCommand::DoCommand() { // ---------------------------------------------------------------------------- +const std::string CheckPointCommand::ARG_CHECKPOINT_DIR = "checkpoint_dir"; + +CheckPointCommand::CheckPointCommand( + const std::vector& params, + const std::map& options, + const std::vector& flags) + : LDBCommand(options, flags, false /* is_read_only */, + BuildCmdLineOptions({ARG_CHECKPOINT_DIR})) { + auto itr = options.find(ARG_CHECKPOINT_DIR); + if (itr == options.end()) { + exec_state_ = LDBCommandExecuteResult::Failed( + "--" + ARG_CHECKPOINT_DIR + ": missing checkpoint directory"); + } else { + checkpoint_dir_ = itr->second; + } +} + +void CheckPointCommand::Help(std::string& ret) { + ret.append(" "); + ret.append(CheckPointCommand::Name()); + ret.append(" [--" + ARG_CHECKPOINT_DIR + "] "); + ret.append("\n"); +} + +void CheckPointCommand::DoCommand() { + if (!db_) { + assert(GetExecuteState().IsFailed()); + return; + } + Checkpoint* checkpoint; + Status status = Checkpoint::Create(db_, &checkpoint); + status = checkpoint->CreateCheckpoint(checkpoint_dir_); + if (status.ok()) { + printf("OK\n"); + } else { + exec_state_ = LDBCommandExecuteResult::Failed(status.ToString()); + } +} + +// ---------------------------------------------------------------------------- + RepairCommand::RepairCommand(const std::vector& params, const std::map& options, const std::vector& flags) diff --git a/tools/ldb_cmd_impl.h b/tools/ldb_cmd_impl.h index 1ac86132e..40437f46f 100644 --- a/tools/ldb_cmd_impl.h +++ b/tools/ldb_cmd_impl.h @@ -448,6 +448,23 @@ class CheckConsistencyCommand : public LDBCommand { static void Help(std::string& ret); }; +class CheckPointCommand : public LDBCommand { + public: + static std::string Name() { return "checkpoint"; } + + CheckPointCommand(const std::vector& params, + const std::map& options, + const std::vector& flags); + + virtual void DoCommand() override; + + static void Help(std::string& ret); + + std::string checkpoint_dir_; + private: + static const std::string ARG_CHECKPOINT_DIR; +}; + class RepairCommand : public LDBCommand { public: static std::string Name() { return "repair"; } @@ -503,4 +520,5 @@ class RestoreCommand : public BackupableCommand { virtual bool NoDBOpen() override { return true; } static void Help(std::string& ret); }; + } // namespace rocksdb diff --git a/tools/ldb_tool.cc b/tools/ldb_tool.cc index 291cf56a1..8a138a0eb 100644 --- a/tools/ldb_tool.cc +++ b/tools/ldb_tool.cc @@ -82,6 +82,7 @@ void LDBCommandRunner::PrintHelp(const char* exec_name) { RepairCommand::Help(ret); BackupCommand::Help(ret); RestoreCommand::Help(ret); + CheckPointCommand::Help(ret); fprintf(stderr, "%s\n", ret.c_str()); }