From 9bf91c74b898e6fa41466c2fd4b6fa2cc7816c45 Mon Sep 17 00:00:00 2001 From: Abhishek Kona Date: Tue, 19 Feb 2013 18:12:20 -0800 Subject: [PATCH] ldb waldump to print the keys along with other stats + NULL to nullptr in ldb_cmd.cc Summary: LDB tool to print the deleted/put keys in hex in the wal file. Test Plan: run ldb on a db to check if output was satisfactory Reviewers: dhruba Reviewed By: dhruba CC: leveldb Differential Revision: https://reviews.facebook.net/D8691 --- util/ldb_cmd.cc | 77 +++++++++++++++++++++++++++++++++++++++++-------- util/ldb_cmd.h | 2 ++ 2 files changed, 67 insertions(+), 12 deletions(-) diff --git a/util/ldb_cmd.cc b/util/ldb_cmd.cc index e60ea60ac..cf791e7ed 100644 --- a/util/ldb_cmd.cc +++ b/util/ldb_cmd.cc @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "util/ldb_cmd.h" + #include #include @@ -9,7 +11,6 @@ #include "db/dbformat.h" #include "db/log_reader.h" #include "db/write_batch_internal.h" -#include "util/ldb_cmd.h" namespace leveldb { @@ -46,7 +47,7 @@ LDBCommand* LDBCommand::InitFromCmdLineArgs(int argc, char** argv) { * COMMAND ... [-cmdSpecificOpt1=cmdSpecificOpt1Val] .. * This is similar to the command line format used by HBaseClientTool. * Command name is not included in args. - * Returns NULL if the command-line cannot be parsed. + * Returns nullptr if the command-line cannot be parsed. */ LDBCommand* LDBCommand::InitFromCmdLineArgs(const vector& args) { // --x=y command line arguments are added as x->y map entries. @@ -81,7 +82,7 @@ LDBCommand* LDBCommand::InitFromCmdLineArgs(const vector& args) { if (cmdTokens.size() < 1) { fprintf(stderr, "Command not specified!"); - return NULL; + return nullptr; } string cmd = cmdTokens[0]; @@ -113,7 +114,7 @@ LDBCommand* LDBCommand::InitFromCmdLineArgs(const vector& args) { return new DBLoaderCommand(cmdParams, options, flags); } - return NULL; + return nullptr; } /** @@ -306,8 +307,8 @@ void CompactorCommand::Help(string& ret) { void CompactorCommand::DoCommand() { - leveldb::Slice* begin = NULL; - leveldb::Slice* end = NULL; + leveldb::Slice* begin = nullptr; + leveldb::Slice* end = nullptr; if (!null_from_) { begin = new leveldb::Slice(from_); } @@ -601,7 +602,7 @@ void ReduceDBLevelsCommand::DoCommand() { } // Compact the whole DB to put all files to the highest level. fprintf(stdout, "Compacting the db...\n"); - db_->CompactRange(NULL, NULL); + db_->CompactRange(nullptr, nullptr); CloseDB(); TableCache* tc = new TableCache(db_path_, &opt, 10); @@ -629,14 +630,39 @@ void ReduceDBLevelsCommand::DoCommand() { } } +class InMemoryHandler : public WriteBatch::Handler { + public: + + virtual void Put(const Slice& key, const Slice& value) { + putMap_[key.ToString()] = value.ToString(); + } + virtual void Delete(const Slice& key) { + deleteList_.push_back(key.ToString(true)); + } + virtual ~InMemoryHandler() { }; + + map PutMap() { + return putMap_; + } + vector DeleteList() { + return deleteList_; + } + + private: + std::map putMap_; + std::vector deleteList_; +}; + const string WALDumperCommand::ARG_WAL_FILE = "walfile"; +const string WALDumperCommand::ARG_PRINT_VALUE = "print_value"; const string WALDumperCommand::ARG_PRINT_HEADER = "header"; WALDumperCommand::WALDumperCommand(const vector& params, const map& options, const vector& flags) : LDBCommand(options, flags, true, - BuildCmdLineOptions({ARG_WAL_FILE, ARG_PRINT_HEADER})), - print_header_(false) { + BuildCmdLineOptions( + {ARG_WAL_FILE, ARG_PRINT_HEADER, ARG_PRINT_VALUE})), + print_header_(false), print_values_(false) { wal_file_.clear(); @@ -645,8 +671,9 @@ WALDumperCommand::WALDumperCommand(const vector& params, wal_file_ = itr->second; } - print_header_ = IsFlagPresent(flags, ARG_PRINT_HEADER); + print_header_ = IsFlagPresent(flags, ARG_PRINT_HEADER); + print_values_ = IsFlagPresent(flags, ARG_PRINT_VALUE); if (wal_file_.empty()) { exec_state_ = LDBCommandExecuteResult::FAILED( "Argument " + ARG_WAL_FILE + " must be specified."); @@ -658,6 +685,7 @@ void WALDumperCommand::Help(string& ret) { ret.append(WALDumperCommand::Name()); ret.append(" --" + ARG_WAL_FILE + "="); ret.append(" --[" + ARG_PRINT_HEADER + "] "); + ret.append(" --[ " + ARG_PRINT_VALUE + "] "); ret.append("\n"); } @@ -682,7 +710,11 @@ void WALDumperCommand::DoCommand() { Slice record; std::stringstream row; if (print_header_) { - std::cout<<"Sequence,Count,ByteSize,Physical Offset\n"; + std::cout<<"Sequence,Count,ByteSize,Physical Offset,Key(s)"; + if (print_values_) { + std::cout << " : value "; + } + std::cout << "\n"; } while(reader.ReadRecord(&record, &scratch)) { row.str(""); @@ -694,7 +726,28 @@ void WALDumperCommand::DoCommand() { row<