Skip to content

Commit

Permalink
Minor refactor on LDB command for wide column support and release note (
Browse files Browse the repository at this point in the history
facebook#11777)

Summary:
As mentioned in facebook#11754 , refactor to clean up some nearly identical logic. This PR changes the existing debugging string format of Scan command as the following.

```
❯ ./ldb --db=/tmp/rocksdbtest-226125/db_wide_basic_test_2675429_2308393776696827948/ scan --hex
```

Before
```
0x6669727374 : :0x68656C6C6F 0x617474725F6E616D6531:0x666F6F 0x617474725F6E616D6532:0x626172
0x7365636F6E64 : 0x617474725F6F6E65:0x74776F 0x617474725F7468726565:0x666F7572
0x7468697264 : 0x62617A
```
After
```
0x6669727374 ==> :0x68656C6C6F 0x617474725F6E616D6531:0x666F6F 0x617474725F6E616D6532:0x626172
0x7365636F6E64 ==> 0x617474725F6F6E65:0x74776F 0x617474725F7468726565:0x666F7572
0x7468697264 ==> 0x62617A
```

Pull Request resolved: facebook#11777

Test Plan:
```
❯ ./ldb --db=/tmp/rocksdbtest-226125/db_wide_basic_test_2675429_2308393776696827948/ dump
first ==> :hello attr_name1:foo attr_name2:bar
second ==> attr_one:two attr_three:four
third ==> baz
Keys in range: 3

❯ ./ldb --db=/tmp/rocksdbtest-226125/db_wide_basic_test_2675429_2308393776696827948/ scan
first ==> :hello attr_name1:foo attr_name2:bar
second ==> attr_one:two attr_three:four
third ==> baz

❯ ./ldb --db=/tmp/rocksdbtest-226125/db_wide_basic_test_2675429_2308393776696827948/ dump --hex
0x6669727374 ==> :0x68656C6C6F 0x617474725F6E616D6531:0x666F6F 0x617474725F6E616D6532:0x626172
0x7365636F6E64 ==> 0x617474725F6F6E65:0x74776F 0x617474725F7468726565:0x666F7572
0x7468697264 ==> 0x62617A
Keys in range: 3

❯ ./ldb --db=/tmp/rocksdbtest-226125/db_wide_basic_test_2675429_2308393776696827948/ scan --hex
0x6669727374 ==> :0x68656C6C6F 0x617474725F6E616D6531:0x666F6F 0x617474725F6E616D6532:0x626172
0x7365636F6E64 ==> 0x617474725F6F6E65:0x74776F 0x617474725F7468726565:0x666F7572
0x7468697264 ==> 0x62617A
```

Reviewed By: jowlyzhang

Differential Revision: D48876755

Pulled By: jaykorean

fbshipit-source-id: b1c608a810fe038999ac528b690d398abf5f21d7
  • Loading branch information
jaykorean authored and facebook-github-bot committed Aug 31, 2023
1 parent 83eb7b8 commit 47be3ff
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 114 deletions.
4 changes: 2 additions & 2 deletions db/wide/wide_columns_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ void WideColumnsHelper::DumpWideColumns(const WideColumns& columns,
}
}
Status WideColumnsHelper::DumpSliceAsWideColumns(const Slice& value,
std::ostream& oss, bool hex) {
std::ostream& os, bool hex) {
WideColumns columns;
Slice value_copy = value;
const Status s = WideColumnSerialization::Deserialize(value_copy, columns);
if (s.ok()) {
DumpWideColumns(columns, oss, hex);
DumpWideColumns(columns, os, hex);
}
return s;
}
Expand Down
7 changes: 6 additions & 1 deletion include/rocksdb/utilities/ldb_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ class LDBCommand {
static std::string PrintKeyValue(const std::string& key,
const std::string& value, bool is_hex);

static std::string PrintKeyValueOrWideColumns(const Slice& key,
const Slice& value,
const WideColumns& wide_columns,
bool is_key_hex,
bool is_value_hex);

/**
* Return true if the specified flag is present in the specified flags vector
*/
Expand Down Expand Up @@ -313,4 +319,3 @@ class LDBCommandRunner {
};

} // namespace ROCKSDB_NAMESPACE

106 changes: 44 additions & 62 deletions tools/ldb_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,29 @@ std::string LDBCommand::PrintKeyValue(const std::string& key,
return PrintKeyValue(key, value, is_hex, is_hex);
}

std::string LDBCommand::PrintKeyValueOrWideColumns(
const Slice& key, const Slice& value, const WideColumns& wide_columns,
bool is_key_hex, bool is_value_hex) {
if (wide_columns.empty() ||
(wide_columns.size() == 1 &&
wide_columns.front().name() == kDefaultWideColumnName)) {
return PrintKeyValue(key.ToString(), value.ToString(), is_key_hex,
is_value_hex);
}
/*
// Sample plaintext output (first column is kDefaultWideColumnName)
key_1 ==> :foo attr_name1:bar attr_name2:baz
// Sample hex output (first column is kDefaultWideColumnName)
0x6669727374 ==> :0x68656C6C6F 0x617474725F6E616D6531:0x666F6F
*/
std::ostringstream oss;
WideColumnsHelper::DumpWideColumns(wide_columns, oss, is_value_hex);
return PrintKeyValue(key.ToString(), oss.str().c_str(), is_key_hex,
false); // is_value_hex_ is already honored in oss.
// avoid double-hexing it.
}

std::string LDBCommand::HelpRangeCmdArgs() {
std::ostringstream str_stream;
str_stream << " ";
Expand Down Expand Up @@ -2200,30 +2223,14 @@ void DBDumperCommand::DoDumpCommand() {
fprintf(stdout, "%s ", TimeToHumanString(rawtime).c_str());
}
// (TODO) TTL Iterator does not support wide columns yet.
if (is_db_ttl_ || iter->columns().empty() ||
(iter->columns().size() == 1 &&
iter->columns().front().name() == kDefaultWideColumnName)) {
std::string str =
PrintKeyValue(iter->key().ToString(), iter->value().ToString(),
is_key_hex_, is_value_hex_);
fprintf(stdout, "%s\n", str.c_str());
} else {
/*
// Sample plaintext output (first column is kDefaultWideColumnName)
key_1 ==> :foo attr_name1:bar attr_name2:baz
// Sample hex output (first column is kDefaultWideColumnName)
0x6669727374 ==> :0x68656C6C6F 0x617474725F6E616D6531:0x666F6F
*/

std::ostringstream oss;
WideColumnsHelper::DumpWideColumns(iter->columns(), oss, is_value_hex_);
std::string str = PrintKeyValue(
iter->key().ToString(), oss.str().c_str(), is_key_hex_,
false); // is_value_hex_ is already honored in oss. avoid
// double-hexing it.
fprintf(stdout, "%s\n", str.c_str());
}
std::string str =
is_db_ttl_
? PrintKeyValue(iter->key().ToString(), iter->value().ToString(),
is_key_hex_, is_value_hex_)
: PrintKeyValueOrWideColumns(iter->key(), iter->value(),
iter->columns(), is_key_hex_,
is_value_hex_);
fprintf(stdout, "%s\n", str.c_str());
}
}

Expand Down Expand Up @@ -3073,47 +3080,22 @@ void ScanCommand::DoCommand() {
}
}

Slice key_slice = it->key();

std::string formatted_key;
if (is_key_hex_) {
formatted_key = "0x" + key_slice.ToString(true /* hex */);
key_slice = formatted_key;
} else if (ldb_options_.key_formatter) {
formatted_key = ldb_options_.key_formatter->Format(key_slice);
key_slice = formatted_key;
}

if (no_value_) {
fprintf(stdout, "%.*s\n", static_cast<int>(key_slice.size()),
key_slice.data());
// (TODO) TTL Iterator does not support wide columns yet.
} else if (is_db_ttl_ || it->columns().empty() ||
(it->columns().size() == 1 &&
it->columns().front().name() == kDefaultWideColumnName)) {
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;
std::string key_str = it->key().ToString();
if (is_key_hex_) {
key_str = StringToHex(key_str);
} else if (ldb_options_.key_formatter) {
key_str = ldb_options_.key_formatter->Format(key_str);
}
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\n", key_str.c_str());
} else {
/*
// Sample plaintext output (first column is kDefaultWideColumnName)
key_1 : :foo attr_name1:bar attr_name2:baz
// Sample hex output (first column is kDefaultWideColumnName)
0x6669727374 : :0x68656C6C6F 0x617474725F6E616D6531:0x666F6F
*/

std::ostringstream oss;
WideColumnsHelper::DumpWideColumns(it->columns(), oss, is_value_hex_);
fprintf(stdout, "%.*s : %.*s\n", static_cast<int>(key_slice.size()),
key_slice.data(), static_cast<int>(oss.str().length()),
oss.str().c_str());
std::string str = is_db_ttl_ ? PrintKeyValue(it->key().ToString(),
it->value().ToString(),
is_key_hex_, is_value_hex_)
: PrintKeyValueOrWideColumns(
it->key(), it->value(), it->columns(),
is_key_hex_, is_value_hex_);
fprintf(stdout, "%s\n", str.c_str());
}

num_keys_scanned++;
Expand Down
Loading

0 comments on commit 47be3ff

Please sign in to comment.