//! 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;

#[allow(missing_docs)]
struct RawCache {
    ptr: *mut leveldb_cache_t,
}

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

/// Represents a LevelDB cache
pub struct Cache {
    raw: RawCache,
}

impl Cache {
    /// Create a LevelDB LRU cache of a given size
    pub fn new(size: size_t) -> Cache {
        let cache = unsafe { leveldb_cache_create_lru(size) };
        Cache {
            raw: RawCache { ptr: cache },
        }
    }

    #[allow(missing_docs)]
    pub fn raw_ptr(&self) -> *mut leveldb_cache_t {
        self.raw.ptr
    }
}
