Skip to content

Commit

Permalink
Merge pull request facebook#938 from alexander-fenster/master
Browse files Browse the repository at this point in the history
added --no_value option to ldb scan to dump key only
  • Loading branch information
igorcanadi committed Mar 11, 2016
2 parents 765597f + f0161c3 commit ee8cc35
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
49 changes: 32 additions & 17 deletions tools/ldb_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const string LDBCommand::ARG_DB_WRITE_BUFFER_SIZE = "db_write_buffer_size";
const string LDBCommand::ARG_WRITE_BUFFER_SIZE = "write_buffer_size";
const string LDBCommand::ARG_FILE_SIZE = "file_size";
const string LDBCommand::ARG_CREATE_IF_MISSING = "create_if_missing";
const string LDBCommand::ARG_NO_VALUE = "no_value";

const char* LDBCommand::DELIM = " ==> ";

Expand Down Expand Up @@ -1792,14 +1793,17 @@ Options BatchPutCommand::PrepareOptionsForOpenDB() {
// ----------------------------------------------------------------------------

ScanCommand::ScanCommand(const vector<string>& params,
const map<string, string>& options, const vector<string>& flags) :
LDBCommand(options, flags, true,
BuildCmdLineOptions({ARG_TTL, ARG_HEX, ARG_KEY_HEX, ARG_TO,
ARG_VALUE_HEX, ARG_FROM, ARG_TIMESTAMP,
ARG_MAX_KEYS, ARG_TTL_START, ARG_TTL_END})),
start_key_specified_(false),
end_key_specified_(false),
max_keys_scanned_(-1) {
const map<string, string>& options,
const vector<string>& flags)
: LDBCommand(options, flags, true,
BuildCmdLineOptions(
{ARG_TTL, ARG_NO_VALUE, ARG_HEX, ARG_KEY_HEX,
ARG_TO, ARG_VALUE_HEX, ARG_FROM, ARG_TIMESTAMP,
ARG_MAX_KEYS, ARG_TTL_START, ARG_TTL_END})),
start_key_specified_(false),
end_key_specified_(false),
max_keys_scanned_(-1),
no_value_(false) {

map<string, string>::const_iterator itr = options.find(ARG_FROM);
if (itr != options.end()) {
Expand All @@ -1818,6 +1822,12 @@ ScanCommand::ScanCommand(const vector<string>& params,
end_key_specified_ = true;
}

vector<string>::const_iterator vitr =
std::find(flags.begin(), flags.end(), ARG_NO_VALUE);
if (vitr != flags.end()) {
no_value_ = true;
}

itr = options.find(ARG_MAX_KEYS);
if (itr != options.end()) {
try {
Expand Down Expand Up @@ -1845,6 +1855,7 @@ void ScanCommand::Help(string& ret) {
ret.append(" [--" + ARG_MAX_KEYS + "=<N>q] ");
ret.append(" [--" + ARG_TTL_START + "=<N>:- is inclusive]");
ret.append(" [--" + ARG_TTL_END + "=<N>:- is exclusive]");
ret.append(" [--" + ARG_NO_VALUE + "]");
ret.append("\n");
}

Expand Down Expand Up @@ -1894,7 +1905,6 @@ void ScanCommand::DoCommand() {
}

Slice key_slice = it->key();
Slice val_slice = it->value();

std::string formatted_key;
if (is_key_hex_) {
Expand All @@ -1905,16 +1915,21 @@ void ScanCommand::DoCommand() {
key_slice = formatted_key;
}

std::string formatted_value;
if (is_value_hex_) {
formatted_value = "0x" + val_slice.ToString(true /* hex */);
val_slice = formatted_value;
if (no_value_) {
fprintf(stdout, "%.*s\n", static_cast<int>(key_slice.size()),
key_slice.data());
} else {
Slice val_slice = it->value();
std::string formatted_value;
if (is_value_hex_) {
formatted_value = "0x" + val_slice.ToString(true /* hex */);
val_slice = formatted_value;
}
fprintf(stdout, "%.*s : %.*s\n", static_cast<int>(key_slice.size()),
key_slice.data(), static_cast<int>(val_slice.size()),
val_slice.data());
}

fprintf(stdout, "%.*s : %.*s\n",
static_cast<int>(key_slice.size()), key_slice.data(),
static_cast<int>(val_slice.size()), val_slice.data());

num_keys_scanned++;
if (max_keys_scanned_ >= 0 && num_keys_scanned >= max_keys_scanned_) {
break;
Expand Down
4 changes: 3 additions & 1 deletion tools/ldb_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class LDBCommand {
static const string ARG_WRITE_BUFFER_SIZE;
static const string ARG_FILE_SIZE;
static const string ARG_CREATE_IF_MISSING;
static const string ARG_NO_VALUE;

static LDBCommand* InitFromCmdLineArgs(
const vector<string>& args, const Options& options,
Expand Down Expand Up @@ -461,7 +462,7 @@ class LDBCommand {
*/
bool StringToBool(string val) {
std::transform(val.begin(), val.end(), val.begin(),
[](char ch) -> char { return ::tolower(ch); });
[](char ch)->char { return (char)::tolower(ch); });

if (val == "true") {
return true;
Expand Down Expand Up @@ -810,6 +811,7 @@ class ScanCommand : public LDBCommand {
bool start_key_specified_;
bool end_key_specified_;
int max_keys_scanned_;
bool no_value_;
};

class DeleteCommand : public LDBCommand {
Expand Down

0 comments on commit ee8cc35

Please sign in to comment.