From 3a3b1c3e6cb0f39ed55a9699ed30e5e1eb18eead Mon Sep 17 00:00:00 2001 From: Haobo Xu Date: Thu, 8 Aug 2013 15:51:16 -0700 Subject: [PATCH] [RocksDB] Improve manifest dump to print internal keys in hex for version edits. Summary: Currently, VersionEdit::DebugString always display internal keys in the original ascii format. This could cause manifest dump to be truncated if internal keys contain special charactors (like null). Also added an option --input_key_hex for ldb idump to indicate that the passed in user keys are in hex. Test Plan: run ldb manifest_dump Reviewers: dhruba, emayanke CC: leveldb Differential Revision: https://reviews.facebook.net/D12111 --- db/version_edit.cc | 8 ++++---- db/version_edit.h | 2 +- db/version_set.cc | 2 +- util/ldb_cmd.cc | 13 +++++++++---- util/ldb_cmd.h | 2 ++ 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/db/version_edit.cc b/db/version_edit.cc index ed63c1013..f85bec086 100644 --- a/db/version_edit.cc +++ b/db/version_edit.cc @@ -218,7 +218,7 @@ Status VersionEdit::DecodeFrom(const Slice& src) { return result; } -std::string VersionEdit::DebugString() const { +std::string VersionEdit::DebugString(bool hex_key) const { std::string r; r.append("VersionEdit {"); if (has_comparator_) { @@ -245,7 +245,7 @@ std::string VersionEdit::DebugString() const { r.append("\n CompactPointer: "); AppendNumberTo(&r, compact_pointers_[i].first); r.append(" "); - r.append(compact_pointers_[i].second.DebugString()); + r.append(compact_pointers_[i].second.DebugString(hex_key)); } for (DeletedFileSet::const_iterator iter = deleted_files_.begin(); iter != deleted_files_.end(); @@ -264,9 +264,9 @@ std::string VersionEdit::DebugString() const { r.append(" "); AppendNumberTo(&r, f.file_size); r.append(" "); - r.append(f.smallest.DebugString()); + r.append(f.smallest.DebugString(hex_key)); r.append(" .. "); - r.append(f.largest.DebugString()); + r.append(f.largest.DebugString(hex_key)); } r.append("\n}\n"); return r; diff --git a/db/version_edit.h b/db/version_edit.h index 2743e9e0d..20b234470 100644 --- a/db/version_edit.h +++ b/db/version_edit.h @@ -89,7 +89,7 @@ class VersionEdit { void EncodeTo(std::string* dst) const; Status DecodeFrom(const Slice& src); - std::string DebugString() const; + std::string DebugString(bool hex_key = false) const; private: friend class VersionSet; diff --git a/db/version_set.cc b/db/version_set.cc index 9ea9ad809..807d99c3c 100644 --- a/db/version_set.cc +++ b/db/version_set.cc @@ -1496,7 +1496,7 @@ Status VersionSet::DumpManifest(Options& options, std::string& dscname, // Write out each individual edit if (verbose) { printf("*************************Edit[%d] = %s\n", - count, edit.DebugString().c_str()); + count, edit.DebugString(hex).c_str()); } count++; diff --git a/util/ldb_cmd.cc b/util/ldb_cmd.cc index 083683ee5..597e75bff 100644 --- a/util/ldb_cmd.cc +++ b/util/ldb_cmd.cc @@ -578,6 +578,7 @@ void PrintBucketCounts(const vector& bucket_counts, int ttl_start, const string InternalDumpCommand::ARG_COUNT_ONLY = "count_only"; const string InternalDumpCommand::ARG_STATS = "stats"; +const string InternalDumpCommand::ARG_INPUT_KEY_HEX = "input_key_hex"; InternalDumpCommand::InternalDumpCommand(const vector& params, const map& options, @@ -585,12 +586,14 @@ InternalDumpCommand::InternalDumpCommand(const vector& params, LDBCommand(options, flags, true, BuildCmdLineOptions({ ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX, ARG_FROM, ARG_TO, ARG_MAX_KEYS, - ARG_COUNT_ONLY, ARG_STATS})), + ARG_COUNT_ONLY, ARG_STATS, + ARG_INPUT_KEY_HEX})), has_from_(false), has_to_(false), max_keys_(-1), count_only_(false), - print_stats_(false) { + print_stats_(false), + is_input_key_hex_(false) { has_from_ = ParseStringOption(options, ARG_FROM, &from_); has_to_ = ParseStringOption(options, ARG_TO, &to_); @@ -599,8 +602,9 @@ InternalDumpCommand::InternalDumpCommand(const vector& params, print_stats_ = IsFlagPresent(flags, ARG_STATS); count_only_ = IsFlagPresent(flags, ARG_COUNT_ONLY); + is_input_key_hex_ = IsFlagPresent(flags, ARG_INPUT_KEY_HEX); - if (is_key_hex_) { + if (is_input_key_hex_) { if (has_from_) { from_ = HexToString(from_); } @@ -614,6 +618,7 @@ void InternalDumpCommand::Help(string& ret) { ret.append(" "); ret.append(InternalDumpCommand::Name()); ret.append(HelpRangeCmdArgs()); + ret.append(" [--" + ARG_INPUT_KEY_HEX + "]"); ret.append(" [--" + ARG_MAX_KEYS + "=]"); ret.append(" [--" + ARG_COUNT_ONLY + "]"); ret.append(" [--" + ARG_STATS + "]"); @@ -674,7 +679,7 @@ void InternalDumpCommand::DoCommand() { if (!count_only_) { string key = ikey.DebugString(is_key_hex_); string value = iter->value().ToString(is_value_hex_); - fprintf(stdout, "%s => %s\n", key.data(), value.data()); + std::cout << key << " => " << value << "\n"; } // Terminate if maximum number of keys have been dumped diff --git a/util/ldb_cmd.h b/util/ldb_cmd.h index cc0b93c06..753322b61 100644 --- a/util/ldb_cmd.h +++ b/util/ldb_cmd.h @@ -424,9 +424,11 @@ private: int max_keys_; bool count_only_; bool print_stats_; + bool is_input_key_hex_; static const string ARG_COUNT_ONLY; static const string ARG_STATS; + static const string ARG_INPUT_KEY_HEX; }; class DBLoaderCommand: public LDBCommand {