From 8e79ce68cef08aaa9dbc62d948d2fdefff2f315c Mon Sep 17 00:00:00 2001 From: Igor Canadi Date: Fri, 31 Oct 2014 19:22:49 -0700 Subject: [PATCH] Revert "Fix lint errors and coding style of ldb related codes." This reverts commit bc9f36fd5e5f0eae69a5a1b7269bb2623cc0eb1f. --- tools/sst_dump.cc | 4 +- util/ldb_cmd.cc | 904 ++++++++++++++++------------------ util/ldb_cmd.h | 465 +++++++++-------- util/ldb_cmd_execute_result.h | 6 +- util/ldb_tool.cc | 36 +- 5 files changed, 674 insertions(+), 741 deletions(-) diff --git a/tools/sst_dump.cc b/tools/sst_dump.cc index e9fdf1c3f..6c496e8dd 100644 --- a/tools/sst_dump.cc +++ b/tools/sst_dump.cc @@ -275,8 +275,8 @@ static void print_help() { } namespace { -std::string HexToString(const std::string& str) { - std::string parsed; +string HexToString(const string& str) { + string parsed; if (str[0] != '0' || str[1] != 'x') { fprintf(stderr, "Invalid hex input %s. Must start with 0x\n", str.c_str()); diff --git a/util/ldb_cmd.cc b/util/ldb_cmd.cc index c03c1b31a..618c10a35 100644 --- a/util/ldb_cmd.cc +++ b/util/ldb_cmd.cc @@ -4,20 +4,6 @@ // of patent rights can be found in the PATENTS file in the same directory. // #ifndef ROCKSDB_LITE -#include -#include -#include -#include -#include -#include -#include - -#ifndef __STDC_FORMAT_MACROS -#define __STDC_FORMAT_MACROS -#endif - -#include - #include "util/ldb_cmd.h" #include "db/dbformat.h" @@ -31,36 +17,46 @@ #include "util/scoped_arena_iterator.h" #include "utilities/ttl/db_ttl_impl.h" +#include +#include +#include +#include +#include +#include + namespace rocksdb { -const std::string LDBCommand::ARG_DB = "db"; -const std::string LDBCommand::ARG_HEX = "hex"; -const std::string LDBCommand::ARG_KEY_HEX = "key_hex"; -const std::string LDBCommand::ARG_VALUE_HEX = "value_hex"; -const std::string LDBCommand::ARG_TTL = "ttl"; -const std::string LDBCommand::ARG_TTL_START = "start_time"; -const std::string LDBCommand::ARG_TTL_END = "end_time"; -const std::string LDBCommand::ARG_TIMESTAMP = "timestamp"; -const std::string LDBCommand::ARG_FROM = "from"; -const std::string LDBCommand::ARG_TO = "to"; -const std::string LDBCommand::ARG_MAX_KEYS = "max_keys"; -const std::string LDBCommand::ARG_BLOOM_BITS = "bloom_bits"; -const std::string LDBCommand::ARG_FIX_PREFIX_LEN = "fix_prefix_len"; -const std::string LDBCommand::ARG_COMPRESSION_TYPE = "compression_type"; -const std::string LDBCommand::ARG_BLOCK_SIZE = "block_size"; -const std::string LDBCommand::ARG_AUTO_COMPACTION = "auto_compaction"; -const std::string LDBCommand::ARG_WRITE_BUFFER_SIZE = "write_buffer_size"; -const std::string LDBCommand::ARG_FILE_SIZE = "file_size"; -const std::string LDBCommand::ARG_CREATE_IF_MISSING = "create_if_missing"; +using namespace std; + +const string LDBCommand::ARG_DB = "db"; +const string LDBCommand::ARG_HEX = "hex"; +const string LDBCommand::ARG_KEY_HEX = "key_hex"; +const string LDBCommand::ARG_VALUE_HEX = "value_hex"; +const string LDBCommand::ARG_TTL = "ttl"; +const string LDBCommand::ARG_TTL_START = "start_time"; +const string LDBCommand::ARG_TTL_END = "end_time"; +const string LDBCommand::ARG_TIMESTAMP = "timestamp"; +const string LDBCommand::ARG_FROM = "from"; +const string LDBCommand::ARG_TO = "to"; +const string LDBCommand::ARG_MAX_KEYS = "max_keys"; +const string LDBCommand::ARG_BLOOM_BITS = "bloom_bits"; +const string LDBCommand::ARG_FIX_PREFIX_LEN = "fix_prefix_len"; +const string LDBCommand::ARG_COMPRESSION_TYPE = "compression_type"; +const string LDBCommand::ARG_BLOCK_SIZE = "block_size"; +const string LDBCommand::ARG_AUTO_COMPACTION = "auto_compaction"; +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 char* LDBCommand::DELIM = " ==> "; LDBCommand* LDBCommand::InitFromCmdLineArgs( - int argc, - char** argv, - const Options& options, - const LDBOptions& ldb_options) { - std::vector args; + int argc, + char** argv, + const Options& options, + const LDBOptions& ldb_options +) { + vector args; for (int i = 1; i < argc; i++) { args.push_back(argv[i]); } @@ -71,37 +67,37 @@ LDBCommand* LDBCommand::InitFromCmdLineArgs( * Parse the command-line arguments and create the appropriate LDBCommand2 * instance. * The command line arguments must be in the following format: - * ./ldb --db = PATH_TO_DB [--commonOpt1 = commonOpt1Val] .. - * COMMAND ... - * [-cmdSpecificOpt1 = cmdSpecificOpt1Val] .. + * ./ldb --db=PATH_TO_DB [--commonOpt1=commonOpt1Val] .. + * COMMAND ... [-cmdSpecificOpt1=cmdSpecificOpt1Val] .. * This is similar to the command line format used by HBaseClientTool. * Command name is not included in args. * Returns nullptr if the command-line cannot be parsed. */ LDBCommand* LDBCommand::InitFromCmdLineArgs( - const std::vector& args, - const Options& options, - const LDBOptions& ldb_options) { - // --x = y command line arguments are added as x->y std::map entries. - std::map option_map; + const vector& args, + const Options& options, + const LDBOptions& ldb_options +) { + // --x=y command line arguments are added as x->y map entries. + map option_map; // Command-line arguments of the form --hex end up in this array as hex - std::vector flags; + vector flags; // Everything other than option_map and flags. Represents commands - // and their parameters. For eg: put key1 value1 go into this std::vector. - std::vector cmdTokens; + // and their parameters. For eg: put key1 value1 go into this vector. + vector cmdTokens; - const std::string OPTION_PREFIX = "--"; + const string OPTION_PREFIX = "--"; for (const auto& arg : args) { - if (arg[0] == '-' && arg[1] == '-') { - std::vector splits = stringSplit(arg, '='); + if (arg[0] == '-' && arg[1] == '-'){ + vector splits = stringSplit(arg, '='); if (splits.size() == 2) { - std::string optionKey = splits[0].substr(OPTION_PREFIX.size()); + string optionKey = splits[0].substr(OPTION_PREFIX.size()); option_map[optionKey] = splits[1]; } else { - std::string optionKey = splits[0].substr(OPTION_PREFIX.size()); + string optionKey = splits[0].substr(OPTION_PREFIX.size()); flags.push_back(optionKey); } } else { @@ -114,10 +110,14 @@ LDBCommand* LDBCommand::InitFromCmdLineArgs( return nullptr; } - std::string cmd = cmdTokens[0]; - std::vector cmdParams(cmdTokens.begin()+1, cmdTokens.end()); + string cmd = cmdTokens[0]; + vector cmdParams(cmdTokens.begin()+1, cmdTokens.end()); LDBCommand* command = LDBCommand::SelectCommand( - cmd, cmdParams, option_map, flags); + cmd, + cmdParams, + option_map, + flags + ); if (command) { command->SetDBOptions(options); @@ -128,9 +128,11 @@ LDBCommand* LDBCommand::InitFromCmdLineArgs( LDBCommand* LDBCommand::SelectCommand( const std::string& cmd, - const std::vector& cmdParams, - const std::map& option_map, - const std::vector& flags) { + const vector& cmdParams, + const map& option_map, + const vector& flags + ) { + if (cmd == GetCommand::Name()) { return new GetCommand(cmdParams, option_map, flags); } else if (cmd == PutCommand::Name()) { @@ -177,21 +179,21 @@ LDBCommand* LDBCommand::SelectCommand( * value. If there is an error, the specified exec_state is also * updated. */ -bool LDBCommand::ParseIntOption( - const std::map& options, - const std::string& option, int* value, - LDBCommandExecuteResult* exec_state) { - auto itr = option_map_.find(option); +bool LDBCommand::ParseIntOption(const map& options, + const string& option, int& value, + LDBCommandExecuteResult& exec_state) { + + map::const_iterator itr = option_map_.find(option); if (itr != option_map_.end()) { try { - *value = stoi(itr->second); + value = stoi(itr->second); return true; - } catch(const std::invalid_argument&) { - *exec_state = LDBCommandExecuteResult::FAILED( - option + " has an invalid value."); - } catch(const std::out_of_range&) { - *exec_state = LDBCommandExecuteResult::FAILED( - option + " has a value out-of-range."); + } catch(const invalid_argument&) { + exec_state = LDBCommandExecuteResult::FAILED(option + + " has an invalid value."); + } catch(const out_of_range&) { + exec_state = LDBCommandExecuteResult::FAILED(option + + " has a value out-of-range."); } } return false; @@ -202,9 +204,8 @@ bool LDBCommand::ParseIntOption( * Returns true if the option is found. * Returns false otherwise. */ -bool LDBCommand::ParseStringOption( - const std::map& options, - const std::string& option, std::string* value) { +bool LDBCommand::ParseStringOption(const map& options, + const string& option, string* value) { auto itr = option_map_.find(option); if (itr != option_map_.end()) { *value = itr->second; @@ -218,12 +219,12 @@ Options LDBCommand::PrepareOptionsForOpenDB() { Options opt = options_; opt.create_if_missing = false; - std::map::const_iterator itr; + map::const_iterator itr; BlockBasedTableOptions table_options; bool use_table_options = false; int bits; - if (ParseIntOption(option_map_, ARG_BLOOM_BITS, &bits, &exec_state_)) { + if (ParseIntOption(option_map_, ARG_BLOOM_BITS, bits, exec_state_)) { if (bits > 0) { use_table_options = true; table_options.filter_policy.reset(NewBloomFilterPolicy(bits)); @@ -234,7 +235,7 @@ Options LDBCommand::PrepareOptionsForOpenDB() { } int block_size; - if (ParseIntOption(option_map_, ARG_BLOCK_SIZE, &block_size, &exec_state_)) { + if (ParseIntOption(option_map_, ARG_BLOCK_SIZE, block_size, exec_state_)) { if (block_size > 0) { use_table_options = true; table_options.block_size = block_size; @@ -255,7 +256,7 @@ Options LDBCommand::PrepareOptionsForOpenDB() { itr = option_map_.find(ARG_COMPRESSION_TYPE); if (itr != option_map_.end()) { - std::string comp = itr->second; + string comp = itr->second; if (comp == "no") { opt.compression = kNoCompression; } else if (comp == "snappy") { @@ -276,8 +277,8 @@ Options LDBCommand::PrepareOptionsForOpenDB() { } int write_buffer_size; - if (ParseIntOption(option_map_, ARG_WRITE_BUFFER_SIZE, - &write_buffer_size, &exec_state_)) { + if (ParseIntOption(option_map_, ARG_WRITE_BUFFER_SIZE, write_buffer_size, + exec_state_)) { if (write_buffer_size > 0) { opt.write_buffer_size = write_buffer_size; } else { @@ -287,7 +288,7 @@ Options LDBCommand::PrepareOptionsForOpenDB() { } int file_size; - if (ParseIntOption(option_map_, ARG_FILE_SIZE, &file_size, &exec_state_)) { + if (ParseIntOption(option_map_, ARG_FILE_SIZE, file_size, exec_state_)) { if (file_size > 0) { opt.target_file_size_base = file_size; } else { @@ -301,13 +302,13 @@ Options LDBCommand::PrepareOptionsForOpenDB() { } int fix_prefix_len; - if (ParseIntOption(option_map_, ARG_FIX_PREFIX_LEN, - &fix_prefix_len, &exec_state_)) { + if (ParseIntOption(option_map_, ARG_FIX_PREFIX_LEN, fix_prefix_len, + exec_state_)) { if (fix_prefix_len > 0) { opt.prefix_extractor.reset( NewFixedPrefixTransform(static_cast(fix_prefix_len))); } else { - exec_state_ = + exec_state_ = LDBCommandExecuteResult::FAILED(ARG_FIX_PREFIX_LEN + " must be > 0."); } } @@ -315,11 +316,10 @@ Options LDBCommand::PrepareOptionsForOpenDB() { return opt; } -bool LDBCommand::ParseKeyValue( - const std::string& line, std::string* key, std::string* value, - bool is_key_hex, bool is_value_hex) { +bool LDBCommand::ParseKeyValue(const string& line, string* key, string* value, + bool is_key_hex, bool is_value_hex) { size_t pos = line.find(DELIM); - if (pos != std::string::npos) { + if (pos != string::npos) { *key = line.substr(0, pos); *value = line.substr(pos + strlen(DELIM)); if (is_key_hex) { @@ -343,20 +343,20 @@ bool LDBCommand::ParseKeyValue( */ bool LDBCommand::ValidateCmdLineOptions() { - for (auto itr = option_map_.begin(); - itr != option_map_.end(); ++itr) { + for (map::const_iterator itr = option_map_.begin(); + itr != option_map_.end(); ++itr) { if (find(valid_cmd_line_options_.begin(), - valid_cmd_line_options_.end(), itr->first) == + valid_cmd_line_options_.end(), itr->first) == valid_cmd_line_options_.end()) { fprintf(stderr, "Invalid command-line option %s\n", itr->first.c_str()); return false; } } - for (std::vector::const_iterator itr = flags_.begin(); + for (vector::const_iterator itr = flags_.begin(); itr != flags_.end(); ++itr) { if (find(valid_cmd_line_options_.begin(), - valid_cmd_line_options_.end(), *itr) == + valid_cmd_line_options_.end(), *itr) == valid_cmd_line_options_.end()) { fprintf(stderr, "Invalid command-line flag %s\n", itr->c_str()); return false; @@ -371,15 +371,14 @@ bool LDBCommand::ValidateCmdLineOptions() { return true; } -CompactorCommand::CompactorCommand( - const std::vector& params, - const std::map& options, - const std::vector& flags) : +CompactorCommand::CompactorCommand(const vector& params, + const map& options, const vector& flags) : LDBCommand(options, flags, false, BuildCmdLineOptions({ARG_FROM, ARG_TO, ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX, ARG_TTL})), null_from_(true), null_to_(true) { - auto itr = options.find(ARG_FROM); + + map::const_iterator itr = options.find(ARG_FROM); if (itr != options.end()) { null_from_ = false; from_ = itr->second; @@ -401,11 +400,11 @@ CompactorCommand::CompactorCommand( } } -void CompactorCommand::Help(std::string* ret) { - ret->append(" "); - ret->append(CompactorCommand::Name()); - ret->append(HelpRangeCmdArgs()); - ret->append("\n"); +void CompactorCommand::Help(string& ret) { + ret.append(" "); + ret.append(CompactorCommand::Name()); + ret.append(HelpRangeCmdArgs()); + ret.append("\n"); } void CompactorCommand::DoCommand() { @@ -426,14 +425,12 @@ void CompactorCommand::DoCommand() { delete end; } -const std::string DBLoaderCommand::ARG_DISABLE_WAL = "disable_wal"; -const std::string DBLoaderCommand::ARG_BULK_LOAD = "bulk_load"; -const std::string DBLoaderCommand::ARG_COMPACT = "compact"; +const string DBLoaderCommand::ARG_DISABLE_WAL = "disable_wal"; +const string DBLoaderCommand::ARG_BULK_LOAD = "bulk_load"; +const string DBLoaderCommand::ARG_COMPACT = "compact"; -DBLoaderCommand::DBLoaderCommand( - const std::vector& params, - const std::map& options, - const std::vector& flags) : +DBLoaderCommand::DBLoaderCommand(const vector& params, + const map& options, const vector& flags) : LDBCommand(options, flags, false, BuildCmdLineOptions({ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX, ARG_FROM, ARG_TO, ARG_CREATE_IF_MISSING, @@ -448,14 +445,14 @@ DBLoaderCommand::DBLoaderCommand( compact_ = IsFlagPresent(flags, ARG_COMPACT); } -void DBLoaderCommand::Help(std::string* ret) { - ret->append(" "); - ret->append(DBLoaderCommand::Name()); - ret->append(" [--" + ARG_CREATE_IF_MISSING + "]"); - ret->append(" [--" + ARG_DISABLE_WAL + "]"); - ret->append(" [--" + ARG_BULK_LOAD + "]"); - ret->append(" [--" + ARG_COMPACT + "]"); - ret->append("\n"); +void DBLoaderCommand::Help(string& ret) { + ret.append(" "); + ret.append(DBLoaderCommand::Name()); + ret.append(" [--" + ARG_CREATE_IF_MISSING + "]"); + ret.append(" [--" + ARG_DISABLE_WAL + "]"); + ret.append(" [--" + ARG_BULK_LOAD + "]"); + ret.append(" [--" + ARG_COMPACT + "]"); + ret.append("\n"); } Options DBLoaderCommand::PrepareOptionsForOpenDB() { @@ -478,10 +475,10 @@ void DBLoaderCommand::DoCommand() { } int bad_lines = 0; - std::string line; - while (getline(std::cin, line, '\n')) { - std::string key; - std::string value; + string line; + while (getline(cin, line, '\n')) { + string key; + string value; if (ParseKeyValue(line, &key, &value, is_key_hex_, is_value_hex_)) { db_->Put(write_options, Slice(key), Slice(value)); } else if (0 == line.find("Keys in range:")) { @@ -494,7 +491,7 @@ void DBLoaderCommand::DoCommand() { } if (bad_lines > 0) { - std::cout << "Warning: " << bad_lines << " bad lines ignored." << std::endl; + cout << "Warning: " << bad_lines << " bad lines ignored." << endl; } if (compact_) { db_->CompactRange(nullptr, nullptr); @@ -503,28 +500,27 @@ void DBLoaderCommand::DoCommand() { // ---------------------------------------------------------------------------- -const std::string ManifestDumpCommand::ARG_VERBOSE = "verbose"; -const std::string ManifestDumpCommand::ARG_PATH = "path"; +const string ManifestDumpCommand::ARG_VERBOSE = "verbose"; +const string ManifestDumpCommand::ARG_PATH = "path"; -void ManifestDumpCommand::Help(std::string* ret) { - ret->append(" "); - ret->append(ManifestDumpCommand::Name()); - ret->append(" [--" + ARG_VERBOSE + "]"); - ret->append(" [--" + ARG_PATH + " = ]"); - ret->append("\n"); +void ManifestDumpCommand::Help(string& ret) { + ret.append(" "); + ret.append(ManifestDumpCommand::Name()); + ret.append(" [--" + ARG_VERBOSE + "]"); + ret.append(" [--" + ARG_PATH + "=]"); + ret.append("\n"); } -ManifestDumpCommand::ManifestDumpCommand( - const std::vector& params, - const std::map& options, - const std::vector& flags) : +ManifestDumpCommand::ManifestDumpCommand(const vector& params, + const map& options, const vector& flags) : LDBCommand(options, flags, false, BuildCmdLineOptions({ARG_VERBOSE, ARG_PATH, ARG_HEX})), verbose_(false), - path_("") { + path_("") +{ verbose_ = IsFlagPresent(flags, ARG_VERBOSE); - auto itr = options.find(ARG_PATH); + map::const_iterator itr = options.find(ARG_PATH); if (itr != options.end()) { path_ = itr->second; if (path_.empty()) { @@ -601,17 +597,16 @@ void ManifestDumpCommand::DoCommand() { // ---------------------------------------------------------------------------- -void ListColumnFamiliesCommand::Help(std::string* ret) { - ret->append(" "); - ret->append(ListColumnFamiliesCommand::Name()); - ret->append(" full_path_to_db_directory "); - ret->append("\n"); +void ListColumnFamiliesCommand::Help(string& ret) { + ret.append(" "); + ret.append(ListColumnFamiliesCommand::Name()); + ret.append(" full_path_to_db_directory "); + ret.append("\n"); } ListColumnFamiliesCommand::ListColumnFamiliesCommand( - const std::vector& params, - const std::map& options, - const std::vector& flags) + const vector& params, const map& options, + const vector& flags) : LDBCommand(options, flags, false, {}) { if (params.size() != 1) { @@ -623,7 +618,7 @@ ListColumnFamiliesCommand::ListColumnFamiliesCommand( } void ListColumnFamiliesCommand::DoCommand() { - std::vector column_families; + vector column_families; Status s = DB::ListColumnFamilies(DBOptions(), dbname_, &column_families); if (!s.ok()) { printf("Error in processing db %s %s\n", dbname_.c_str(), @@ -646,56 +641,54 @@ void ListColumnFamiliesCommand::DoCommand() { namespace { -std::string ReadableTime(int unixtime) { +string ReadableTime(int unixtime) { char time_buffer [80]; time_t rawtime = unixtime; struct tm * timeinfo = localtime(&rawtime); strftime(time_buffer, 80, "%c", timeinfo); - return std::string(time_buffer); + return string(time_buffer); } // This function only called when it's the sane case of >1 buckets in time-range // Also called only when timekv falls between ttl_start and ttl_end provided -void IncBucketCounts(std::vector* bucket_counts, int ttl_start, +void IncBucketCounts(vector& bucket_counts, int ttl_start, int time_range, int bucket_size, int timekv, int num_buckets) { assert(time_range > 0 && timekv >= ttl_start && bucket_size > 0 && timekv < (ttl_start + time_range) && num_buckets > 1); int bucket = (timekv - ttl_start) / bucket_size; - (*bucket_counts)[bucket]++; + bucket_counts[bucket]++; } -void PrintBucketCounts( - const std::vector& bucket_counts, int ttl_start, - int ttl_end, int bucket_size, int num_buckets) { +void PrintBucketCounts(const vector& bucket_counts, int ttl_start, + int ttl_end, int bucket_size, int num_buckets) { int time_point = ttl_start; - for (int i = 0; i < num_buckets - 1; i++, time_point += bucket_size) { - fprintf(stdout, "Keys in range %s to %s : %" PRIu64 "\n", + for(int i = 0; i < num_buckets - 1; i++, time_point += bucket_size) { + fprintf(stdout, "Keys in range %s to %s : %lu\n", ReadableTime(time_point).c_str(), ReadableTime(time_point + bucket_size).c_str(), - bucket_counts[i]); + (unsigned long)bucket_counts[i]); } - fprintf(stdout, "Keys in range %s to %s : %" PRIu64 "\n", + fprintf(stdout, "Keys in range %s to %s : %lu\n", ReadableTime(time_point).c_str(), ReadableTime(ttl_end).c_str(), - bucket_counts[num_buckets - 1]); + (unsigned long)bucket_counts[num_buckets - 1]); } } // namespace -const std::string InternalDumpCommand::ARG_COUNT_ONLY = "count_only"; -const std::string InternalDumpCommand::ARG_COUNT_DELIM = "count_delim"; -const std::string InternalDumpCommand::ARG_STATS = "stats"; -const std::string InternalDumpCommand::ARG_INPUT_KEY_HEX = "input_key_hex"; +const string InternalDumpCommand::ARG_COUNT_ONLY = "count_only"; +const string InternalDumpCommand::ARG_COUNT_DELIM = "count_delim"; +const string InternalDumpCommand::ARG_STATS = "stats"; +const string InternalDumpCommand::ARG_INPUT_KEY_HEX = "input_key_hex"; -InternalDumpCommand::InternalDumpCommand( - const std::vector& params, - const std::map& options, - const std::vector& flags) : +InternalDumpCommand::InternalDumpCommand(const vector& params, + const map& options, + const vector& flags) : LDBCommand(options, flags, true, - BuildCmdLineOptions({ ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX, - ARG_FROM, ARG_TO, ARG_MAX_KEYS, - ARG_COUNT_ONLY, ARG_COUNT_DELIM, ARG_STATS, - ARG_INPUT_KEY_HEX})), + BuildCmdLineOptions({ ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX, + ARG_FROM, ARG_TO, ARG_MAX_KEYS, + ARG_COUNT_ONLY, ARG_COUNT_DELIM, ARG_STATS, + ARG_INPUT_KEY_HEX})), has_from_(false), has_to_(false), max_keys_(-1), @@ -708,14 +701,15 @@ InternalDumpCommand::InternalDumpCommand( has_from_ = ParseStringOption(options, ARG_FROM, &from_); has_to_ = ParseStringOption(options, ARG_TO, &to_); - ParseIntOption(options, ARG_MAX_KEYS, &max_keys_, &exec_state_); - auto itr = options.find(ARG_COUNT_DELIM); + ParseIntOption(options, ARG_MAX_KEYS, max_keys_, exec_state_); + map::const_iterator itr = options.find(ARG_COUNT_DELIM); if (itr != options.end()) { delim_ = itr->second; count_delim_ = true; + // fprintf(stdout,"delim = %c\n",delim_[0]); } else { count_delim_ = IsFlagPresent(flags, ARG_COUNT_DELIM); - delim_ = "."; + delim_="."; } print_stats_ = IsFlagPresent(flags, ARG_STATS); @@ -732,16 +726,16 @@ InternalDumpCommand::InternalDumpCommand( } } -void InternalDumpCommand::Help(std::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_COUNT_DELIM + " = ]"); - ret->append(" [--" + ARG_STATS + "]"); - ret->append("\n"); +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_COUNT_DELIM + "=]"); + ret.append(" [--" + ARG_STATS + "]"); + ret.append("\n"); } void InternalDumpCommand::DoCommand() { @@ -750,7 +744,7 @@ void InternalDumpCommand::DoCommand() { } if (print_stats_) { - std::string stats; + string stats; if (db_->GetProperty("rocksdb.stats", &stats)) { fprintf(stdout, "%s\n", stats.c_str()); } @@ -762,10 +756,10 @@ void InternalDumpCommand::DoCommand() { exec_state_ = LDBCommandExecuteResult::FAILED("DB is not DBImpl"); return; } - std::string rtype1, rtype2, row, val; + string rtype1,rtype2,row,val; rtype2 = ""; - uint64_t c = 0; - uint64_t s1 = 0, s2 = 0; + uint64_t c=0; + uint64_t s1=0,s2=0; // Setup internal key iterator Arena arena; ScopedArenaIterator iter(idb->TEST_NewInternalIterator(&arena)); @@ -782,7 +776,7 @@ void InternalDumpCommand::DoCommand() { iter->SeekToFirst(); } - uint64_t count = 0; + long long count = 0; for (; iter->Valid(); iter->Next()) { ParsedInternalKey ikey; if (!ParseInternalKey(iter->key(), &ikey)) { @@ -801,69 +795,59 @@ void InternalDumpCommand::DoCommand() { int k; if (count_delim_) { rtype1 = ""; - s1 = 0; + s1=0; row = iter->key().ToString(); val = iter->value().ToString(); - for (k = 0; row[k] != '\x01' && row[k] != '\0'; k++) { + for(k=0;row[k]!='\x01' && row[k]!='\0';k++) s1++; - } - for (k = 0; val[k] != '\x01' && val[k] != '\0'; k++) { + for(k=0;val[k]!='\x01' && val[k]!='\0';k++) s1++; - } - for (int j = 0; - row[j] != delim_[0] && row[j] != '\0' && row[j] != '\x01'; - j++) { - rtype1+= row[j]; - } - if (rtype2.compare("") && rtype2.compare(rtype1) != 0) { - fprintf(stdout, "%s = > count:%" PRIu64 "\tsize:%" PRIu64 "\n", - rtype2.c_str(), c, s2); - c = 1; - s2 = s1; + for(int j=0;row[j]!=delim_[0] && row[j]!='\0' && row[j]!='\x01';j++) + rtype1+=row[j]; + if(rtype2.compare("") && rtype2.compare(rtype1)!=0) { + fprintf(stdout,"%s => count:%lld\tsize:%lld\n",rtype2.c_str(), + (long long)c,(long long)s2); + c=1; + s2=s1; rtype2 = rtype1; } else { c++; - s2+= s1; - rtype2 = rtype1; + s2+=s1; + rtype2=rtype1; } } if (!count_only_ && !count_delim_) { - std::string key = ikey.DebugString(is_key_hex_); - std::string value = iter->value().ToString(is_value_hex_); - std::cout << key << " = > " << value << "\n"; + string key = ikey.DebugString(is_key_hex_); + string value = iter->value().ToString(is_value_hex_); + std::cout << key << " => " << value << "\n"; } // Terminate if maximum number of keys have been dumped - if (max_keys_ > 0 && count >= static_cast(max_keys_)) { - break; - } + if (max_keys_ > 0 && count >= max_keys_) break; } - if (count_delim_) { - fprintf(stdout, - "%s = > count:%" PRIu64 "\tsize:%" PRIu64 "\n", - rtype2.c_str(), c, s2); + if(count_delim_) { + fprintf(stdout,"%s => count:%lld\tsize:%lld\n", rtype2.c_str(), + (long long)c,(long long)s2); } else - fprintf(stdout, "Internal keys in range: %" PRIu64 "\n", count); + fprintf(stdout, "Internal keys in range: %lld\n", (long long) count); } -const std::string DBDumperCommand::ARG_COUNT_ONLY = "count_only"; -const std::string DBDumperCommand::ARG_COUNT_DELIM = "count_delim"; -const std::string DBDumperCommand::ARG_STATS = "stats"; -const std::string DBDumperCommand::ARG_TTL_BUCKET = "bucket"; +const string DBDumperCommand::ARG_COUNT_ONLY = "count_only"; +const string DBDumperCommand::ARG_COUNT_DELIM = "count_delim"; +const string DBDumperCommand::ARG_STATS = "stats"; +const string DBDumperCommand::ARG_TTL_BUCKET = "bucket"; -DBDumperCommand::DBDumperCommand( - const std::vector& params, - const std::map& options, - const std::vector& flags) : +DBDumperCommand::DBDumperCommand(const vector& params, + const map& options, const vector& flags) : LDBCommand(options, flags, true, - BuildCmdLineOptions({ARG_TTL, ARG_HEX, ARG_KEY_HEX, - ARG_VALUE_HEX, ARG_FROM, ARG_TO, - ARG_MAX_KEYS, ARG_COUNT_ONLY, - ARG_COUNT_DELIM, ARG_STATS, ARG_TTL_START, - ARG_TTL_END, ARG_TTL_BUCKET, - ARG_TIMESTAMP})), + BuildCmdLineOptions({ARG_TTL, ARG_HEX, ARG_KEY_HEX, + ARG_VALUE_HEX, ARG_FROM, ARG_TO, + ARG_MAX_KEYS, ARG_COUNT_ONLY, + ARG_COUNT_DELIM, ARG_STATS, ARG_TTL_START, + ARG_TTL_END, ARG_TTL_BUCKET, + ARG_TIMESTAMP})), null_from_(true), null_to_(true), max_keys_(-1), @@ -871,7 +855,7 @@ DBDumperCommand::DBDumperCommand( count_delim_(false), print_stats_(false) { - auto itr = options.find(ARG_FROM); + map::const_iterator itr = options.find(ARG_FROM); if (itr != options.end()) { null_from_ = false; from_ = itr->second; @@ -887,10 +871,10 @@ DBDumperCommand::DBDumperCommand( if (itr != options.end()) { try { max_keys_ = stoi(itr->second); - } catch(const std::invalid_argument&) { + } catch(const invalid_argument&) { exec_state_ = LDBCommandExecuteResult::FAILED(ARG_MAX_KEYS + " has an invalid value"); - } catch(const std::out_of_range&) { + } catch(const out_of_range&) { exec_state_ = LDBCommandExecuteResult::FAILED(ARG_MAX_KEYS + " has a value out-of-range"); } @@ -901,7 +885,7 @@ DBDumperCommand::DBDumperCommand( count_delim_ = true; } else { count_delim_ = IsFlagPresent(flags, ARG_COUNT_DELIM); - delim_ = "."; + delim_="."; } print_stats_ = IsFlagPresent(flags, ARG_STATS); @@ -917,20 +901,20 @@ DBDumperCommand::DBDumperCommand( } } -void DBDumperCommand::Help(std::string* ret) { - ret->append(" "); - ret->append(DBDumperCommand::Name()); - ret->append(HelpRangeCmdArgs()); - ret->append(" [--" + ARG_TTL + "]"); - ret->append(" [--" + ARG_MAX_KEYS + " = ]"); - ret->append(" [--" + ARG_TIMESTAMP + "]"); - ret->append(" [--" + ARG_COUNT_ONLY + "]"); - ret->append(" [--" + ARG_COUNT_DELIM + " = ]"); - ret->append(" [--" + ARG_STATS + "]"); - ret->append(" [--" + ARG_TTL_BUCKET + " = ]"); - ret->append(" [--" + ARG_TTL_START + " = :- is inclusive]"); - ret->append(" [--" + ARG_TTL_END + " = :- is exclusive]"); - ret->append("\n"); +void DBDumperCommand::Help(string& ret) { + ret.append(" "); + ret.append(DBDumperCommand::Name()); + ret.append(HelpRangeCmdArgs()); + ret.append(" [--" + ARG_TTL + "]"); + ret.append(" [--" + ARG_MAX_KEYS + "=]"); + ret.append(" [--" + ARG_TIMESTAMP + "]"); + ret.append(" [--" + ARG_COUNT_ONLY + "]"); + ret.append(" [--" + ARG_COUNT_DELIM + "=]"); + ret.append(" [--" + ARG_STATS + "]"); + ret.append(" [--" + ARG_TTL_BUCKET + "=]"); + ret.append(" [--" + ARG_TTL_START + "=:- is inclusive]"); + ret.append(" [--" + ARG_TTL_END + "=:- is exclusive]"); + ret.append("\n"); } void DBDumperCommand::DoCommand() { @@ -940,7 +924,7 @@ void DBDumperCommand::DoCommand() { // Parse command line args uint64_t count = 0; if (print_stats_) { - std::string stats; + string stats; if (db_->GetProperty("rocksdb.stats", &stats)) { fprintf(stdout, "%s\n", stats.c_str()); } @@ -962,11 +946,11 @@ void DBDumperCommand::DoCommand() { int max_keys = max_keys_; int ttl_start; - if (!ParseIntOption(option_map_, ARG_TTL_START, &ttl_start, &exec_state_)) { + if (!ParseIntOption(option_map_, ARG_TTL_START, ttl_start, exec_state_)) { ttl_start = DBWithTTLImpl::kMinTimestamp; // TTL introduction time } int ttl_end; - if (!ParseIntOption(option_map_, ARG_TTL_END, &ttl_end, &exec_state_)) { + if (!ParseIntOption(option_map_, ARG_TTL_END, ttl_end, exec_state_)) { ttl_end = DBWithTTLImpl::kMaxTimestamp; // Max time allowed by TTL feature } if (ttl_end < ttl_start) { @@ -976,21 +960,20 @@ void DBDumperCommand::DoCommand() { } int time_range = ttl_end - ttl_start; int bucket_size; - if (!ParseIntOption( - option_map_, ARG_TTL_BUCKET, &bucket_size, &exec_state_) || + if (!ParseIntOption(option_map_, ARG_TTL_BUCKET, bucket_size, exec_state_) || bucket_size <= 0) { bucket_size = time_range; // Will have just 1 bucket by default } //cretaing variables for row count of each type - std::string rtype1, rtype2, row, val; + string rtype1,rtype2,row,val; rtype2 = ""; - uint64_t c = 0; - uint64_t s1 = 0, s2 = 0; + uint64_t c=0; + uint64_t s1=0,s2=0; - // At this point, bucket_size = 0 = > time_range = 0 + // At this point, bucket_size=0 => time_range=0 uint64_t num_buckets = (bucket_size >= time_range) ? 1 : ((time_range + bucket_size - 1) / bucket_size); - std::vector bucket_counts(num_buckets, 0); + vector bucket_counts(num_buckets, 0); if (is_db_ttl_ && !count_only_ && timestamp_ && !count_delim_) { fprintf(stdout, "Dumping key-values from %s to %s\n", ReadableTime(ttl_start).c_str(), ReadableTime(ttl_end).c_str()); @@ -1016,7 +999,7 @@ void DBDumperCommand::DoCommand() { --max_keys; } if (is_db_ttl_ && num_buckets > 1) { - IncBucketCounts(&bucket_counts, ttl_start, time_range, bucket_size, + IncBucketCounts(bucket_counts, ttl_start, time_range, bucket_size, rawtime, num_buckets); } ++count; @@ -1025,28 +1008,29 @@ void DBDumperCommand::DoCommand() { row = iter->key().ToString(); val = iter->value().ToString(); s1 = row.size()+val.size(); - for (int j = 0; row[j] != delim_[0] && row[j] != '\0'; j++) { - rtype1 += row[j]; - } - if (rtype2.compare("") && rtype2.compare(rtype1) != 0) { - fprintf(stdout, - "%s = > count:%" PRIu64 "\tsize:%" PRIu64 "\n", - rtype2.c_str(), c, s2); - c = 1; - s2 = s1; + for(int j=0;row[j]!=delim_[0] && row[j]!='\0';j++) + rtype1+=row[j]; + if(rtype2.compare("") && rtype2.compare(rtype1)!=0) { + fprintf(stdout,"%s => count:%lld\tsize:%lld\n",rtype2.c_str(), + (long long )c,(long long)s2); + c=1; + s2=s1; rtype2 = rtype1; } else { - c++; - s2 += s1; - rtype2 = rtype1; + c++; + s2+=s1; + rtype2=rtype1; } + } + + if (!count_only_ && !count_delim_) { if (is_db_ttl_ && timestamp_) { fprintf(stdout, "%s ", ReadableTime(rawtime).c_str()); } - std::string str = PrintKeyValue(iter->key().ToString(), + string str = PrintKeyValue(iter->key().ToString(), iter->value().ToString(), is_key_hex_, is_value_hex_); fprintf(stdout, "%s\n", str.c_str()); @@ -1056,25 +1040,21 @@ void DBDumperCommand::DoCommand() { if (num_buckets > 1 && is_db_ttl_) { PrintBucketCounts(bucket_counts, ttl_start, ttl_end, bucket_size, num_buckets); - } else if (count_delim_) { - fprintf(stdout, "%s = > count:%" PRIu64 "\tsize:%" PRIu64 "\n", - rtype2.c_str(), c, s2); + } else if(count_delim_) { + fprintf(stdout,"%s => count:%lld\tsize:%lld\n",rtype2.c_str(), + (long long )c,(long long)s2); } else { - fprintf(stdout, "Keys in range: %" PRIu64 "\n", count); + fprintf(stdout, "Keys in range: %lld\n", (long long) count); } // Clean up delete iter; } -const std::string - ReduceDBLevelsCommand::ARG_NEW_LEVELS = "new_levels"; -const std::string - ReduceDBLevelsCommand::ARG_PRINT_OLD_LEVELS = "print_old_levels"; +const string ReduceDBLevelsCommand::ARG_NEW_LEVELS = "new_levels"; +const string ReduceDBLevelsCommand::ARG_PRINT_OLD_LEVELS = "print_old_levels"; -ReduceDBLevelsCommand::ReduceDBLevelsCommand( - const std::vector& params, - const std::map& options, - const std::vector& flags) : +ReduceDBLevelsCommand::ReduceDBLevelsCommand(const vector& params, + const map& options, const vector& flags) : LDBCommand(options, flags, false, BuildCmdLineOptions({ARG_NEW_LEVELS, ARG_PRINT_OLD_LEVELS})), old_levels_(1 << 16), @@ -1082,34 +1062,33 @@ ReduceDBLevelsCommand::ReduceDBLevelsCommand( print_old_levels_(false) { - ParseIntOption(option_map_, ARG_NEW_LEVELS, &new_levels_, &exec_state_); + ParseIntOption(option_map_, ARG_NEW_LEVELS, new_levels_, exec_state_); print_old_levels_ = IsFlagPresent(flags, ARG_PRINT_OLD_LEVELS); - if (new_levels_ <= 0) { + if(new_levels_ <= 0) { exec_state_ = LDBCommandExecuteResult::FAILED( " Use --" + ARG_NEW_LEVELS + " to specify a new level number\n"); } } -std::vector ReduceDBLevelsCommand::PrepareArgs( - const std::string& db_path, +vector ReduceDBLevelsCommand::PrepareArgs(const string& db_path, int new_levels, bool print_old_level) { - std::vector ret; + vector ret; ret.push_back("reduce_levels"); - ret.push_back("--" + ARG_DB + " = " + db_path); - ret.push_back("--" + ARG_NEW_LEVELS + " = " + std::to_string(new_levels)); - if (print_old_level) { + ret.push_back("--" + ARG_DB + "=" + db_path); + ret.push_back("--" + ARG_NEW_LEVELS + "=" + to_string(new_levels)); + if(print_old_level) { ret.push_back("--" + ARG_PRINT_OLD_LEVELS); } return ret; } -void ReduceDBLevelsCommand::Help(std::string* ret) { - ret->append(" "); - ret->append(ReduceDBLevelsCommand::Name()); - ret->append(" --" + ARG_NEW_LEVELS + " = "); - ret->append(" [--" + ARG_PRINT_OLD_LEVELS + "]"); - ret->append("\n"); +void ReduceDBLevelsCommand::Help(string& ret) { + ret.append(" "); + ret.append(ReduceDBLevelsCommand::Name()); + ret.append(" --" + ARG_NEW_LEVELS + "="); + ret.append(" [--" + ARG_PRINT_OLD_LEVELS + "]"); + ret.append("\n"); } Options ReduceDBLevelsCommand::PrepareOptionsForOpenDB() { @@ -1172,8 +1151,7 @@ void ReduceDBLevelsCommand::DoCommand() { } if (print_old_levels_) { - fprintf(stdout, "The old number of levels in use is %d\n", - old_level_num); + fprintf(stdout, "The old number of levels in use is %d\n", old_level_num); } if (old_level_num <= new_levels_) { @@ -1192,31 +1170,29 @@ void ReduceDBLevelsCommand::DoCommand() { CloseDB(); EnvOptions soptions; - st = VersionSet::ReduceNumberOfLevels( - db_path_, &opt, soptions, new_levels_); + st = VersionSet::ReduceNumberOfLevels(db_path_, &opt, soptions, new_levels_); if (!st.ok()) { exec_state_ = LDBCommandExecuteResult::FAILED(st.ToString()); return; } } -const std::string ChangeCompactionStyleCommand::ARG_OLD_COMPACTION_STYLE = +const string ChangeCompactionStyleCommand::ARG_OLD_COMPACTION_STYLE = "old_compaction_style"; -const std::string ChangeCompactionStyleCommand::ARG_NEW_COMPACTION_STYLE = +const string ChangeCompactionStyleCommand::ARG_NEW_COMPACTION_STYLE = "new_compaction_style"; ChangeCompactionStyleCommand::ChangeCompactionStyleCommand( - const std::vector& params, - const std::map& options, - const std::vector& flags) : + const vector& params, const map& options, + const vector& flags) : LDBCommand(options, flags, false, BuildCmdLineOptions({ARG_OLD_COMPACTION_STYLE, ARG_NEW_COMPACTION_STYLE})), old_compaction_style_(-1), new_compaction_style_(-1) { - ParseIntOption(option_map_, ARG_OLD_COMPACTION_STYLE, - &old_compaction_style_, &exec_state_); + ParseIntOption(option_map_, ARG_OLD_COMPACTION_STYLE, old_compaction_style_, + exec_state_); if (old_compaction_style_ != kCompactionStyleLevel && old_compaction_style_ != kCompactionStyleUniversal) { exec_state_ = LDBCommandExecuteResult::FAILED( @@ -1225,8 +1201,8 @@ ChangeCompactionStyleCommand::ChangeCompactionStyleCommand( return; } - ParseIntOption(option_map_, ARG_NEW_COMPACTION_STYLE, - &new_compaction_style_, &exec_state_); + ParseIntOption(option_map_, ARG_NEW_COMPACTION_STYLE, new_compaction_style_, + exec_state_); if (new_compaction_style_ != kCompactionStyleLevel && new_compaction_style_ != kCompactionStyleUniversal) { exec_state_ = LDBCommandExecuteResult::FAILED( @@ -1251,16 +1227,14 @@ ChangeCompactionStyleCommand::ChangeCompactionStyleCommand( } } -void ChangeCompactionStyleCommand::Help(std::string* ret) { - ret->append(" "); - ret->append(ChangeCompactionStyleCommand::Name()); - ret->append( - " --" + ARG_OLD_COMPACTION_STYLE + " = "); - ret->append( - " --" + ARG_NEW_COMPACTION_STYLE + " = "); - ret->append("\n"); +void ChangeCompactionStyleCommand::Help(string& ret) { + ret.append(" "); + ret.append(ChangeCompactionStyleCommand::Name()); + ret.append(" --" + ARG_OLD_COMPACTION_STYLE + "="); + ret.append(" --" + ARG_NEW_COMPACTION_STYLE + "="); + ret.append("\n"); } Options ChangeCompactionStyleCommand::PrepareOptionsForOpenDB() { @@ -1288,9 +1262,9 @@ void ChangeCompactionStyleCommand::DoCommand() { db_->GetProperty("rocksdb.num-files-at-level" + NumberToString(i), &property); - // format print std::string + // format print string char buf[100]; - snprintf(buf, sizeof(buf), "%s%s", (i ? ", " : ""), property.c_str()); + snprintf(buf, sizeof(buf), "%s%s", (i ? "," : ""), property.c_str()); files_per_level += buf; } fprintf(stdout, "files per level before compaction: %s\n", @@ -1308,9 +1282,9 @@ void ChangeCompactionStyleCommand::DoCommand() { db_->GetProperty("rocksdb.num-files-at-level" + NumberToString(i), &property); - // format print std::string + // format print string char buf[100]; - snprintf(buf, sizeof(buf), "%s%s", (i ? ", " : ""), property.c_str()); + snprintf(buf, sizeof(buf), "%s%s", (i ? "," : ""), property.c_str()); files_per_level += buf; num_files = atoi(property.c_str()); @@ -1318,15 +1292,15 @@ void ChangeCompactionStyleCommand::DoCommand() { // level 0 should have only 1 file if (i == 0 && num_files != 1) { exec_state_ = LDBCommandExecuteResult::FAILED("Number of db files at " - "level 0 after compaction is " + std::to_string(num_files) + - ", not 1.\n"); + "level 0 after compaction is " + std::to_string(num_files) + + ", not 1.\n"); return; } // other levels should have no file if (i > 0 && num_files != 0) { exec_state_ = LDBCommandExecuteResult::FAILED("Number of db files at " - "level " + std::to_string(i) + " after compaction is " + - std::to_string(num_files) + ", not 0.\n"); + "level " + std::to_string(i) + " after compaction is " + + std::to_string(num_files) + ", not 0.\n"); return; } } @@ -1337,15 +1311,14 @@ void ChangeCompactionStyleCommand::DoCommand() { class InMemoryHandler : public WriteBatch::Handler { public: - InMemoryHandler(std::stringstream& row, bool print_values) - : Handler(), row_(row) { + InMemoryHandler(stringstream& row, bool print_values) : Handler(),row_(row) { print_values_ = print_values; } void commonPutMerge(const Slice& key, const Slice& value) { - std::string k = LDBCommand::StringToHex(key.ToString()); + string k = LDBCommand::StringToHex(key.ToString()); if (print_values_) { - std::string v = LDBCommand::StringToHex(value.ToString()); + string v = LDBCommand::StringToHex(value.ToString()); row_ << k << " : "; row_ << v << " "; } else { @@ -1364,25 +1337,23 @@ class InMemoryHandler : public WriteBatch::Handler { } virtual void Delete(const Slice& key) { - row_ << ", DELETE : "; + row_ <<",DELETE : "; row_ << LDBCommand::StringToHex(key.ToString()) << " "; } virtual ~InMemoryHandler() { }; private: - std::stringstream & row_; + stringstream & row_; bool print_values_; }; -const std::string WALDumperCommand::ARG_WAL_FILE = "walfile"; -const std::string WALDumperCommand::ARG_PRINT_VALUE = "print_value"; -const std::string WALDumperCommand::ARG_PRINT_HEADER = "header"; +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 std::vector& params, - const std::map& options, - const std::vector& flags) : +WALDumperCommand::WALDumperCommand(const vector& params, + const map& options, const vector& flags) : LDBCommand(options, flags, true, BuildCmdLineOptions( {ARG_WAL_FILE, ARG_PRINT_HEADER, ARG_PRINT_VALUE})), @@ -1390,8 +1361,7 @@ WALDumperCommand::WALDumperCommand( wal_file_.clear(); - std::map::const_iterator itr = - options.find(ARG_WAL_FILE); + map::const_iterator itr = options.find(ARG_WAL_FILE); if (itr != options.end()) { wal_file_ = itr->second; } @@ -1405,19 +1375,19 @@ WALDumperCommand::WALDumperCommand( } } -void WALDumperCommand::Help(std::string* ret) { - ret->append(" "); - ret->append(WALDumperCommand::Name()); - ret->append(" --" + ARG_WAL_FILE + " = "); - ret->append(" [--" + ARG_PRINT_HEADER + "] "); - ret->append(" [--" + ARG_PRINT_VALUE + "] "); - ret->append("\n"); +void WALDumperCommand::Help(string& ret) { + ret.append(" "); + ret.append(WALDumperCommand::Name()); + ret.append(" --" + ARG_WAL_FILE + "="); + ret.append(" [--" + ARG_PRINT_HEADER + "] "); + ret.append(" [--" + ARG_PRINT_VALUE + "] "); + ret.append("\n"); } void WALDumperCommand::DoCommand() { struct StdErrReporter : public log::Reader::Reporter { virtual void Corruption(size_t bytes, const Status& s) { - std::cerr << "Corruption detected in log file " << s.ToString() << "\n"; + cerr<<"Corruption detected in log file "<NewSequentialFile(wal_file_, &file, soptions); if (!status.ok()) { - exec_state_ = LDBCommandExecuteResult::FAILED( - "Failed to open WAL file " + status.ToString()); + exec_state_ = LDBCommandExecuteResult::FAILED("Failed to open WAL file " + + status.ToString()); } else { StdErrReporter reporter; log::Reader reader(move(file), &reporter, true, 0); - std::string scratch; + string scratch; WriteBatch batch; Slice record; - std::stringstream row; + stringstream row; if (print_header_) { - std::cout << "Sequence, Count, ByteSize, Physical Offset, Key(s)"; + cout<<"Sequence,Count,ByteSize,Physical Offset,Key(s)"; if (print_values_) { - std::cout << " : value "; + cout << " : value "; } - std::cout << "\n"; + cout << "\n"; } while(reader.ReadRecord(&record, &scratch)) { row.str(""); @@ -1449,24 +1419,22 @@ void WALDumperCommand::DoCommand() { record.size(), Status::Corruption("log record too small")); } else { WriteBatchInternal::SetContents(&batch, record); - row << WriteBatchInternal::Sequence(&batch) << ", "; - row << WriteBatchInternal::Count(&batch) << ", "; - row << WriteBatchInternal::ByteSize(&batch) << ", "; - row << reader.LastRecordOffset() << ", "; + row<& params, - const std::map& options, - const std::vector& flags) : +GetCommand::GetCommand(const vector& params, + const map& options, const vector& flags) : LDBCommand(options, flags, true, BuildCmdLineOptions({ARG_TTL, ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX})) { @@ -1483,16 +1451,16 @@ GetCommand::GetCommand( } } -void GetCommand::Help(std::string* ret) { - ret->append(" "); - ret->append(GetCommand::Name()); - ret->append(" "); - ret->append(" [--" + ARG_TTL + "]"); - ret->append("\n"); +void GetCommand::Help(string& ret) { + ret.append(" "); + ret.append(GetCommand::Name()); + ret.append(" "); + ret.append(" [--" + ARG_TTL + "]"); + ret.append("\n"); } void GetCommand::DoCommand() { - std::string value; + string value; Status st = db_->Get(ReadOptions(), key_, &value); if (st.ok()) { fprintf(stdout, "%s\n", @@ -1503,10 +1471,8 @@ void GetCommand::DoCommand() { } -ApproxSizeCommand::ApproxSizeCommand( - const std::vector& params, - const std::map& options, - const std::vector& flags) : +ApproxSizeCommand::ApproxSizeCommand(const vector& params, + const map& options, const vector& flags) : LDBCommand(options, flags, true, BuildCmdLineOptions({ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX, ARG_FROM, ARG_TO})) { @@ -1533,11 +1499,11 @@ ApproxSizeCommand::ApproxSizeCommand( } } -void ApproxSizeCommand::Help(std::string* ret) { - ret->append(" "); - ret->append(ApproxSizeCommand::Name()); - ret->append(HelpRangeCmdArgs()); - ret->append("\n"); +void ApproxSizeCommand::Help(string& ret) { + ret.append(" "); + ret.append(ApproxSizeCommand::Name()); + ret.append(HelpRangeCmdArgs()); + ret.append("\n"); } void ApproxSizeCommand::DoCommand() { @@ -1556,45 +1522,43 @@ void ApproxSizeCommand::DoCommand() { } -BatchPutCommand::BatchPutCommand( - const std::vector& params, - const std::map& options, - const std::vector& flags) : - LDBCommand(options, flags, false, - BuildCmdLineOptions({ARG_TTL, ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX, - ARG_CREATE_IF_MISSING})) { +BatchPutCommand::BatchPutCommand(const vector& params, + const map& options, const vector& flags) : + LDBCommand(options, flags, false, + BuildCmdLineOptions({ARG_TTL, ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX, + ARG_CREATE_IF_MISSING})) { if (params.size() < 2) { exec_state_ = LDBCommandExecuteResult::FAILED( - "At least one std::pair must be specified batchput."); + "At least one pair must be specified batchput."); } else if (params.size() % 2 != 0) { exec_state_ = LDBCommandExecuteResult::FAILED( "Equal number of s and s must be specified for batchput."); } else { for (size_t i = 0; i < params.size(); i += 2) { - std::string key = params.at(i); - std::string value = params.at(i+1); - key_values_.push_back(std::pair( + string key = params.at(i); + string value = params.at(i+1); + key_values_.push_back(pair( is_key_hex_ ? HexToString(key) : key, is_value_hex_ ? HexToString(value) : value)); } } } -void BatchPutCommand::Help(std::string* ret) { - ret->append(" "); - ret->append(BatchPutCommand::Name()); - ret->append(" [ ] [..]"); - ret->append(" [--" + ARG_TTL + "]"); - ret->append("\n"); +void BatchPutCommand::Help(string& ret) { + ret.append(" "); + ret.append(BatchPutCommand::Name()); + ret.append(" [ ] [..]"); + ret.append(" [--" + ARG_TTL + "]"); + ret.append("\n"); } void BatchPutCommand::DoCommand() { WriteBatch batch; - for (std::vector>::const_iterator itr + for (vector>::const_iterator itr = key_values_.begin(); itr != key_values_.end(); ++itr) { - batch.Put(itr->first, itr->second); + batch.Put(itr->first, itr->second); } Status st = db_->Write(WriteOptions(), &batch); if (st.ok()) { @@ -1611,10 +1575,8 @@ Options BatchPutCommand::PrepareOptionsForOpenDB() { } -ScanCommand::ScanCommand( - const std::vector& params, - const std::map& options, - const std::vector& flags) : +ScanCommand::ScanCommand(const vector& params, + const map& options, const vector& flags) : LDBCommand(options, flags, true, BuildCmdLineOptions({ARG_TTL, ARG_HEX, ARG_KEY_HEX, ARG_TO, ARG_VALUE_HEX, ARG_FROM, ARG_TIMESTAMP, @@ -1623,7 +1585,7 @@ ScanCommand::ScanCommand( end_key_specified_(false), max_keys_scanned_(-1) { - auto itr = options.find(ARG_FROM); + map::const_iterator itr = options.find(ARG_FROM); if (itr != options.end()) { start_key_ = itr->second; if (is_key_hex_) { @@ -1644,26 +1606,26 @@ ScanCommand::ScanCommand( if (itr != options.end()) { try { max_keys_scanned_ = stoi(itr->second); - } catch(const std::invalid_argument&) { + } catch(const invalid_argument&) { exec_state_ = LDBCommandExecuteResult::FAILED(ARG_MAX_KEYS + " has an invalid value"); - } catch(const std::out_of_range&) { + } catch(const out_of_range&) { exec_state_ = LDBCommandExecuteResult::FAILED(ARG_MAX_KEYS + " has a value out-of-range"); } } } -void ScanCommand::Help(std::string* ret) { - ret->append(" "); - ret->append(ScanCommand::Name()); - ret->append(HelpRangeCmdArgs()); - ret->append(" [--" + ARG_TTL + "]"); - ret->append(" [--" + ARG_TIMESTAMP + "]"); - ret->append(" [--" + ARG_MAX_KEYS + " = q] "); - ret->append(" [--" + ARG_TTL_START + " = :- is inclusive]"); - ret->append(" [--" + ARG_TTL_END + " = :- is exclusive]"); - ret->append("\n"); +void ScanCommand::Help(string& ret) { + ret.append(" "); + ret.append(ScanCommand::Name()); + ret.append(HelpRangeCmdArgs()); + ret.append(" [--" + ARG_TTL + "]"); + ret.append(" [--" + ARG_TIMESTAMP + "]"); + ret.append(" [--" + ARG_MAX_KEYS + "=q] "); + ret.append(" [--" + ARG_TTL_START + "=:- is inclusive]"); + ret.append(" [--" + ARG_TTL_END + "=:- is exclusive]"); + ret.append("\n"); } void ScanCommand::DoCommand() { @@ -1676,11 +1638,11 @@ void ScanCommand::DoCommand() { it->SeekToFirst(); } int ttl_start; - if (!ParseIntOption(option_map_, ARG_TTL_START, &ttl_start, &exec_state_)) { + if (!ParseIntOption(option_map_, ARG_TTL_START, ttl_start, exec_state_)) { ttl_start = DBWithTTLImpl::kMinTimestamp; // TTL introduction time } int ttl_end; - if (!ParseIntOption(option_map_, ARG_TTL_END, &ttl_end, &exec_state_)) { + if (!ParseIntOption(option_map_, ARG_TTL_END, ttl_end, exec_state_)) { ttl_end = DBWithTTLImpl::kMaxTimestamp; // Max time allowed by TTL feature } if (ttl_end < ttl_start) { @@ -1693,9 +1655,9 @@ void ScanCommand::DoCommand() { ReadableTime(ttl_start).c_str(), ReadableTime(ttl_end).c_str()); } for ( ; - it->Valid() && (!end_key_specified_ || it->key().ToString() < end_key_); - it->Next()) { - std::string key = ldb_options_.key_formatter->Format(it->key()); + it->Valid() && (!end_key_specified_ || it->key().ToString() < end_key_); + it->Next()) { + string key = ldb_options_.key_formatter->Format(it->key()); if (is_db_ttl_) { TtlIterator* it_ttl = dynamic_cast(it); assert(it_ttl); @@ -1707,10 +1669,11 @@ void ScanCommand::DoCommand() { fprintf(stdout, "%s ", ReadableTime(rawtime).c_str()); } } - std::string value = it->value().ToString(); + string value = it->value().ToString(); fprintf(stdout, "%s : %s\n", (is_key_hex_ ? "0x" + it->key().ToString(true) : key).c_str(), - (is_value_hex_ ? StringToHex(value) : value).c_str()); + (is_value_hex_ ? StringToHex(value) : value).c_str() + ); num_keys_scanned++; if (max_keys_scanned_ >= 0 && num_keys_scanned >= max_keys_scanned_) { break; @@ -1723,9 +1686,8 @@ void ScanCommand::DoCommand() { } -DeleteCommand::DeleteCommand(const std::vector& params, - const std::map& options, - const std::vector& flags) : +DeleteCommand::DeleteCommand(const vector& params, + const map& options, const vector& flags) : LDBCommand(options, flags, false, BuildCmdLineOptions({ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX})) { @@ -1740,10 +1702,10 @@ DeleteCommand::DeleteCommand(const std::vector& params, } } -void DeleteCommand::Help(std::string* ret) { - ret->append(" "); - ret->append(DeleteCommand::Name() + " "); - ret->append("\n"); +void DeleteCommand::Help(string& ret) { + ret.append(" "); + ret.append(DeleteCommand::Name() + " "); + ret.append("\n"); } void DeleteCommand::DoCommand() { @@ -1756,10 +1718,8 @@ void DeleteCommand::DoCommand() { } -PutCommand::PutCommand( - const std::vector& params, - const std::map& options, - const std::vector& flags) : +PutCommand::PutCommand(const vector& params, + const map& options, const vector& flags) : LDBCommand(options, flags, false, BuildCmdLineOptions({ARG_TTL, ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX, ARG_CREATE_IF_MISSING})) { @@ -1781,12 +1741,12 @@ PutCommand::PutCommand( } } -void PutCommand::Help(std::string* ret) { - ret->append(" "); - ret->append(PutCommand::Name()); - ret->append(" "); - ret->append(" [--" + ARG_TTL + "]"); - ret->append("\n"); +void PutCommand::Help(string& ret) { + ret.append(" "); + ret.append(PutCommand::Name()); + ret.append(" "); + ret.append(" [--" + ARG_TTL + "]"); + ret.append("\n"); } void PutCommand::DoCommand() { @@ -1810,43 +1770,43 @@ const char* DBQuerierCommand::GET_CMD = "get"; const char* DBQuerierCommand::PUT_CMD = "put"; const char* DBQuerierCommand::DELETE_CMD = "delete"; -DBQuerierCommand::DBQuerierCommand( - const std::vector& params, - const std::map& options, - const std::vector& flags) : +DBQuerierCommand::DBQuerierCommand(const vector& params, + const map& options, const vector& flags) : LDBCommand(options, flags, false, BuildCmdLineOptions({ARG_TTL, ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX})) { } -void DBQuerierCommand::Help(std::string* ret) { - ret->append(" "); - ret->append(DBQuerierCommand::Name()); - ret->append(" [--" + ARG_TTL + "]"); - ret->append("\n"); - ret->append(" Starts a REPL shell. Type help for list of available " +void DBQuerierCommand::Help(string& ret) { + ret.append(" "); + ret.append(DBQuerierCommand::Name()); + ret.append(" [--" + ARG_TTL + "]"); + ret.append("\n"); + ret.append(" Starts a REPL shell. Type help for list of available " "commands."); - ret->append("\n"); + ret.append("\n"); } void DBQuerierCommand::DoCommand() { if (!db_) { return; } + ReadOptions read_options; WriteOptions write_options; - std::string line; - std::string key; - std::string value; - while (getline(std::cin, line, '\n')) { - // Parse line into std::vector - std::vector tokens; + string line; + string key; + string value; + while (getline(cin, line, '\n')) { + + // Parse line into vector + vector tokens; size_t pos = 0; while (true) { size_t pos2 = line.find(' ', pos); - if (pos2 == std::string::npos) { + if (pos2 == string::npos) { break; } tokens.push_back(line.substr(pos, pos2-pos)); @@ -1854,7 +1814,7 @@ void DBQuerierCommand::DoCommand() { } tokens.push_back(line.substr(pos)); - const std::string& cmd = tokens[0]; + const string& cmd = tokens[0]; if (cmd == HELP_CMD) { fprintf(stdout, @@ -1885,18 +1845,16 @@ void DBQuerierCommand::DoCommand() { } } -CheckConsistencyCommand::CheckConsistencyCommand( - const std::vector& params, - const std::map& options, - const std::vector& flags) : +CheckConsistencyCommand::CheckConsistencyCommand(const vector& params, + const map& options, const vector& flags) : LDBCommand(options, flags, false, BuildCmdLineOptions({})) { } -void CheckConsistencyCommand::Help(std::string* ret) { - ret->append(" "); - ret->append(CheckConsistencyCommand::Name()); - ret->append("\n"); +void CheckConsistencyCommand::Help(string& ret) { + ret.append(" "); + ret.append(CheckConsistencyCommand::Name()); + ret.append("\n"); } void CheckConsistencyCommand::DoCommand() { diff --git a/util/ldb_cmd.h b/util/ldb_cmd.h index b42d779c3..9ffe0eabc 100644 --- a/util/ldb_cmd.h +++ b/util/ldb_cmd.h @@ -4,15 +4,12 @@ // of patent rights can be found in the PATENTS file in the same directory. // #pragma once -#include -#include #include #include #include +#include #include -#include -#include -#include +#include #include "db/version_set.h" #include "rocksdb/env.h" @@ -26,34 +23,39 @@ #include "util/string_util.h" #include "utilities/ttl/db_ttl_impl.h" +using std::string; +using std::map; +using std::vector; +using std::ostringstream; + namespace rocksdb { class LDBCommand { - public: +public: // Command-line arguments - static const std::string ARG_DB; - static const std::string ARG_HEX; - static const std::string ARG_KEY_HEX; - static const std::string ARG_VALUE_HEX; - static const std::string ARG_TTL; - static const std::string ARG_TTL_START; - static const std::string ARG_TTL_END; - static const std::string ARG_TIMESTAMP; - static const std::string ARG_FROM; - static const std::string ARG_TO; - static const std::string ARG_MAX_KEYS; - static const std::string ARG_BLOOM_BITS; - static const std::string ARG_FIX_PREFIX_LEN; - static const std::string ARG_COMPRESSION_TYPE; - static const std::string ARG_BLOCK_SIZE; - static const std::string ARG_AUTO_COMPACTION; - static const std::string ARG_WRITE_BUFFER_SIZE; - static const std::string ARG_FILE_SIZE; - static const std::string ARG_CREATE_IF_MISSING; + static const string ARG_DB; + static const string ARG_HEX; + static const string ARG_KEY_HEX; + static const string ARG_VALUE_HEX; + static const string ARG_TTL; + static const string ARG_TTL_START; + static const string ARG_TTL_END; + static const string ARG_TIMESTAMP; + static const string ARG_FROM; + static const string ARG_TO; + static const string ARG_MAX_KEYS; + static const string ARG_BLOOM_BITS; + static const string ARG_FIX_PREFIX_LEN; + static const string ARG_COMPRESSION_TYPE; + static const string ARG_BLOCK_SIZE; + static const string ARG_AUTO_COMPACTION; + static const string ARG_WRITE_BUFFER_SIZE; + static const string ARG_FILE_SIZE; + static const string ARG_CREATE_IF_MISSING; static LDBCommand* InitFromCmdLineArgs( - const std::vector& args, + const vector& args, const Options& options, const LDBOptions& ldb_options ); @@ -121,8 +123,8 @@ class LDBCommand { exec_state_.Reset(); } - static std::string HexToString(const std::string& str) { - std::string parsed; + static string HexToString(const string& str) { + string parsed; if (str[0] != '0' || str[1] != 'x') { fprintf(stderr, "Invalid hex input %s. Must start with 0x\n", str.c_str()); @@ -138,8 +140,8 @@ class LDBCommand { return parsed; } - static std::string StringToHex(const std::string& str) { - std::string result = "0x"; + static string StringToHex(const string& str) { + string result = "0x"; char buf[10]; for (size_t i = 0; i < str.length(); i++) { snprintf(buf, 10, "%02X", (unsigned char)str[i]); @@ -153,7 +155,7 @@ class LDBCommand { protected: LDBCommandExecuteResult exec_state_; - std::string db_path_; + string db_path_; DB* db_; DBWithTTL* db_ttl_; @@ -178,24 +180,21 @@ protected: /** * Map of options passed on the command-line. */ - const std::map option_map_; + const map option_map_; /** * Flags passed on the command-line. */ - const std::vector flags_; + const vector flags_; /** List of command-line options valid for this command */ - const std::vector valid_cmd_line_options_; + const vector valid_cmd_line_options_; - bool ParseKeyValue(const std::string& line, - std::string* key, std::string* value, - bool is_key_hex, bool is_value_hex); + bool ParseKeyValue(const string& line, string* key, string* value, + bool is_key_hex, bool is_value_hex); - LDBCommand(const std::map& options, - const std::vector& flags, - bool is_read_only, - const std::vector& valid_cmd_line_options) : + LDBCommand(const map& options, const vector& flags, + bool is_read_only, const vector& valid_cmd_line_options) : db_(nullptr), is_read_only_(is_read_only), is_key_hex_(false), @@ -206,7 +205,7 @@ protected: flags_(flags), valid_cmd_line_options_(valid_cmd_line_options) { - auto itr = options.find(ARG_DB); + map::const_iterator itr = options.find(ARG_DB); if (itr != options.end()) { db_path_ = itr->second; } @@ -237,7 +236,7 @@ protected: st = DB::Open(opt, db_path_, &db_); } if (!st.ok()) { - std::string msg = st.ToString(); + string msg = st.ToString(); exec_state_ = LDBCommandExecuteResult::FAILED(msg); } @@ -251,33 +250,29 @@ protected: } } - static std::string PrintKeyValue( - const std::string& key, const std::string& value, - bool is_key_hex, bool is_value_hex) { - std::string result; + static string PrintKeyValue(const string& key, const string& value, + bool is_key_hex, bool is_value_hex) { + string result; result.append(is_key_hex ? StringToHex(key) : key); result.append(DELIM); result.append(is_value_hex ? StringToHex(value) : value); return result; } - static std::string PrintKeyValue( - const std::string& key, const std::string& value, - bool is_hex) { + static string PrintKeyValue(const string& key, const string& value, + bool is_hex) { return PrintKeyValue(key, value, is_hex, is_hex); } /** - * Return true if the specified flag is present in the specified - * flags vector + * Return true if the specified flag is present in the specified flags vector */ - static bool IsFlagPresent( - const std::vector& flags, const std::string& flag) { + static bool IsFlagPresent(const vector& flags, const string& flag) { return (std::find(flags.begin(), flags.end(), flag) != flags.end()); } - static std::string HelpRangeCmdArgs() { - std::ostringstream str_stream; + static string HelpRangeCmdArgs() { + ostringstream str_stream; str_stream << " "; str_stream << "[--" << ARG_FROM << "] "; str_stream << "[--" << ARG_TO << "] "; @@ -289,35 +284,32 @@ protected: * used by this command. It includes the common options and the ones * passed in. */ - std::vector BuildCmdLineOptions( - std::vector options) { - std::vector ret = { - ARG_DB, ARG_BLOOM_BITS, - ARG_BLOCK_SIZE, ARG_AUTO_COMPACTION, - ARG_COMPRESSION_TYPE, ARG_WRITE_BUFFER_SIZE, - ARG_FILE_SIZE, ARG_FIX_PREFIX_LEN}; + vector BuildCmdLineOptions(vector options) { + vector ret = {ARG_DB, ARG_BLOOM_BITS, + ARG_BLOCK_SIZE, ARG_AUTO_COMPACTION, + ARG_COMPRESSION_TYPE, ARG_WRITE_BUFFER_SIZE, + ARG_FILE_SIZE, ARG_FIX_PREFIX_LEN}; ret.insert(ret.end(), options.begin(), options.end()); return ret; } - bool ParseIntOption(const std::map& options, - const std::string& option, - int* value, LDBCommandExecuteResult* exec_state); + bool ParseIntOption(const map& options, const string& option, + int& value, LDBCommandExecuteResult& exec_state); - bool ParseStringOption(const std::map& options, - const std::string& option, std::string* value); + bool ParseStringOption(const map& options, + const string& option, string* value); Options options_; LDBOptions ldb_options_; - private: +private: /** * Interpret command line options and flags to determine if the key * should be input/output in hex. */ - bool IsKeyHex(const std::map& options, - const std::vector& flags) { + bool IsKeyHex(const map& options, + const vector& flags) { return (IsFlagPresent(flags, ARG_HEX) || IsFlagPresent(flags, ARG_KEY_HEX) || ParseBooleanOption(options, ARG_HEX, false) || @@ -328,8 +320,8 @@ protected: * Interpret command line options and flags to determine if the value * should be input/output in hex. */ - bool IsValueHex(const std::map& options, - const std::vector& flags) { + bool IsValueHex(const map& options, + const vector& flags) { return (IsFlagPresent(flags, ARG_HEX) || IsFlagPresent(flags, ARG_VALUE_HEX) || ParseBooleanOption(options, ARG_HEX, false) || @@ -342,13 +334,12 @@ protected: * Throws an exception if the value of the option is not * "true" or "false" (case insensitive). */ - bool ParseBooleanOption( - const std::map& options, - const std::string& option, bool default_val) { + bool ParseBooleanOption(const map& options, + const string& option, bool default_val) { - auto itr = options.find(option); + map::const_iterator itr = options.find(option); if (itr != options.end()) { - std::string option_val = itr->second; + string option_val = itr->second; return StringToBool(itr->second); } return default_val; @@ -359,7 +350,7 @@ protected: * val must be either true or false (case insensitive). * Otherwise an exception is thrown. */ - bool StringToBool(std::string val) { + bool StringToBool(string val) { std::transform(val.begin(), val.end(), val.begin(), ::tolower); if (val == "true") { return true; @@ -371,165 +362,161 @@ protected: } static LDBCommand* SelectCommand( - const std::string& cmd, - const std::vector& cmdParams, - const std::map& option_map, - const std::vector& flags + const string& cmd, + const vector& cmdParams, + const map& option_map, + const vector& flags ); }; class CompactorCommand: public LDBCommand { - public: - static std::string Name() { return "compact"; } +public: + static string Name() { return "compact"; } - CompactorCommand(const std::vector& params, - const std::map& options, - const std::vector& flags); + CompactorCommand(const vector& params, + const map& options, const vector& flags); - static void Help(std::string* ret); + static void Help(string& ret); virtual void DoCommand(); - private: +private: bool null_from_; - std::string from_; + string from_; bool null_to_; - std::string to_; + string to_; }; class DBDumperCommand: public LDBCommand { - public: - static std::string Name() { return "dump"; } +public: + static string Name() { return "dump"; } - DBDumperCommand(const std::vector& params, - const std::map& options, - const std::vector& flags); + DBDumperCommand(const vector& params, + const map& options, const vector& flags); - static void Help(std::string* ret); + static void Help(string& ret); virtual void DoCommand(); - private: +private: bool null_from_; - std::string from_; + string from_; bool null_to_; - std::string to_; - uint64_t max_keys_; - std::string delim_; + string to_; + int max_keys_; + string delim_; bool count_only_; bool count_delim_; bool print_stats_; - static const std::string ARG_COUNT_ONLY; - static const std::string ARG_COUNT_DELIM; - static const std::string ARG_STATS; - static const std::string ARG_TTL_BUCKET; + static const string ARG_COUNT_ONLY; + static const string ARG_COUNT_DELIM; + static const string ARG_STATS; + static const string ARG_TTL_BUCKET; }; class InternalDumpCommand: public LDBCommand { - public: - static std::string Name() { return "idump"; } +public: + static string Name() { return "idump"; } - InternalDumpCommand(const std::vector& params, - const std::map& options, - const std::vector& flags); + InternalDumpCommand(const vector& params, + const map& options, + const vector& flags); - static void Help(std::string* ret); + static void Help(string& ret); virtual void DoCommand(); - private: +private: bool has_from_; - std::string from_; + string from_; bool has_to_; - std::string to_; + string to_; int max_keys_; - std::string delim_; + string delim_; bool count_only_; bool count_delim_; bool print_stats_; bool is_input_key_hex_; - static const std::string ARG_DELIM; - static const std::string ARG_COUNT_ONLY; - static const std::string ARG_COUNT_DELIM; - static const std::string ARG_STATS; - static const std::string ARG_INPUT_KEY_HEX; + static const string ARG_DELIM; + static const string ARG_COUNT_ONLY; + static const string ARG_COUNT_DELIM; + static const string ARG_STATS; + static const string ARG_INPUT_KEY_HEX; }; class DBLoaderCommand: public LDBCommand { - public: - static std::string Name() { return "load"; } +public: + static string Name() { return "load"; } - DBLoaderCommand(const std::vector& params, - const std::map& options, - const std::vector& flags); + DBLoaderCommand(string& db_name, vector& args); - static void Help(std::string* ret); + DBLoaderCommand(const vector& params, + const map& options, const vector& flags); + + static void Help(string& ret); virtual void DoCommand(); virtual Options PrepareOptionsForOpenDB(); - private: +private: bool create_if_missing_; bool disable_wal_; bool bulk_load_; bool compact_; - static const std::string ARG_DISABLE_WAL; - static const std::string ARG_BULK_LOAD; - static const std::string ARG_COMPACT; + static const string ARG_DISABLE_WAL; + static const string ARG_BULK_LOAD; + static const string ARG_COMPACT; }; class ManifestDumpCommand: public LDBCommand { - public: - static std::string Name() { return "manifest_dump"; } +public: + static string Name() { return "manifest_dump"; } - ManifestDumpCommand(const std::vector& params, - const std::map& options, - const std::vector& flags); + ManifestDumpCommand(const vector& params, + const map& options, const vector& flags); - static void Help(std::string* ret); + static void Help(string& ret); virtual void DoCommand(); virtual bool NoDBOpen() { return true; } - private: +private: bool verbose_; - std::string path_; + string path_; - static const std::string ARG_VERBOSE; - static const std::string ARG_PATH; + static const string ARG_VERBOSE; + static const string ARG_PATH; }; class ListColumnFamiliesCommand : public LDBCommand { public: - static std::string Name() { return "list_column_families"; } + static string Name() { return "list_column_families"; } - ListColumnFamiliesCommand( - const std::vector& params, - const std::map& options, - const std::vector& flags); + ListColumnFamiliesCommand(const vector& params, + const map& options, + const vector& flags); - static void Help(std::string* ret); + static void Help(string& ret); virtual void DoCommand(); virtual bool NoDBOpen() { return true; } private: - std::string dbname_; + string dbname_; }; class ReduceDBLevelsCommand : public LDBCommand { - public: - static std::string Name() { return "reduce_levels"; } +public: + static string Name() { return "reduce_levels"; } - ReduceDBLevelsCommand(const std::vector& params, - const std::map& options, - const std::vector& flags); + ReduceDBLevelsCommand(const vector& params, + const map& options, const vector& flags); virtual Options PrepareOptionsForOpenDB(); @@ -539,179 +526,169 @@ class ReduceDBLevelsCommand : public LDBCommand { return true; } - static void Help(std::string* msg); + static void Help(string& msg); - static std::vector PrepareArgs( - const std::string& db_path, - int new_levels, + static vector PrepareArgs(const string& db_path, int new_levels, bool print_old_level = false); - private: +private: int old_levels_; int new_levels_; bool print_old_levels_; - static const std::string ARG_NEW_LEVELS; - static const std::string ARG_PRINT_OLD_LEVELS; + static const string ARG_NEW_LEVELS; + static const string ARG_PRINT_OLD_LEVELS; Status GetOldNumOfLevels(Options& opt, int* levels); }; class ChangeCompactionStyleCommand : public LDBCommand { - public: - static std::string Name() { return "change_compaction_style"; } +public: + static string Name() { return "change_compaction_style"; } - ChangeCompactionStyleCommand(const std::vector& params, - const std::map& options, - const std::vector& flags); + ChangeCompactionStyleCommand(const vector& params, + const map& options, const vector& flags); virtual Options PrepareOptionsForOpenDB(); virtual void DoCommand(); - static void Help(std::string* msg); + static void Help(string& msg); - private: +private: int old_compaction_style_; int new_compaction_style_; - static const std::string ARG_OLD_COMPACTION_STYLE; - static const std::string ARG_NEW_COMPACTION_STYLE; + static const string ARG_OLD_COMPACTION_STYLE; + static const string ARG_NEW_COMPACTION_STYLE; }; class WALDumperCommand : public LDBCommand { - public: - static std::string Name() { return "dump_wal"; } +public: + static string Name() { return "dump_wal"; } - WALDumperCommand(const std::vector& params, - const std::map& options, - const std::vector& flags); + WALDumperCommand(const vector& params, + const map& options, const vector& flags); virtual bool NoDBOpen() { return true; } - static void Help(std::string* ret); + static void Help(string& ret); virtual void DoCommand(); - private: +private: bool print_header_; - std::string wal_file_; + string wal_file_; bool print_values_; - static const std::string ARG_WAL_FILE; - static const std::string ARG_PRINT_HEADER; - static const std::string ARG_PRINT_VALUE; + static const string ARG_WAL_FILE; + static const string ARG_PRINT_HEADER; + static const string ARG_PRINT_VALUE; }; class GetCommand : public LDBCommand { - public: - static std::string Name() { return "get"; } +public: + static string Name() { return "get"; } - GetCommand(const std::vector& params, - const std::map& options, - const std::vector& flags); + GetCommand(const vector& params, const map& options, + const vector& flags); virtual void DoCommand(); - static void Help(std::string* ret); + static void Help(string& ret); - private: - std::string key_; +private: + string key_; }; class ApproxSizeCommand : public LDBCommand { - public: - static std::string Name() { return "approxsize"; } +public: + static string Name() { return "approxsize"; } - ApproxSizeCommand(const std::vector& params, - const std::map& options, - const std::vector& flags); + ApproxSizeCommand(const vector& params, + const map& options, const vector& flags); virtual void DoCommand(); - static void Help(std::string* ret); + static void Help(string& ret); - private: - std::string start_key_; - std::string end_key_; +private: + string start_key_; + string end_key_; }; class BatchPutCommand : public LDBCommand { - public: - static std::string Name() { return "batchput"; } +public: + static string Name() { return "batchput"; } - BatchPutCommand(const std::vector& params, - const std::map& options, - const std::vector& flags); + BatchPutCommand(const vector& params, + const map& options, const vector& flags); virtual void DoCommand(); - static void Help(std::string* ret); + static void Help(string& ret); virtual Options PrepareOptionsForOpenDB(); - private: +private: /** * The key-values to be inserted. */ - std::vector> key_values_; + vector> key_values_; }; class ScanCommand : public LDBCommand { - public: - static std::string Name() { return "scan"; } +public: + static string Name() { return "scan"; } - ScanCommand(const std::vector& params, - const std::map& options, - const std::vector& flags); + ScanCommand(const vector& params, const map& options, + const vector& flags); virtual void DoCommand(); - static void Help(std::string* ret); + static void Help(string& ret); - private: - std::string start_key_; - std::string end_key_; +private: + string start_key_; + string end_key_; bool start_key_specified_; bool end_key_specified_; int max_keys_scanned_; }; class DeleteCommand : public LDBCommand { - public: - static std::string Name() { return "delete"; } +public: + static string Name() { return "delete"; } - DeleteCommand(const std::vector& params, - const std::map& options, - const std::vector& flags); + DeleteCommand(const vector& params, + const map& options, const vector& flags); virtual void DoCommand(); - static void Help(std::string* ret); + static void Help(string& ret); - private: - std::string key_; +private: + string key_; }; class PutCommand : public LDBCommand { - public: - static std::string Name() { return "put"; } +public: + static string Name() { return "put"; } - PutCommand(const std::vector& params, - const std::map& options, - const std::vector& flags); + PutCommand(const vector& params, const map& options, + const vector& flags); virtual void DoCommand(); - static void Help(std::string* ret); + static void Help(string& ret); virtual Options PrepareOptionsForOpenDB(); - private: - std::string key_; - std::string value_; +private: + string key_; + string value_; }; /** @@ -719,18 +696,17 @@ class PutCommand : public LDBCommand { * get/put/delete. */ class DBQuerierCommand: public LDBCommand { - public: - static std::string Name() { return "query"; } +public: + static string Name() { return "query"; } - DBQuerierCommand(const std::vector& params, - const std::map& options, - const std::vector& flags); + DBQuerierCommand(const vector& params, + const map& options, const vector& flags); - static void Help(std::string* ret); + static void Help(string& ret); virtual void DoCommand(); - private: +private: static const char* HELP_CMD; static const char* GET_CMD; static const char* PUT_CMD; @@ -738,12 +714,11 @@ class DBQuerierCommand: public LDBCommand { }; class CheckConsistencyCommand : public LDBCommand { - public: - static std::string Name() { return "checkconsistency"; } +public: + static string Name() { return "checkconsistency"; } - CheckConsistencyCommand(const std::vector& params, - const std::map& options, - const std::vector& flags); + CheckConsistencyCommand(const vector& params, + const map& options, const vector& flags); virtual void DoCommand(); @@ -751,7 +726,7 @@ class CheckConsistencyCommand : public LDBCommand { return true; } - static void Help(std::string* ret); + static void Help(string& ret); }; } // namespace rocksdb diff --git a/util/ldb_cmd_execute_result.h b/util/ldb_cmd_execute_result.h index 48a4b495c..b8e6c4634 100644 --- a/util/ldb_cmd_execute_result.h +++ b/util/ldb_cmd_execute_result.h @@ -15,7 +15,7 @@ public: LDBCommandExecuteResult() : state_(EXEC_NOT_STARTED), message_("") {} - LDBCommandExecuteResult(State state, const std::string& msg) : + LDBCommandExecuteResult(State state, std::string& msg) : state_(state), message_(msg) {} std::string ToString() { @@ -52,11 +52,11 @@ public: return state_ == EXEC_FAILED; } - static LDBCommandExecuteResult SUCCEED(const std::string& msg) { + static LDBCommandExecuteResult SUCCEED(std::string msg) { return LDBCommandExecuteResult(EXEC_SUCCEED, msg); } - static LDBCommandExecuteResult FAILED(const std::string& msg) { + static LDBCommandExecuteResult FAILED(std::string msg) { return LDBCommandExecuteResult(EXEC_FAILED, msg); } diff --git a/util/ldb_tool.cc b/util/ldb_tool.cc index 9824c0210..bb6c8ffca 100644 --- a/util/ldb_tool.cc +++ b/util/ldb_tool.cc @@ -24,7 +24,7 @@ class LDBCommandRunner { public: static void PrintHelp(const char* exec_name) { - std::string ret; + string ret; ret.append("ldb - LevelDB Tool"); ret.append("\n\n"); @@ -59,26 +59,26 @@ public: ret.append("\n\n"); ret.append("Data Access Commands:\n"); - PutCommand::Help(&ret); - GetCommand::Help(&ret); - BatchPutCommand::Help(&ret); - ScanCommand::Help(&ret); - DeleteCommand::Help(&ret); - DBQuerierCommand::Help(&ret); - ApproxSizeCommand::Help(&ret); - CheckConsistencyCommand::Help(&ret); + PutCommand::Help(ret); + GetCommand::Help(ret); + BatchPutCommand::Help(ret); + ScanCommand::Help(ret); + DeleteCommand::Help(ret); + DBQuerierCommand::Help(ret); + ApproxSizeCommand::Help(ret); + CheckConsistencyCommand::Help(ret); ret.append("\n\n"); ret.append("Admin Commands:\n"); - WALDumperCommand::Help(&ret); - CompactorCommand::Help(&ret); - ReduceDBLevelsCommand::Help(&ret); - ChangeCompactionStyleCommand::Help(&ret); - DBDumperCommand::Help(&ret); - DBLoaderCommand::Help(&ret); - ManifestDumpCommand::Help(&ret); - ListColumnFamiliesCommand::Help(&ret); - InternalDumpCommand::Help(&ret); + WALDumperCommand::Help(ret); + CompactorCommand::Help(ret); + ReduceDBLevelsCommand::Help(ret); + ChangeCompactionStyleCommand::Help(ret); + DBDumperCommand::Help(ret); + DBLoaderCommand::Help(ret); + ManifestDumpCommand::Help(ret); + ListColumnFamiliesCommand::Help(ret); + InternalDumpCommand::Help(ret); fprintf(stderr, "%s\n", ret.c_str()); }