leveldb-rs-binding 2.2.0

An interface for the LevelDB
Documentation
//! Structs and traits to work with the LevelDB cache.
use crate::binding::{leveldb_cache_create_lru, leveldb_cache_destroy, leveldb_cache_t};
use libc::size_t;

// FFI declarations for FlatT3Cache extension
#[cfg(feature = "experimental-extension")]
unsafe extern "C" {
    /// Create a new FlatT3Cache with the specified parameters
    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;
    /// Destroy a FlatT3Cache
    fn leveldb_rs_destroy_cache(cache: *mut leveldb_cache_t);
}

/// Type alias for cache destroy function pointer
type DestroyFn = unsafe extern "C" fn(*mut leveldb_cache_t);

/// Raw cache pointer wrapper
struct RawCache {
    ptr: *mut leveldb_cache_t,
    destroy_fn: DestroyFn,
}

impl Drop for RawCache {
    fn drop(&mut self) {
        unsafe {
            (self.destroy_fn)(self.ptr);
        }
    }
}

/// LevelDB cache contains accessed blocks if enabled in the ReadOption
pub struct Cache {
    raw: RawCache,
}

/// Configuration for [`FlatT3Cache`](crate::database::cache).
///
/// Use [`FlatT3CacheConfig::default`] for sensible defaults, overriding only the fields you need:
///
/// ```
/// # use leveldb::database::cache::FlatT3CacheConfig;
/// let config = FlatT3CacheConfig {
///     capacity: 1024,
///     ..Default::default()
/// };
/// ```
#[cfg(feature = "experimental-extension")]
#[derive(Debug, Clone)]
pub struct FlatT3CacheConfig {
    /// Total cache capacity in bytes.
    pub capacity: size_t,
    /// Soft limit ratio for the hot tier (0.0–1.0 of capacity).
    pub hot_soft_limit: f64,
    /// Soft limit ratio for the warm tier (0.0–1.0 of capacity).
    pub warm_soft_limit: f64,
    /// Soft limit ratio for the cold tier (0.0–1.0 of capacity).
    pub cold_soft_limit: f64,
    /// Half-life in ticks for the usage decay function.
    pub half_life_ticks: u32,
    /// Per-thread hot cache ring buffer capacity.
    pub hot_cache_capacity: size_t,
    /// Per-thread hot cache eviction interval (lookup-miss count between evictions).
    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 {
    /// Create a LevelDB LRU cache of given size
    ///
    /// # Deprecated
    ///
    /// Use [`Options::set_builtin_lru_cache`](crate::database::options::Options::set_builtin_lru_cache) instead.
    #[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,
            },
        }
    }

    /// Create a FlatT3Cache from a [`FlatT3CacheConfig`].
    ///
    /// # Deprecated
    ///
    /// Use [`Options::set_flat_t3_cache`](crate::database::options::Options::set_flat_t3_cache) instead.
    #[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
    }
}