fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2082 lines
65 KiB
2082 lines
65 KiB
11 years ago
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
||
|
// This source code is licensed under the BSD-style license found in the
|
||
|
// LICENSE file in the root directory of this source tree. An additional grant
|
||
|
// of patent rights can be found in the PATENTS file in the same directory.
|
||
|
//
|
||
11 years ago
|
#ifndef ROCKSDB_LITE
|
||
9 years ago
|
#include "tools/ldb_cmd.h"
|
||
12 years ago
|
|
||
9 years ago
|
#ifndef __STDC_FORMAT_MACROS
|
||
|
#define __STDC_FORMAT_MACROS
|
||
|
#endif
|
||
|
|
||
|
#include <inttypes.h>
|
||
|
|
||
12 years ago
|
#include "db/dbformat.h"
|
||
12 years ago
|
#include "db/db_impl.h"
|
||
12 years ago
|
#include "db/log_reader.h"
|
||
12 years ago
|
#include "db/filename.h"
|
||
10 years ago
|
#include "db/writebuffer.h"
|
||
12 years ago
|
#include "db/write_batch_internal.h"
|
||
11 years ago
|
#include "rocksdb/write_batch.h"
|
||
11 years ago
|
#include "rocksdb/cache.h"
|
||
10 years ago
|
#include "rocksdb/table_properties.h"
|
||
9 years ago
|
#include "table/scoped_arena_iterator.h"
|
||
10 years ago
|
#include "port/dirent.h"
|
||
9 years ago
|
#include "tools/sst_dump_tool_imp.h"
|
||
12 years ago
|
#include "util/coding.h"
|
||
10 years ago
|
#include "util/string_util.h"
|
||
11 years ago
|
#include "utilities/ttl/db_ttl_impl.h"
|
||
12 years ago
|
|
||
10 years ago
|
#include <cstdlib>
|
||
10 years ago
|
#include <ctime>
|
||
|
#include <limits>
|
||
|
#include <sstream>
|
||
|
#include <string>
|
||
|
#include <stdexcept>
|
||
|
|
||
11 years ago
|
namespace rocksdb {
|
||
12 years ago
|
|
||
10 years ago
|
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";
|
||
10 years ago
|
const string LDBCommand::ARG_DB_WRITE_BUFFER_SIZE = "db_write_buffer_size";
|
||
10 years ago
|
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";
|
||
12 years ago
|
|
||
12 years ago
|
const char* LDBCommand::DELIM = " ==> ";
|
||
12 years ago
|
|
||
12 years ago
|
LDBCommand* LDBCommand::InitFromCmdLineArgs(
|
||
10 years ago
|
int argc,
|
||
|
char** argv,
|
||
|
const Options& options,
|
||
|
const LDBOptions& ldb_options
|
||
|
) {
|
||
|
vector<string> args;
|
||
12 years ago
|
for (int i = 1; i < argc; i++) {
|
||
|
args.push_back(argv[i]);
|
||
|
}
|
||
11 years ago
|
return InitFromCmdLineArgs(args, options, ldb_options);
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Parse the command-line arguments and create the appropriate LDBCommand2
|
||
|
* instance.
|
||
|
* The command line arguments must be in the following format:
|
||
10 years ago
|
* ./ldb --db=PATH_TO_DB [--commonOpt1=commonOpt1Val] ..
|
||
|
* COMMAND <PARAM1> <PARAM2> ... [-cmdSpecificOpt1=cmdSpecificOpt1Val] ..
|
||
12 years ago
|
* This is similar to the command line format used by HBaseClientTool.
|
||
|
* Command name is not included in args.
|
||
12 years ago
|
* Returns nullptr if the command-line cannot be parsed.
|
||
12 years ago
|
*/
|
||
12 years ago
|
LDBCommand* LDBCommand::InitFromCmdLineArgs(
|
||
10 years ago
|
const vector<string>& args,
|
||
|
const Options& options,
|
||
|
const LDBOptions& ldb_options
|
||
|
) {
|
||
|
// --x=y command line arguments are added as x->y map entries.
|
||
|
map<string, string> option_map;
|
||
12 years ago
|
|
||
|
// Command-line arguments of the form --hex end up in this array as hex
|
||
10 years ago
|
vector<string> flags;
|
||
12 years ago
|
|
||
12 years ago
|
// Everything other than option_map and flags. Represents commands
|
||
10 years ago
|
// and their parameters. For eg: put key1 value1 go into this vector.
|
||
|
vector<string> cmdTokens;
|
||
12 years ago
|
|
||
10 years ago
|
const string OPTION_PREFIX = "--";
|
||
12 years ago
|
|
||
12 years ago
|
for (const auto& arg : args) {
|
||
10 years ago
|
if (arg[0] == '-' && arg[1] == '-'){
|
||
10 years ago
|
vector<string> splits = StringSplit(arg, '=');
|
||
12 years ago
|
if (splits.size() == 2) {
|
||
10 years ago
|
string optionKey = splits[0].substr(OPTION_PREFIX.size());
|
||
12 years ago
|
option_map[optionKey] = splits[1];
|
||
12 years ago
|
} else {
|
||
10 years ago
|
string optionKey = splits[0].substr(OPTION_PREFIX.size());
|
||
12 years ago
|
flags.push_back(optionKey);
|
||
|
}
|
||
12 years ago
|
} else {
|
||
12 years ago
|
cmdTokens.push_back(arg);
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
|
if (cmdTokens.size() < 1) {
|
||
|
fprintf(stderr, "Command not specified!");
|
||
12 years ago
|
return nullptr;
|
||
12 years ago
|
}
|
||
|
|
||
10 years ago
|
string cmd = cmdTokens[0];
|
||
|
vector<string> cmdParams(cmdTokens.begin()+1, cmdTokens.end());
|
||
12 years ago
|
LDBCommand* command = LDBCommand::SelectCommand(
|
||
10 years ago
|
cmd,
|
||
|
cmdParams,
|
||
|
option_map,
|
||
|
flags
|
||
|
);
|
||
12 years ago
|
|
||
|
if (command) {
|
||
11 years ago
|
command->SetDBOptions(options);
|
||
|
command->SetLDBOptions(ldb_options);
|
||
12 years ago
|
}
|
||
|
return command;
|
||
|
}
|
||
|
|
||
|
LDBCommand* LDBCommand::SelectCommand(
|
||
|
const std::string& cmd,
|
||
10 years ago
|
const vector<string>& cmdParams,
|
||
|
const map<string, string>& option_map,
|
||
|
const vector<string>& flags
|
||
|
) {
|
||
|
|
||
12 years ago
|
if (cmd == GetCommand::Name()) {
|
||
12 years ago
|
return new GetCommand(cmdParams, option_map, flags);
|
||
12 years ago
|
} else if (cmd == PutCommand::Name()) {
|
||
12 years ago
|
return new PutCommand(cmdParams, option_map, flags);
|
||
12 years ago
|
} else if (cmd == BatchPutCommand::Name()) {
|
||
12 years ago
|
return new BatchPutCommand(cmdParams, option_map, flags);
|
||
12 years ago
|
} else if (cmd == ScanCommand::Name()) {
|
||
12 years ago
|
return new ScanCommand(cmdParams, option_map, flags);
|
||
12 years ago
|
} else if (cmd == DeleteCommand::Name()) {
|
||
12 years ago
|
return new DeleteCommand(cmdParams, option_map, flags);
|
||
12 years ago
|
} else if (cmd == ApproxSizeCommand::Name()) {
|
||
12 years ago
|
return new ApproxSizeCommand(cmdParams, option_map, flags);
|
||
12 years ago
|
} else if (cmd == DBQuerierCommand::Name()) {
|
||
12 years ago
|
return new DBQuerierCommand(cmdParams, option_map, flags);
|
||
12 years ago
|
} else if (cmd == CompactorCommand::Name()) {
|
||
12 years ago
|
return new CompactorCommand(cmdParams, option_map, flags);
|
||
12 years ago
|
} else if (cmd == WALDumperCommand::Name()) {
|
||
12 years ago
|
return new WALDumperCommand(cmdParams, option_map, flags);
|
||
12 years ago
|
} else if (cmd == ReduceDBLevelsCommand::Name()) {
|
||
12 years ago
|
return new ReduceDBLevelsCommand(cmdParams, option_map, flags);
|
||
11 years ago
|
} else if (cmd == ChangeCompactionStyleCommand::Name()) {
|
||
|
return new ChangeCompactionStyleCommand(cmdParams, option_map, flags);
|
||
12 years ago
|
} else if (cmd == DBDumperCommand::Name()) {
|
||
12 years ago
|
return new DBDumperCommand(cmdParams, option_map, flags);
|
||
12 years ago
|
} else if (cmd == DBLoaderCommand::Name()) {
|
||
12 years ago
|
return new DBLoaderCommand(cmdParams, option_map, flags);
|
||
12 years ago
|
} else if (cmd == ManifestDumpCommand::Name()) {
|
||
12 years ago
|
return new ManifestDumpCommand(cmdParams, option_map, flags);
|
||
11 years ago
|
} else if (cmd == ListColumnFamiliesCommand::Name()) {
|
||
|
return new ListColumnFamiliesCommand(cmdParams, option_map, flags);
|
||
10 years ago
|
} else if (cmd == DBFileDumperCommand::Name()) {
|
||
|
return new DBFileDumperCommand(cmdParams, option_map, flags);
|
||
12 years ago
|
} else if (cmd == InternalDumpCommand::Name()) {
|
||
|
return new InternalDumpCommand(cmdParams, option_map, flags);
|
||
11 years ago
|
} else if (cmd == CheckConsistencyCommand::Name()) {
|
||
|
return new CheckConsistencyCommand(cmdParams, option_map, flags);
|
||
12 years ago
|
}
|
||
12 years ago
|
return nullptr;
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
|
||
12 years ago
|
/**
|
||
|
* Parses the specific integer option and fills in the value.
|
||
|
* Returns true if the option is found.
|
||
|
* Returns false if the option is not found or if there is an error parsing the
|
||
|
* value. If there is an error, the specified exec_state is also
|
||
|
* updated.
|
||
|
*/
|
||
10 years ago
|
bool LDBCommand::ParseIntOption(const map<string, string>& options,
|
||
|
const string& option, int& value,
|
||
|
LDBCommandExecuteResult& exec_state) {
|
||
|
|
||
|
map<string, string>::const_iterator itr = option_map_.find(option);
|
||
12 years ago
|
if (itr != option_map_.end()) {
|
||
12 years ago
|
try {
|
||
10 years ago
|
#if defined(CYGWIN)
|
||
|
value = strtol(itr->second.c_str(), 0, 10);
|
||
|
#else
|
||
10 years ago
|
value = stoi(itr->second);
|
||
10 years ago
|
#endif
|
||
12 years ago
|
return true;
|
||
10 years ago
|
} catch(const invalid_argument&) {
|
||
10 years ago
|
exec_state =
|
||
|
LDBCommandExecuteResult::Failed(option + " has an invalid value.");
|
||
10 years ago
|
} catch(const out_of_range&) {
|
||
10 years ago
|
exec_state = LDBCommandExecuteResult::Failed(
|
||
|
option + " has a value out-of-range.");
|
||
12 years ago
|
}
|
||
|
}
|
||
12 years ago
|
return false;
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Parses the specified option and fills in the value.
|
||
|
* Returns true if the option is found.
|
||
|
* Returns false otherwise.
|
||
|
*/
|
||
10 years ago
|
bool LDBCommand::ParseStringOption(const map<string, string>& options,
|
||
|
const string& option, string* value) {
|
||
12 years ago
|
auto itr = option_map_.find(option);
|
||
|
if (itr != option_map_.end()) {
|
||
|
*value = itr->second;
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
12 years ago
|
Options LDBCommand::PrepareOptionsForOpenDB() {
|
||
12 years ago
|
|
||
12 years ago
|
Options opt = options_;
|
||
12 years ago
|
opt.create_if_missing = false;
|
||
12 years ago
|
|
||
10 years ago
|
map<string, string>::const_iterator itr;
|
||
12 years ago
|
|
||
10 years ago
|
BlockBasedTableOptions table_options;
|
||
10 years ago
|
bool use_table_options = false;
|
||
12 years ago
|
int bits;
|
||
10 years ago
|
if (ParseIntOption(option_map_, ARG_BLOOM_BITS, bits, exec_state_)) {
|
||
12 years ago
|
if (bits > 0) {
|
||
10 years ago
|
use_table_options = true;
|
||
10 years ago
|
table_options.filter_policy.reset(NewBloomFilterPolicy(bits));
|
||
12 years ago
|
} else {
|
||
10 years ago
|
exec_state_ =
|
||
|
LDBCommandExecuteResult::Failed(ARG_BLOOM_BITS + " must be > 0.");
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
|
int block_size;
|
||
10 years ago
|
if (ParseIntOption(option_map_, ARG_BLOCK_SIZE, block_size, exec_state_)) {
|
||
12 years ago
|
if (block_size > 0) {
|
||
10 years ago
|
use_table_options = true;
|
||
10 years ago
|
table_options.block_size = block_size;
|
||
12 years ago
|
} else {
|
||
10 years ago
|
exec_state_ =
|
||
|
LDBCommandExecuteResult::Failed(ARG_BLOCK_SIZE + " must be > 0.");
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
if (use_table_options) {
|
||
|
opt.table_factory.reset(NewBlockBasedTableFactory(table_options));
|
||
|
}
|
||
|
|
||
12 years ago
|
itr = option_map_.find(ARG_AUTO_COMPACTION);
|
||
|
if (itr != option_map_.end()) {
|
||
12 years ago
|
opt.disable_auto_compactions = ! StringToBool(itr->second);
|
||
|
}
|
||
|
|
||
12 years ago
|
itr = option_map_.find(ARG_COMPRESSION_TYPE);
|
||
|
if (itr != option_map_.end()) {
|
||
10 years ago
|
string comp = itr->second;
|
||
12 years ago
|
if (comp == "no") {
|
||
12 years ago
|
opt.compression = kNoCompression;
|
||
12 years ago
|
} else if (comp == "snappy") {
|
||
12 years ago
|
opt.compression = kSnappyCompression;
|
||
12 years ago
|
} else if (comp == "zlib") {
|
||
12 years ago
|
opt.compression = kZlibCompression;
|
||
12 years ago
|
} else if (comp == "bzip2") {
|
||
12 years ago
|
opt.compression = kBZip2Compression;
|
||
11 years ago
|
} else if (comp == "lz4") {
|
||
|
opt.compression = kLZ4Compression;
|
||
|
} else if (comp == "lz4hc") {
|
||
|
opt.compression = kLZ4HCCompression;
|
||
9 years ago
|
} else if (comp == "zstd") {
|
||
|
opt.compression = kZSTDNotFinalCompression;
|
||
12 years ago
|
} else {
|
||
|
// Unknown compression.
|
||
10 years ago
|
exec_state_ =
|
||
|
LDBCommandExecuteResult::Failed("Unknown compression level: " + comp);
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
int db_write_buffer_size;
|
||
|
if (ParseIntOption(option_map_, ARG_DB_WRITE_BUFFER_SIZE,
|
||
|
db_write_buffer_size, exec_state_)) {
|
||
|
if (db_write_buffer_size >= 0) {
|
||
|
opt.db_write_buffer_size = db_write_buffer_size;
|
||
|
} else {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(ARG_DB_WRITE_BUFFER_SIZE +
|
||
10 years ago
|
" must be >= 0.");
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
int write_buffer_size;
|
||
10 years ago
|
if (ParseIntOption(option_map_, ARG_WRITE_BUFFER_SIZE, write_buffer_size,
|
||
|
exec_state_)) {
|
||
12 years ago
|
if (write_buffer_size > 0) {
|
||
12 years ago
|
opt.write_buffer_size = write_buffer_size;
|
||
12 years ago
|
} else {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(ARG_WRITE_BUFFER_SIZE +
|
||
10 years ago
|
" must be > 0.");
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
|
int file_size;
|
||
10 years ago
|
if (ParseIntOption(option_map_, ARG_FILE_SIZE, file_size, exec_state_)) {
|
||
12 years ago
|
if (file_size > 0) {
|
||
12 years ago
|
opt.target_file_size_base = file_size;
|
||
|
} else {
|
||
10 years ago
|
exec_state_ =
|
||
|
LDBCommandExecuteResult::Failed(ARG_FILE_SIZE + " must be > 0.");
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
if (opt.db_paths.size() == 0) {
|
||
10 years ago
|
opt.db_paths.emplace_back(db_path_, std::numeric_limits<uint64_t>::max());
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
int fix_prefix_len;
|
||
10 years ago
|
if (ParseIntOption(option_map_, ARG_FIX_PREFIX_LEN, fix_prefix_len,
|
||
|
exec_state_)) {
|
||
10 years ago
|
if (fix_prefix_len > 0) {
|
||
|
opt.prefix_extractor.reset(
|
||
|
NewFixedPrefixTransform(static_cast<size_t>(fix_prefix_len)));
|
||
|
} else {
|
||
10 years ago
|
exec_state_ =
|
||
10 years ago
|
LDBCommandExecuteResult::Failed(ARG_FIX_PREFIX_LEN + " must be > 0.");
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
return opt;
|
||
|
}
|
||
|
|
||
10 years ago
|
bool LDBCommand::ParseKeyValue(const string& line, string* key, string* value,
|
||
|
bool is_key_hex, bool is_value_hex) {
|
||
12 years ago
|
size_t pos = line.find(DELIM);
|
||
10 years ago
|
if (pos != string::npos) {
|
||
12 years ago
|
*key = line.substr(0, pos);
|
||
|
*value = line.substr(pos + strlen(DELIM));
|
||
|
if (is_key_hex) {
|
||
|
*key = HexToString(*key);
|
||
|
}
|
||
|
if (is_value_hex) {
|
||
|
*value = HexToString(*value);
|
||
|
}
|
||
|
return true;
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
12 years ago
|
|
||
12 years ago
|
/**
|
||
|
* Make sure that ONLY the command-line options and flags expected by this
|
||
|
* command are specified on the command-line. Extraneous options are usually
|
||
|
* the result of user error.
|
||
|
* Returns true if all checks pass. Else returns false, and prints an
|
||
|
* appropriate error msg to stderr.
|
||
|
*/
|
||
|
bool LDBCommand::ValidateCmdLineOptions() {
|
||
|
|
||
10 years ago
|
for (map<string, string>::const_iterator itr = option_map_.begin();
|
||
|
itr != option_map_.end(); ++itr) {
|
||
12 years ago
|
if (find(valid_cmd_line_options_.begin(),
|
||
10 years ago
|
valid_cmd_line_options_.end(), itr->first) ==
|
||
12 years ago
|
valid_cmd_line_options_.end()) {
|
||
|
fprintf(stderr, "Invalid command-line option %s\n", itr->first.c_str());
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
12 years ago
|
|
||
10 years ago
|
for (vector<string>::const_iterator itr = flags_.begin();
|
||
10 years ago
|
itr != flags_.end(); ++itr) {
|
||
12 years ago
|
if (find(valid_cmd_line_options_.begin(),
|
||
10 years ago
|
valid_cmd_line_options_.end(), *itr) ==
|
||
12 years ago
|
valid_cmd_line_options_.end()) {
|
||
|
fprintf(stderr, "Invalid command-line flag %s\n", itr->c_str());
|
||
|
return false;
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
if (!NoDBOpen() && option_map_.find(ARG_DB) == option_map_.end()) {
|
||
12 years ago
|
fprintf(stderr, "%s must be specified\n", ARG_DB.c_str());
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
10 years ago
|
CompactorCommand::CompactorCommand(const vector<string>& params,
|
||
|
const map<string, string>& options, const vector<string>& flags) :
|
||
12 years ago
|
LDBCommand(options, flags, false,
|
||
|
BuildCmdLineOptions({ARG_FROM, ARG_TO, ARG_HEX, ARG_KEY_HEX,
|
||
11 years ago
|
ARG_VALUE_HEX, ARG_TTL})),
|
||
12 years ago
|
null_from_(true), null_to_(true) {
|
||
10 years ago
|
|
||
|
map<string, string>::const_iterator itr = options.find(ARG_FROM);
|
||
12 years ago
|
if (itr != options.end()) {
|
||
|
null_from_ = false;
|
||
|
from_ = itr->second;
|
||
|
}
|
||
|
|
||
|
itr = options.find(ARG_TO);
|
||
|
if (itr != options.end()) {
|
||
|
null_to_ = false;
|
||
|
to_ = itr->second;
|
||
|
}
|
||
|
|
||
|
if (is_key_hex_) {
|
||
12 years ago
|
if (!null_from_) {
|
||
|
from_ = HexToString(from_);
|
||
|
}
|
||
|
if (!null_to_) {
|
||
|
to_ = HexToString(to_);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void CompactorCommand::Help(string& ret) {
|
||
|
ret.append(" ");
|
||
|
ret.append(CompactorCommand::Name());
|
||
|
ret.append(HelpRangeCmdArgs());
|
||
|
ret.append("\n");
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
void CompactorCommand::DoCommand() {
|
||
12 years ago
|
|
||
12 years ago
|
Slice* begin = nullptr;
|
||
|
Slice* end = nullptr;
|
||
12 years ago
|
if (!null_from_) {
|
||
12 years ago
|
begin = new Slice(from_);
|
||
12 years ago
|
}
|
||
|
if (!null_to_) {
|
||
12 years ago
|
end = new Slice(to_);
|
||
12 years ago
|
}
|
||
|
|
||
10 years ago
|
db_->CompactRange(CompactRangeOptions(), begin, end);
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Succeed("");
|
||
12 years ago
|
|
||
|
delete begin;
|
||
|
delete end;
|
||
|
}
|
||
|
|
||
10 years ago
|
// ----------------------------------------------------------------------------
|
||
|
|
||
10 years ago
|
const string DBLoaderCommand::ARG_DISABLE_WAL = "disable_wal";
|
||
|
const string DBLoaderCommand::ARG_BULK_LOAD = "bulk_load";
|
||
|
const string DBLoaderCommand::ARG_COMPACT = "compact";
|
||
12 years ago
|
|
||
10 years ago
|
DBLoaderCommand::DBLoaderCommand(const vector<string>& params,
|
||
|
const map<string, string>& options, const vector<string>& flags) :
|
||
12 years ago
|
LDBCommand(options, flags, false,
|
||
|
BuildCmdLineOptions({ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX,
|
||
|
ARG_FROM, ARG_TO, ARG_CREATE_IF_MISSING,
|
||
12 years ago
|
ARG_DISABLE_WAL, ARG_BULK_LOAD,
|
||
|
ARG_COMPACT})),
|
||
|
create_if_missing_(false), disable_wal_(false), bulk_load_(false),
|
||
|
compact_(false) {
|
||
12 years ago
|
|
||
|
create_if_missing_ = IsFlagPresent(flags, ARG_CREATE_IF_MISSING);
|
||
|
disable_wal_ = IsFlagPresent(flags, ARG_DISABLE_WAL);
|
||
12 years ago
|
bulk_load_ = IsFlagPresent(flags, ARG_BULK_LOAD);
|
||
|
compact_ = IsFlagPresent(flags, ARG_COMPACT);
|
||
12 years ago
|
}
|
||
|
|
||
10 years ago
|
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");
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
Options DBLoaderCommand::PrepareOptionsForOpenDB() {
|
||
|
Options opt = LDBCommand::PrepareOptionsForOpenDB();
|
||
12 years ago
|
opt.create_if_missing = create_if_missing_;
|
||
12 years ago
|
if (bulk_load_) {
|
||
|
opt.PrepareForBulkLoad();
|
||
|
}
|
||
12 years ago
|
return opt;
|
||
|
}
|
||
|
|
||
12 years ago
|
void DBLoaderCommand::DoCommand() {
|
||
12 years ago
|
if (!db_) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
WriteOptions write_options;
|
||
|
if (disable_wal_) {
|
||
|
write_options.disableWAL = true;
|
||
|
}
|
||
|
|
||
|
int bad_lines = 0;
|
||
10 years ago
|
string line;
|
||
|
while (getline(cin, line, '\n')) {
|
||
|
string key;
|
||
|
string value;
|
||
12 years ago
|
if (ParseKeyValue(line, &key, &value, is_key_hex_, is_value_hex_)) {
|
||
12 years ago
|
db_->Put(write_options, Slice(key), Slice(value));
|
||
|
} else if (0 == line.find("Keys in range:")) {
|
||
|
// ignore this line
|
||
|
} else if (0 == line.find("Created bg thread 0x")) {
|
||
|
// ignore this line
|
||
|
} else {
|
||
|
bad_lines ++;
|
||
|
}
|
||
|
}
|
||
12 years ago
|
|
||
12 years ago
|
if (bad_lines > 0) {
|
||
10 years ago
|
cout << "Warning: " << bad_lines << " bad lines ignored." << endl;
|
||
12 years ago
|
}
|
||
12 years ago
|
if (compact_) {
|
||
10 years ago
|
db_->CompactRange(CompactRangeOptions(), nullptr, nullptr);
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
// ----------------------------------------------------------------------------
|
||
|
|
||
10 years ago
|
namespace {
|
||
|
|
||
9 years ago
|
void DumpManifestFile(std::string file, bool verbose, bool hex, bool json) {
|
||
10 years ago
|
Options options;
|
||
|
EnvOptions sopt;
|
||
|
std::string dbname("dummy");
|
||
10 years ago
|
std::shared_ptr<Cache> tc(NewLRUCache(options.max_open_files - 10,
|
||
|
options.table_cache_numshardbits));
|
||
10 years ago
|
// Notice we are using the default options not through SanitizeOptions(),
|
||
|
// if VersionSet::DumpManifest() depends on any option done by
|
||
|
// SanitizeOptions(), we need to initialize it manually.
|
||
|
options.db_paths.emplace_back("dummy", 0);
|
||
9 years ago
|
options.num_levels = 64;
|
||
10 years ago
|
WriteController wc(options.delayed_write_rate);
|
||
10 years ago
|
WriteBuffer wb(options.db_write_buffer_size);
|
||
|
VersionSet versions(dbname, &options, sopt, tc.get(), &wb, &wc);
|
||
9 years ago
|
Status s = versions.DumpManifest(options, file, verbose, hex, json);
|
||
10 years ago
|
if (!s.ok()) {
|
||
|
printf("Error in processing file %s %s\n", file.c_str(),
|
||
|
s.ToString().c_str());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
} // namespace
|
||
|
|
||
10 years ago
|
const string ManifestDumpCommand::ARG_VERBOSE = "verbose";
|
||
9 years ago
|
const string ManifestDumpCommand::ARG_JSON = "json";
|
||
|
const string ManifestDumpCommand::ARG_PATH = "path";
|
||
12 years ago
|
|
||
10 years ago
|
void ManifestDumpCommand::Help(string& ret) {
|
||
|
ret.append(" ");
|
||
|
ret.append(ManifestDumpCommand::Name());
|
||
|
ret.append(" [--" + ARG_VERBOSE + "]");
|
||
9 years ago
|
ret.append(" [--" + ARG_JSON + "]");
|
||
10 years ago
|
ret.append(" [--" + ARG_PATH + "=<path_to_manifest_file>]");
|
||
|
ret.append("\n");
|
||
12 years ago
|
}
|
||
|
|
||
10 years ago
|
ManifestDumpCommand::ManifestDumpCommand(const vector<string>& params,
|
||
|
const map<string, string>& options, const vector<string>& flags) :
|
||
12 years ago
|
LDBCommand(options, flags, false,
|
||
9 years ago
|
BuildCmdLineOptions({ARG_VERBOSE, ARG_PATH, ARG_HEX, ARG_JSON})),
|
||
12 years ago
|
verbose_(false),
|
||
9 years ago
|
json_(false),
|
||
10 years ago
|
path_("")
|
||
|
{
|
||
12 years ago
|
verbose_ = IsFlagPresent(flags, ARG_VERBOSE);
|
||
9 years ago
|
json_ = IsFlagPresent(flags, ARG_JSON);
|
||
12 years ago
|
|
||
10 years ago
|
map<string, string>::const_iterator itr = options.find(ARG_PATH);
|
||
12 years ago
|
if (itr != options.end()) {
|
||
|
path_ = itr->second;
|
||
|
if (path_.empty()) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed("--path: missing pathname");
|
||
12 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void ManifestDumpCommand::DoCommand() {
|
||
|
|
||
|
std::string manifestfile;
|
||
|
|
||
|
if (!path_.empty()) {
|
||
|
manifestfile = path_;
|
||
|
} else {
|
||
|
bool found = false;
|
||
|
// We need to find the manifest file by searching the directory
|
||
|
// containing the db for files of the form MANIFEST_[0-9]+
|
||
10 years ago
|
|
||
|
auto CloseDir = [](DIR* p) { closedir(p); };
|
||
9 years ago
|
std::unique_ptr<DIR, decltype(CloseDir)> d(opendir(db_path_.c_str()),
|
||
|
CloseDir);
|
||
10 years ago
|
|
||
12 years ago
|
if (d == nullptr) {
|
||
10 years ago
|
exec_state_ =
|
||
|
LDBCommandExecuteResult::Failed(db_path_ + " is not a directory");
|
||
12 years ago
|
return;
|
||
|
}
|
||
|
struct dirent* entry;
|
||
10 years ago
|
while ((entry = readdir(d.get())) != nullptr) {
|
||
12 years ago
|
unsigned int match;
|
||
9 years ago
|
uint64_t num;
|
||
9 years ago
|
if (sscanf(entry->d_name, "MANIFEST-%" PRIu64 "%n", &num, &match) &&
|
||
9 years ago
|
match == strlen(entry->d_name)) {
|
||
12 years ago
|
if (!found) {
|
||
|
manifestfile = db_path_ + "/" + std::string(entry->d_name);
|
||
|
found = true;
|
||
|
} else {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(
|
||
10 years ago
|
"Multiple MANIFEST files found; use --path to select one");
|
||
12 years ago
|
return;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (verbose_) {
|
||
|
printf("Processing Manifest file %s\n", manifestfile.c_str());
|
||
|
}
|
||
|
|
||
9 years ago
|
DumpManifestFile(manifestfile, verbose_, is_key_hex_, json_);
|
||
|
|
||
12 years ago
|
if (verbose_) {
|
||
|
printf("Processing Manifest file %s done\n", manifestfile.c_str());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// ----------------------------------------------------------------------------
|
||
11 years ago
|
|
||
10 years ago
|
void ListColumnFamiliesCommand::Help(string& ret) {
|
||
|
ret.append(" ");
|
||
|
ret.append(ListColumnFamiliesCommand::Name());
|
||
|
ret.append(" full_path_to_db_directory ");
|
||
|
ret.append("\n");
|
||
11 years ago
|
}
|
||
|
|
||
|
ListColumnFamiliesCommand::ListColumnFamiliesCommand(
|
||
10 years ago
|
const vector<string>& params, const map<string, string>& options,
|
||
|
const vector<string>& flags)
|
||
11 years ago
|
: LDBCommand(options, flags, false, {}) {
|
||
|
|
||
|
if (params.size() != 1) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(
|
||
11 years ago
|
"dbname must be specified for the list_column_families command");
|
||
|
} else {
|
||
|
dbname_ = params[0];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void ListColumnFamiliesCommand::DoCommand() {
|
||
10 years ago
|
vector<string> column_families;
|
||
11 years ago
|
Status s = DB::ListColumnFamilies(DBOptions(), dbname_, &column_families);
|
||
|
if (!s.ok()) {
|
||
|
printf("Error in processing db %s %s\n", dbname_.c_str(),
|
||
|
s.ToString().c_str());
|
||
|
} else {
|
||
|
printf("Column families in %s: \n{", dbname_.c_str());
|
||
|
bool first = true;
|
||
|
for (auto cf : column_families) {
|
||
|
if (!first) {
|
||
|
printf(", ");
|
||
|
}
|
||
|
first = false;
|
||
|
printf("%s", cf.c_str());
|
||
|
}
|
||
|
printf("}\n");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// ----------------------------------------------------------------------------
|
||
12 years ago
|
|
||
11 years ago
|
namespace {
|
||
|
|
||
10 years ago
|
string ReadableTime(int unixtime) {
|
||
12 years ago
|
char time_buffer [80];
|
||
|
time_t rawtime = unixtime;
|
||
9 years ago
|
struct tm tInfo;
|
||
|
struct tm* timeinfo = localtime_r(&rawtime, &tInfo);
|
||
|
assert(timeinfo == &tInfo);
|
||
12 years ago
|
strftime(time_buffer, 80, "%c", timeinfo);
|
||
10 years ago
|
return string(time_buffer);
|
||
12 years ago
|
}
|
||
|
|
||
|
// 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
|
||
10 years ago
|
void IncBucketCounts(vector<uint64_t>& bucket_counts, int ttl_start,
|
||
12 years ago
|
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;
|
||
10 years ago
|
bucket_counts[bucket]++;
|
||
12 years ago
|
}
|
||
|
|
||
10 years ago
|
void PrintBucketCounts(const vector<uint64_t>& bucket_counts, int ttl_start,
|
||
|
int ttl_end, int bucket_size, int num_buckets) {
|
||
12 years ago
|
int time_point = ttl_start;
|
||
10 years ago
|
for(int i = 0; i < num_buckets - 1; i++, time_point += bucket_size) {
|
||
|
fprintf(stdout, "Keys in range %s to %s : %lu\n",
|
||
12 years ago
|
ReadableTime(time_point).c_str(),
|
||
11 years ago
|
ReadableTime(time_point + bucket_size).c_str(),
|
||
10 years ago
|
(unsigned long)bucket_counts[i]);
|
||
12 years ago
|
}
|
||
10 years ago
|
fprintf(stdout, "Keys in range %s to %s : %lu\n",
|
||
12 years ago
|
ReadableTime(time_point).c_str(),
|
||
11 years ago
|
ReadableTime(ttl_end).c_str(),
|
||
10 years ago
|
(unsigned long)bucket_counts[num_buckets - 1]);
|
||
12 years ago
|
}
|
||
|
|
||
11 years ago
|
} // namespace
|
||
|
|
||
10 years ago
|
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";
|
||
12 years ago
|
|
||
10 years ago
|
InternalDumpCommand::InternalDumpCommand(const vector<string>& params,
|
||
|
const map<string, string>& options,
|
||
|
const vector<string>& flags) :
|
||
12 years ago
|
LDBCommand(options, flags, true,
|
||
10 years ago
|
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})),
|
||
12 years ago
|
has_from_(false),
|
||
|
has_to_(false),
|
||
|
max_keys_(-1),
|
||
11 years ago
|
delim_("."),
|
||
12 years ago
|
count_only_(false),
|
||
11 years ago
|
count_delim_(false),
|
||
11 years ago
|
print_stats_(false),
|
||
|
is_input_key_hex_(false) {
|
||
12 years ago
|
|
||
|
has_from_ = ParseStringOption(options, ARG_FROM, &from_);
|
||
|
has_to_ = ParseStringOption(options, ARG_TO, &to_);
|
||
|
|
||
10 years ago
|
ParseIntOption(options, ARG_MAX_KEYS, max_keys_, exec_state_);
|
||
|
map<string, string>::const_iterator itr = options.find(ARG_COUNT_DELIM);
|
||
11 years ago
|
if (itr != options.end()) {
|
||
|
delim_ = itr->second;
|
||
|
count_delim_ = true;
|
||
10 years ago
|
// fprintf(stdout,"delim = %c\n",delim_[0]);
|
||
11 years ago
|
} else {
|
||
|
count_delim_ = IsFlagPresent(flags, ARG_COUNT_DELIM);
|
||
10 years ago
|
delim_=".";
|
||
11 years ago
|
}
|
||
12 years ago
|
|
||
|
print_stats_ = IsFlagPresent(flags, ARG_STATS);
|
||
|
count_only_ = IsFlagPresent(flags, ARG_COUNT_ONLY);
|
||
11 years ago
|
is_input_key_hex_ = IsFlagPresent(flags, ARG_INPUT_KEY_HEX);
|
||
12 years ago
|
|
||
11 years ago
|
if (is_input_key_hex_) {
|
||
12 years ago
|
if (has_from_) {
|
||
|
from_ = HexToString(from_);
|
||
|
}
|
||
|
if (has_to_) {
|
||
|
to_ = HexToString(to_);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
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 + "=<N>]");
|
||
|
ret.append(" [--" + ARG_COUNT_ONLY + "]");
|
||
|
ret.append(" [--" + ARG_COUNT_DELIM + "=<char>]");
|
||
|
ret.append(" [--" + ARG_STATS + "]");
|
||
|
ret.append("\n");
|
||
12 years ago
|
}
|
||
|
|
||
|
void InternalDumpCommand::DoCommand() {
|
||
|
if (!db_) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (print_stats_) {
|
||
10 years ago
|
string stats;
|
||
11 years ago
|
if (db_->GetProperty("rocksdb.stats", &stats)) {
|
||
12 years ago
|
fprintf(stdout, "%s\n", stats.c_str());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Cast as DBImpl to get internal iterator
|
||
|
DBImpl* idb = dynamic_cast<DBImpl*>(db_);
|
||
|
if (!idb) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed("DB is not DBImpl");
|
||
12 years ago
|
return;
|
||
|
}
|
||
10 years ago
|
string rtype1,rtype2,row,val;
|
||
11 years ago
|
rtype2 = "";
|
||
10 years ago
|
uint64_t c=0;
|
||
|
uint64_t s1=0,s2=0;
|
||
12 years ago
|
// Setup internal key iterator
|
||
10 years ago
|
Arena arena;
|
||
9 years ago
|
ScopedArenaIterator iter(idb->NewInternalIterator(&arena));
|
||
12 years ago
|
Status st = iter->status();
|
||
|
if (!st.ok()) {
|
||
10 years ago
|
exec_state_ =
|
||
|
LDBCommandExecuteResult::Failed("Iterator error:" + st.ToString());
|
||
12 years ago
|
}
|
||
|
|
||
|
if (has_from_) {
|
||
10 years ago
|
InternalKey ikey;
|
||
|
ikey.SetMaxPossibleForUserKey(from_);
|
||
12 years ago
|
iter->Seek(ikey.Encode());
|
||
|
} else {
|
||
|
iter->SeekToFirst();
|
||
|
}
|
||
|
|
||
10 years ago
|
long long count = 0;
|
||
12 years ago
|
for (; iter->Valid(); iter->Next()) {
|
||
|
ParsedInternalKey ikey;
|
||
|
if (!ParseInternalKey(iter->key(), &ikey)) {
|
||
|
fprintf(stderr, "Internal Key [%s] parse error!\n",
|
||
|
iter->key().ToString(true /* in hex*/).data());
|
||
|
// TODO: add error counter
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
// If end marker was specified, we stop before it
|
||
|
if (has_to_ && options_.comparator->Compare(ikey.user_key, to_) >= 0) {
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
++count;
|
||
11 years ago
|
int k;
|
||
|
if (count_delim_) {
|
||
|
rtype1 = "";
|
||
10 years ago
|
s1=0;
|
||
11 years ago
|
row = iter->key().ToString();
|
||
|
val = iter->value().ToString();
|
||
10 years ago
|
for(k=0;row[k]!='\x01' && row[k]!='\0';k++)
|
||
11 years ago
|
s1++;
|
||
10 years ago
|
for(k=0;val[k]!='\x01' && val[k]!='\0';k++)
|
||
11 years ago
|
s1++;
|
||
10 years ago
|
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;
|
||
11 years ago
|
rtype2 = rtype1;
|
||
|
} else {
|
||
|
c++;
|
||
10 years ago
|
s2+=s1;
|
||
|
rtype2=rtype1;
|
||
11 years ago
|
}
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
if (!count_only_ && !count_delim_) {
|
||
10 years ago
|
string key = ikey.DebugString(is_key_hex_);
|
||
|
string value = iter->value().ToString(is_value_hex_);
|
||
|
std::cout << key << " => " << value << "\n";
|
||
12 years ago
|
}
|
||
|
|
||
|
// Terminate if maximum number of keys have been dumped
|
||
10 years ago
|
if (max_keys_ > 0 && count >= max_keys_) break;
|
||
12 years ago
|
}
|
||
10 years ago
|
if(count_delim_) {
|
||
|
fprintf(stdout,"%s => count:%lld\tsize:%lld\n", rtype2.c_str(),
|
||
|
(long long)c,(long long)s2);
|
||
11 years ago
|
} else
|
||
10 years ago
|
fprintf(stdout, "Internal keys in range: %lld\n", (long long) count);
|
||
12 years ago
|
}
|
||
|
|
||
|
|
||
10 years ago
|
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";
|
||
12 years ago
|
|
||
10 years ago
|
DBDumperCommand::DBDumperCommand(const vector<string>& params,
|
||
|
const map<string, string>& options, const vector<string>& flags) :
|
||
12 years ago
|
LDBCommand(options, flags, true,
|
||
10 years ago
|
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})),
|
||
12 years ago
|
null_from_(true),
|
||
|
null_to_(true),
|
||
|
max_keys_(-1),
|
||
|
count_only_(false),
|
||
11 years ago
|
count_delim_(false),
|
||
12 years ago
|
print_stats_(false) {
|
||
|
|
||
10 years ago
|
map<string, string>::const_iterator itr = options.find(ARG_FROM);
|
||
12 years ago
|
if (itr != options.end()) {
|
||
|
null_from_ = false;
|
||
|
from_ = itr->second;
|
||
|
}
|
||
|
|
||
|
itr = options.find(ARG_TO);
|
||
|
if (itr != options.end()) {
|
||
|
null_to_ = false;
|
||
|
to_ = itr->second;
|
||
|
}
|
||
|
|
||
|
itr = options.find(ARG_MAX_KEYS);
|
||
|
if (itr != options.end()) {
|
||
|
try {
|
||
10 years ago
|
#if defined(CYGWIN)
|
||
|
max_keys_ = strtol(itr->second.c_str(), 0, 10);
|
||
|
#else
|
||
12 years ago
|
max_keys_ = stoi(itr->second);
|
||
10 years ago
|
#endif
|
||
10 years ago
|
} catch(const invalid_argument&) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(ARG_MAX_KEYS +
|
||
10 years ago
|
" has an invalid value");
|
||
10 years ago
|
} catch(const out_of_range&) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(
|
||
|
ARG_MAX_KEYS + " has a value out-of-range");
|
||
12 years ago
|
}
|
||
|
}
|
||
11 years ago
|
itr = options.find(ARG_COUNT_DELIM);
|
||
|
if (itr != options.end()) {
|
||
|
delim_ = itr->second;
|
||
|
count_delim_ = true;
|
||
|
} else {
|
||
|
count_delim_ = IsFlagPresent(flags, ARG_COUNT_DELIM);
|
||
10 years ago
|
delim_=".";
|
||
11 years ago
|
}
|
||
12 years ago
|
|
||
12 years ago
|
print_stats_ = IsFlagPresent(flags, ARG_STATS);
|
||
|
count_only_ = IsFlagPresent(flags, ARG_COUNT_ONLY);
|
||
|
|
||
|
if (is_key_hex_) {
|
||
12 years ago
|
if (!null_from_) {
|
||
|
from_ = HexToString(from_);
|
||
|
}
|
||
|
if (!null_to_) {
|
||
|
to_ = HexToString(to_);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void DBDumperCommand::Help(string& ret) {
|
||
|
ret.append(" ");
|
||
|
ret.append(DBDumperCommand::Name());
|
||
|
ret.append(HelpRangeCmdArgs());
|
||
|
ret.append(" [--" + ARG_TTL + "]");
|
||
|
ret.append(" [--" + ARG_MAX_KEYS + "=<N>]");
|
||
|
ret.append(" [--" + ARG_TIMESTAMP + "]");
|
||
|
ret.append(" [--" + ARG_COUNT_ONLY + "]");
|
||
|
ret.append(" [--" + ARG_COUNT_DELIM + "=<char>]");
|
||
|
ret.append(" [--" + ARG_STATS + "]");
|
||
|
ret.append(" [--" + ARG_TTL_BUCKET + "=<N>]");
|
||
|
ret.append(" [--" + ARG_TTL_START + "=<N>:- is inclusive]");
|
||
|
ret.append(" [--" + ARG_TTL_END + "=<N>:- is exclusive]");
|
||
|
ret.append("\n");
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
void DBDumperCommand::DoCommand() {
|
||
12 years ago
|
if (!db_) {
|
||
|
return;
|
||
|
}
|
||
12 years ago
|
// Parse command line args
|
||
|
uint64_t count = 0;
|
||
|
if (print_stats_) {
|
||
10 years ago
|
string stats;
|
||
11 years ago
|
if (db_->GetProperty("rocksdb.stats", &stats)) {
|
||
12 years ago
|
fprintf(stdout, "%s\n", stats.c_str());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Setup key iterator
|
||
12 years ago
|
Iterator* iter = db_->NewIterator(ReadOptions());
|
||
|
Status st = iter->status();
|
||
12 years ago
|
if (!st.ok()) {
|
||
10 years ago
|
exec_state_ =
|
||
|
LDBCommandExecuteResult::Failed("Iterator error." + st.ToString());
|
||
12 years ago
|
}
|
||
|
|
||
|
if (!null_from_) {
|
||
|
iter->Seek(from_);
|
||
|
} else {
|
||
|
iter->SeekToFirst();
|
||
|
}
|
||
|
|
||
|
int max_keys = max_keys_;
|
||
12 years ago
|
int ttl_start;
|
||
10 years ago
|
if (!ParseIntOption(option_map_, ARG_TTL_START, ttl_start, exec_state_)) {
|
||
11 years ago
|
ttl_start = DBWithTTLImpl::kMinTimestamp; // TTL introduction time
|
||
12 years ago
|
}
|
||
|
int ttl_end;
|
||
10 years ago
|
if (!ParseIntOption(option_map_, ARG_TTL_END, ttl_end, exec_state_)) {
|
||
11 years ago
|
ttl_end = DBWithTTLImpl::kMaxTimestamp; // Max time allowed by TTL feature
|
||
12 years ago
|
}
|
||
|
if (ttl_end < ttl_start) {
|
||
|
fprintf(stderr, "Error: End time can't be less than start time\n");
|
||
|
delete iter;
|
||
|
return;
|
||
|
}
|
||
|
int time_range = ttl_end - ttl_start;
|
||
|
int bucket_size;
|
||
10 years ago
|
if (!ParseIntOption(option_map_, ARG_TTL_BUCKET, bucket_size, exec_state_) ||
|
||
12 years ago
|
bucket_size <= 0) {
|
||
|
bucket_size = time_range; // Will have just 1 bucket by default
|
||
|
}
|
||
11 years ago
|
//cretaing variables for row count of each type
|
||
10 years ago
|
string rtype1,rtype2,row,val;
|
||
11 years ago
|
rtype2 = "";
|
||
10 years ago
|
uint64_t c=0;
|
||
|
uint64_t s1=0,s2=0;
|
||
11 years ago
|
|
||
10 years ago
|
// At this point, bucket_size=0 => time_range=0
|
||
10 years ago
|
int num_buckets = (bucket_size >= time_range)
|
||
|
? 1
|
||
|
: ((time_range + bucket_size - 1) / bucket_size);
|
||
10 years ago
|
vector<uint64_t> bucket_counts(num_buckets, 0);
|
||
11 years ago
|
if (is_db_ttl_ && !count_only_ && timestamp_ && !count_delim_) {
|
||
12 years ago
|
fprintf(stdout, "Dumping key-values from %s to %s\n",
|
||
|
ReadableTime(ttl_start).c_str(), ReadableTime(ttl_end).c_str());
|
||
|
}
|
||
|
|
||
12 years ago
|
for (; iter->Valid(); iter->Next()) {
|
||
12 years ago
|
int rawtime = 0;
|
||
12 years ago
|
// If end marker was specified, we stop before it
|
||
|
if (!null_to_ && (iter->key().ToString() >= to_))
|
||
|
break;
|
||
|
// Terminate if maximum number of keys have been dumped
|
||
|
if (max_keys == 0)
|
||
|
break;
|
||
12 years ago
|
if (is_db_ttl_) {
|
||
11 years ago
|
TtlIterator* it_ttl = dynamic_cast<TtlIterator*>(iter);
|
||
|
assert(it_ttl);
|
||
12 years ago
|
rawtime = it_ttl->timestamp();
|
||
|
if (rawtime < ttl_start || rawtime >= ttl_end) {
|
||
12 years ago
|
continue;
|
||
|
}
|
||
|
}
|
||
12 years ago
|
if (max_keys > 0) {
|
||
|
--max_keys;
|
||
|
}
|
||
12 years ago
|
if (is_db_ttl_ && num_buckets > 1) {
|
||
10 years ago
|
IncBucketCounts(bucket_counts, ttl_start, time_range, bucket_size,
|
||
12 years ago
|
rawtime, num_buckets);
|
||
|
}
|
||
12 years ago
|
++count;
|
||
11 years ago
|
if (count_delim_) {
|
||
|
rtype1 = "";
|
||
|
row = iter->key().ToString();
|
||
|
val = iter->value().ToString();
|
||
|
s1 = row.size()+val.size();
|
||
10 years ago
|
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;
|
||
11 years ago
|
rtype2 = rtype1;
|
||
|
} else {
|
||
10 years ago
|
c++;
|
||
|
s2+=s1;
|
||
|
rtype2=rtype1;
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
|
||
|
|
||
11 years ago
|
if (!count_only_ && !count_delim_) {
|
||
12 years ago
|
if (is_db_ttl_ && timestamp_) {
|
||
|
fprintf(stdout, "%s ", ReadableTime(rawtime).c_str());
|
||
|
}
|
||
10 years ago
|
string str = PrintKeyValue(iter->key().ToString(),
|
||
12 years ago
|
iter->value().ToString(), is_key_hex_,
|
||
|
is_value_hex_);
|
||
12 years ago
|
fprintf(stdout, "%s\n", str.c_str());
|
||
12 years ago
|
}
|
||
|
}
|
||
11 years ago
|
|
||
12 years ago
|
if (num_buckets > 1 && is_db_ttl_) {
|
||
12 years ago
|
PrintBucketCounts(bucket_counts, ttl_start, ttl_end, bucket_size,
|
||
12 years ago
|
num_buckets);
|
||
10 years ago
|
} else if(count_delim_) {
|
||
|
fprintf(stdout,"%s => count:%lld\tsize:%lld\n",rtype2.c_str(),
|
||
|
(long long )c,(long long)s2);
|
||
12 years ago
|
} else {
|
||
10 years ago
|
fprintf(stdout, "Keys in range: %lld\n", (long long) count);
|
||
12 years ago
|
}
|
||
12 years ago
|
// Clean up
|
||
|
delete iter;
|
||
|
}
|
||
|
|
||
10 years ago
|
const string ReduceDBLevelsCommand::ARG_NEW_LEVELS = "new_levels";
|
||
|
const string ReduceDBLevelsCommand::ARG_PRINT_OLD_LEVELS = "print_old_levels";
|
||
12 years ago
|
|
||
10 years ago
|
ReduceDBLevelsCommand::ReduceDBLevelsCommand(const vector<string>& params,
|
||
|
const map<string, string>& options, const vector<string>& flags) :
|
||
12 years ago
|
LDBCommand(options, flags, false,
|
||
|
BuildCmdLineOptions({ARG_NEW_LEVELS, ARG_PRINT_OLD_LEVELS})),
|
||
10 years ago
|
old_levels_(1 << 7),
|
||
12 years ago
|
new_levels_(-1),
|
||
|
print_old_levels_(false) {
|
||
12 years ago
|
|
||
|
|
||
10 years ago
|
ParseIntOption(option_map_, ARG_NEW_LEVELS, new_levels_, exec_state_);
|
||
12 years ago
|
print_old_levels_ = IsFlagPresent(flags, ARG_PRINT_OLD_LEVELS);
|
||
12 years ago
|
|
||
10 years ago
|
if(new_levels_ <= 0) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(
|
||
10 years ago
|
" Use --" + ARG_NEW_LEVELS + " to specify a new level number\n");
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
vector<string> ReduceDBLevelsCommand::PrepareArgs(const string& db_path,
|
||
12 years ago
|
int new_levels, bool print_old_level) {
|
||
10 years ago
|
vector<string> ret;
|
||
12 years ago
|
ret.push_back("reduce_levels");
|
||
10 years ago
|
ret.push_back("--" + ARG_DB + "=" + db_path);
|
||
10 years ago
|
ret.push_back("--" + ARG_NEW_LEVELS + "=" + rocksdb::ToString(new_levels));
|
||
10 years ago
|
if(print_old_level) {
|
||
12 years ago
|
ret.push_back("--" + ARG_PRINT_OLD_LEVELS);
|
||
12 years ago
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
10 years ago
|
void ReduceDBLevelsCommand::Help(string& ret) {
|
||
|
ret.append(" ");
|
||
|
ret.append(ReduceDBLevelsCommand::Name());
|
||
|
ret.append(" --" + ARG_NEW_LEVELS + "=<New number of levels>");
|
||
|
ret.append(" [--" + ARG_PRINT_OLD_LEVELS + "]");
|
||
|
ret.append("\n");
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
Options ReduceDBLevelsCommand::PrepareOptionsForOpenDB() {
|
||
|
Options opt = LDBCommand::PrepareOptionsForOpenDB();
|
||
12 years ago
|
opt.num_levels = old_levels_;
|
||
12 years ago
|
opt.max_bytes_for_level_multiplier_additional.resize(opt.num_levels, 1);
|
||
12 years ago
|
// Disable size compaction
|
||
11 years ago
|
opt.max_bytes_for_level_base = 1ULL << 50;
|
||
12 years ago
|
opt.max_bytes_for_level_multiplier = 1;
|
||
12 years ago
|
return opt;
|
||
|
}
|
||
|
|
||
12 years ago
|
Status ReduceDBLevelsCommand::GetOldNumOfLevels(Options& opt,
|
||
12 years ago
|
int* levels) {
|
||
12 years ago
|
EnvOptions soptions;
|
||
11 years ago
|
std::shared_ptr<Cache> tc(
|
||
10 years ago
|
NewLRUCache(opt.max_open_files - 10, opt.table_cache_numshardbits));
|
||
12 years ago
|
const InternalKeyComparator cmp(opt.comparator);
|
||
10 years ago
|
WriteController wc(opt.delayed_write_rate);
|
||
10 years ago
|
WriteBuffer wb(opt.db_write_buffer_size);
|
||
|
VersionSet versions(db_path_, &opt, soptions, tc.get(), &wb, &wc);
|
||
11 years ago
|
std::vector<ColumnFamilyDescriptor> dummy;
|
||
11 years ago
|
ColumnFamilyDescriptor dummy_descriptor(kDefaultColumnFamilyName,
|
||
11 years ago
|
ColumnFamilyOptions(opt));
|
||
|
dummy.push_back(dummy_descriptor);
|
||
12 years ago
|
// We rely the VersionSet::Recover to tell us the internal data structures
|
||
|
// in the db. And the Recover() should never do any change
|
||
|
// (like LogAndApply) to the manifest file.
|
||
11 years ago
|
Status st = versions.Recover(dummy);
|
||
12 years ago
|
if (!st.ok()) {
|
||
|
return st;
|
||
|
}
|
||
|
int max = -1;
|
||
11 years ago
|
auto default_cfd = versions.GetColumnFamilySet()->GetDefault();
|
||
11 years ago
|
for (int i = 0; i < default_cfd->NumberLevels(); i++) {
|
||
10 years ago
|
if (default_cfd->current()->storage_info()->NumLevelFiles(i)) {
|
||
12 years ago
|
max = i;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
*levels = max + 1;
|
||
|
return st;
|
||
|
}
|
||
|
|
||
12 years ago
|
void ReduceDBLevelsCommand::DoCommand() {
|
||
12 years ago
|
if (new_levels_ <= 1) {
|
||
10 years ago
|
exec_state_ =
|
||
|
LDBCommandExecuteResult::Failed("Invalid number of levels.\n");
|
||
12 years ago
|
return;
|
||
|
}
|
||
|
|
||
12 years ago
|
Status st;
|
||
|
Options opt = PrepareOptionsForOpenDB();
|
||
12 years ago
|
int old_level_num = -1;
|
||
|
st = GetOldNumOfLevels(opt, &old_level_num);
|
||
|
if (!st.ok()) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(st.ToString());
|
||
12 years ago
|
return;
|
||
|
}
|
||
|
|
||
12 years ago
|
if (print_old_levels_) {
|
||
10 years ago
|
fprintf(stdout, "The old number of levels in use is %d\n", old_level_num);
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
12 years ago
|
if (old_level_num <= new_levels_) {
|
||
|
return;
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
old_levels_ = old_level_num;
|
||
|
|
||
|
OpenDB();
|
||
12 years ago
|
if (!db_) {
|
||
|
return;
|
||
|
}
|
||
12 years ago
|
// Compact the whole DB to put all files to the highest level.
|
||
12 years ago
|
fprintf(stdout, "Compacting the db...\n");
|
||
10 years ago
|
db_->CompactRange(CompactRangeOptions(), nullptr, nullptr);
|
||
12 years ago
|
CloseDB();
|
||
|
|
||
12 years ago
|
EnvOptions soptions;
|
||
10 years ago
|
st = VersionSet::ReduceNumberOfLevels(db_path_, &opt, soptions, new_levels_);
|
||
12 years ago
|
if (!st.ok()) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(st.ToString());
|
||
12 years ago
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
const string ChangeCompactionStyleCommand::ARG_OLD_COMPACTION_STYLE =
|
||
11 years ago
|
"old_compaction_style";
|
||
10 years ago
|
const string ChangeCompactionStyleCommand::ARG_NEW_COMPACTION_STYLE =
|
||
11 years ago
|
"new_compaction_style";
|
||
|
|
||
|
ChangeCompactionStyleCommand::ChangeCompactionStyleCommand(
|
||
10 years ago
|
const vector<string>& params, const map<string, string>& options,
|
||
|
const vector<string>& flags) :
|
||
11 years ago
|
LDBCommand(options, flags, false,
|
||
|
BuildCmdLineOptions({ARG_OLD_COMPACTION_STYLE,
|
||
|
ARG_NEW_COMPACTION_STYLE})),
|
||
|
old_compaction_style_(-1),
|
||
|
new_compaction_style_(-1) {
|
||
|
|
||
10 years ago
|
ParseIntOption(option_map_, ARG_OLD_COMPACTION_STYLE, old_compaction_style_,
|
||
|
exec_state_);
|
||
11 years ago
|
if (old_compaction_style_ != kCompactionStyleLevel &&
|
||
|
old_compaction_style_ != kCompactionStyleUniversal) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(
|
||
10 years ago
|
"Use --" + ARG_OLD_COMPACTION_STYLE + " to specify old compaction " +
|
||
|
"style. Check ldb help for proper compaction style value.\n");
|
||
11 years ago
|
return;
|
||
|
}
|
||
|
|
||
10 years ago
|
ParseIntOption(option_map_, ARG_NEW_COMPACTION_STYLE, new_compaction_style_,
|
||
|
exec_state_);
|
||
11 years ago
|
if (new_compaction_style_ != kCompactionStyleLevel &&
|
||
|
new_compaction_style_ != kCompactionStyleUniversal) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(
|
||
10 years ago
|
"Use --" + ARG_NEW_COMPACTION_STYLE + " to specify new compaction " +
|
||
|
"style. Check ldb help for proper compaction style value.\n");
|
||
11 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
if (new_compaction_style_ == old_compaction_style_) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(
|
||
10 years ago
|
"Old compaction style is the same as new compaction style. "
|
||
|
"Nothing to do.\n");
|
||
11 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
if (old_compaction_style_ == kCompactionStyleUniversal &&
|
||
|
new_compaction_style_ == kCompactionStyleLevel) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(
|
||
10 years ago
|
"Convert from universal compaction to level compaction. "
|
||
|
"Nothing to do.\n");
|
||
11 years ago
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void ChangeCompactionStyleCommand::Help(string& ret) {
|
||
|
ret.append(" ");
|
||
|
ret.append(ChangeCompactionStyleCommand::Name());
|
||
|
ret.append(" --" + ARG_OLD_COMPACTION_STYLE + "=<Old compaction style: 0 " +
|
||
|
"for level compaction, 1 for universal compaction>");
|
||
|
ret.append(" --" + ARG_NEW_COMPACTION_STYLE + "=<New compaction style: 0 " +
|
||
|
"for level compaction, 1 for universal compaction>");
|
||
|
ret.append("\n");
|
||
11 years ago
|
}
|
||
|
|
||
|
Options ChangeCompactionStyleCommand::PrepareOptionsForOpenDB() {
|
||
|
Options opt = LDBCommand::PrepareOptionsForOpenDB();
|
||
|
|
||
|
if (old_compaction_style_ == kCompactionStyleLevel &&
|
||
|
new_compaction_style_ == kCompactionStyleUniversal) {
|
||
|
// In order to convert from level compaction to universal compaction, we
|
||
|
// need to compact all data into a single file and move it to level 0.
|
||
|
opt.disable_auto_compactions = true;
|
||
|
opt.target_file_size_base = INT_MAX;
|
||
|
opt.target_file_size_multiplier = 1;
|
||
|
opt.max_bytes_for_level_base = INT_MAX;
|
||
|
opt.max_bytes_for_level_multiplier = 1;
|
||
|
}
|
||
|
|
||
|
return opt;
|
||
|
}
|
||
|
|
||
|
void ChangeCompactionStyleCommand::DoCommand() {
|
||
|
// print db stats before we have made any change
|
||
|
std::string property;
|
||
|
std::string files_per_level;
|
||
|
for (int i = 0; i < db_->NumberLevels(); i++) {
|
||
11 years ago
|
db_->GetProperty("rocksdb.num-files-at-level" + NumberToString(i),
|
||
11 years ago
|
&property);
|
||
|
|
||
10 years ago
|
// format print string
|
||
11 years ago
|
char buf[100];
|
||
10 years ago
|
snprintf(buf, sizeof(buf), "%s%s", (i ? "," : ""), property.c_str());
|
||
11 years ago
|
files_per_level += buf;
|
||
|
}
|
||
|
fprintf(stdout, "files per level before compaction: %s\n",
|
||
|
files_per_level.c_str());
|
||
|
|
||
|
// manual compact into a single file and move the file to level 0
|
||
10 years ago
|
CompactRangeOptions compact_options;
|
||
|
compact_options.change_level = true;
|
||
|
compact_options.target_level = 0;
|
||
|
db_->CompactRange(compact_options, nullptr, nullptr);
|
||
11 years ago
|
|
||
|
// verify compaction result
|
||
|
files_per_level = "";
|
||
|
int num_files = 0;
|
||
|
for (int i = 0; i < db_->NumberLevels(); i++) {
|
||
11 years ago
|
db_->GetProperty("rocksdb.num-files-at-level" + NumberToString(i),
|
||
11 years ago
|
&property);
|
||
|
|
||
10 years ago
|
// format print string
|
||
11 years ago
|
char buf[100];
|
||
10 years ago
|
snprintf(buf, sizeof(buf), "%s%s", (i ? "," : ""), property.c_str());
|
||
11 years ago
|
files_per_level += buf;
|
||
|
|
||
|
num_files = atoi(property.c_str());
|
||
|
|
||
|
// level 0 should have only 1 file
|
||
|
if (i == 0 && num_files != 1) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(
|
||
|
"Number of db files at "
|
||
|
"level 0 after compaction is " +
|
||
|
ToString(num_files) + ", not 1.\n");
|
||
11 years ago
|
return;
|
||
|
}
|
||
|
// other levels should have no file
|
||
|
if (i > 0 && num_files != 0) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(
|
||
|
"Number of db files at "
|
||
|
"level " +
|
||
|
ToString(i) + " after compaction is " + ToString(num_files) +
|
||
|
", not 0.\n");
|
||
11 years ago
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fprintf(stdout, "files per level after compaction: %s\n",
|
||
|
files_per_level.c_str());
|
||
|
}
|
||
|
|
||
10 years ago
|
// ----------------------------------------------------------------------------
|
||
|
|
||
|
namespace {
|
||
|
|
||
|
struct StdErrReporter : public log::Reader::Reporter {
|
||
10 years ago
|
virtual void Corruption(size_t bytes, const Status& s) override {
|
||
10 years ago
|
cerr << "Corruption detected in log file " << s.ToString() << "\n";
|
||
|
}
|
||
|
};
|
||
|
|
||
12 years ago
|
class InMemoryHandler : public WriteBatch::Handler {
|
||
|
public:
|
||
10 years ago
|
InMemoryHandler(stringstream& row, bool print_values) : Handler(), row_(row) {
|
||
11 years ago
|
print_values_ = print_values;
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
void commonPutMerge(const Slice& key, const Slice& value) {
|
||
10 years ago
|
string k = LDBCommand::StringToHex(key.ToString());
|
||
11 years ago
|
if (print_values_) {
|
||
10 years ago
|
string v = LDBCommand::StringToHex(value.ToString());
|
||
11 years ago
|
row_ << k << " : ";
|
||
|
row_ << v << " ";
|
||
|
} else {
|
||
|
row_ << k << " ";
|
||
|
}
|
||
12 years ago
|
}
|
||
11 years ago
|
|
||
10 years ago
|
virtual void Put(const Slice& key, const Slice& value) override {
|
||
11 years ago
|
row_ << "PUT : ";
|
||
|
commonPutMerge(key, value);
|
||
12 years ago
|
}
|
||
|
|
||
10 years ago
|
virtual void Merge(const Slice& key, const Slice& value) override {
|
||
11 years ago
|
row_ << "MERGE : ";
|
||
|
commonPutMerge(key, value);
|
||
12 years ago
|
}
|
||
11 years ago
|
|
||
10 years ago
|
virtual void Delete(const Slice& key) override {
|
||
10 years ago
|
row_ <<",DELETE : ";
|
||
11 years ago
|
row_ << LDBCommand::StringToHex(key.ToString()) << " ";
|
||
12 years ago
|
}
|
||
|
|
||
10 years ago
|
virtual ~InMemoryHandler() {}
|
||
11 years ago
|
|
||
12 years ago
|
private:
|
||
10 years ago
|
stringstream & row_;
|
||
11 years ago
|
bool print_values_;
|
||
12 years ago
|
};
|
||
|
|
||
10 years ago
|
void DumpWalFile(std::string wal_file, bool print_header, bool print_values,
|
||
|
LDBCommandExecuteResult* exec_state) {
|
||
|
Env* env_ = Env::Default();
|
||
|
EnvOptions soptions;
|
||
9 years ago
|
unique_ptr<SequentialFileReader> wal_file_reader;
|
||
|
|
||
|
Status status;
|
||
|
{
|
||
|
unique_ptr<SequentialFile> file;
|
||
|
status = env_->NewSequentialFile(wal_file, &file, soptions);
|
||
|
if (status.ok()) {
|
||
|
wal_file_reader.reset(new SequentialFileReader(std::move(file)));
|
||
|
}
|
||
|
}
|
||
10 years ago
|
if (!status.ok()) {
|
||
|
if (exec_state) {
|
||
10 years ago
|
*exec_state = LDBCommandExecuteResult::Failed("Failed to open WAL file " +
|
||
10 years ago
|
status.ToString());
|
||
|
} else {
|
||
|
cerr << "Error: Failed to open WAL file " << status.ToString()
|
||
|
<< std::endl;
|
||
|
}
|
||
|
} else {
|
||
|
StdErrReporter reporter;
|
||
9 years ago
|
log::Reader reader(move(wal_file_reader), &reporter, true, 0);
|
||
10 years ago
|
string scratch;
|
||
|
WriteBatch batch;
|
||
|
Slice record;
|
||
|
stringstream row;
|
||
|
if (print_header) {
|
||
|
cout << "Sequence,Count,ByteSize,Physical Offset,Key(s)";
|
||
|
if (print_values) {
|
||
|
cout << " : value ";
|
||
|
}
|
||
|
cout << "\n";
|
||
|
}
|
||
|
while (reader.ReadRecord(&record, &scratch)) {
|
||
|
row.str("");
|
||
|
if (record.size() < 12) {
|
||
|
reporter.Corruption(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() << ",";
|
||
|
InMemoryHandler handler(row, print_values);
|
||
|
batch.Iterate(&handler);
|
||
|
row << "\n";
|
||
|
}
|
||
|
cout << row.str();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
} // namespace
|
||
|
|
||
10 years ago
|
const string WALDumperCommand::ARG_WAL_FILE = "walfile";
|
||
|
const string WALDumperCommand::ARG_PRINT_VALUE = "print_value";
|
||
|
const string WALDumperCommand::ARG_PRINT_HEADER = "header";
|
||
12 years ago
|
|
||
10 years ago
|
WALDumperCommand::WALDumperCommand(const vector<string>& params,
|
||
|
const map<string, string>& options, const vector<string>& flags) :
|
||
12 years ago
|
LDBCommand(options, flags, true,
|
||
12 years ago
|
BuildCmdLineOptions(
|
||
|
{ARG_WAL_FILE, ARG_PRINT_HEADER, ARG_PRINT_VALUE})),
|
||
|
print_header_(false), print_values_(false) {
|
||
12 years ago
|
|
||
12 years ago
|
wal_file_.clear();
|
||
12 years ago
|
|
||
10 years ago
|
map<string, string>::const_iterator itr = options.find(ARG_WAL_FILE);
|
||
12 years ago
|
if (itr != options.end()) {
|
||
|
wal_file_ = itr->second;
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
|
|
||
12 years ago
|
print_header_ = IsFlagPresent(flags, ARG_PRINT_HEADER);
|
||
|
print_values_ = IsFlagPresent(flags, ARG_PRINT_VALUE);
|
||
12 years ago
|
if (wal_file_.empty()) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed("Argument " + ARG_WAL_FILE +
|
||
|
" must be specified.");
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void WALDumperCommand::Help(string& ret) {
|
||
|
ret.append(" ");
|
||
|
ret.append(WALDumperCommand::Name());
|
||
|
ret.append(" --" + ARG_WAL_FILE + "=<write_ahead_log_file_path>");
|
||
|
ret.append(" [--" + ARG_PRINT_HEADER + "] ");
|
||
|
ret.append(" [--" + ARG_PRINT_VALUE + "] ");
|
||
|
ret.append("\n");
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
void WALDumperCommand::DoCommand() {
|
||
10 years ago
|
DumpWalFile(wal_file_, print_header_, print_values_, &exec_state_);
|
||
12 years ago
|
}
|
||
|
|
||
10 years ago
|
// ----------------------------------------------------------------------------
|
||
12 years ago
|
|
||
10 years ago
|
GetCommand::GetCommand(const vector<string>& params,
|
||
|
const map<string, string>& options, const vector<string>& flags) :
|
||
12 years ago
|
LDBCommand(options, flags, true, BuildCmdLineOptions({ARG_TTL, ARG_HEX,
|
||
|
ARG_KEY_HEX,
|
||
|
ARG_VALUE_HEX})) {
|
||
12 years ago
|
|
||
|
if (params.size() != 1) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(
|
||
10 years ago
|
"<key> must be specified for the get command");
|
||
12 years ago
|
} else {
|
||
|
key_ = params.at(0);
|
||
|
}
|
||
|
|
||
|
if (is_key_hex_) {
|
||
|
key_ = HexToString(key_);
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void GetCommand::Help(string& ret) {
|
||
|
ret.append(" ");
|
||
|
ret.append(GetCommand::Name());
|
||
|
ret.append(" <key>");
|
||
|
ret.append(" [--" + ARG_TTL + "]");
|
||
|
ret.append("\n");
|
||
12 years ago
|
}
|
||
|
|
||
|
void GetCommand::DoCommand() {
|
||
10 years ago
|
string value;
|
||
12 years ago
|
Status st = db_->Get(ReadOptions(), key_, &value);
|
||
12 years ago
|
if (st.ok()) {
|
||
|
fprintf(stdout, "%s\n",
|
||
|
(is_value_hex_ ? StringToHex(value) : value).c_str());
|
||
|
} else {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(st.ToString());
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
// ----------------------------------------------------------------------------
|
||
12 years ago
|
|
||
10 years ago
|
ApproxSizeCommand::ApproxSizeCommand(const vector<string>& params,
|
||
|
const map<string, string>& options, const vector<string>& flags) :
|
||
12 years ago
|
LDBCommand(options, flags, true,
|
||
|
BuildCmdLineOptions({ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX,
|
||
|
ARG_FROM, ARG_TO})) {
|
||
|
|
||
|
if (options.find(ARG_FROM) != options.end()) {
|
||
|
start_key_ = options.find(ARG_FROM)->second;
|
||
|
} else {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(
|
||
|
ARG_FROM + " must be specified for approxsize command");
|
||
12 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
if (options.find(ARG_TO) != options.end()) {
|
||
|
end_key_ = options.find(ARG_TO)->second;
|
||
|
} else {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(
|
||
|
ARG_TO + " must be specified for approxsize command");
|
||
12 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
if (is_key_hex_) {
|
||
|
start_key_ = HexToString(start_key_);
|
||
|
end_key_ = HexToString(end_key_);
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void ApproxSizeCommand::Help(string& ret) {
|
||
|
ret.append(" ");
|
||
|
ret.append(ApproxSizeCommand::Name());
|
||
|
ret.append(HelpRangeCmdArgs());
|
||
|
ret.append("\n");
|
||
12 years ago
|
}
|
||
|
|
||
|
void ApproxSizeCommand::DoCommand() {
|
||
|
|
||
12 years ago
|
Range ranges[1];
|
||
|
ranges[0] = Range(start_key_, end_key_);
|
||
12 years ago
|
uint64_t sizes[1];
|
||
|
db_->GetApproximateSizes(ranges, 1, sizes);
|
||
11 years ago
|
fprintf(stdout, "%lu\n", (unsigned long)sizes[0]);
|
||
12 years ago
|
/* Weird that GetApproximateSizes() returns void, although documentation
|
||
12 years ago
|
* says that it returns a Status object.
|
||
|
if (!st.ok()) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(st.ToString());
|
||
12 years ago
|
}
|
||
|
*/
|
||
|
}
|
||
|
|
||
10 years ago
|
// ----------------------------------------------------------------------------
|
||
12 years ago
|
|
||
10 years ago
|
BatchPutCommand::BatchPutCommand(const vector<string>& params,
|
||
|
const map<string, string>& options, const vector<string>& flags) :
|
||
|
LDBCommand(options, flags, false,
|
||
|
BuildCmdLineOptions({ARG_TTL, ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX,
|
||
|
ARG_CREATE_IF_MISSING})) {
|
||
12 years ago
|
|
||
|
if (params.size() < 2) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(
|
||
10 years ago
|
"At least one <key> <value> pair must be specified batchput.");
|
||
12 years ago
|
} else if (params.size() % 2 != 0) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(
|
||
12 years ago
|
"Equal number of <key>s and <value>s must be specified for batchput.");
|
||
|
} else {
|
||
|
for (size_t i = 0; i < params.size(); i += 2) {
|
||
10 years ago
|
string key = params.at(i);
|
||
|
string value = params.at(i+1);
|
||
|
key_values_.push_back(pair<string, string>(
|
||
12 years ago
|
is_key_hex_ ? HexToString(key) : key,
|
||
|
is_value_hex_ ? HexToString(value) : value));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void BatchPutCommand::Help(string& ret) {
|
||
|
ret.append(" ");
|
||
|
ret.append(BatchPutCommand::Name());
|
||
|
ret.append(" <key> <value> [<key> <value>] [..]");
|
||
|
ret.append(" [--" + ARG_TTL + "]");
|
||
|
ret.append("\n");
|
||
12 years ago
|
}
|
||
|
|
||
|
void BatchPutCommand::DoCommand() {
|
||
12 years ago
|
WriteBatch batch;
|
||
12 years ago
|
|
||
10 years ago
|
for (vector<pair<string, string>>::const_iterator itr
|
||
10 years ago
|
= key_values_.begin(); itr != key_values_.end(); ++itr) {
|
||
10 years ago
|
batch.Put(itr->first, itr->second);
|
||
12 years ago
|
}
|
||
12 years ago
|
Status st = db_->Write(WriteOptions(), &batch);
|
||
12 years ago
|
if (st.ok()) {
|
||
|
fprintf(stdout, "OK\n");
|
||
|
} else {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(st.ToString());
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
Options BatchPutCommand::PrepareOptionsForOpenDB() {
|
||
|
Options opt = LDBCommand::PrepareOptionsForOpenDB();
|
||
12 years ago
|
opt.create_if_missing = IsFlagPresent(flags_, ARG_CREATE_IF_MISSING);
|
||
|
return opt;
|
||
|
}
|
||
|
|
||
10 years ago
|
// ----------------------------------------------------------------------------
|
||
12 years ago
|
|
||
10 years ago
|
ScanCommand::ScanCommand(const vector<string>& params,
|
||
|
const map<string, string>& options, const vector<string>& flags) :
|
||
12 years ago
|
LDBCommand(options, flags, true,
|
||
12 years ago
|
BuildCmdLineOptions({ARG_TTL, ARG_HEX, ARG_KEY_HEX, ARG_TO,
|
||
|
ARG_VALUE_HEX, ARG_FROM, ARG_TIMESTAMP,
|
||
|
ARG_MAX_KEYS, ARG_TTL_START, ARG_TTL_END})),
|
||
12 years ago
|
start_key_specified_(false),
|
||
|
end_key_specified_(false),
|
||
|
max_keys_scanned_(-1) {
|
||
|
|
||
10 years ago
|
map<string, string>::const_iterator itr = options.find(ARG_FROM);
|
||
12 years ago
|
if (itr != options.end()) {
|
||
|
start_key_ = itr->second;
|
||
|
if (is_key_hex_) {
|
||
|
start_key_ = HexToString(start_key_);
|
||
|
}
|
||
|
start_key_specified_ = true;
|
||
|
}
|
||
|
itr = options.find(ARG_TO);
|
||
|
if (itr != options.end()) {
|
||
|
end_key_ = itr->second;
|
||
|
if (is_key_hex_) {
|
||
|
end_key_ = HexToString(end_key_);
|
||
|
}
|
||
|
end_key_specified_ = true;
|
||
|
}
|
||
|
|
||
|
itr = options.find(ARG_MAX_KEYS);
|
||
|
if (itr != options.end()) {
|
||
|
try {
|
||
10 years ago
|
#if defined(CYGWIN)
|
||
|
max_keys_scanned_ = strtol(itr->second.c_str(), 0, 10);
|
||
|
#else
|
||
12 years ago
|
max_keys_scanned_ = stoi(itr->second);
|
||
10 years ago
|
#endif
|
||
10 years ago
|
} catch(const invalid_argument&) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(ARG_MAX_KEYS +
|
||
10 years ago
|
" has an invalid value");
|
||
10 years ago
|
} catch(const out_of_range&) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(
|
||
|
ARG_MAX_KEYS + " has a value out-of-range");
|
||
12 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
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 + "=<N>q] ");
|
||
|
ret.append(" [--" + ARG_TTL_START + "=<N>:- is inclusive]");
|
||
|
ret.append(" [--" + ARG_TTL_END + "=<N>:- is exclusive]");
|
||
|
ret.append("\n");
|
||
12 years ago
|
}
|
||
|
|
||
|
void ScanCommand::DoCommand() {
|
||
|
|
||
|
int num_keys_scanned = 0;
|
||
12 years ago
|
Iterator* it = db_->NewIterator(ReadOptions());
|
||
12 years ago
|
if (start_key_specified_) {
|
||
|
it->Seek(start_key_);
|
||
|
} else {
|
||
|
it->SeekToFirst();
|
||
|
}
|
||
12 years ago
|
int ttl_start;
|
||
10 years ago
|
if (!ParseIntOption(option_map_, ARG_TTL_START, ttl_start, exec_state_)) {
|
||
11 years ago
|
ttl_start = DBWithTTLImpl::kMinTimestamp; // TTL introduction time
|
||
12 years ago
|
}
|
||
|
int ttl_end;
|
||
10 years ago
|
if (!ParseIntOption(option_map_, ARG_TTL_END, ttl_end, exec_state_)) {
|
||
11 years ago
|
ttl_end = DBWithTTLImpl::kMaxTimestamp; // Max time allowed by TTL feature
|
||
12 years ago
|
}
|
||
|
if (ttl_end < ttl_start) {
|
||
|
fprintf(stderr, "Error: End time can't be less than start time\n");
|
||
|
delete it;
|
||
|
return;
|
||
|
}
|
||
|
if (is_db_ttl_ && timestamp_) {
|
||
|
fprintf(stdout, "Scanning key-values from %s to %s\n",
|
||
|
ReadableTime(ttl_start).c_str(), ReadableTime(ttl_end).c_str());
|
||
|
}
|
||
12 years ago
|
for ( ;
|
||
10 years ago
|
it->Valid() && (!end_key_specified_ || it->key().ToString() < end_key_);
|
||
|
it->Next()) {
|
||
|
string key = ldb_options_.key_formatter->Format(it->key());
|
||
12 years ago
|
if (is_db_ttl_) {
|
||
11 years ago
|
TtlIterator* it_ttl = dynamic_cast<TtlIterator*>(it);
|
||
|
assert(it_ttl);
|
||
12 years ago
|
int rawtime = it_ttl->timestamp();
|
||
|
if (rawtime < ttl_start || rawtime >= ttl_end) {
|
||
12 years ago
|
continue;
|
||
|
}
|
||
|
if (timestamp_) {
|
||
|
fprintf(stdout, "%s ", ReadableTime(rawtime).c_str());
|
||
|
}
|
||
|
}
|
||
10 years ago
|
string value = it->value().ToString();
|
||
12 years ago
|
fprintf(stdout, "%s : %s\n",
|
||
11 years ago
|
(is_key_hex_ ? "0x" + it->key().ToString(true) : key).c_str(),
|
||
10 years ago
|
(is_value_hex_ ? StringToHex(value) : value).c_str()
|
||
|
);
|
||
12 years ago
|
num_keys_scanned++;
|
||
|
if (max_keys_scanned_ >= 0 && num_keys_scanned >= max_keys_scanned_) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
if (!it->status().ok()) { // Check for any errors found during the scan
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(it->status().ToString());
|
||
12 years ago
|
}
|
||
|
delete it;
|
||
|
}
|
||
|
|
||
10 years ago
|
// ----------------------------------------------------------------------------
|
||
12 years ago
|
|
||
10 years ago
|
DeleteCommand::DeleteCommand(const vector<string>& params,
|
||
|
const map<string, string>& options, const vector<string>& flags) :
|
||
12 years ago
|
LDBCommand(options, flags, false,
|
||
|
BuildCmdLineOptions({ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX})) {
|
||
|
|
||
|
if (params.size() != 1) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(
|
||
10 years ago
|
"KEY must be specified for the delete command");
|
||
12 years ago
|
} else {
|
||
|
key_ = params.at(0);
|
||
|
if (is_key_hex_) {
|
||
|
key_ = HexToString(key_);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void DeleteCommand::Help(string& ret) {
|
||
|
ret.append(" ");
|
||
|
ret.append(DeleteCommand::Name() + " <key>");
|
||
|
ret.append("\n");
|
||
12 years ago
|
}
|
||
|
|
||
|
void DeleteCommand::DoCommand() {
|
||
12 years ago
|
Status st = db_->Delete(WriteOptions(), key_);
|
||
12 years ago
|
if (st.ok()) {
|
||
|
fprintf(stdout, "OK\n");
|
||
|
} else {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(st.ToString());
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
|
|
||
10 years ago
|
PutCommand::PutCommand(const vector<string>& params,
|
||
|
const map<string, string>& options, const vector<string>& flags) :
|
||
12 years ago
|
LDBCommand(options, flags, false,
|
||
12 years ago
|
BuildCmdLineOptions({ARG_TTL, ARG_HEX, ARG_KEY_HEX, ARG_VALUE_HEX,
|
||
12 years ago
|
ARG_CREATE_IF_MISSING})) {
|
||
|
|
||
|
if (params.size() != 2) {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(
|
||
10 years ago
|
"<key> and <value> must be specified for the put command");
|
||
12 years ago
|
} else {
|
||
|
key_ = params.at(0);
|
||
|
value_ = params.at(1);
|
||
|
}
|
||
|
|
||
|
if (is_key_hex_) {
|
||
|
key_ = HexToString(key_);
|
||
|
}
|
||
|
|
||
|
if (is_value_hex_) {
|
||
|
value_ = HexToString(value_);
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void PutCommand::Help(string& ret) {
|
||
|
ret.append(" ");
|
||
|
ret.append(PutCommand::Name());
|
||
|
ret.append(" <key> <value> ");
|
||
|
ret.append(" [--" + ARG_TTL + "]");
|
||
|
ret.append("\n");
|
||
12 years ago
|
}
|
||
|
|
||
|
void PutCommand::DoCommand() {
|
||
12 years ago
|
Status st = db_->Put(WriteOptions(), key_, value_);
|
||
12 years ago
|
if (st.ok()) {
|
||
|
fprintf(stdout, "OK\n");
|
||
|
} else {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(st.ToString());
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
Options PutCommand::PrepareOptionsForOpenDB() {
|
||
|
Options opt = LDBCommand::PrepareOptionsForOpenDB();
|
||
12 years ago
|
opt.create_if_missing = IsFlagPresent(flags_, ARG_CREATE_IF_MISSING);
|
||
|
return opt;
|
||
|
}
|
||
|
|
||
10 years ago
|
// ----------------------------------------------------------------------------
|
||
12 years ago
|
|
||
|
const char* DBQuerierCommand::HELP_CMD = "help";
|
||
|
const char* DBQuerierCommand::GET_CMD = "get";
|
||
|
const char* DBQuerierCommand::PUT_CMD = "put";
|
||
|
const char* DBQuerierCommand::DELETE_CMD = "delete";
|
||
|
|
||
10 years ago
|
DBQuerierCommand::DBQuerierCommand(const vector<string>& params,
|
||
|
const map<string, string>& options, const vector<string>& flags) :
|
||
12 years ago
|
LDBCommand(options, flags, false,
|
||
12 years ago
|
BuildCmdLineOptions({ARG_TTL, ARG_HEX, ARG_KEY_HEX,
|
||
|
ARG_VALUE_HEX})) {
|
||
12 years ago
|
|
||
|
}
|
||
|
|
||
10 years ago
|
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 "
|
||
12 years ago
|
"commands.");
|
||
10 years ago
|
ret.append("\n");
|
||
12 years ago
|
}
|
||
|
|
||
|
void DBQuerierCommand::DoCommand() {
|
||
|
if (!db_) {
|
||
|
return;
|
||
|
}
|
||
10 years ago
|
|
||
12 years ago
|
ReadOptions read_options;
|
||
|
WriteOptions write_options;
|
||
12 years ago
|
|
||
10 years ago
|
string line;
|
||
|
string key;
|
||
|
string value;
|
||
|
while (getline(cin, line, '\n')) {
|
||
|
|
||
|
// Parse line into vector<string>
|
||
|
vector<string> tokens;
|
||
12 years ago
|
size_t pos = 0;
|
||
|
while (true) {
|
||
|
size_t pos2 = line.find(' ', pos);
|
||
10 years ago
|
if (pos2 == string::npos) {
|
||
12 years ago
|
break;
|
||
|
}
|
||
|
tokens.push_back(line.substr(pos, pos2-pos));
|
||
|
pos = pos2 + 1;
|
||
|
}
|
||
|
tokens.push_back(line.substr(pos));
|
||
|
|
||
10 years ago
|
const string& cmd = tokens[0];
|
||
12 years ago
|
|
||
|
if (cmd == HELP_CMD) {
|
||
|
fprintf(stdout,
|
||
|
"get <key>\n"
|
||
|
"put <key> <value>\n"
|
||
|
"delete <key>\n");
|
||
|
} else if (cmd == DELETE_CMD && tokens.size() == 2) {
|
||
|
key = (is_key_hex_ ? HexToString(tokens[1]) : tokens[1]);
|
||
|
db_->Delete(write_options, Slice(key));
|
||
|
fprintf(stdout, "Successfully deleted %s\n", tokens[1].c_str());
|
||
|
} else if (cmd == PUT_CMD && tokens.size() == 3) {
|
||
|
key = (is_key_hex_ ? HexToString(tokens[1]) : tokens[1]);
|
||
|
value = (is_value_hex_ ? HexToString(tokens[2]) : tokens[2]);
|
||
|
db_->Put(write_options, Slice(key), Slice(value));
|
||
|
fprintf(stdout, "Successfully put %s %s\n",
|
||
|
tokens[1].c_str(), tokens[2].c_str());
|
||
|
} else if (cmd == GET_CMD && tokens.size() == 2) {
|
||
|
key = (is_key_hex_ ? HexToString(tokens[1]) : tokens[1]);
|
||
|
if (db_->Get(read_options, Slice(key), &value).ok()) {
|
||
|
fprintf(stdout, "%s\n", PrintKeyValue(key, value,
|
||
|
is_key_hex_, is_value_hex_).c_str());
|
||
|
} else {
|
||
|
fprintf(stdout, "Not found %s\n", tokens[1].c_str());
|
||
|
}
|
||
|
} else {
|
||
|
fprintf(stdout, "Unknown command %s\n", line.c_str());
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
// ----------------------------------------------------------------------------
|
||
|
|
||
10 years ago
|
CheckConsistencyCommand::CheckConsistencyCommand(const vector<string>& params,
|
||
|
const map<string, string>& options, const vector<string>& flags) :
|
||
11 years ago
|
LDBCommand(options, flags, false,
|
||
|
BuildCmdLineOptions({})) {
|
||
|
}
|
||
|
|
||
10 years ago
|
void CheckConsistencyCommand::Help(string& ret) {
|
||
|
ret.append(" ");
|
||
|
ret.append(CheckConsistencyCommand::Name());
|
||
|
ret.append("\n");
|
||
11 years ago
|
}
|
||
12 years ago
|
|
||
11 years ago
|
void CheckConsistencyCommand::DoCommand() {
|
||
|
Options opt = PrepareOptionsForOpenDB();
|
||
11 years ago
|
opt.paranoid_checks = true;
|
||
11 years ago
|
if (!exec_state_.IsNotStarted()) {
|
||
|
return;
|
||
|
}
|
||
11 years ago
|
DB* db;
|
||
|
Status st = DB::OpenForReadOnly(opt, db_path_, &db, false);
|
||
|
delete db;
|
||
11 years ago
|
if (st.ok()) {
|
||
|
fprintf(stdout, "OK\n");
|
||
|
} else {
|
||
10 years ago
|
exec_state_ = LDBCommandExecuteResult::Failed(st.ToString());
|
||
11 years ago
|
}
|
||
12 years ago
|
}
|
||
11 years ago
|
|
||
10 years ago
|
// ----------------------------------------------------------------------------
|
||
|
|
||
|
namespace {
|
||
|
|
||
|
void DumpSstFile(std::string filename, bool output_hex, bool show_properties) {
|
||
|
std::string from_key;
|
||
|
std::string to_key;
|
||
|
if (filename.length() <= 4 ||
|
||
|
filename.rfind(".sst") != filename.length() - 4) {
|
||
|
std::cout << "Invalid sst file name." << std::endl;
|
||
|
return;
|
||
|
}
|
||
|
// no verification
|
||
|
rocksdb::SstFileReader reader(filename, false, output_hex);
|
||
|
Status st = reader.ReadSequential(true, -1, false, // has_from
|
||
|
from_key, false, // has_to
|
||
|
to_key);
|
||
|
if (!st.ok()) {
|
||
|
std::cerr << "Error in reading SST file " << filename << st.ToString()
|
||
|
<< std::endl;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (show_properties) {
|
||
|
const rocksdb::TableProperties* table_properties;
|
||
|
|
||
|
std::shared_ptr<const rocksdb::TableProperties>
|
||
|
table_properties_from_reader;
|
||
|
st = reader.ReadTableProperties(&table_properties_from_reader);
|
||
|
if (!st.ok()) {
|
||
|
std::cerr << filename << ": " << st.ToString()
|
||
|
<< ". Try to use initial table properties" << std::endl;
|
||
|
table_properties = reader.GetInitTableProperties();
|
||
|
} else {
|
||
|
table_properties = table_properties_from_reader.get();
|
||
|
}
|
||
|
if (table_properties != nullptr) {
|
||
|
std::cout << std::endl << "Table Properties:" << std::endl;
|
||
|
std::cout << table_properties->ToString("\n") << std::endl;
|
||
|
std::cout << "# deleted keys: "
|
||
|
<< rocksdb::GetDeletedKeys(
|
||
|
table_properties->user_collected_properties)
|
||
|
<< std::endl;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
} // namespace
|
||
|
|
||
|
DBFileDumperCommand::DBFileDumperCommand(const vector<string>& params,
|
||
|
const map<string, string>& options,
|
||
|
const vector<string>& flags)
|
||
|
: LDBCommand(options, flags, true, BuildCmdLineOptions({})) {}
|
||
|
|
||
|
void DBFileDumperCommand::Help(string& ret) {
|
||
|
ret.append(" ");
|
||
|
ret.append(DBFileDumperCommand::Name());
|
||
|
ret.append("\n");
|
||
|
}
|
||
|
|
||
|
void DBFileDumperCommand::DoCommand() {
|
||
|
if (!db_) {
|
||
|
return;
|
||
|
}
|
||
|
Status s;
|
||
|
|
||
|
std::cout << "Manifest File" << std::endl;
|
||
|
std::cout << "==============================" << std::endl;
|
||
|
std::string manifest_filename;
|
||
|
s = ReadFileToString(db_->GetEnv(), CurrentFileName(db_->GetName()),
|
||
|
&manifest_filename);
|
||
|
if (!s.ok() || manifest_filename.empty() ||
|
||
|
manifest_filename.back() != '\n') {
|
||
|
std::cerr << "Error when reading CURRENT file "
|
||
|
<< CurrentFileName(db_->GetName()) << std::endl;
|
||
|
}
|
||
|
// remove the trailing '\n'
|
||
|
manifest_filename.resize(manifest_filename.size() - 1);
|
||
|
string manifest_filepath = db_->GetName() + "/" + manifest_filename;
|
||
|
std::cout << manifest_filepath << std::endl;
|
||
9 years ago
|
DumpManifestFile(manifest_filepath, false, false, false);
|
||
10 years ago
|
std::cout << std::endl;
|
||
|
|
||
|
std::cout << "SST Files" << std::endl;
|
||
|
std::cout << "==============================" << std::endl;
|
||
|
std::vector<LiveFileMetaData> metadata;
|
||
|
db_->GetLiveFilesMetaData(&metadata);
|
||
|
for (auto& fileMetadata : metadata) {
|
||
|
std::string filename = fileMetadata.db_path + fileMetadata.name;
|
||
|
std::cout << filename << " level:" << fileMetadata.level << std::endl;
|
||
|
std::cout << "------------------------------" << std::endl;
|
||
|
DumpSstFile(filename, false, true);
|
||
|
std::cout << std::endl;
|
||
|
}
|
||
|
std::cout << std::endl;
|
||
|
|
||
|
std::cout << "Write Ahead Log Files" << std::endl;
|
||
|
std::cout << "==============================" << std::endl;
|
||
|
rocksdb::VectorLogPtr wal_files;
|
||
|
s = db_->GetSortedWalFiles(wal_files);
|
||
|
if (!s.ok()) {
|
||
|
std::cerr << "Error when getting WAL files" << std::endl;
|
||
|
} else {
|
||
|
for (auto& wal : wal_files) {
|
||
|
// TODO(qyang): option.wal_dir should be passed into ldb command
|
||
|
std::string filename = db_->GetOptions().wal_dir + wal->PathName();
|
||
|
std::cout << filename << std::endl;
|
||
|
DumpWalFile(filename, true, true, &exec_state_);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
} // namespace rocksdb
|
||
11 years ago
|
#endif // ROCKSDB_LITE
|