From 53b693f5feb0316e87ddfe189b51238ec2967d48 Mon Sep 17 00:00:00 2001 From: Andrew Kryczka Date: Tue, 15 Nov 2016 15:49:15 -0800 Subject: [PATCH] ldb support for range delete Summary: Add a subcommand to ldb with which we can delete a range of keys. Closes https://github.com/facebook/rocksdb/pull/1521 Differential Revision: D4186338 Pulled By: ajkr fbshipit-source-id: b8e9861 --- tools/ldb_cmd.cc | 51 ++++++++++++++++++++++++++++++++++++++++++++ tools/ldb_cmd_impl.h | 17 +++++++++++++++ tools/ldb_tool.cc | 1 + 3 files changed, 69 insertions(+) diff --git a/tools/ldb_cmd.cc b/tools/ldb_cmd.cc index fd1348d59..da170776f 100644 --- a/tools/ldb_cmd.cc +++ b/tools/ldb_cmd.cc @@ -167,6 +167,10 @@ LDBCommand* LDBCommand::SelectCommand(const ParsedParams& parsed_params) { } else if (parsed_params.cmd == DeleteCommand::Name()) { return new DeleteCommand(parsed_params.cmd_params, parsed_params.option_map, parsed_params.flags); + } else if (parsed_params.cmd == DeleteRangeCommand::Name()) { + return new DeleteRangeCommand(parsed_params.cmd_params, + parsed_params.option_map, + parsed_params.flags); } else if (parsed_params.cmd == ApproxSizeCommand::Name()) { return new ApproxSizeCommand(parsed_params.cmd_params, parsed_params.option_map, parsed_params.flags); @@ -1854,6 +1858,14 @@ class InMemoryHandler : public WriteBatch::Handler { return Status::OK(); } + virtual Status DeleteRangeCF(uint32_t cf, const Slice& begin_key, + const Slice& end_key) override { + row_ << "DELETE_RANGE(" << cf << ") : "; + row_ << LDBCommand::StringToHex(begin_key.ToString()) << " "; + row_ << LDBCommand::StringToHex(end_key.ToString()) << " "; + return Status::OK(); + } + virtual Status MarkBeginPrepare() override { row_ << "BEGIN_PREARE "; return Status::OK(); @@ -2346,6 +2358,45 @@ void DeleteCommand::DoCommand() { } } +DeleteRangeCommand::DeleteRangeCommand( + const std::vector& params, + const std::map& options, + const std::vector& flags) + : LDBCommand(options, flags, false, + BuildCmdLineOptions({ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX})) { + if (params.size() != 2) { + exec_state_ = LDBCommandExecuteResult::Failed( + "begin and end keys must be specified for the delete command"); + } else { + begin_key_ = params.at(0); + end_key_ = params.at(1); + if (is_key_hex_) { + begin_key_ = HexToString(begin_key_); + end_key_ = HexToString(end_key_); + } + } +} + +void DeleteRangeCommand::Help(std::string& ret) { + ret.append(" "); + ret.append(DeleteRangeCommand::Name() + " "); + ret.append("\n"); +} + +void DeleteRangeCommand::DoCommand() { + if (!db_) { + assert(GetExecuteState().IsFailed()); + return; + } + Status st = + db_->DeleteRange(WriteOptions(), GetCfHandle(), begin_key_, end_key_); + if (st.ok()) { + fprintf(stdout, "OK\n"); + } else { + exec_state_ = LDBCommandExecuteResult::Failed(st.ToString()); + } +} + PutCommand::PutCommand(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 ceae95a76..27a822f0f 100644 --- a/tools/ldb_cmd_impl.h +++ b/tools/ldb_cmd_impl.h @@ -374,6 +374,23 @@ class DeleteCommand : public LDBCommand { std::string key_; }; +class DeleteRangeCommand : public LDBCommand { + public: + static std::string Name() { return "deleterange"; } + + DeleteRangeCommand(const std::vector& params, + const std::map& options, + const std::vector& flags); + + virtual void DoCommand() override; + + static void Help(std::string& ret); + + private: + std::string begin_key_; + std::string end_key_; +}; + class PutCommand : public LDBCommand { public: static std::string Name() { return "put"; } diff --git a/tools/ldb_tool.cc b/tools/ldb_tool.cc index a606af171..a2bee1d5d 100644 --- a/tools/ldb_tool.cc +++ b/tools/ldb_tool.cc @@ -62,6 +62,7 @@ void LDBCommandRunner::PrintHelp(const char* exec_name) { BatchPutCommand::Help(ret); ScanCommand::Help(ret); DeleteCommand::Help(ret); + DeleteRangeCommand::Help(ret); DBQuerierCommand::Help(ret); ApproxSizeCommand::Help(ret); CheckConsistencyCommand::Help(ret);