From 26768edb659a45737966b956c3032e4d77b971b1 Mon Sep 17 00:00:00 2001 From: yaphet <4414314+darionyaphet@users.noreply.github.com> Date: Tue, 10 May 2022 16:37:19 -0700 Subject: [PATCH] Support single delete in ldb (#9469) Summary: Pull Request resolved: https://github.com/facebook/rocksdb/pull/9469 Reviewed By: riversand963 Differential Revision: D33953484 fbshipit-source-id: f4e84a2d9865957d744c7e84ff02ffbb0a62b0a8 --- tools/ldb_cmd.cc | 40 +++++++++++++++ tools/ldb_cmd_impl.h | 120 +++++++++++++++++++++++++------------------ 2 files changed, 111 insertions(+), 49 deletions(-) diff --git a/tools/ldb_cmd.cc b/tools/ldb_cmd.cc index a7ba92b73..26d1cb1d0 100644 --- a/tools/ldb_cmd.cc +++ b/tools/ldb_cmd.cc @@ -214,6 +214,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 == SingleDeleteCommand::Name()) { + return new SingleDeleteCommand(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, @@ -3063,6 +3067,42 @@ void DeleteCommand::DoCommand() { } } +SingleDeleteCommand::SingleDeleteCommand( + 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() != 1) { + exec_state_ = LDBCommandExecuteResult::Failed( + "KEY must be specified for the single delete command"); + } else { + key_ = params.at(0); + if (is_key_hex_) { + key_ = HexToString(key_); + } + } +} + +void SingleDeleteCommand::Help(std::string& ret) { + ret.append(" "); + ret.append(SingleDeleteCommand::Name() + " "); + ret.append("\n"); +} + +void SingleDeleteCommand::DoCommand() { + if (!db_) { + assert(GetExecuteState().IsFailed()); + return; + } + Status st = db_->SingleDelete(WriteOptions(), GetCfHandle(), key_); + if (st.ok()) { + fprintf(stdout, "OK\n"); + } else { + exec_state_ = LDBCommandExecuteResult::Failed(st.ToString()); + } +} + DeleteRangeCommand::DeleteRangeCommand( const std::vector& params, const std::map& options, diff --git a/tools/ldb_cmd_impl.h b/tools/ldb_cmd_impl.h index 18af43574..17848f2cd 100644 --- a/tools/ldb_cmd_impl.h +++ b/tools/ldb_cmd_impl.h @@ -24,7 +24,7 @@ class CompactorCommand : public LDBCommand { static void Help(std::string& ret); - virtual void DoCommand() override; + void DoCommand() override; private: bool null_from_; @@ -43,7 +43,7 @@ class DBFileDumperCommand : public LDBCommand { static void Help(std::string& ret); - virtual void DoCommand() override; + void DoCommand() override; private: bool decode_blob_index_; @@ -61,7 +61,7 @@ class DBLiveFilesMetadataDumperCommand : public LDBCommand { static void Help(std::string& ret); - virtual void DoCommand() override; + void DoCommand() override; private: bool sort_by_filename_; @@ -79,7 +79,7 @@ class DBDumperCommand : public LDBCommand { static void Help(std::string& ret); - virtual void DoCommand() override; + void DoCommand() override; private: /** @@ -127,7 +127,7 @@ class InternalDumpCommand : public LDBCommand { static void Help(std::string& ret); - virtual void DoCommand() override; + void DoCommand() override; private: bool has_from_; @@ -160,9 +160,10 @@ class DBLoaderCommand : public LDBCommand { const std::vector& flags); static void Help(std::string& ret); - virtual void DoCommand() override; - virtual void OverrideBaseOptions() override; + void DoCommand() override; + + void OverrideBaseOptions() override; private: bool disable_wal_; @@ -183,9 +184,10 @@ class ManifestDumpCommand : public LDBCommand { const std::vector& flags); static void Help(std::string& ret); - virtual void DoCommand() override; - virtual bool NoDBOpen() override { return true; } + void DoCommand() override; + + bool NoDBOpen() override { return true; } private: bool verbose_; @@ -263,9 +265,10 @@ class ListColumnFamiliesCommand : public LDBCommand { const std::vector& flags); static void Help(std::string& ret); - virtual void DoCommand() override; - virtual bool NoDBOpen() override { return true; } + void DoCommand() override; + + bool NoDBOpen() override { return true; } }; class CreateColumnFamilyCommand : public LDBCommand { @@ -277,9 +280,10 @@ class CreateColumnFamilyCommand : public LDBCommand { const std::vector& flags); static void Help(std::string& ret); - virtual void DoCommand() override; - virtual bool NoDBOpen() override { return false; } + void DoCommand() override; + + bool NoDBOpen() override { return false; } private: std::string new_cf_name_; @@ -294,9 +298,10 @@ class DropColumnFamilyCommand : public LDBCommand { const std::vector& flags); static void Help(std::string& ret); - virtual void DoCommand() override; - virtual bool NoDBOpen() override { return false; } + void DoCommand() override; + + bool NoDBOpen() override { return false; } private: std::string cf_name_to_drop_; @@ -310,11 +315,11 @@ class ReduceDBLevelsCommand : public LDBCommand { const std::map& options, const std::vector& flags); - virtual void OverrideBaseCFOptions(ColumnFamilyOptions* cf_opts) override; + void OverrideBaseCFOptions(ColumnFamilyOptions* cf_opts) override; - virtual void DoCommand() override; + void DoCommand() override; - virtual bool NoDBOpen() override { return true; } + bool NoDBOpen() override { return true; } static void Help(std::string& msg); @@ -342,9 +347,9 @@ class ChangeCompactionStyleCommand : public LDBCommand { const std::map& options, const std::vector& flags); - virtual void OverrideBaseCFOptions(ColumnFamilyOptions* cf_opts) override; + void OverrideBaseCFOptions(ColumnFamilyOptions* cf_opts) override; - virtual void DoCommand() override; + void DoCommand() override; static void Help(std::string& msg); @@ -364,10 +369,11 @@ class WALDumperCommand : public LDBCommand { const std::map& options, const std::vector& flags); - virtual bool NoDBOpen() override { return true; } + bool NoDBOpen() override { return true; } static void Help(std::string& ret); - virtual void DoCommand() override; + + void DoCommand() override; private: bool print_header_; @@ -389,7 +395,7 @@ class GetCommand : public LDBCommand { const std::map& options, const std::vector& flags); - virtual void DoCommand() override; + void DoCommand() override; static void Help(std::string& ret); @@ -405,7 +411,7 @@ class ApproxSizeCommand : public LDBCommand { const std::map& options, const std::vector& flags); - virtual void DoCommand() override; + void DoCommand() override; static void Help(std::string& ret); @@ -422,11 +428,11 @@ class BatchPutCommand : public LDBCommand { const std::map& options, const std::vector& flags); - virtual void DoCommand() override; + void DoCommand() override; static void Help(std::string& ret); - virtual void OverrideBaseOptions() override; + void OverrideBaseOptions() override; private: /** @@ -443,7 +449,7 @@ class ScanCommand : public LDBCommand { const std::map& options, const std::vector& flags); - virtual void DoCommand() override; + void DoCommand() override; static void Help(std::string& ret); @@ -464,7 +470,23 @@ class DeleteCommand : public LDBCommand { const std::map& options, const std::vector& flags); - virtual void DoCommand() override; + void DoCommand() override; + + static void Help(std::string& ret); + + private: + std::string key_; +}; + +class SingleDeleteCommand : public LDBCommand { + public: + static std::string Name() { return "singledelete"; } + + SingleDeleteCommand(const std::vector& params, + const std::map& options, + const std::vector& flags); + + void DoCommand() override; static void Help(std::string& ret); @@ -480,7 +502,7 @@ class DeleteRangeCommand : public LDBCommand { const std::map& options, const std::vector& flags); - virtual void DoCommand() override; + void DoCommand() override; static void Help(std::string& ret); @@ -497,11 +519,11 @@ class PutCommand : public LDBCommand { const std::map& options, const std::vector& flags); - virtual void DoCommand() override; + void DoCommand() override; static void Help(std::string& ret); - virtual void OverrideBaseOptions() override; + void OverrideBaseOptions() override; private: std::string key_; @@ -522,7 +544,7 @@ class DBQuerierCommand : public LDBCommand { static void Help(std::string& ret); - virtual void DoCommand() override; + void DoCommand() override; private: static const char* HELP_CMD; @@ -539,9 +561,9 @@ class CheckConsistencyCommand : public LDBCommand { const std::map& options, const std::vector& flags); - virtual void DoCommand() override; + void DoCommand() override; - virtual bool NoDBOpen() override { return true; } + bool NoDBOpen() override { return true; } static void Help(std::string& ret); }; @@ -554,7 +576,7 @@ class CheckPointCommand : public LDBCommand { const std::map& options, const std::vector& flags); - virtual void DoCommand() override; + void DoCommand() override; static void Help(std::string& ret); @@ -571,11 +593,11 @@ class RepairCommand : public LDBCommand { const std::map& options, const std::vector& flags); - virtual void DoCommand() override; + void DoCommand() override; - virtual bool NoDBOpen() override { return true; } + bool NoDBOpen() override { return true; } - virtual void OverrideBaseOptions() override; + void OverrideBaseOptions() override; static void Help(std::string& ret); @@ -615,7 +637,7 @@ class BackupCommand : public BackupEngineCommand { BackupCommand(const std::vector& params, const std::map& options, const std::vector& flags); - virtual void DoCommand() override; + void DoCommand() override; static void Help(std::string& ret); }; @@ -625,8 +647,8 @@ class RestoreCommand : public BackupEngineCommand { RestoreCommand(const std::vector& params, const std::map& options, const std::vector& flags); - virtual void DoCommand() override; - virtual bool NoDBOpen() override { return true; } + void DoCommand() override; + bool NoDBOpen() override { return true; } static void Help(std::string& ret); }; @@ -638,11 +660,11 @@ class WriteExternalSstFilesCommand : public LDBCommand { const std::map& options, const std::vector& flags); - virtual void DoCommand() override; + void DoCommand() override; - virtual bool NoDBOpen() override { return false; } + bool NoDBOpen() override { return false; } - virtual void OverrideBaseOptions() override; + void OverrideBaseOptions() override; static void Help(std::string& ret); @@ -658,11 +680,11 @@ class IngestExternalSstFilesCommand : public LDBCommand { const std::map& options, const std::vector& flags); - virtual void DoCommand() override; + void DoCommand() override; - virtual bool NoDBOpen() override { return false; } + bool NoDBOpen() override { return false; } - virtual void OverrideBaseOptions() override; + void OverrideBaseOptions() override; static void Help(std::string& ret); @@ -710,9 +732,9 @@ class UnsafeRemoveSstFileCommand : public LDBCommand { static void Help(std::string& ret); - virtual void DoCommand() override; + void DoCommand() override; - virtual bool NoDBOpen() override { return true; } + bool NoDBOpen() override { return true; } private: uint64_t sst_file_number_;