#include <cstdio>
#include <string>
#include <vector>
#include "rocksdb/cache.h"
#include "rocksdb/compaction_filter.h"
#include "rocksdb/db.h"
#include "rocksdb/options.h"
#include "rocksdb/slice.h"
#include "rocksdb/table.h"
#include "rocksdb/utilities/options_util.h"
using
namespace
rocksdb;
std::string kDBPath =
"/tmp/rocksdb_options_file_example"
;
namespace
{
class
DummyCompactionFilter :
public
CompactionFilter {
public
:
virtual
~DummyCompactionFilter() {}
virtual
bool
Filter(
int
level,
const
Slice& key,
const
Slice& existing_value,
std::string* new_value,
bool
* value_changed)
const
{
return
false
;
}
virtual
const
char
* Name()
const
{
return
"DummyCompactionFilter"
; }
};
}
int
main() {
DBOptions db_opt;
db_opt.create_if_missing =
true
;
std::vector<ColumnFamilyDescriptor> cf_descs;
cf_descs.push_back({kDefaultColumnFamilyName, ColumnFamilyOptions()});
cf_descs.push_back({
"new_cf"
, ColumnFamilyOptions()});
auto
cache = NewLRUCache(1 * 1024 * 1024 * 1024);
BlockBasedTableOptions bbt_opts;
bbt_opts.block_size = 32 * 1024;
bbt_opts.block_cache = cache;
std::unique_ptr<CompactionFilter> compaction_filter;
compaction_filter.reset(
new
DummyCompactionFilter());
cf_descs[0].options.table_factory.reset(NewBlockBasedTableFactory(bbt_opts));
cf_descs[0].options.compaction_filter = compaction_filter.get();
cf_descs[1].options.table_factory.reset(NewBlockBasedTableFactory(bbt_opts));
DB* db;
Status s = DestroyDB(kDBPath, Options(db_opt, cf_descs[0].options));
assert
(s.ok());
s = DB::Open(Options(db_opt, cf_descs[0].options), kDBPath, &db);
assert
(s.ok());
ColumnFamilyHandle* cf;
s = db->CreateColumnFamily(ColumnFamilyOptions(),
"new_cf"
, &cf);
assert
(s.ok());
delete
cf;
delete
db;
DBOptions loaded_db_opt;
std::vector<ColumnFamilyDescriptor> loaded_cf_descs;
s = LoadLatestOptions(kDBPath, Env::Default(), &loaded_db_opt,
&loaded_cf_descs);
assert
(s.ok());
assert
(loaded_db_opt.create_if_missing == db_opt.create_if_missing);
for
(
size_t
i = 0; i < loaded_cf_descs.size(); ++i) {
auto
* loaded_bbt_opt =
reinterpret_cast
<BlockBasedTableOptions*>(
loaded_cf_descs[0].options.table_factory->GetOptions());
assert
(loaded_bbt_opt->block_size == bbt_opts.block_size);
loaded_bbt_opt->block_cache = cache;
}
assert
(loaded_cf_descs[0].options.compaction_filter ==
nullptr
);
loaded_cf_descs[0].options.compaction_filter = compaction_filter.get();
std::vector<ColumnFamilyHandle*> handles;
s = DB::Open(loaded_db_opt, kDBPath, loaded_cf_descs, &handles, &db);
assert
(s.ok());
for
(
auto
* handle : handles) {
delete
handle;
}
delete
db;
}