Commit cd2ba89b authored by UncP's avatar UncP Committed by siddontang

add `cache_index_and_filter_blocks_with_high_priority` option (#138)

parent 229732cb
......@@ -1558,6 +1558,11 @@ void crocksdb_block_based_options_set_cache_index_and_filter_blocks(
options->rep.cache_index_and_filter_blocks = v;
}
void crocksdb_block_based_options_set_cache_index_and_filter_blocks_with_high_priority(
crocksdb_block_based_table_options_t* options, unsigned char v) {
options->rep.cache_index_and_filter_blocks_with_high_priority = v;
}
void crocksdb_block_based_options_set_pin_l0_filter_and_index_blocks_in_cache(
crocksdb_block_based_table_options_t* options, unsigned char v) {
options->rep.pin_l0_filter_and_index_blocks_in_cache = v;
......@@ -2765,9 +2770,10 @@ void crocksdb_flushoptions_set_wait(
opt->rep.wait = v;
}
crocksdb_cache_t* crocksdb_cache_create_lru(size_t capacity) {
crocksdb_cache_t* crocksdb_cache_create_lru(size_t capacity,
int num_shard_bits, unsigned char strict_capacity_limit, double high_pri_pool_ratio) {
crocksdb_cache_t* c = new crocksdb_cache_t;
c->rep = NewLRUCache(capacity);
c->rep = NewLRUCache(capacity, num_shard_bits, strict_capacity_limit, high_pri_pool_ratio);
return c;
}
......
......@@ -592,6 +592,9 @@ extern C_ROCKSDB_LIBRARY_API void
crocksdb_block_based_options_set_cache_index_and_filter_blocks(
crocksdb_block_based_table_options_t*, unsigned char);
extern C_ROCKSDB_LIBRARY_API void
crocksdb_block_based_options_set_cache_index_and_filter_blocks_with_high_priority(
crocksdb_block_based_table_options_t*, unsigned char);
extern C_ROCKSDB_LIBRARY_API void
crocksdb_block_based_options_set_pin_l0_filter_and_index_blocks_in_cache(
crocksdb_block_based_table_options_t*, unsigned char);
extern C_ROCKSDB_LIBRARY_API void
......@@ -1153,7 +1156,7 @@ extern C_ROCKSDB_LIBRARY_API void crocksdb_flushoptions_set_wait(
/* Cache */
extern C_ROCKSDB_LIBRARY_API crocksdb_cache_t* crocksdb_cache_create_lru(
size_t capacity);
size_t capacity, int num_shard_bits, unsigned char strict_capacity_limit, double high_pri_pool_ratio);
extern C_ROCKSDB_LIBRARY_API void crocksdb_cache_destroy(crocksdb_cache_t* cache);
extern C_ROCKSDB_LIBRARY_API void crocksdb_cache_set_capacity(
crocksdb_cache_t* cache, size_t capacity);
......
......@@ -62,8 +62,13 @@ pub fn new_bloom_filter(bits: c_int) -> *mut DBFilterPolicy {
unsafe { crocksdb_filterpolicy_create_bloom(bits) }
}
pub fn new_cache(capacity: size_t) -> *mut DBCache {
unsafe { crocksdb_cache_create_lru(capacity) }
pub fn new_cache(
capacity: size_t,
shard_bits: c_int,
capacity_limit: c_uchar,
pri_ratio: c_double,
) -> *mut DBCache {
unsafe { crocksdb_cache_create_lru(capacity, shard_bits, capacity_limit, pri_ratio) }
}
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
......@@ -303,7 +308,12 @@ extern "C" {
pub fn crocksdb_options_create() -> *mut Options;
pub fn crocksdb_options_copy(opts: *const Options) -> *mut Options;
pub fn crocksdb_options_destroy(opts: *mut Options);
pub fn crocksdb_cache_create_lru(capacity: size_t) -> *mut DBCache;
pub fn crocksdb_cache_create_lru(
capacity: size_t,
shard_bits: c_int,
capacity_limit: c_uchar,
pri_ratio: c_double,
) -> *mut DBCache;
pub fn crocksdb_cache_destroy(cache: *mut DBCache);
pub fn crocksdb_block_based_options_create() -> *mut DBBlockBasedTableOptions;
pub fn crocksdb_block_based_options_destroy(opts: *mut DBBlockBasedTableOptions);
......@@ -323,6 +333,10 @@ extern "C" {
block_options: *mut DBBlockBasedTableOptions,
v: c_uchar,
);
pub fn crocksdb_block_based_options_set_cache_index_and_filter_blocks_with_high_priority(
block_options: *mut DBBlockBasedTableOptions,
v: c_uchar,
);
pub fn crocksdb_block_based_options_set_filter_policy(
block_options: *mut DBBlockBasedTableOptions,
filter_policy: *mut DBFilterPolicy,
......
......@@ -20,7 +20,7 @@ use crocksdb_ffi::{self, DBBlockBasedTableOptions, DBCompactOptions, DBCompressi
DBRecoveryMode, DBRestoreOptions, DBSnapshot, DBStatisticsHistogramType,
DBStatisticsTickerType, DBWriteOptions, Options};
use event_listener::{new_event_listener, EventListener};
use libc::{self, c_int, c_void, size_t};
use libc::{self, c_double, c_int, c_uchar, c_void, size_t};
use merge_operator::{self, full_merge_callback, partial_merge_callback, MergeOperatorCallback};
use merge_operator::MergeFn;
use slice_transform::{new_slice_transform, SliceTransform};
......@@ -76,8 +76,18 @@ impl BlockBasedOptions {
}
}
pub fn set_lru_cache(&mut self, size: size_t) {
let cache = crocksdb_ffi::new_cache(size);
/// the recommanded shard_bits is 6, also you can set a larger value as long as it is
/// smaller than 20, also you can set shard_bits to -1, RocksDB will choose a value for you
/// the recommanded capacity_limit is 0(false) if your memory is sufficient
/// the recommanded pri_ratio should be 0.05 or 0.1
pub fn set_lru_cache(
&mut self,
size: size_t,
shard_bits: c_int,
capacity_limit: c_uchar,
pri_ratio: c_double,
) {
let cache = crocksdb_ffi::new_cache(size, shard_bits, capacity_limit, pri_ratio);
unsafe {
crocksdb_ffi::crocksdb_block_based_options_set_block_cache(self.inner, cache);
crocksdb_ffi::crocksdb_cache_destroy(cache);
......@@ -105,6 +115,16 @@ impl BlockBasedOptions {
}
}
pub fn set_cache_index_and_filter_blocks_with_high_priority(&mut self, v: bool) {
unsafe {
crocksdb_ffi::
crocksdb_block_based_options_set_cache_index_and_filter_blocks_with_high_priority(
self.inner,
v as u8,
);
}
}
pub fn set_whole_key_filtering(&mut self, v: bool) {
unsafe {
crocksdb_ffi::crocksdb_block_based_options_set_whole_key_filtering(self.inner, v);
......
......@@ -262,6 +262,35 @@ fn test_set_pin_l0_filter_and_index_blocks_in_cache() {
vec![("default", cf_opts)],
).unwrap();
}
#[test]
fn test_set_lru_cache() {
let path = TempDir::new("_rust_rocksdb_set_set_lru_cache").expect("");
let mut opts = DBOptions::new();
let mut cf_opts = ColumnFamilyOptions::new();
opts.create_if_missing(true);
let mut block_opts = BlockBasedOptions::new();
block_opts.set_lru_cache(8388608, -1, 0, 0.0);
cf_opts.set_block_based_table_factory(&block_opts);
DB::open_cf(opts, path.path().to_str().unwrap(), vec!["default"]).unwrap();
}
#[test]
fn test_set_cache_index_and_filter_blocks_with_high_priority() {
let path = TempDir::new("_rust_rocksdb_set_cache_and_index_with_high_priority").expect("");
let mut opts = DBOptions::new();
let mut cf_opts = ColumnFamilyOptions::new();
opts.create_if_missing(true);
let mut block_opts = BlockBasedOptions::new();
block_opts.set_cache_index_and_filter_blocks_with_high_priority(true);
cf_opts.set_block_based_table_factory(&block_opts);
DB::open_cf(
opts,
path.path().to_str().unwrap(),
vec![("default", cf_opts)],
).unwrap();
}
#[test]
fn test_pending_compaction_bytes_limit() {
let path = TempDir::new("_rust_rocksdb_pending_compaction_bytes_limit").expect("");
......@@ -320,7 +349,7 @@ fn test_get_block_cache_usage() {
opts.create_if_missing(true);
let mut block_opts = BlockBasedOptions::new();
block_opts.set_lru_cache(16 * 1024 * 1024);
block_opts.set_lru_cache(16 * 1024 * 1024, -1, 0, 0.0);
cf_opts.set_block_based_table_factory(&block_opts);
let db = DB::open_cf(
opts,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment