diff --git a/tools/sst_dump.cc b/tools/sst_dump.cc index 6c496e8dd..e9fdf1c3f 100644 --- a/tools/sst_dump.cc +++ b/tools/sst_dump.cc @@ -275,8 +275,8 @@ static void print_help() { } namespace { -string HexToString(const string& str) { - string parsed; +std::string HexToString(const std::string& str) { + std::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 618c10a35..c03c1b31a 100644 --- a/util/ldb_cmd.cc +++ b/util/ldb_cmd.cc @@ -4,6 +4,20 @@ // 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" @@ -17,46 +31,36 @@ #include "util/scoped_arena_iterator.h" #include "utilities/ttl/db_ttl_impl.h" -#include -#include -#include -#include -#include -#include - namespace rocksdb { -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 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"; const char* LDBCommand::DELIM = " ==> "; LDBCommand* LDBCommand::InitFromCmdLineArgs( - int argc, - char** argv, - const Options& options, - const LDBOptions& ldb_options -) { - vector args; + int argc, + char** argv, + const Options& options, + const LDBOptions& ldb_options) { + std::vector args; for (int i = 1; i < argc; i++) { args.push_back(argv[i]); } @@ -67,37 +71,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 vector& args, - const Options& options, - const LDBOptions& ldb_options -) { - // --x=y command line arguments are added as x->y map entries. - map option_map; + 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; // Command-line arguments of the form --hex end up in this array as hex - vector flags; + std::vector flags; // Everything other than option_map and flags. Represents commands - // and their parameters. For eg: put key1 value1 go into this vector. - vector cmdTokens; + // and their parameters. For eg: put key1 value1 go into this std::vector. + std::vector cmdTokens; - const string OPTION_PREFIX = "--"; + const std::string OPTION_PREFIX = "--"; for (const auto& arg : args) { - if (arg[0] == '-' && arg[1] == '-'){ - vector splits = stringSplit(arg, '='); + if (arg[0] == '-' && arg[1] == '-') { + std::vector splits = stringSplit(arg, '='); if (splits.size() == 2) { - string optionKey = splits[0].substr(OPTION_PREFIX.size()); + std::string optionKey = splits[0].substr(OPTION_PREFIX.size()); option_map[optionKey] = splits[1]; } else { - string optionKey = splits[0].substr(OPTION_PREFIX.size()); + std::string optionKey = splits[0].substr(OPTION_PREFIX.size()); flags.push_back(optionKey); } } else { @@ -110,14 +114,10 @@ LDBCommand* LDBCommand::InitFromCmdLineArgs( return nullptr; } - string cmd = cmdTokens[0]; - vector cmdParams(cmdTokens.begin()+1, cmdTokens.end()); + std::string cmd = cmdTokens[0]; + std::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,11 +128,9 @@ LDBCommand* LDBCommand::InitFromCmdLineArgs( LDBCommand* LDBCommand::SelectCommand( const std::string& cmd, - const vector& cmdParams, - const map& option_map, - const vector& flags - ) { - + const std::vector& cmdParams, + const std::map& option_map, + const std::vector& flags) { if (cmd == GetCommand::Name()) { return new GetCommand(cmdParams, option_map, flags); } else if (cmd == PutCommand::Name()) { @@ -179,21 +177,21 @@ LDBCommand* LDBCommand::SelectCommand( * value. If there is an error, the specified exec_state is also * updated. */ -bool LDBCommand::ParseIntOption(const map& options, - const string& option, int& value, - LDBCommandExecuteResult& exec_state) { - - map::const_iterator itr = option_map_.find(option); +bool LDBCommand::ParseIntOption( + const std::map& options, + const std::string& option, int* value, + LDBCommandExecuteResult* exec_state) { + auto itr = option_map_.find(option); if (itr != option_map_.end()) { try { - value = stoi(itr->second); + *value = stoi(itr->second); return true; - } 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."); + } 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."); } } return false; @@ -204,8 +202,9 @@ bool LDBCommand::ParseIntOption(const map& options, * Returns true if the option is found. * Returns false otherwise. */ -bool LDBCommand::ParseStringOption(const map& options, - const string& option, string* value) { +bool LDBCommand::ParseStringOption( + const std::map& options, + const std::string& option, std::string* value) { auto itr = option_map_.find(option); if (itr != option_map_.end()) { *value = itr->second; @@ -219,12 +218,12 @@ Options LDBCommand::PrepareOptionsForOpenDB() { Options opt = options_; opt.create_if_missing = false; - map::const_iterator itr; + std::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)); @@ -235,7 +234,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; @@ -256,7 +255,7 @@ Options LDBCommand::PrepareOptionsForOpenDB() { itr = option_map_.find(ARG_COMPRESSION_TYPE); if (itr != option_map_.end()) { - string comp = itr->second; + std::string comp = itr->second; if (comp == "no") { opt.compression = kNoCompression; } else if (comp == "snappy") { @@ -277,8 +276,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 { @@ -288,7 +287,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 { @@ -302,13 +301,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."); } } @@ -316,10 +315,11 @@ Options LDBCommand::PrepareOptionsForOpenDB() { return opt; } -bool LDBCommand::ParseKeyValue(const string& line, string* key, string* value, - bool is_key_hex, bool is_value_hex) { +bool LDBCommand::ParseKeyValue( + const std::string& line, std::string* key, std::string* value, + bool is_key_hex, bool is_value_hex) { size_t pos = line.find(DELIM); - if (pos != string::npos) { + if (pos != std::string::npos) { *key = line.substr(0, pos); *value = line.substr(pos + strlen(DELIM)); if (is_key_hex) { @@ -343,20 +343,20 @@ bool LDBCommand::ParseKeyValue(const string& line, string* key, string* value, */ bool LDBCommand::ValidateCmdLineOptions() { - for (map::const_iterator itr = option_map_.begin(); - itr != option_map_.end(); ++itr) { + for (auto 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 (vector::const_iterator itr = flags_.begin(); + for (std::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,14 +371,15 @@ bool LDBCommand::ValidateCmdLineOptions() { return true; } -CompactorCommand::CompactorCommand(const vector& params, - const map& options, const vector& flags) : +CompactorCommand::CompactorCommand( + const std::vector& params, + const std::map& options, + const std::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) { - - map::const_iterator itr = options.find(ARG_FROM); + auto itr = options.find(ARG_FROM); if (itr != options.end()) { null_from_ = false; from_ = itr->second; @@ -400,11 +401,11 @@ CompactorCommand::CompactorCommand(const vector& params, } } -void CompactorCommand::Help(string& ret) { - ret.append(" "); - ret.append(CompactorCommand::Name()); - ret.append(HelpRangeCmdArgs()); - ret.append("\n"); +void CompactorCommand::Help(std::string* ret) { + ret->append(" "); + ret->append(CompactorCommand::Name()); + ret->append(HelpRangeCmdArgs()); + ret->append("\n"); } void CompactorCommand::DoCommand() { @@ -425,12 +426,14 @@ void CompactorCommand::DoCommand() { delete end; } -const string DBLoaderCommand::ARG_DISABLE_WAL = "disable_wal"; -const string DBLoaderCommand::ARG_BULK_LOAD = "bulk_load"; -const string DBLoaderCommand::ARG_COMPACT = "compact"; +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"; -DBLoaderCommand::DBLoaderCommand(const vector& params, - const map& options, const vector& flags) : +DBLoaderCommand::DBLoaderCommand( + const std::vector& params, + const std::map& options, + const std::vector& flags) : LDBCommand(options, flags, false, BuildCmdLineOptions({ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX, ARG_FROM, ARG_TO, ARG_CREATE_IF_MISSING, @@ -445,14 +448,14 @@ DBLoaderCommand::DBLoaderCommand(const vector& params, compact_ = IsFlagPresent(flags, ARG_COMPACT); } -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"); +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"); } Options DBLoaderCommand::PrepareOptionsForOpenDB() { @@ -475,10 +478,10 @@ void DBLoaderCommand::DoCommand() { } int bad_lines = 0; - string line; - while (getline(cin, line, '\n')) { - string key; - string value; + std::string line; + while (getline(std::cin, line, '\n')) { + std::string key; + std::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:")) { @@ -491,7 +494,7 @@ void DBLoaderCommand::DoCommand() { } if (bad_lines > 0) { - cout << "Warning: " << bad_lines << " bad lines ignored." << endl; + std::cout << "Warning: " << bad_lines << " bad lines ignored." << std::endl; } if (compact_) { db_->CompactRange(nullptr, nullptr); @@ -500,27 +503,28 @@ void DBLoaderCommand::DoCommand() { // ---------------------------------------------------------------------------- -const string ManifestDumpCommand::ARG_VERBOSE = "verbose"; -const string ManifestDumpCommand::ARG_PATH = "path"; +const std::string ManifestDumpCommand::ARG_VERBOSE = "verbose"; +const std::string ManifestDumpCommand::ARG_PATH = "path"; -void ManifestDumpCommand::Help(string& ret) { - ret.append(" "); - ret.append(ManifestDumpCommand::Name()); - ret.append(" [--" + ARG_VERBOSE + "]"); - ret.append(" [--" + ARG_PATH + "=]"); - ret.append("\n"); +void ManifestDumpCommand::Help(std::string* ret) { + ret->append(" "); + ret->append(ManifestDumpCommand::Name()); + ret->append(" [--" + ARG_VERBOSE + "]"); + ret->append(" [--" + ARG_PATH + " = ]"); + ret->append("\n"); } -ManifestDumpCommand::ManifestDumpCommand(const vector& params, - const map& options, const vector& flags) : +ManifestDumpCommand::ManifestDumpCommand( + const std::vector& params, + const std::map& options, + const std::vector& flags) : LDBCommand(options, flags, false, BuildCmdLineOptions({ARG_VERBOSE, ARG_PATH, ARG_HEX})), verbose_(false), - path_("") -{ + path_("") { verbose_ = IsFlagPresent(flags, ARG_VERBOSE); - map::const_iterator itr = options.find(ARG_PATH); + auto itr = options.find(ARG_PATH); if (itr != options.end()) { path_ = itr->second; if (path_.empty()) { @@ -597,16 +601,17 @@ void ManifestDumpCommand::DoCommand() { // ---------------------------------------------------------------------------- -void ListColumnFamiliesCommand::Help(string& ret) { - ret.append(" "); - ret.append(ListColumnFamiliesCommand::Name()); - ret.append(" full_path_to_db_directory "); - ret.append("\n"); +void ListColumnFamiliesCommand::Help(std::string* ret) { + ret->append(" "); + ret->append(ListColumnFamiliesCommand::Name()); + ret->append(" full_path_to_db_directory "); + ret->append("\n"); } ListColumnFamiliesCommand::ListColumnFamiliesCommand( - const vector& params, const map& options, - const vector& flags) + const std::vector& params, + const std::map& options, + const std::vector& flags) : LDBCommand(options, flags, false, {}) { if (params.size() != 1) { @@ -618,7 +623,7 @@ ListColumnFamiliesCommand::ListColumnFamiliesCommand( } void ListColumnFamiliesCommand::DoCommand() { - vector column_families; + std::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(), @@ -641,54 +646,56 @@ void ListColumnFamiliesCommand::DoCommand() { namespace { -string ReadableTime(int unixtime) { +std::string ReadableTime(int unixtime) { char time_buffer [80]; time_t rawtime = unixtime; struct tm * timeinfo = localtime(&rawtime); strftime(time_buffer, 80, "%c", timeinfo); - return string(time_buffer); + return std::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(vector& bucket_counts, int ttl_start, +void IncBucketCounts(std::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 vector& bucket_counts, int ttl_start, - int ttl_end, int bucket_size, int num_buckets) { +void PrintBucketCounts( + const std::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 : %lu\n", + for (int i = 0; i < num_buckets - 1; i++, time_point += bucket_size) { + fprintf(stdout, "Keys in range %s to %s : %" PRIu64 "\n", ReadableTime(time_point).c_str(), ReadableTime(time_point + bucket_size).c_str(), - (unsigned long)bucket_counts[i]); + bucket_counts[i]); } - fprintf(stdout, "Keys in range %s to %s : %lu\n", + fprintf(stdout, "Keys in range %s to %s : %" PRIu64 "\n", ReadableTime(time_point).c_str(), ReadableTime(ttl_end).c_str(), - (unsigned long)bucket_counts[num_buckets - 1]); + bucket_counts[num_buckets - 1]); } } // namespace -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"; +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"; -InternalDumpCommand::InternalDumpCommand(const vector& params, - const map& options, - const vector& flags) : +InternalDumpCommand::InternalDumpCommand( + const std::vector& params, + const std::map& options, + const std::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), @@ -701,15 +708,14 @@ InternalDumpCommand::InternalDumpCommand(const vector& params, has_from_ = ParseStringOption(options, ARG_FROM, &from_); has_to_ = ParseStringOption(options, ARG_TO, &to_); - ParseIntOption(options, ARG_MAX_KEYS, max_keys_, exec_state_); - map::const_iterator itr = options.find(ARG_COUNT_DELIM); + ParseIntOption(options, ARG_MAX_KEYS, &max_keys_, &exec_state_); + auto 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); @@ -726,16 +732,16 @@ InternalDumpCommand::InternalDumpCommand(const vector& params, } } -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::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::DoCommand() { @@ -744,7 +750,7 @@ void InternalDumpCommand::DoCommand() { } if (print_stats_) { - string stats; + std::string stats; if (db_->GetProperty("rocksdb.stats", &stats)) { fprintf(stdout, "%s\n", stats.c_str()); } @@ -756,10 +762,10 @@ void InternalDumpCommand::DoCommand() { exec_state_ = LDBCommandExecuteResult::FAILED("DB is not DBImpl"); return; } - string rtype1,rtype2,row,val; + std::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)); @@ -776,7 +782,7 @@ void InternalDumpCommand::DoCommand() { iter->SeekToFirst(); } - long long count = 0; + uint64_t count = 0; for (; iter->Valid(); iter->Next()) { ParsedInternalKey ikey; if (!ParseInternalKey(iter->key(), &ikey)) { @@ -795,59 +801,69 @@ 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:%lld\tsize:%lld\n",rtype2.c_str(), - (long long)c,(long long)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:%" PRIu64 "\tsize:%" PRIu64 "\n", + rtype2.c_str(), c, s2); + c = 1; + s2 = s1; rtype2 = rtype1; } else { c++; - s2+=s1; - rtype2=rtype1; + s2+= s1; + rtype2 = rtype1; } } if (!count_only_ && !count_delim_) { - string key = ikey.DebugString(is_key_hex_); - string value = iter->value().ToString(is_value_hex_); - std::cout << key << " => " << value << "\n"; + std::string key = ikey.DebugString(is_key_hex_); + std::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 >= max_keys_) break; + if (max_keys_ > 0 && count >= static_cast(max_keys_)) { + break; + } } - if(count_delim_) { - fprintf(stdout,"%s => count:%lld\tsize:%lld\n", rtype2.c_str(), - (long long)c,(long long)s2); + if (count_delim_) { + fprintf(stdout, + "%s = > count:%" PRIu64 "\tsize:%" PRIu64 "\n", + rtype2.c_str(), c, s2); } else - fprintf(stdout, "Internal keys in range: %lld\n", (long long) count); + fprintf(stdout, "Internal keys in range: %" PRIu64 "\n", count); } -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"; +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"; -DBDumperCommand::DBDumperCommand(const vector& params, - const map& options, const vector& flags) : +DBDumperCommand::DBDumperCommand( + const std::vector& params, + const std::map& options, + const std::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), @@ -855,7 +871,7 @@ DBDumperCommand::DBDumperCommand(const vector& params, count_delim_(false), print_stats_(false) { - map::const_iterator itr = options.find(ARG_FROM); + auto itr = options.find(ARG_FROM); if (itr != options.end()) { null_from_ = false; from_ = itr->second; @@ -871,10 +887,10 @@ DBDumperCommand::DBDumperCommand(const vector& params, if (itr != options.end()) { try { max_keys_ = stoi(itr->second); - } catch(const invalid_argument&) { + } catch(const std::invalid_argument&) { exec_state_ = LDBCommandExecuteResult::FAILED(ARG_MAX_KEYS + " has an invalid value"); - } catch(const out_of_range&) { + } catch(const std::out_of_range&) { exec_state_ = LDBCommandExecuteResult::FAILED(ARG_MAX_KEYS + " has a value out-of-range"); } @@ -885,7 +901,7 @@ DBDumperCommand::DBDumperCommand(const vector& params, count_delim_ = true; } else { count_delim_ = IsFlagPresent(flags, ARG_COUNT_DELIM); - delim_="."; + delim_ = "."; } print_stats_ = IsFlagPresent(flags, ARG_STATS); @@ -901,20 +917,20 @@ DBDumperCommand::DBDumperCommand(const vector& params, } } -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::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::DoCommand() { @@ -924,7 +940,7 @@ void DBDumperCommand::DoCommand() { // Parse command line args uint64_t count = 0; if (print_stats_) { - string stats; + std::string stats; if (db_->GetProperty("rocksdb.stats", &stats)) { fprintf(stdout, "%s\n", stats.c_str()); } @@ -946,11 +962,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) { @@ -960,20 +976,21 @@ 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 - string rtype1,rtype2,row,val; + std::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); - vector bucket_counts(num_buckets, 0); + std::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()); @@ -999,7 +1016,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; @@ -1008,29 +1025,28 @@ 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:%lld\tsize:%lld\n",rtype2.c_str(), - (long long )c,(long long)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:%" PRIu64 "\tsize:%" PRIu64 "\n", + rtype2.c_str(), c, 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()); } - string str = PrintKeyValue(iter->key().ToString(), + std::string str = PrintKeyValue(iter->key().ToString(), iter->value().ToString(), is_key_hex_, is_value_hex_); fprintf(stdout, "%s\n", str.c_str()); @@ -1040,21 +1056,25 @@ 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:%lld\tsize:%lld\n",rtype2.c_str(), - (long long )c,(long long)s2); + } else if (count_delim_) { + fprintf(stdout, "%s = > count:%" PRIu64 "\tsize:%" PRIu64 "\n", + rtype2.c_str(), c, s2); } else { - fprintf(stdout, "Keys in range: %lld\n", (long long) count); + fprintf(stdout, "Keys in range: %" PRIu64 "\n", count); } // Clean up delete iter; } -const string ReduceDBLevelsCommand::ARG_NEW_LEVELS = "new_levels"; -const string ReduceDBLevelsCommand::ARG_PRINT_OLD_LEVELS = "print_old_levels"; +const std::string + ReduceDBLevelsCommand::ARG_NEW_LEVELS = "new_levels"; +const std::string + ReduceDBLevelsCommand::ARG_PRINT_OLD_LEVELS = "print_old_levels"; -ReduceDBLevelsCommand::ReduceDBLevelsCommand(const vector& params, - const map& options, const vector& flags) : +ReduceDBLevelsCommand::ReduceDBLevelsCommand( + const std::vector& params, + const std::map& options, + const std::vector& flags) : LDBCommand(options, flags, false, BuildCmdLineOptions({ARG_NEW_LEVELS, ARG_PRINT_OLD_LEVELS})), old_levels_(1 << 16), @@ -1062,33 +1082,34 @@ ReduceDBLevelsCommand::ReduceDBLevelsCommand(const vector& params, 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"); } } -vector ReduceDBLevelsCommand::PrepareArgs(const string& db_path, +std::vector ReduceDBLevelsCommand::PrepareArgs( + const std::string& db_path, int new_levels, bool print_old_level) { - vector ret; + std::vector ret; ret.push_back("reduce_levels"); - 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_DB + " = " + db_path); + ret.push_back("--" + ARG_NEW_LEVELS + " = " + std::to_string(new_levels)); + if (print_old_level) { ret.push_back("--" + ARG_PRINT_OLD_LEVELS); } return ret; } -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"); +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"); } Options ReduceDBLevelsCommand::PrepareOptionsForOpenDB() { @@ -1151,7 +1172,8 @@ 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_) { @@ -1170,29 +1192,31 @@ 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 string ChangeCompactionStyleCommand::ARG_OLD_COMPACTION_STYLE = +const std::string ChangeCompactionStyleCommand::ARG_OLD_COMPACTION_STYLE = "old_compaction_style"; -const string ChangeCompactionStyleCommand::ARG_NEW_COMPACTION_STYLE = +const std::string ChangeCompactionStyleCommand::ARG_NEW_COMPACTION_STYLE = "new_compaction_style"; ChangeCompactionStyleCommand::ChangeCompactionStyleCommand( - const vector& params, const map& options, - const vector& flags) : + const std::vector& params, + const std::map& options, + const std::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( @@ -1201,8 +1225,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( @@ -1227,14 +1251,16 @@ ChangeCompactionStyleCommand::ChangeCompactionStyleCommand( } } -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"); +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"); } Options ChangeCompactionStyleCommand::PrepareOptionsForOpenDB() { @@ -1262,9 +1288,9 @@ void ChangeCompactionStyleCommand::DoCommand() { db_->GetProperty("rocksdb.num-files-at-level" + NumberToString(i), &property); - // format print string + // format print std::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", @@ -1282,9 +1308,9 @@ void ChangeCompactionStyleCommand::DoCommand() { db_->GetProperty("rocksdb.num-files-at-level" + NumberToString(i), &property); - // format print string + // format print std::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()); @@ -1292,15 +1318,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; } } @@ -1311,14 +1337,15 @@ void ChangeCompactionStyleCommand::DoCommand() { class InMemoryHandler : public WriteBatch::Handler { public: - InMemoryHandler(stringstream& row, bool print_values) : Handler(),row_(row) { + InMemoryHandler(std::stringstream& row, bool print_values) + : Handler(), row_(row) { print_values_ = print_values; } void commonPutMerge(const Slice& key, const Slice& value) { - string k = LDBCommand::StringToHex(key.ToString()); + std::string k = LDBCommand::StringToHex(key.ToString()); if (print_values_) { - string v = LDBCommand::StringToHex(value.ToString()); + std::string v = LDBCommand::StringToHex(value.ToString()); row_ << k << " : "; row_ << v << " "; } else { @@ -1337,23 +1364,25 @@ class InMemoryHandler : public WriteBatch::Handler { } virtual void Delete(const Slice& key) { - row_ <<",DELETE : "; + row_ << ", DELETE : "; row_ << LDBCommand::StringToHex(key.ToString()) << " "; } virtual ~InMemoryHandler() { }; private: - stringstream & row_; + std::stringstream & row_; bool print_values_; }; -const string WALDumperCommand::ARG_WAL_FILE = "walfile"; -const string WALDumperCommand::ARG_PRINT_VALUE = "print_value"; -const string WALDumperCommand::ARG_PRINT_HEADER = "header"; +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"; -WALDumperCommand::WALDumperCommand(const vector& params, - const map& options, const vector& flags) : +WALDumperCommand::WALDumperCommand( + const std::vector& params, + const std::map& options, + const std::vector& flags) : LDBCommand(options, flags, true, BuildCmdLineOptions( {ARG_WAL_FILE, ARG_PRINT_HEADER, ARG_PRINT_VALUE})), @@ -1361,7 +1390,8 @@ WALDumperCommand::WALDumperCommand(const vector& params, wal_file_.clear(); - map::const_iterator itr = options.find(ARG_WAL_FILE); + std::map::const_iterator itr = + options.find(ARG_WAL_FILE); if (itr != options.end()) { wal_file_ = itr->second; } @@ -1375,19 +1405,19 @@ WALDumperCommand::WALDumperCommand(const vector& params, } } -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::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::DoCommand() { struct StdErrReporter : public log::Reader::Reporter { virtual void Corruption(size_t bytes, const Status& s) { - 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); - string scratch; + std::string scratch; WriteBatch batch; Slice record; - stringstream row; + std::stringstream row; if (print_header_) { - cout<<"Sequence,Count,ByteSize,Physical Offset,Key(s)"; + std::cout << "Sequence, Count, ByteSize, Physical Offset, Key(s)"; if (print_values_) { - cout << " : value "; + std::cout << " : value "; } - cout << "\n"; + std::cout << "\n"; } while(reader.ReadRecord(&record, &scratch)) { row.str(""); @@ -1419,22 +1449,24 @@ void WALDumperCommand::DoCommand() { record.size(), Status::Corruption("log record too small")); } else { WriteBatchInternal::SetContents(&batch, record); - row<& params, - const map& options, const vector& flags) : +GetCommand::GetCommand( + const std::vector& params, + const std::map& options, + const std::vector& flags) : LDBCommand(options, flags, true, BuildCmdLineOptions({ARG_TTL, ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX})) { @@ -1451,16 +1483,16 @@ GetCommand::GetCommand(const vector& params, } } -void GetCommand::Help(string& ret) { - ret.append(" "); - ret.append(GetCommand::Name()); - ret.append(" "); - ret.append(" [--" + ARG_TTL + "]"); - ret.append("\n"); +void GetCommand::Help(std::string* ret) { + ret->append(" "); + ret->append(GetCommand::Name()); + ret->append(" "); + ret->append(" [--" + ARG_TTL + "]"); + ret->append("\n"); } void GetCommand::DoCommand() { - string value; + std::string value; Status st = db_->Get(ReadOptions(), key_, &value); if (st.ok()) { fprintf(stdout, "%s\n", @@ -1471,8 +1503,10 @@ void GetCommand::DoCommand() { } -ApproxSizeCommand::ApproxSizeCommand(const vector& params, - const map& options, const vector& flags) : +ApproxSizeCommand::ApproxSizeCommand( + const std::vector& params, + const std::map& options, + const std::vector& flags) : LDBCommand(options, flags, true, BuildCmdLineOptions({ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX, ARG_FROM, ARG_TO})) { @@ -1499,11 +1533,11 @@ ApproxSizeCommand::ApproxSizeCommand(const vector& params, } } -void ApproxSizeCommand::Help(string& ret) { - ret.append(" "); - ret.append(ApproxSizeCommand::Name()); - ret.append(HelpRangeCmdArgs()); - ret.append("\n"); +void ApproxSizeCommand::Help(std::string* ret) { + ret->append(" "); + ret->append(ApproxSizeCommand::Name()); + ret->append(HelpRangeCmdArgs()); + ret->append("\n"); } void ApproxSizeCommand::DoCommand() { @@ -1522,43 +1556,45 @@ void ApproxSizeCommand::DoCommand() { } -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})) { +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})) { if (params.size() < 2) { exec_state_ = LDBCommandExecuteResult::FAILED( - "At least one pair must be specified batchput."); + "At least one std::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) { - string key = params.at(i); - string value = params.at(i+1); - key_values_.push_back(pair( + std::string key = params.at(i); + std::string value = params.at(i+1); + key_values_.push_back(std::pair( is_key_hex_ ? HexToString(key) : key, is_value_hex_ ? HexToString(value) : value)); } } } -void BatchPutCommand::Help(string& ret) { - ret.append(" "); - ret.append(BatchPutCommand::Name()); - ret.append(" [ ] [..]"); - ret.append(" [--" + ARG_TTL + "]"); - ret.append("\n"); +void BatchPutCommand::Help(std::string* ret) { + ret->append(" "); + ret->append(BatchPutCommand::Name()); + ret->append(" [ ] [..]"); + ret->append(" [--" + ARG_TTL + "]"); + ret->append("\n"); } void BatchPutCommand::DoCommand() { WriteBatch batch; - for (vector>::const_iterator itr + for (std::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()) { @@ -1575,8 +1611,10 @@ Options BatchPutCommand::PrepareOptionsForOpenDB() { } -ScanCommand::ScanCommand(const vector& params, - const map& options, const vector& flags) : +ScanCommand::ScanCommand( + const std::vector& params, + const std::map& options, + const std::vector& flags) : LDBCommand(options, flags, true, BuildCmdLineOptions({ARG_TTL, ARG_HEX, ARG_KEY_HEX, ARG_TO, ARG_VALUE_HEX, ARG_FROM, ARG_TIMESTAMP, @@ -1585,7 +1623,7 @@ ScanCommand::ScanCommand(const vector& params, end_key_specified_(false), max_keys_scanned_(-1) { - map::const_iterator itr = options.find(ARG_FROM); + auto itr = options.find(ARG_FROM); if (itr != options.end()) { start_key_ = itr->second; if (is_key_hex_) { @@ -1606,26 +1644,26 @@ ScanCommand::ScanCommand(const vector& params, if (itr != options.end()) { try { max_keys_scanned_ = stoi(itr->second); - } catch(const invalid_argument&) { + } catch(const std::invalid_argument&) { exec_state_ = LDBCommandExecuteResult::FAILED(ARG_MAX_KEYS + " has an invalid value"); - } catch(const out_of_range&) { + } catch(const std::out_of_range&) { exec_state_ = LDBCommandExecuteResult::FAILED(ARG_MAX_KEYS + " has a value out-of-range"); } } } -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::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::DoCommand() { @@ -1638,11 +1676,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) { @@ -1655,9 +1693,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()) { - string key = ldb_options_.key_formatter->Format(it->key()); + it->Valid() && (!end_key_specified_ || it->key().ToString() < end_key_); + it->Next()) { + std::string key = ldb_options_.key_formatter->Format(it->key()); if (is_db_ttl_) { TtlIterator* it_ttl = dynamic_cast(it); assert(it_ttl); @@ -1669,11 +1707,10 @@ void ScanCommand::DoCommand() { fprintf(stdout, "%s ", ReadableTime(rawtime).c_str()); } } - string value = it->value().ToString(); + std::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; @@ -1686,8 +1723,9 @@ void ScanCommand::DoCommand() { } -DeleteCommand::DeleteCommand(const vector& params, - const map& options, const vector& flags) : +DeleteCommand::DeleteCommand(const std::vector& params, + const std::map& options, + const std::vector& flags) : LDBCommand(options, flags, false, BuildCmdLineOptions({ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX})) { @@ -1702,10 +1740,10 @@ DeleteCommand::DeleteCommand(const vector& params, } } -void DeleteCommand::Help(string& ret) { - ret.append(" "); - ret.append(DeleteCommand::Name() + " "); - ret.append("\n"); +void DeleteCommand::Help(std::string* ret) { + ret->append(" "); + ret->append(DeleteCommand::Name() + " "); + ret->append("\n"); } void DeleteCommand::DoCommand() { @@ -1718,8 +1756,10 @@ void DeleteCommand::DoCommand() { } -PutCommand::PutCommand(const vector& params, - const map& options, const vector& flags) : +PutCommand::PutCommand( + 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})) { @@ -1741,12 +1781,12 @@ PutCommand::PutCommand(const vector& params, } } -void PutCommand::Help(string& ret) { - ret.append(" "); - ret.append(PutCommand::Name()); - ret.append(" "); - ret.append(" [--" + ARG_TTL + "]"); - ret.append("\n"); +void PutCommand::Help(std::string* ret) { + ret->append(" "); + ret->append(PutCommand::Name()); + ret->append(" "); + ret->append(" [--" + ARG_TTL + "]"); + ret->append("\n"); } void PutCommand::DoCommand() { @@ -1770,43 +1810,43 @@ const char* DBQuerierCommand::GET_CMD = "get"; const char* DBQuerierCommand::PUT_CMD = "put"; const char* DBQuerierCommand::DELETE_CMD = "delete"; -DBQuerierCommand::DBQuerierCommand(const vector& params, - const map& options, const vector& flags) : +DBQuerierCommand::DBQuerierCommand( + 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})) { } -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 " +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 " "commands."); - ret.append("\n"); + ret->append("\n"); } void DBQuerierCommand::DoCommand() { if (!db_) { return; } - ReadOptions read_options; WriteOptions write_options; - string line; - string key; - string value; - while (getline(cin, line, '\n')) { - - // Parse line into vector - vector tokens; + std::string line; + std::string key; + std::string value; + while (getline(std::cin, line, '\n')) { + // Parse line into std::vector + std::vector tokens; size_t pos = 0; while (true) { size_t pos2 = line.find(' ', pos); - if (pos2 == string::npos) { + if (pos2 == std::string::npos) { break; } tokens.push_back(line.substr(pos, pos2-pos)); @@ -1814,7 +1854,7 @@ void DBQuerierCommand::DoCommand() { } tokens.push_back(line.substr(pos)); - const string& cmd = tokens[0]; + const std::string& cmd = tokens[0]; if (cmd == HELP_CMD) { fprintf(stdout, @@ -1845,16 +1885,18 @@ void DBQuerierCommand::DoCommand() { } } -CheckConsistencyCommand::CheckConsistencyCommand(const vector& params, - const map& options, const vector& flags) : +CheckConsistencyCommand::CheckConsistencyCommand( + const std::vector& params, + const std::map& options, + const std::vector& flags) : LDBCommand(options, flags, false, BuildCmdLineOptions({})) { } -void CheckConsistencyCommand::Help(string& ret) { - ret.append(" "); - ret.append(CheckConsistencyCommand::Name()); - ret.append("\n"); +void CheckConsistencyCommand::Help(std::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 9ffe0eabc..b42d779c3 100644 --- a/util/ldb_cmd.h +++ b/util/ldb_cmd.h @@ -4,12 +4,15 @@ // 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" @@ -23,39 +26,34 @@ #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 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 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 LDBCommand* InitFromCmdLineArgs( - const vector& args, + const std::vector& args, const Options& options, const LDBOptions& ldb_options ); @@ -123,8 +121,8 @@ public: exec_state_.Reset(); } - static string HexToString(const string& str) { - string parsed; + static std::string HexToString(const std::string& str) { + std::string parsed; if (str[0] != '0' || str[1] != 'x') { fprintf(stderr, "Invalid hex input %s. Must start with 0x\n", str.c_str()); @@ -140,8 +138,8 @@ public: return parsed; } - static string StringToHex(const string& str) { - string result = "0x"; + static std::string StringToHex(const std::string& str) { + std::string result = "0x"; char buf[10]; for (size_t i = 0; i < str.length(); i++) { snprintf(buf, 10, "%02X", (unsigned char)str[i]); @@ -155,7 +153,7 @@ public: protected: LDBCommandExecuteResult exec_state_; - string db_path_; + std::string db_path_; DB* db_; DBWithTTL* db_ttl_; @@ -180,21 +178,24 @@ protected: /** * Map of options passed on the command-line. */ - const map option_map_; + const std::map option_map_; /** * Flags passed on the command-line. */ - const vector flags_; + const std::vector flags_; /** List of command-line options valid for this command */ - const vector valid_cmd_line_options_; + const std::vector valid_cmd_line_options_; - bool ParseKeyValue(const string& line, string* key, string* value, - bool is_key_hex, bool is_value_hex); + bool ParseKeyValue(const std::string& line, + std::string* key, std::string* value, + bool is_key_hex, bool is_value_hex); - LDBCommand(const map& options, const vector& flags, - bool is_read_only, const vector& valid_cmd_line_options) : + LDBCommand(const std::map& options, + const std::vector& flags, + bool is_read_only, + const std::vector& valid_cmd_line_options) : db_(nullptr), is_read_only_(is_read_only), is_key_hex_(false), @@ -205,7 +206,7 @@ protected: flags_(flags), valid_cmd_line_options_(valid_cmd_line_options) { - map::const_iterator itr = options.find(ARG_DB); + auto itr = options.find(ARG_DB); if (itr != options.end()) { db_path_ = itr->second; } @@ -236,7 +237,7 @@ protected: st = DB::Open(opt, db_path_, &db_); } if (!st.ok()) { - string msg = st.ToString(); + std::string msg = st.ToString(); exec_state_ = LDBCommandExecuteResult::FAILED(msg); } @@ -250,29 +251,33 @@ protected: } } - static string PrintKeyValue(const string& key, const string& value, - bool is_key_hex, bool is_value_hex) { - string result; + static std::string PrintKeyValue( + const std::string& key, const std::string& value, + bool is_key_hex, bool is_value_hex) { + std::string result; result.append(is_key_hex ? StringToHex(key) : key); result.append(DELIM); result.append(is_value_hex ? StringToHex(value) : value); return result; } - static string PrintKeyValue(const string& key, const string& value, - bool is_hex) { + static std::string PrintKeyValue( + const std::string& key, const std::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 vector& flags, const string& flag) { + static bool IsFlagPresent( + const std::vector& flags, const std::string& flag) { return (std::find(flags.begin(), flags.end(), flag) != flags.end()); } - static string HelpRangeCmdArgs() { - ostringstream str_stream; + static std::string HelpRangeCmdArgs() { + std::ostringstream str_stream; str_stream << " "; str_stream << "[--" << ARG_FROM << "] "; str_stream << "[--" << ARG_TO << "] "; @@ -284,32 +289,35 @@ protected: * used by this command. It includes the common options and the ones * passed in. */ - 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}; + 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}; ret.insert(ret.end(), options.begin(), options.end()); return ret; } - bool ParseIntOption(const map& options, const string& option, - int& value, LDBCommandExecuteResult& exec_state); + bool ParseIntOption(const std::map& options, + const std::string& option, + int* value, LDBCommandExecuteResult* exec_state); - bool ParseStringOption(const map& options, - const string& option, string* value); + bool ParseStringOption(const std::map& options, + const std::string& option, std::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 map& options, - const vector& flags) { + bool IsKeyHex(const std::map& options, + const std::vector& flags) { return (IsFlagPresent(flags, ARG_HEX) || IsFlagPresent(flags, ARG_KEY_HEX) || ParseBooleanOption(options, ARG_HEX, false) || @@ -320,8 +328,8 @@ private: * Interpret command line options and flags to determine if the value * should be input/output in hex. */ - bool IsValueHex(const map& options, - const vector& flags) { + bool IsValueHex(const std::map& options, + const std::vector& flags) { return (IsFlagPresent(flags, ARG_HEX) || IsFlagPresent(flags, ARG_VALUE_HEX) || ParseBooleanOption(options, ARG_HEX, false) || @@ -334,12 +342,13 @@ private: * Throws an exception if the value of the option is not * "true" or "false" (case insensitive). */ - bool ParseBooleanOption(const map& options, - const string& option, bool default_val) { + bool ParseBooleanOption( + const std::map& options, + const std::string& option, bool default_val) { - map::const_iterator itr = options.find(option); + auto itr = options.find(option); if (itr != options.end()) { - string option_val = itr->second; + std::string option_val = itr->second; return StringToBool(itr->second); } return default_val; @@ -350,7 +359,7 @@ private: * val must be either true or false (case insensitive). * Otherwise an exception is thrown. */ - bool StringToBool(string val) { + bool StringToBool(std::string val) { std::transform(val.begin(), val.end(), val.begin(), ::tolower); if (val == "true") { return true; @@ -362,161 +371,165 @@ private: } static LDBCommand* SelectCommand( - const string& cmd, - const vector& cmdParams, - const map& option_map, - const vector& flags + const std::string& cmd, + const std::vector& cmdParams, + const std::map& option_map, + const std::vector& flags ); }; class CompactorCommand: public LDBCommand { -public: - static string Name() { return "compact"; } + public: + static std::string Name() { return "compact"; } - CompactorCommand(const vector& params, - const map& options, const vector& flags); + CompactorCommand(const std::vector& params, + const std::map& options, + const std::vector& flags); - static void Help(string& ret); + static void Help(std::string* ret); virtual void DoCommand(); -private: + private: bool null_from_; - string from_; + std::string from_; bool null_to_; - string to_; + std::string to_; }; class DBDumperCommand: public LDBCommand { -public: - static string Name() { return "dump"; } + public: + static std::string Name() { return "dump"; } - DBDumperCommand(const vector& params, - const map& options, const vector& flags); + DBDumperCommand(const std::vector& params, + const std::map& options, + const std::vector& flags); - static void Help(string& ret); + static void Help(std::string* ret); virtual void DoCommand(); -private: + private: bool null_from_; - string from_; + std::string from_; bool null_to_; - string to_; - int max_keys_; - string delim_; + std::string to_; + uint64_t max_keys_; + std::string delim_; bool count_only_; bool count_delim_; bool print_stats_; - static const string ARG_COUNT_ONLY; - static const string ARG_COUNT_DELIM; - static const string ARG_STATS; - static const string ARG_TTL_BUCKET; + 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; }; class InternalDumpCommand: public LDBCommand { -public: - static string Name() { return "idump"; } + public: + static std::string Name() { return "idump"; } - InternalDumpCommand(const vector& params, - const map& options, - const vector& flags); + InternalDumpCommand(const std::vector& params, + const std::map& options, + const std::vector& flags); - static void Help(string& ret); + static void Help(std::string* ret); virtual void DoCommand(); -private: + private: bool has_from_; - string from_; + std::string from_; bool has_to_; - string to_; + std::string to_; int max_keys_; - string delim_; + std::string delim_; bool count_only_; bool count_delim_; bool print_stats_; bool is_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; + 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; }; class DBLoaderCommand: public LDBCommand { -public: - static string Name() { return "load"; } - - DBLoaderCommand(string& db_name, vector& args); + public: + static std::string Name() { return "load"; } - DBLoaderCommand(const vector& params, - const map& options, const vector& flags); + DBLoaderCommand(const std::vector& params, + const std::map& options, + const std::vector& flags); - static void Help(string& ret); + static void Help(std::string* ret); virtual void DoCommand(); virtual Options PrepareOptionsForOpenDB(); -private: + private: bool create_if_missing_; bool disable_wal_; bool bulk_load_; bool compact_; - static const string ARG_DISABLE_WAL; - static const string ARG_BULK_LOAD; - static const string ARG_COMPACT; + static const std::string ARG_DISABLE_WAL; + static const std::string ARG_BULK_LOAD; + static const std::string ARG_COMPACT; }; class ManifestDumpCommand: public LDBCommand { -public: - static string Name() { return "manifest_dump"; } + public: + static std::string Name() { return "manifest_dump"; } - ManifestDumpCommand(const vector& params, - const map& options, const vector& flags); + ManifestDumpCommand(const std::vector& params, + const std::map& options, + const std::vector& flags); - static void Help(string& ret); + static void Help(std::string* ret); virtual void DoCommand(); virtual bool NoDBOpen() { return true; } -private: + private: bool verbose_; - string path_; + std::string path_; - static const string ARG_VERBOSE; - static const string ARG_PATH; + static const std::string ARG_VERBOSE; + static const std::string ARG_PATH; }; class ListColumnFamiliesCommand : public LDBCommand { public: - static string Name() { return "list_column_families"; } + static std::string Name() { return "list_column_families"; } - ListColumnFamiliesCommand(const vector& params, - const map& options, - const vector& flags); + ListColumnFamiliesCommand( + const std::vector& params, + const std::map& options, + const std::vector& flags); - static void Help(string& ret); + static void Help(std::string* ret); virtual void DoCommand(); virtual bool NoDBOpen() { return true; } private: - string dbname_; + std::string dbname_; }; class ReduceDBLevelsCommand : public LDBCommand { -public: - static string Name() { return "reduce_levels"; } + public: + static std::string Name() { return "reduce_levels"; } - ReduceDBLevelsCommand(const vector& params, - const map& options, const vector& flags); + ReduceDBLevelsCommand(const std::vector& params, + const std::map& options, + const std::vector& flags); virtual Options PrepareOptionsForOpenDB(); @@ -526,169 +539,179 @@ public: return true; } - static void Help(string& msg); + static void Help(std::string* msg); - static vector PrepareArgs(const string& db_path, int new_levels, + static std::vector PrepareArgs( + const std::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 string ARG_NEW_LEVELS; - static const string ARG_PRINT_OLD_LEVELS; + static const std::string ARG_NEW_LEVELS; + static const std::string ARG_PRINT_OLD_LEVELS; Status GetOldNumOfLevels(Options& opt, int* levels); }; class ChangeCompactionStyleCommand : public LDBCommand { -public: - static string Name() { return "change_compaction_style"; } + public: + static std::string Name() { return "change_compaction_style"; } - ChangeCompactionStyleCommand(const vector& params, - const map& options, const vector& flags); + ChangeCompactionStyleCommand(const std::vector& params, + const std::map& options, + const std::vector& flags); virtual Options PrepareOptionsForOpenDB(); virtual void DoCommand(); - static void Help(string& msg); + static void Help(std::string* msg); -private: + private: int old_compaction_style_; int new_compaction_style_; - static const string ARG_OLD_COMPACTION_STYLE; - static const string ARG_NEW_COMPACTION_STYLE; + static const std::string ARG_OLD_COMPACTION_STYLE; + static const std::string ARG_NEW_COMPACTION_STYLE; }; class WALDumperCommand : public LDBCommand { -public: - static string Name() { return "dump_wal"; } + public: + static std::string Name() { return "dump_wal"; } - WALDumperCommand(const vector& params, - const map& options, const vector& flags); + WALDumperCommand(const std::vector& params, + const std::map& options, + const std::vector& flags); virtual bool NoDBOpen() { return true; } - static void Help(string& ret); + static void Help(std::string* ret); virtual void DoCommand(); -private: + private: bool print_header_; - string wal_file_; + std::string wal_file_; bool print_values_; - static const string ARG_WAL_FILE; - static const string ARG_PRINT_HEADER; - static const string ARG_PRINT_VALUE; + static const std::string ARG_WAL_FILE; + static const std::string ARG_PRINT_HEADER; + static const std::string ARG_PRINT_VALUE; }; class GetCommand : public LDBCommand { -public: - static string Name() { return "get"; } + public: + static std::string Name() { return "get"; } - GetCommand(const vector& params, const map& options, - const vector& flags); + GetCommand(const std::vector& params, + const std::map& options, + const std::vector& flags); virtual void DoCommand(); - static void Help(string& ret); + static void Help(std::string* ret); -private: - string key_; + private: + std::string key_; }; class ApproxSizeCommand : public LDBCommand { -public: - static string Name() { return "approxsize"; } + public: + static std::string Name() { return "approxsize"; } - ApproxSizeCommand(const vector& params, - const map& options, const vector& flags); + ApproxSizeCommand(const std::vector& params, + const std::map& options, + const std::vector& flags); virtual void DoCommand(); - static void Help(string& ret); + static void Help(std::string* ret); -private: - string start_key_; - string end_key_; + private: + std::string start_key_; + std::string end_key_; }; class BatchPutCommand : public LDBCommand { -public: - static string Name() { return "batchput"; } + public: + static std::string Name() { return "batchput"; } - BatchPutCommand(const vector& params, - const map& options, const vector& flags); + BatchPutCommand(const std::vector& params, + const std::map& options, + const std::vector& flags); virtual void DoCommand(); - static void Help(string& ret); + static void Help(std::string* ret); virtual Options PrepareOptionsForOpenDB(); -private: + private: /** * The key-values to be inserted. */ - vector> key_values_; + std::vector> key_values_; }; class ScanCommand : public LDBCommand { -public: - static string Name() { return "scan"; } + public: + static std::string Name() { return "scan"; } - ScanCommand(const vector& params, const map& options, - const vector& flags); + ScanCommand(const std::vector& params, + const std::map& options, + const std::vector& flags); virtual void DoCommand(); - static void Help(string& ret); + static void Help(std::string* ret); -private: - string start_key_; - string end_key_; + private: + std::string start_key_; + std::string end_key_; bool start_key_specified_; bool end_key_specified_; int max_keys_scanned_; }; class DeleteCommand : public LDBCommand { -public: - static string Name() { return "delete"; } + public: + static std::string Name() { return "delete"; } - DeleteCommand(const vector& params, - const map& options, const vector& flags); + DeleteCommand(const std::vector& params, + const std::map& options, + const std::vector& flags); virtual void DoCommand(); - static void Help(string& ret); + static void Help(std::string* ret); -private: - string key_; + private: + std::string key_; }; class PutCommand : public LDBCommand { -public: - static string Name() { return "put"; } + public: + static std::string Name() { return "put"; } - PutCommand(const vector& params, const map& options, - const vector& flags); + PutCommand(const std::vector& params, + const std::map& options, + const std::vector& flags); virtual void DoCommand(); - static void Help(string& ret); + static void Help(std::string* ret); virtual Options PrepareOptionsForOpenDB(); -private: - string key_; - string value_; + private: + std::string key_; + std::string value_; }; /** @@ -696,17 +719,18 @@ private: * get/put/delete. */ class DBQuerierCommand: public LDBCommand { -public: - static string Name() { return "query"; } + public: + static std::string Name() { return "query"; } - DBQuerierCommand(const vector& params, - const map& options, const vector& flags); + DBQuerierCommand(const std::vector& params, + const std::map& options, + const std::vector& flags); - static void Help(string& ret); + static void Help(std::string* ret); virtual void DoCommand(); -private: + private: static const char* HELP_CMD; static const char* GET_CMD; static const char* PUT_CMD; @@ -714,11 +738,12 @@ private: }; class CheckConsistencyCommand : public LDBCommand { -public: - static string Name() { return "checkconsistency"; } + public: + static std::string Name() { return "checkconsistency"; } - CheckConsistencyCommand(const vector& params, - const map& options, const vector& flags); + CheckConsistencyCommand(const std::vector& params, + const std::map& options, + const std::vector& flags); virtual void DoCommand(); @@ -726,7 +751,7 @@ public: return true; } - static void Help(string& ret); + static void Help(std::string* ret); }; } // namespace rocksdb diff --git a/util/ldb_cmd_execute_result.h b/util/ldb_cmd_execute_result.h index b8e6c4634..48a4b495c 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, std::string& msg) : + LDBCommandExecuteResult(State state, const std::string& msg) : state_(state), message_(msg) {} std::string ToString() { @@ -52,11 +52,11 @@ public: return state_ == EXEC_FAILED; } - static LDBCommandExecuteResult SUCCEED(std::string msg) { + static LDBCommandExecuteResult SUCCEED(const std::string& msg) { return LDBCommandExecuteResult(EXEC_SUCCEED, msg); } - static LDBCommandExecuteResult FAILED(std::string msg) { + static LDBCommandExecuteResult FAILED(const std::string& msg) { return LDBCommandExecuteResult(EXEC_FAILED, msg); } diff --git a/util/ldb_tool.cc b/util/ldb_tool.cc index bb6c8ffca..9824c0210 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) { - string ret; + std::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()); }