From 08304c0867dba9d9c4ae6ce18bf5317d89f85f51 Mon Sep 17 00:00:00 2001 From: Andrew Kryczka Date: Sat, 12 Mar 2016 13:50:20 -0800 Subject: [PATCH] Expose RepairDB as ldb command Summary: This will make it easier for admins and devs to use RepairDB. Test Plan: Tried deleting the manifest and verified it recovers: $ ldb --create_if_missing --db=/tmp/test_db put ok ok $ rm -f /tmp/test_db/MANIFEST-000001 $ ./ldb --db=/tmp/test_db repair $ ldb --db=/tmp/test_db get ok ok Reviewers: yhchiang, sdong, IslamAbdelRahman Reviewed By: IslamAbdelRahman Subscribers: leveldb, andrewkr, dhruba Differential Revision: https://reviews.facebook.net/D55359 --- tools/ldb_cmd.cc | 25 +++++++++++++++++++++++++ tools/ldb_cmd.h | 15 +++++++++++++++ tools/ldb_tool.cc | 1 + 3 files changed, 41 insertions(+) diff --git a/tools/ldb_cmd.cc b/tools/ldb_cmd.cc index 44571a879..be743955d 100644 --- a/tools/ldb_cmd.cc +++ b/tools/ldb_cmd.cc @@ -191,6 +191,8 @@ LDBCommand* LDBCommand::SelectCommand( return new InternalDumpCommand(cmdParams, option_map, flags); } else if (cmd == CheckConsistencyCommand::Name()) { return new CheckConsistencyCommand(cmdParams, option_map, flags); + } else if (cmd == RepairCommand::Name()) { + return new RepairCommand(cmdParams, option_map, flags); } return nullptr; } @@ -2144,6 +2146,29 @@ void CheckConsistencyCommand::DoCommand() { // ---------------------------------------------------------------------------- +RepairCommand::RepairCommand(const vector& params, + const map& options, + const vector& flags) + : LDBCommand(options, flags, false, BuildCmdLineOptions({})) {} + +void RepairCommand::Help(string& ret) { + ret.append(" "); + ret.append(RepairCommand::Name()); + ret.append("\n"); +} + +void RepairCommand::DoCommand() { + Options options = PrepareOptionsForOpenDB(); + Status status = RepairDB(db_path_, options); + if (status.ok()) { + printf("OK\n"); + } else { + exec_state_ = LDBCommandExecuteResult::Failed(status.ToString()); + } +} + +// ---------------------------------------------------------------------------- + namespace { void DumpSstFile(std::string filename, bool output_hex, bool show_properties) { diff --git a/tools/ldb_cmd.h b/tools/ldb_cmd.h index af6d6019c..cc3814c2f 100644 --- a/tools/ldb_cmd.h +++ b/tools/ldb_cmd.h @@ -883,6 +883,21 @@ public: static void Help(string& ret); }; +class RepairCommand : public LDBCommand { + public: + static string Name() { return "repair"; } + + RepairCommand(const vector& params, + const map& options, + const vector& flags); + + virtual void DoCommand() override; + + virtual bool NoDBOpen() override { return true; } + + static void Help(string& ret); +}; + } // namespace rocksdb #endif // ROCKSDB_LITE diff --git a/tools/ldb_tool.cc b/tools/ldb_tool.cc index 4e24997af..7ec56f115 100644 --- a/tools/ldb_tool.cc +++ b/tools/ldb_tool.cc @@ -77,6 +77,7 @@ public: ListColumnFamiliesCommand::Help(ret); DBFileDumperCommand::Help(ret); InternalDumpCommand::Help(ret); + RepairCommand::Help(ret); fprintf(stderr, "%s\n", ret.c_str()); }