diff --git a/db/dbformat.cc b/db/dbformat.cc index 28e11b398..7b7336abc 100644 --- a/db/dbformat.cc +++ b/db/dbformat.cc @@ -20,22 +20,22 @@ void AppendInternalKey(std::string* result, const ParsedInternalKey& key) { PutFixed64(result, PackSequenceAndType(key.sequence, key.type)); } -std::string ParsedInternalKey::DebugString() const { +std::string ParsedInternalKey::DebugString(bool hex) const { char buf[50]; snprintf(buf, sizeof(buf), "' @ %llu : %d", (unsigned long long) sequence, int(type)); std::string result = "'"; - result += user_key.ToString(); + result += user_key.ToString(hex); result += buf; return result; } -std::string InternalKey::DebugString() const { +std::string InternalKey::DebugString(bool hex) const { std::string result; ParsedInternalKey parsed; if (ParseInternalKey(rep_, &parsed)) { - result = parsed.DebugString(); + result = parsed.DebugString(hex); } else { result = "(bad)"; result.append(EscapeString(rep_)); diff --git a/db/dbformat.h b/db/dbformat.h index 89f42fbf5..66f0e76fa 100644 --- a/db/dbformat.h +++ b/db/dbformat.h @@ -47,7 +47,7 @@ struct ParsedInternalKey { ParsedInternalKey() { } // Intentionally left uninitialized (for speed) ParsedInternalKey(const Slice& u, const SequenceNumber& seq, ValueType t) : user_key(u), sequence(seq), type(t) { } - std::string DebugString() const; + std::string DebugString(bool hex = false) const; }; // Return the length of the encoding of "key". @@ -137,7 +137,7 @@ class InternalKey { void Clear() { rep_.clear(); } - std::string DebugString() const; + std::string DebugString(bool hex = false) const; }; inline int InternalKeyComparator::Compare( diff --git a/db/version_set.cc b/db/version_set.cc index 4852148d6..03bd2a757 100644 --- a/db/version_set.cc +++ b/db/version_set.cc @@ -612,7 +612,7 @@ void Version::ExtendOverlappingInputs( } } -std::string Version::DebugString() const { +std::string Version::DebugString(bool hex) const { std::string r; for (int level = 0; level < vset_->NumberLevels(); level++) { // E.g., @@ -631,9 +631,9 @@ std::string Version::DebugString() const { r.push_back(':'); AppendNumberTo(&r, files[i]->file_size); r.append("["); - r.append(files[i]->smallest.DebugString()); + r.append(files[i]->smallest.DebugString(hex)); r.append(" .. "); - r.append(files[i]->largest.DebugString()); + r.append(files[i]->largest.DebugString(hex)); r.append("]\n"); } } @@ -1233,7 +1233,7 @@ Status VersionSet::Recover() { } Status VersionSet::DumpManifest(Options& options, std::string& dscname, - bool verbose) { + bool verbose, bool hex) { struct LogReporter : public log::Reader::Reporter { Status* status; virtual void Corruption(size_t bytes, const Status& s) { @@ -1347,7 +1347,7 @@ Status VersionSet::DumpManifest(Options& options, std::string& dscname, printf("manifest_file_number %ld next_file_number %ld last_sequence %ld log_number %ld prev_log_number %ld\n", manifest_file_number_, next_file_number_, last_sequence, log_number, prev_log_number); - printf("%s \n", v->DebugString().c_str()); + printf("%s \n", v->DebugString(hex).c_str()); } return s; diff --git a/db/version_set.h b/db/version_set.h index b008d50e7..88e215626 100644 --- a/db/version_set.h +++ b/db/version_set.h @@ -123,7 +123,7 @@ class Version { int NumFiles(int level) const { return files_[level].size(); } // Return a human readable string that describes this version's contents. - std::string DebugString() const; + std::string DebugString(bool hex = false) const; // Returns the version nuber of this version uint64_t GetVersionNumber() { @@ -334,7 +334,7 @@ class VersionSet { // printf contents (for debugging) Status DumpManifest(Options& options, std::string& manifestFileName, - bool verbose); + bool verbose, bool hex = false); // Return a human-readable short (single-line) summary of the data size // of files per level. Uses *scratch as backing store. diff --git a/include/leveldb/slice.h b/include/leveldb/slice.h index faa30f395..e0fc9aa25 100644 --- a/include/leveldb/slice.h +++ b/include/leveldb/slice.h @@ -63,7 +63,19 @@ class Slice { } // Return a string that contains the copy of the referenced data. - std::string ToString() const { return std::string(data_, size_); } + std::string ToString(bool hex = false) const { + if (hex) { + std::string result; + char buf[10]; + for (size_t i = 0; i < size_; i++) { + snprintf(buf, 10, "%02X", (unsigned char)data_[i]); + result += buf; + } + return result; + } else { + return std::string(data_, size_); + } + } // Three-way comparison. Returns value: // < 0 iff "*this" < "b", diff --git a/tools/manifest_dump.cc b/tools/manifest_dump.cc index 2cdf8c7fe..cdb89fd89 100644 --- a/tools/manifest_dump.cc +++ b/tools/manifest_dump.cc @@ -19,6 +19,7 @@ #include "util/logging.h" static int verbose = 0; +static int hex = 0; using namespace leveldb; @@ -40,6 +41,9 @@ int main(int argc, char** argv) { } else if (sscanf(argv[i], "--verbose=%ld%c", &n, &junk) == 1 && (n == 0 || n == 1)) { verbose = n; + } else if (sscanf(argv[i], "--hex=%ld%c", &n, &junk) == 1 && + (n == 0 || n == 1)) { + hex = n; } } if (!foundfile) { @@ -60,7 +64,7 @@ int main(int argc, char** argv) { VersionSet* versions = new VersionSet(dbname, &options, tc, cmp); - Status s = versions->DumpManifest(options, file, verbose); + Status s = versions->DumpManifest(options, file, verbose, hex); if (!s.ok()) { printf("Error in processing file %s %s\n", manifestfile.c_str(), s.ToString().c_str());