commit
5f91868cee
@ -0,0 +1,70 @@ |
||||
// Copyright (c) 2012 Facebook. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "db/version_set.h" |
||||
|
||||
#include <algorithm> |
||||
#include <stdio.h> |
||||
#include "db/log_reader.h" |
||||
#include "db/log_writer.h" |
||||
#include "util/logging.h" |
||||
|
||||
namespace leveldb { |
||||
|
||||
Status VersionSet::ReduceNumberOfLevels(int new_levels, port::Mutex* mu) { |
||||
|
||||
if(new_levels <= 1) { |
||||
return Status::InvalidArgument( |
||||
"Number of levels needs to be bigger than 1"); |
||||
} |
||||
|
||||
Version* current_version = current_; |
||||
int current_levels = NumberLevels(); |
||||
|
||||
// Make sure there are file only on one level from
|
||||
// (new_levels-1) to (current_levels-1)
|
||||
int first_nonempty_level = -1; |
||||
int first_nonempty_level_filenum = 0; |
||||
for (int i = new_levels - 1; i < current_levels; i++) { |
||||
int file_num = NumLevelFiles(i); |
||||
if (file_num != 0) { |
||||
if (first_nonempty_level < 0) { |
||||
first_nonempty_level = i; |
||||
first_nonempty_level_filenum = file_num; |
||||
} else { |
||||
char msg[255]; |
||||
sprintf(msg, "Found at least two levels containing files: " |
||||
"[%d:%d],[%d:%d].\n", |
||||
first_nonempty_level, first_nonempty_level_filenum, i, file_num); |
||||
return Status::InvalidArgument(msg); |
||||
} |
||||
} |
||||
} |
||||
|
||||
Status st; |
||||
std::vector<FileMetaData*>* old_files_list = current_version->files_; |
||||
std::vector<FileMetaData*>* new_files_list = |
||||
new std::vector<FileMetaData*>[new_levels]; |
||||
for (int i = 0; i < new_levels - 1; i++) { |
||||
new_files_list[i] = old_files_list[i]; |
||||
} |
||||
|
||||
if (first_nonempty_level > 0) { |
||||
new_files_list[new_levels - 1] = old_files_list[first_nonempty_level]; |
||||
} |
||||
|
||||
delete[] current_version->files_; |
||||
current_version->files_ = new_files_list; |
||||
|
||||
delete[] compact_pointer_; |
||||
delete[] max_file_size_; |
||||
delete[] level_max_bytes_; |
||||
num_levels_ = new_levels; |
||||
compact_pointer_ = new std::string[new_levels]; |
||||
Init(new_levels); |
||||
st = LogAndApply(new VersionEdit(new_levels), mu, true); |
||||
return st; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,200 @@ |
||||
// Copyright (c) 2012 Facebook. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
|
||||
#include "leveldb/db.h" |
||||
#include "db/db_impl.h" |
||||
#include "db/version_set.h" |
||||
#include "util/logging.h" |
||||
#include "util/testutil.h" |
||||
#include "util/testharness.h" |
||||
#include "util/ldb_cmd.h" |
||||
|
||||
namespace leveldb { |
||||
|
||||
class ReduceLevelTest { |
||||
public: |
||||
ReduceLevelTest() { |
||||
dbname_ = test::TmpDir() + "/db_reduce_levels_test"; |
||||
DestroyDB(dbname_, Options()); |
||||
db_ = NULL; |
||||
} |
||||
|
||||
Status OpenDB(bool create_if_missing, int levels, |
||||
int mem_table_compact_level); |
||||
|
||||
Status Put(const std::string& k, const std::string& v) { |
||||
return db_->Put(WriteOptions(), k, v); |
||||
} |
||||
|
||||
std::string Get(const std::string& k) { |
||||
ReadOptions options; |
||||
std::string result; |
||||
Status s = db_->Get(options, k, &result); |
||||
if (s.IsNotFound()) { |
||||
result = "NOT_FOUND"; |
||||
} else if (!s.ok()) { |
||||
result = s.ToString(); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
Status CompactMemTable() { |
||||
if (db_ == NULL) { |
||||
return Status::InvalidArgument("DB not opened."); |
||||
} |
||||
DBImpl* db_impl = reinterpret_cast<DBImpl*>(db_); |
||||
return db_impl->TEST_CompactMemTable(); |
||||
} |
||||
|
||||
void CloseDB() { |
||||
if (db_ != NULL) { |
||||
delete db_; |
||||
db_ = NULL; |
||||
} |
||||
} |
||||
|
||||
bool ReduceLevels(int target_level); |
||||
|
||||
int FilesOnLevel(int level) { |
||||
std::string property; |
||||
ASSERT_TRUE( |
||||
db_->GetProperty("leveldb.num-files-at-level" + NumberToString(level), |
||||
&property)); |
||||
return atoi(property.c_str()); |
||||
} |
||||
|
||||
private: |
||||
std::string dbname_; |
||||
DB* db_; |
||||
}; |
||||
|
||||
Status ReduceLevelTest::OpenDB(bool create_if_missing, int num_levels, |
||||
int mem_table_compact_level) { |
||||
leveldb::Options opt; |
||||
opt.num_levels = num_levels; |
||||
opt.create_if_missing = create_if_missing; |
||||
opt.max_mem_compaction_level = mem_table_compact_level; |
||||
leveldb::Status st = leveldb::DB::Open(opt, dbname_, &db_); |
||||
if (!st.ok()) { |
||||
fprintf(stderr, "Can't open the db:%s\n", st.ToString().c_str()); |
||||
} |
||||
return st; |
||||
} |
||||
|
||||
bool ReduceLevelTest::ReduceLevels(int target_level) { |
||||
std::vector<std::string> args = leveldb::ReduceDBLevels::PrepareArgs( |
||||
target_level, false); |
||||
ReduceDBLevels level_reducer(dbname_, args); |
||||
level_reducer.Run(); |
||||
return level_reducer.GetExecuteState().IsSucceed(); |
||||
} |
||||
|
||||
TEST(ReduceLevelTest, Last_Level) { |
||||
// create files on all levels;
|
||||
ASSERT_OK(OpenDB(true, 4, 3)); |
||||
ASSERT_OK(Put("aaaa", "11111")); |
||||
ASSERT_OK(CompactMemTable()); |
||||
ASSERT_EQ(FilesOnLevel(3), 1); |
||||
CloseDB(); |
||||
|
||||
ASSERT_TRUE(ReduceLevels(3)); |
||||
ASSERT_OK(OpenDB(true, 3, 1)); |
||||
ASSERT_EQ(FilesOnLevel(2), 1); |
||||
CloseDB(); |
||||
|
||||
ASSERT_TRUE(ReduceLevels(2)); |
||||
ASSERT_OK(OpenDB(true, 2, 1)); |
||||
ASSERT_EQ(FilesOnLevel(1), 1); |
||||
CloseDB(); |
||||
} |
||||
|
||||
TEST(ReduceLevelTest, Top_Level) { |
||||
// create files on all levels;
|
||||
ASSERT_OK(OpenDB(true, 5, 0)); |
||||
ASSERT_OK(Put("aaaa", "11111")); |
||||
ASSERT_OK(CompactMemTable()); |
||||
ASSERT_EQ(FilesOnLevel(0), 1); |
||||
CloseDB(); |
||||
|
||||
// The CompactRange(NULL, NULL) call in ReduceLevels
|
||||
// will push this file to level-1
|
||||
ASSERT_TRUE(ReduceLevels(4)); |
||||
ASSERT_OK(OpenDB(true, 4, 0)); |
||||
ASSERT_EQ(FilesOnLevel(1), 1); |
||||
CloseDB(); |
||||
|
||||
ASSERT_TRUE(ReduceLevels(3)); |
||||
ASSERT_OK(OpenDB(true, 3, 0)); |
||||
ASSERT_EQ(FilesOnLevel(1), 1); |
||||
CloseDB(); |
||||
|
||||
ASSERT_TRUE(ReduceLevels(2)); |
||||
ASSERT_OK(OpenDB(true, 2, 0)); |
||||
ASSERT_EQ(FilesOnLevel(1), 1); |
||||
CloseDB(); |
||||
} |
||||
|
||||
TEST(ReduceLevelTest, All_Levels) { |
||||
// create files on all levels;
|
||||
ASSERT_OK(OpenDB(true, 5, 1)); |
||||
ASSERT_OK(Put("a", "a11111")); |
||||
ASSERT_OK(CompactMemTable()); |
||||
ASSERT_EQ(FilesOnLevel(1), 1); |
||||
CloseDB(); |
||||
|
||||
ASSERT_OK(OpenDB(true, 5, 2)); |
||||
ASSERT_OK(Put("b", "b11111")); |
||||
ASSERT_OK(CompactMemTable()); |
||||
ASSERT_EQ(FilesOnLevel(1), 1); |
||||
ASSERT_EQ(FilesOnLevel(2), 1); |
||||
CloseDB(); |
||||
|
||||
ASSERT_OK(OpenDB(true, 5, 3)); |
||||
ASSERT_OK(Put("c", "c11111")); |
||||
ASSERT_OK(CompactMemTable()); |
||||
ASSERT_EQ(FilesOnLevel(1), 1); |
||||
ASSERT_EQ(FilesOnLevel(2), 1); |
||||
ASSERT_EQ(FilesOnLevel(3), 1); |
||||
CloseDB(); |
||||
|
||||
ASSERT_OK(OpenDB(true, 5, 4)); |
||||
ASSERT_OK(Put("d", "d11111")); |
||||
ASSERT_OK(CompactMemTable()); |
||||
ASSERT_EQ(FilesOnLevel(1), 1); |
||||
ASSERT_EQ(FilesOnLevel(2), 1); |
||||
ASSERT_EQ(FilesOnLevel(3), 1); |
||||
ASSERT_EQ(FilesOnLevel(4), 1); |
||||
CloseDB(); |
||||
|
||||
ASSERT_TRUE(ReduceLevels(4)); |
||||
ASSERT_OK(OpenDB(true, 4, 0)); |
||||
ASSERT_EQ("a11111", Get("a")); |
||||
ASSERT_EQ("b11111", Get("b")); |
||||
ASSERT_EQ("c11111", Get("c")); |
||||
ASSERT_EQ("d11111", Get("d")); |
||||
CloseDB(); |
||||
|
||||
ASSERT_TRUE(ReduceLevels(3)); |
||||
ASSERT_OK(OpenDB(true, 3, 0)); |
||||
ASSERT_EQ("a11111", Get("a")); |
||||
ASSERT_EQ("b11111", Get("b")); |
||||
ASSERT_EQ("c11111", Get("c")); |
||||
ASSERT_EQ("d11111", Get("d")); |
||||
CloseDB(); |
||||
|
||||
ASSERT_TRUE(ReduceLevels(2)); |
||||
ASSERT_OK(OpenDB(true, 2, 0)); |
||||
ASSERT_EQ("a11111", Get("a")); |
||||
ASSERT_EQ("b11111", Get("b")); |
||||
ASSERT_EQ("c11111", Get("c")); |
||||
ASSERT_EQ("d11111", Get("d")); |
||||
CloseDB(); |
||||
} |
||||
|
||||
} |
||||
|
||||
int main(int argc, char** argv) { |
||||
return leveldb::test::RunAllTests(); |
||||
} |
@ -0,0 +1,294 @@ |
||||
// Copyright (c) 2012 Facebook. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "util/ldb_cmd.h" |
||||
|
||||
namespace leveldb { |
||||
|
||||
const char* LDBCommand::FROM_ARG = "--from="; |
||||
const char* LDBCommand::END_ARG = "--to="; |
||||
const char* LDBCommand::HEX_ARG = "--hex"; |
||||
|
||||
Compactor::Compactor(std::string& db_name, std::vector<std::string>& args) : |
||||
LDBCommand(db_name, args), null_from_(true), null_to_(true), hex_(false) { |
||||
for (int i = 0; i < args.size(); i++) { |
||||
std::string& arg = args.at(i); |
||||
if (arg.find(FROM_ARG) == 0) { |
||||
null_from_ = false; |
||||
from_ = arg.substr(strlen(FROM_ARG)); |
||||
} else if (arg.find(END_ARG) == 0) { |
||||
null_to_ = false; |
||||
to_ = arg.substr(strlen(END_ARG)); |
||||
} else if (arg.find(HEX_ARG) == 0) { |
||||
hex_ = true; |
||||
} else { |
||||
exec_state_ = LDBCommandExecuteResult::FAILED("Unknown argument." + arg); |
||||
} |
||||
} |
||||
|
||||
if (hex_) { |
||||
if (!null_from_) { |
||||
from_ = HexToString(from_); |
||||
} |
||||
if (!null_to_) { |
||||
to_ = HexToString(to_); |
||||
} |
||||
} |
||||
} |
||||
|
||||
void Compactor::Help(std::string& ret) { |
||||
LDBCommand::Help(ret); |
||||
ret.append("[--from=START KEY] "); |
||||
ret.append("[--to=START KEY] "); |
||||
ret.append("[--hex] "); |
||||
} |
||||
|
||||
void Compactor::DoCommand() { |
||||
|
||||
leveldb::Slice* begin = NULL; |
||||
leveldb::Slice* end = NULL; |
||||
if (!null_from_) { |
||||
begin = new leveldb::Slice(from_); |
||||
} |
||||
if (!null_to_) { |
||||
end = new leveldb::Slice(to_); |
||||
} |
||||
|
||||
db_->CompactRange(begin, end); |
||||
exec_state_ = LDBCommandExecuteResult::SUCCEED(""); |
||||
|
||||
delete begin; |
||||
delete end; |
||||
} |
||||
|
||||
const char* DBDumper::MAX_KEYS_ARG = "--max_keys="; |
||||
const char* DBDumper::COUNT_ONLY_ARG = "--count_only"; |
||||
const char* DBDumper::STATS_ARG = "--stats"; |
||||
const char* DBDumper::HEX_OUTPUT_ARG = "--output_hex"; |
||||
|
||||
DBDumper::DBDumper(std::string& db_name, std::vector<std::string>& args) : |
||||
LDBCommand(db_name, args), null_from_(true), null_to_(true), hex_(false), |
||||
count_only_(false), print_stats_(false), max_keys_(-1), |
||||
hex_output_(false) { |
||||
for (int i = 0; i < args.size(); i++) { |
||||
std::string& arg = args.at(i); |
||||
if (arg.find(FROM_ARG) == 0) { |
||||
null_from_ = false; |
||||
from_ = arg.substr(strlen(FROM_ARG)); |
||||
} else if (arg.find(END_ARG) == 0) { |
||||
null_to_ = false; |
||||
to_ = arg.substr(strlen(END_ARG)); |
||||
} else if (arg.find(HEX_ARG) == 0) { |
||||
hex_ = true; |
||||
} else if (arg.find(MAX_KEYS_ARG) == 0) { |
||||
max_keys_ = atoi(arg.substr(strlen(MAX_KEYS_ARG)).c_str()); |
||||
} else if (arg.find(STATS_ARG) == 0) { |
||||
print_stats_ = true; |
||||
} else if (arg.find(COUNT_ONLY_ARG) == 0) { |
||||
count_only_ = true; |
||||
} else if (arg.find(HEX_OUTPUT_ARG) == 0) { |
||||
hex_output_ = true; |
||||
} else { |
||||
exec_state_ = LDBCommandExecuteResult::FAILED("Unknown argument:" + arg); |
||||
} |
||||
} |
||||
|
||||
if (hex_) { |
||||
if (!null_from_) { |
||||
from_ = HexToString(from_); |
||||
} |
||||
if (!null_to_) { |
||||
to_ = HexToString(to_); |
||||
} |
||||
} |
||||
} |
||||
|
||||
void DBDumper::Help(std::string& ret) { |
||||
LDBCommand::Help(ret); |
||||
ret.append("[--from=START KEY] "); |
||||
ret.append("[--to=END Key] "); |
||||
ret.append("[--hex] "); |
||||
ret.append("[--output_hex] "); |
||||
ret.append("[--max_keys=NUM] "); |
||||
ret.append("[--count_only] "); |
||||
ret.append("[--stats] "); |
||||
} |
||||
|
||||
void DBDumper::DoCommand() { |
||||
// Parse command line args
|
||||
uint64_t count = 0; |
||||
if (print_stats_) { |
||||
std::string stats; |
||||
if (db_->GetProperty("leveldb.stats", &stats)) { |
||||
fprintf(stdout, "%s\n", stats.c_str()); |
||||
} |
||||
} |
||||
|
||||
// Setup key iterator
|
||||
leveldb::Iterator* iter = db_->NewIterator(leveldb::ReadOptions()); |
||||
leveldb::Status st = iter->status(); |
||||
if (!st.ok()) { |
||||
exec_state_ = LDBCommandExecuteResult::FAILED("Iterator error." |
||||
+ st.ToString()); |
||||
} |
||||
|
||||
if (!null_from_) { |
||||
iter->Seek(from_); |
||||
} else { |
||||
iter->SeekToFirst(); |
||||
} |
||||
|
||||
int max_keys = max_keys_; |
||||
for (; iter->Valid(); iter->Next()) { |
||||
// 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; |
||||
if (max_keys > 0) { |
||||
--max_keys; |
||||
} |
||||
++count; |
||||
if (!count_only_) { |
||||
if (hex_output_) { |
||||
std::string str = iter->key().ToString(); |
||||
for (int i = 0; i < str.length(); ++i) { |
||||
fprintf(stdout, "%X", str[i]); |
||||
} |
||||
fprintf(stdout, " ==> "); |
||||
str = iter->value().ToString(); |
||||
for (int i = 0; i < str.length(); ++i) { |
||||
fprintf(stdout, "%X", str[i]); |
||||
} |
||||
fprintf(stdout, "\n"); |
||||
} else { |
||||
fprintf(stdout, "%s ==> %s\n", iter->key().ToString().c_str(), |
||||
iter->value().ToString().c_str()); |
||||
} |
||||
} |
||||
} |
||||
fprintf(stdout, "Keys in range: %lld\n", (long long) count); |
||||
// Clean up
|
||||
delete iter; |
||||
} |
||||
|
||||
|
||||
const char* ReduceDBLevels::NEW_LEVLES_ARG = "--new_levels="; |
||||
const char* ReduceDBLevels::PRINT_OLD_LEVELS_ARG = "--print_old_levels"; |
||||
|
||||
ReduceDBLevels::ReduceDBLevels(std::string& db_name, |
||||
std::vector<std::string>& args) |
||||
: LDBCommand(db_name, args), |
||||
new_levels_(-1), |
||||
print_old_levels_(false) { |
||||
for (int i = 0; i < args.size(); i++) { |
||||
std::string& arg = args.at(i); |
||||
if (arg.find(NEW_LEVLES_ARG) == 0) { |
||||
new_levels_ = atoi(arg.substr(strlen(NEW_LEVLES_ARG)).c_str()); |
||||
} else if (arg.find(PRINT_OLD_LEVELS_ARG) == 0) { |
||||
print_old_levels_ = true; |
||||
} else { |
||||
exec_state_ = LDBCommandExecuteResult::FAILED( |
||||
"Unknown argument." + arg); |
||||
} |
||||
} |
||||
|
||||
if(new_levels_ <= 0) { |
||||
exec_state_ = LDBCommandExecuteResult::FAILED( |
||||
" Use --new_levels to specify a new level number\n"); |
||||
} |
||||
} |
||||
|
||||
std::vector<std::string> ReduceDBLevels::PrepareArgs(int new_levels, |
||||
bool print_old_level) { |
||||
std::vector<std::string> ret; |
||||
char arg[100]; |
||||
sprintf(arg, "%s%d", NEW_LEVLES_ARG, new_levels); |
||||
ret.push_back(arg); |
||||
if(print_old_level) { |
||||
sprintf(arg, "%s", PRINT_OLD_LEVELS_ARG); |
||||
ret.push_back(arg); |
||||
} |
||||
return ret; |
||||
} |
||||
|
||||
void ReduceDBLevels::Help(std::string& msg) { |
||||
LDBCommand::Help(msg); |
||||
msg.append("[--new_levels=New number of levels] "); |
||||
msg.append("[--print_old_levels] "); |
||||
} |
||||
|
||||
leveldb::Options ReduceDBLevels::PrepareOptionsForOpenDB() { |
||||
leveldb::Options opt = LDBCommand::PrepareOptionsForOpenDB(); |
||||
// Set to a big value to make sure we can open the db
|
||||
opt.num_levels = 1 << 16; |
||||
return opt; |
||||
} |
||||
|
||||
void ReduceDBLevels::DoCommand() { |
||||
if (new_levels_ <= 1) { |
||||
exec_state_ = LDBCommandExecuteResult::FAILED( |
||||
"Invalid number of levels.\n"); |
||||
return; |
||||
} |
||||
|
||||
leveldb::Status st; |
||||
|
||||
leveldb::Options opt = PrepareOptionsForOpenDB(); |
||||
if (print_old_levels_) { |
||||
TableCache* tc = new TableCache(db_path_, &opt, 10); |
||||
const InternalKeyComparator* cmp = new InternalKeyComparator( |
||||
opt.comparator); |
||||
VersionSet* versions = new VersionSet(db_path_, &opt, |
||||
tc, cmp); |
||||
// 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.
|
||||
st = versions->Recover(); |
||||
int max = -1; |
||||
for(int i = 0; i<versions->NumberLevels(); i++) { |
||||
if (versions->NumLevelFiles(i)) { |
||||
max = i; |
||||
} |
||||
} |
||||
fprintf(stdout, "The old number of levels in use is %d\n", max + 1); |
||||
delete versions; |
||||
|
||||
if (!st.ok()) { |
||||
exec_state_ = LDBCommandExecuteResult::FAILED(st.ToString()); |
||||
return; |
||||
} |
||||
} |
||||
|
||||
// Compact the whole DB to put all files to the highest level.
|
||||
db_->CompactRange(NULL, NULL); |
||||
CloseDB(); |
||||
|
||||
TableCache* tc = new TableCache(db_path_, &opt, 10); |
||||
const InternalKeyComparator* cmp = new InternalKeyComparator( |
||||
opt.comparator); |
||||
VersionSet* versions = new VersionSet(db_path_, &opt, |
||||
tc, cmp); |
||||
// 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.
|
||||
st = versions->Recover(); |
||||
if (!st.ok()) { |
||||
exec_state_ = LDBCommandExecuteResult::FAILED(st.ToString()); |
||||
return; |
||||
} |
||||
|
||||
port::Mutex mu; |
||||
mu.Lock(); |
||||
st = versions->ReduceNumberOfLevels(new_levels_, &mu); |
||||
mu.Unlock(); |
||||
|
||||
if (!st.ok()) { |
||||
exec_state_ = LDBCommandExecuteResult::FAILED(st.ToString()); |
||||
return; |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,248 @@ |
||||
// Copyright (c) 2012 Facebook. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef LEVELDB_UTIL_LDB_H_ |
||||
#define LEVELDB_UTIL_LDB_H_ |
||||
|
||||
#include <string> |
||||
#include <iostream> |
||||
#include <sstream> |
||||
#include <stdlib.h> |
||||
#include <algorithm> |
||||
#include <stdio.h> |
||||
|
||||
#include "leveldb/db.h" |
||||
#include "leveldb/options.h" |
||||
#include "leveldb/iterator.h" |
||||
#include "leveldb/slice.h" |
||||
#include "db/version_set.h" |
||||
#include "util/logging.h" |
||||
|
||||
namespace leveldb { |
||||
|
||||
class LDBCommandExecuteResult { |
||||
public: |
||||
enum State { |
||||
EXEC_NOT_STARTED = 0, EXEC_SUCCEED = 1, EXEC_FAILED = 2, |
||||
}; |
||||
|
||||
LDBCommandExecuteResult() { |
||||
state_ = EXEC_NOT_STARTED; |
||||
message_ = ""; |
||||
} |
||||
|
||||
LDBCommandExecuteResult(State state, std::string& msg) { |
||||
state_ = state; |
||||
message_ = msg; |
||||
} |
||||
|
||||
std::string ToString() { |
||||
std::string ret; |
||||
switch (state_) { |
||||
case EXEC_SUCCEED: |
||||
ret.append("Succeeded."); |
||||
break; |
||||
case EXEC_FAILED: |
||||
ret.append("Failed."); |
||||
break; |
||||
case EXEC_NOT_STARTED: |
||||
ret.append("Not started."); |
||||
} |
||||
if (!message_.empty()) { |
||||
ret.append(message_); |
||||
} |
||||
return ret; |
||||
} |
||||
|
||||
void Reset() { |
||||
state_ = EXEC_NOT_STARTED; |
||||
message_ = ""; |
||||
} |
||||
|
||||
bool IsSucceed() { |
||||
return state_ == EXEC_SUCCEED; |
||||
} |
||||
|
||||
bool IsNotStarted() { |
||||
return state_ == EXEC_NOT_STARTED; |
||||
} |
||||
|
||||
bool IsFailed() { |
||||
return state_ == EXEC_FAILED; |
||||
} |
||||
|
||||
static LDBCommandExecuteResult SUCCEED(std::string msg) { |
||||
return LDBCommandExecuteResult(EXEC_SUCCEED, msg); |
||||
} |
||||
|
||||
static LDBCommandExecuteResult FAILED(std::string msg) { |
||||
return LDBCommandExecuteResult(EXEC_FAILED, msg); |
||||
} |
||||
|
||||
private: |
||||
State state_; |
||||
std::string message_; |
||||
|
||||
bool operator==(const LDBCommandExecuteResult&); |
||||
bool operator!=(const LDBCommandExecuteResult&); |
||||
}; |
||||
|
||||
class LDBCommand { |
||||
public: |
||||
|
||||
/* Constructor */ |
||||
LDBCommand(std::string& db_name, std::vector<std::string>& args) : |
||||
db_path_(db_name), |
||||
db_(NULL) { |
||||
} |
||||
|
||||
virtual leveldb::Options PrepareOptionsForOpenDB() { |
||||
leveldb::Options opt; |
||||
opt.create_if_missing = false; |
||||
return opt; |
||||
} |
||||
|
||||
virtual ~LDBCommand() { |
||||
if (db_ != NULL) { |
||||
delete db_; |
||||
db_ = NULL; |
||||
} |
||||
} |
||||
|
||||
/* Print the help message */ |
||||
static void Help(std::string& ret) { |
||||
ret.append("--db=DB_PATH "); |
||||
} |
||||
|
||||
/* Run the command, and return the execute result. */ |
||||
void Run() { |
||||
if (!exec_state_.IsNotStarted()) { |
||||
return; |
||||
} |
||||
|
||||
if (db_ == NULL) { |
||||
OpenDB(); |
||||
} |
||||
DoCommand(); |
||||
if (exec_state_.IsNotStarted()) { |
||||
exec_state_ = LDBCommandExecuteResult::SUCCEED(""); |
||||
} |
||||
CloseDB (); |
||||
} |
||||
|
||||
virtual void DoCommand() = 0; |
||||
|
||||
LDBCommandExecuteResult GetExecuteState() { |
||||
return exec_state_; |
||||
} |
||||
|
||||
void ClearPreviousRunState() { |
||||
exec_state_.Reset(); |
||||
} |
||||
|
||||
static std::string HexToString(const std::string& str) { |
||||
std::string parsed; |
||||
for (int i = 0; i < str.length();) { |
||||
int c; |
||||
sscanf(str.c_str() + i, "%2X", &c); |
||||
parsed.push_back(c); |
||||
i += 2; |
||||
} |
||||
return parsed; |
||||
} |
||||
|
||||
protected: |
||||
|
||||
void OpenDB() { |
||||
leveldb::Options opt = PrepareOptionsForOpenDB(); |
||||
// Open the DB.
|
||||
leveldb::Status st = leveldb::DB::Open(opt, db_path_, &db_); |
||||
if (!st.ok()) { |
||||
std::string msg = st.ToString(); |
||||
exec_state_ = LDBCommandExecuteResult::FAILED(msg); |
||||
} |
||||
} |
||||
|
||||
void CloseDB () { |
||||
if (db_ != NULL) { |
||||
delete db_; |
||||
db_ = NULL; |
||||
} |
||||
} |
||||
|
||||
static const char* FROM_ARG; |
||||
static const char* END_ARG; |
||||
static const char* HEX_ARG; |
||||
LDBCommandExecuteResult exec_state_; |
||||
std::string db_path_; |
||||
leveldb::DB* db_; |
||||
}; |
||||
|
||||
class Compactor: public LDBCommand { |
||||
public: |
||||
Compactor(std::string& db_name, std::vector<std::string>& args); |
||||
|
||||
virtual ~Compactor() {} |
||||
|
||||
static void Help(std::string& ret); |
||||
|
||||
virtual void DoCommand(); |
||||
|
||||
private: |
||||
bool null_from_; |
||||
std::string from_; |
||||
bool null_to_; |
||||
std::string to_; |
||||
bool hex_; |
||||
}; |
||||
|
||||
class DBDumper: public LDBCommand { |
||||
public: |
||||
DBDumper(std::string& db_name, std::vector<std::string>& args); |
||||
virtual ~DBDumper() {} |
||||
static void Help(std::string& ret); |
||||
virtual void DoCommand(); |
||||
private: |
||||
bool null_from_; |
||||
std::string from_; |
||||
bool null_to_; |
||||
std::string to_; |
||||
int max_keys_; |
||||
bool count_only_; |
||||
bool print_stats_; |
||||
bool hex_; |
||||
bool hex_output_; |
||||
|
||||
static const char* MAX_KEYS_ARG; |
||||
static const char* COUNT_ONLY_ARG; |
||||
static const char* STATS_ARG; |
||||
static const char* HEX_OUTPUT_ARG; |
||||
}; |
||||
|
||||
class ReduceDBLevels : public LDBCommand { |
||||
public: |
||||
|
||||
ReduceDBLevels (std::string& db_name, std::vector<std::string>& args); |
||||
|
||||
~ReduceDBLevels() {} |
||||
|
||||
virtual leveldb::Options PrepareOptionsForOpenDB(); |
||||
|
||||
virtual void DoCommand(); |
||||
static void Help(std::string& msg); |
||||
|
||||
static std::vector<std::string> PrepareArgs(int new_levels, |
||||
bool print_old_level = false); |
||||
|
||||
private: |
||||
int new_levels_; |
||||
bool print_old_levels_; |
||||
|
||||
static const char* NEW_LEVLES_ARG; |
||||
static const char* PRINT_OLD_LEVELS_ARG; |
||||
}; |
||||
|
||||
} |
||||
|
||||
#endif |
Loading…
Reference in new issue