From a6f4275403e8b0be3404eff5ca0ee6917f368a1c Mon Sep 17 00:00:00 2001 From: Mayank Agarwal Date: Tue, 19 Mar 2013 17:28:30 -0700 Subject: [PATCH] Removing boost from ldb_cmd.cc Summary: Getting rid of boost in our github codebase which caused problems on third-party Test Plan: make ldb; python tools/ldb_test.py Reviewers: sheki, dhruba Reviewed By: sheki Differential Revision: https://reviews.facebook.net/D9543 --- util/ldb_cmd.cc | 84 ++++++++++++++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 32 deletions(-) diff --git a/util/ldb_cmd.cc b/util/ldb_cmd.cc index 0c15cf64b..df843cfed 100644 --- a/util/ldb_cmd.cc +++ b/util/ldb_cmd.cc @@ -3,9 +3,9 @@ // found in the LICENSE file. #include "util/ldb_cmd.h" - -#include -#include +#include +#include +#include #include "leveldb/write_batch.h" #include "db/dbformat.h" @@ -14,6 +14,18 @@ namespace leveldb { +using namespace std; + +vector stringSplit(string arg, char delim) { + vector splits; + stringstream ss(arg); + string item; + while(getline(ss, item, delim)) { + splits.push_back(item); + } + return splits; +} + const string LDBCommand::ARG_DB = "db"; const string LDBCommand::ARG_HEX = "hex"; const string LDBCommand::ARG_KEY_HEX = "key_hex"; @@ -65,9 +77,8 @@ LDBCommand* LDBCommand::InitFromCmdLineArgs(const vector& args) { for (vector::const_iterator itr = args.begin(); itr != args.end(); itr++) { string arg = *itr; - if (boost::starts_with(arg, OPTION_PREFIX)){ - vector splits; - boost::split(splits, arg, boost::is_any_of("=")); + if (arg[0] == '-' && arg[1] == '-'){ + vector splits = stringSplit(arg, '='); if (splits.size() == 2) { string optionKey = splits[0].substr(OPTION_PREFIX.size()); options[optionKey] = splits[1]; @@ -130,11 +141,14 @@ bool LDBCommand::ParseIntOption(const map& options, map::const_iterator itr = options_.find(option); if (itr != options_.end()) { try { - value = boost::lexical_cast(itr->second); + value = stoi(itr->second); return true; - } catch( const boost::bad_lexical_cast & ) { + } 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; @@ -217,7 +231,7 @@ leveldb::Options LDBCommand::PrepareOptionsForOpenDB() { 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) { @@ -243,7 +257,7 @@ bool LDBCommand::ValidateCmdLineOptions() { for (map::const_iterator itr = options_.begin(); itr != options_.end(); itr++) { - if (std::find(valid_cmd_line_options_.begin(), + if (find(valid_cmd_line_options_.begin(), valid_cmd_line_options_.end(), itr->first) == valid_cmd_line_options_.end()) { fprintf(stderr, "Invalid command-line option %s\n", itr->first.c_str()); @@ -253,7 +267,7 @@ bool LDBCommand::ValidateCmdLineOptions() { for (vector::const_iterator itr = flags_.begin(); itr != flags_.end(); itr++) { - if (std::find(valid_cmd_line_options_.begin(), + if (find(valid_cmd_line_options_.begin(), valid_cmd_line_options_.end(), *itr) == valid_cmd_line_options_.end()) { fprintf(stderr, "Invalid command-line flag %s\n", itr->c_str()); @@ -374,7 +388,7 @@ void DBLoaderCommand::DoCommand() { int bad_lines = 0; string line; - while (std::getline(std::cin, line, '\n')) { + while (getline(cin, line, '\n')) { string key; string value; if (ParseKeyValue(line, &key, &value, is_key_hex_, is_value_hex_)) { @@ -389,7 +403,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); @@ -426,10 +440,13 @@ DBDumperCommand::DBDumperCommand(const vector& params, itr = options.find(ARG_MAX_KEYS); if (itr != options.end()) { try { - max_keys_ = boost::lexical_cast(itr->second); - } catch( const boost::bad_lexical_cast & ) { + max_keys_ = stoi(itr->second); + } catch(const invalid_argument&) { exec_state_ = LDBCommandExecuteResult::FAILED(ARG_MAX_KEYS + " has an invalid value"); + } catch(const out_of_range&) { + exec_state_ = LDBCommandExecuteResult::FAILED(ARG_MAX_KEYS + + " has a valuei out-of-range"); } } @@ -533,7 +550,7 @@ vector ReduceDBLevelsCommand::PrepareArgs(const string& db_path, 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)); + ret.push_back("--" + ARG_NEW_LEVELS + "=" + to_string(new_levels)); if(print_old_level) { ret.push_back("--" + ARG_PRINT_OLD_LEVELS); } @@ -658,8 +675,8 @@ class InMemoryHandler : public WriteBatch::Handler { } private: - std::map putMap_; - std::vector deleteList_; + map putMap_; + vector deleteList_; }; const string WALDumperCommand::ARG_WAL_FILE = "walfile"; @@ -701,7 +718,7 @@ void WALDumperCommand::Help(string& ret) { 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 "<& params, 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(std::pair( + key_values_.push_back(pair( is_key_hex_ ? HexToString(key) : key, is_value_hex_ ? HexToString(value) : value)); } @@ -884,7 +901,7 @@ void BatchPutCommand::Help(string& ret) { void BatchPutCommand::DoCommand() { leveldb::WriteBatch batch; - for (vector>::const_iterator itr + for (vector>::const_iterator itr = key_values_.begin(); itr != key_values_.end(); itr++) { batch.Put(itr->first, itr->second); } @@ -932,10 +949,13 @@ ScanCommand::ScanCommand(const vector& params, itr = options.find(ARG_MAX_KEYS); if (itr != options.end()) { try { - max_keys_scanned_ = boost::lexical_cast< int >(itr->second); - } catch( const boost::bad_lexical_cast & ) { + max_keys_scanned_ = stoi(itr->second); + } catch(const invalid_argument&) { exec_state_ = LDBCommandExecuteResult::FAILED(ARG_MAX_KEYS + " has an invalid value"); + } catch(const out_of_range&) { + exec_state_ = LDBCommandExecuteResult::FAILED(ARG_MAX_KEYS + + " has a value out-of-range"); } } } @@ -1088,7 +1108,7 @@ void DBQuerierCommand::DoCommand() { string line; string key; string value; - while (getline(std::cin, line, '\n')) { + while (getline(cin, line, '\n')) { // Parse line into vector vector tokens;