ldb: Add compression and bloom filter options.
Summary: Added the following two options: [--bloom_bits=<int,e.g.:14>] [--compression_type=<no|snappy|zlib|bzip2>] These options will be used when ldb opens the leveldb database. Test Plan: Tried by hand for both success and failure cases. We do need a test framework. Reviewers: dhruba, emayanke, sheki Reviewed By: dhruba CC: leveldb Differential Revision: https://reviews.facebook.net/D7197
This commit is contained in:
parent
8055008909
commit
768edfaaed
@ -11,6 +11,59 @@
|
||||
|
||||
namespace leveldb {
|
||||
|
||||
const char* LDBCommand::BLOOM_ARG = "--bloom_bits=";
|
||||
const char* LDBCommand::COMPRESSION_TYPE_ARG = "--compression_type=";
|
||||
|
||||
void LDBCommand::parse_open_args(std::vector<std::string>& args) {
|
||||
std::vector<std::string> rest_of_args;
|
||||
for (unsigned int i = 0; i < args.size(); i++) {
|
||||
std::string& arg = args.at(i);
|
||||
if (arg.find(BLOOM_ARG) == 0
|
||||
|| arg.find(COMPRESSION_TYPE_ARG) == 0) {
|
||||
open_args_.push_back(arg);
|
||||
} else {
|
||||
rest_of_args.push_back(arg);
|
||||
}
|
||||
}
|
||||
swap(args, rest_of_args);
|
||||
}
|
||||
|
||||
leveldb::Options LDBCommand::PrepareOptionsForOpenDB() {
|
||||
leveldb::Options opt;
|
||||
opt.create_if_missing = false;
|
||||
for (unsigned int i = 0; i < open_args_.size(); i++) {
|
||||
std::string& arg = open_args_.at(i);
|
||||
if (arg.find(BLOOM_ARG) == 0) {
|
||||
std::string bits_string = arg.substr(strlen(BLOOM_ARG));
|
||||
int bits = atoi(bits_string.c_str());
|
||||
if (bits == 0) {
|
||||
// Badly-formatted bits.
|
||||
exec_state_ = LDBCommandExecuteResult::FAILED(
|
||||
std::string("Badly-formatted bits: ") + bits_string);
|
||||
}
|
||||
opt.filter_policy = leveldb::NewBloomFilterPolicy(bits);
|
||||
} else if (arg.find(COMPRESSION_TYPE_ARG) == 0) {
|
||||
std::string comp = arg.substr(strlen(COMPRESSION_TYPE_ARG));
|
||||
if (comp == "no") {
|
||||
opt.compression = leveldb::kNoCompression;
|
||||
} else if (comp == "snappy") {
|
||||
opt.compression = leveldb::kSnappyCompression;
|
||||
} else if (comp == "zlib") {
|
||||
opt.compression = leveldb::kZlibCompression;
|
||||
} else if (comp == "bzip2") {
|
||||
opt.compression = leveldb::kBZip2Compression;
|
||||
} else {
|
||||
// Unknown compression.
|
||||
exec_state_ = LDBCommandExecuteResult::FAILED(
|
||||
"Unknown compression level: " + comp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return opt;
|
||||
}
|
||||
|
||||
|
||||
const char* LDBCommand::FROM_ARG = "--from=";
|
||||
const char* LDBCommand::END_ARG = "--to=";
|
||||
const char* LDBCommand::HEX_ARG = "--hex";
|
||||
|
@ -95,18 +95,16 @@ public:
|
||||
LDBCommand(std::string& db_name, std::vector<std::string>& args) :
|
||||
db_path_(db_name),
|
||||
db_(NULL) {
|
||||
parse_open_args(args);
|
||||
}
|
||||
|
||||
LDBCommand(std::vector<std::string>& args) :
|
||||
db_path_(""),
|
||||
db_(NULL) {
|
||||
parse_open_args(args);
|
||||
}
|
||||
|
||||
virtual leveldb::Options PrepareOptionsForOpenDB() {
|
||||
leveldb::Options opt;
|
||||
opt.create_if_missing = false;
|
||||
return opt;
|
||||
}
|
||||
virtual leveldb::Options PrepareOptionsForOpenDB();
|
||||
|
||||
virtual bool NoDBOpen() {
|
||||
return false;
|
||||
@ -121,7 +119,11 @@ public:
|
||||
|
||||
/* Print the help message */
|
||||
static void Help(std::string& ret) {
|
||||
ret.append("--db=DB_PATH ");
|
||||
ret.append("--db=DB_PATH [");
|
||||
ret.append(LDBCommand::BLOOM_ARG);
|
||||
ret.append("<int,e.g.:14>] [");
|
||||
ret.append(LDBCommand::COMPRESSION_TYPE_ARG);
|
||||
ret.append("<no|snappy|zlib|bzip2>] ");
|
||||
}
|
||||
|
||||
/* Run the command, and return the execute result. */
|
||||
@ -130,10 +132,13 @@ public:
|
||||
return;
|
||||
}
|
||||
|
||||
if (db_ == NULL && !NoDBOpen()) {
|
||||
if (db_ == NULL && !NoDBOpen()) {
|
||||
OpenDB();
|
||||
if (!exec_state_.IsNotStarted()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
DoCommand();
|
||||
if (exec_state_.IsNotStarted()) {
|
||||
exec_state_ = LDBCommandExecuteResult::SUCCEED("");
|
||||
@ -169,6 +174,9 @@ protected:
|
||||
|
||||
void OpenDB() {
|
||||
leveldb::Options opt = PrepareOptionsForOpenDB();
|
||||
if (!exec_state_.IsNotStarted()) {
|
||||
return;
|
||||
}
|
||||
// Open the DB.
|
||||
leveldb::Status st = leveldb::DB::Open(opt, db_path_, &db_);
|
||||
if (!st.ok()) {
|
||||
@ -190,6 +198,13 @@ protected:
|
||||
LDBCommandExecuteResult exec_state_;
|
||||
std::string db_path_;
|
||||
leveldb::DB* db_;
|
||||
|
||||
private:
|
||||
|
||||
static const char* BLOOM_ARG;
|
||||
static const char* COMPRESSION_TYPE_ARG;
|
||||
std::vector<std::string> open_args_;
|
||||
void parse_open_args(std::vector<std::string>& args);
|
||||
};
|
||||
|
||||
class Compactor: public LDBCommand {
|
||||
|
Loading…
x
Reference in New Issue
Block a user