use crate::binding::{leveldb_cache_create_lru, leveldb_cache_destroy, leveldb_cache_t};
use libc::size_t;
#[cfg(feature = "experimental-extension")]
unsafe extern "C" {
fn leveldb_rs_create_cache(
capacity: size_t,
hot_soft_limit: f64,
warm_soft_limit: f64,
cold_soft_limit: f64,
half_life_ticks: u32,
hot_cache_capacity: size_t,
hot_cache_eviction_interval: size_t,
) -> *mut leveldb_cache_t;
fn leveldb_rs_destroy_cache(cache: *mut leveldb_cache_t);
}
type DestroyFn = unsafe extern "C" fn(*mut leveldb_cache_t);
struct RawCache {
ptr: *mut leveldb_cache_t,
destroy_fn: DestroyFn,
}
impl Drop for RawCache {
fn drop(&mut self) {
unsafe {
(self.destroy_fn)(self.ptr);
}
}
}
pub struct Cache {
raw: RawCache,
}
#[cfg(feature = "experimental-extension")]
#[derive(Debug, Clone)]
pub struct FlatT3CacheConfig {
pub capacity: size_t,
pub hot_soft_limit: f64,
pub warm_soft_limit: f64,
pub cold_soft_limit: f64,
pub half_life_ticks: u32,
pub hot_cache_capacity: size_t,
pub hot_cache_eviction_interval: size_t,
}
#[cfg(feature = "experimental-extension")]
impl Default for FlatT3CacheConfig {
fn default() -> Self {
Self {
capacity: 0,
hot_soft_limit: 0.02,
warm_soft_limit: 0.53,
cold_soft_limit: 0.45,
half_life_ticks: 300,
hot_cache_capacity: 262140,
hot_cache_eviction_interval: 32768,
}
}
}
impl Cache {
#[deprecated(since = "2.2.0", note = "Use Options::set_builtin_lru_cache instead")]
pub fn new(size: size_t) -> Cache {
let cache = unsafe { leveldb_cache_create_lru(size) };
Cache {
raw: RawCache {
ptr: cache,
destroy_fn: leveldb_cache_destroy,
},
}
}
#[cfg(feature = "experimental-extension")]
#[deprecated(since = "2.2.0", note = "Use Options::set_flat_t3_cache instead")]
pub fn new_flat_t3_cache(config: FlatT3CacheConfig) -> Cache {
let cache = unsafe {
leveldb_rs_create_cache(
config.capacity,
config.hot_soft_limit,
config.warm_soft_limit,
config.cold_soft_limit,
config.half_life_ticks,
config.hot_cache_capacity,
config.hot_cache_eviction_interval,
)
};
Cache {
raw: RawCache {
ptr: cache,
destroy_fn: leveldb_rs_destroy_cache,
},
}
}
#[allow(missing_docs)]
pub fn raw_ptr(&self) -> *mut leveldb_cache_t {
self.raw.ptr
}
}