Skip to content

Commit

Permalink
ldb support for range delete
Browse files Browse the repository at this point in the history
Summary:
Add a subcommand to ldb with which we can delete a range of keys.
Closes facebook#1521

Differential Revision: D4186338

Pulled By: ajkr

fbshipit-source-id: b8e9861
  • Loading branch information
ajkr authored and Facebook Github Bot committed Nov 15, 2016
1 parent 661e4c9 commit 53b693f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
51 changes: 51 additions & 0 deletions tools/ldb_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -2346,6 +2358,45 @@ void DeleteCommand::DoCommand() {
}
}

DeleteRangeCommand::DeleteRangeCommand(
const std::vector<std::string>& params,
const std::map<std::string, std::string>& options,
const std::vector<std::string>& 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() + " <begin key> <end key>");
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<std::string>& params,
const std::map<std::string, std::string>& options,
const std::vector<std::string>& flags)
Expand Down
17 changes: 17 additions & 0 deletions tools/ldb_cmd_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& params,
const std::map<std::string, std::string>& options,
const std::vector<std::string>& 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"; }
Expand Down
1 change: 1 addition & 0 deletions tools/ldb_tool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 53b693f

Please sign in to comment.