Skip to main content

DbCache

Trait DbCache 

Source
pub trait DbCache: Send + Sync {
    // Required methods
    fn get_block<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 CachedKey,
    ) -> Pin<Box<dyn Future<Output = Result<Option<CachedEntry>, Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
    fn get_index<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 CachedKey,
    ) -> Pin<Box<dyn Future<Output = Result<Option<CachedEntry>, Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
    fn get_filter<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 CachedKey,
    ) -> Pin<Box<dyn Future<Output = Result<Option<CachedEntry>, Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
    fn get_stats<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 CachedKey,
    ) -> Pin<Box<dyn Future<Output = Result<Option<CachedEntry>, Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
    fn insert<'life0, 'async_trait>(
        &'life0 self,
        key: CachedKey,
        value: CachedEntry,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             Self: 'async_trait;
    fn remove<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 CachedKey,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
    fn entry_count(&self) -> u64;

    // Provided method
    fn close<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             Self: 'async_trait { ... }
}
Expand description

A trait for slatedb’s in-memory cache.

This trait defines the interface for an in-memory cache, which is used to store and retrieve cached blocks, indices and filters associated with SSTable IDs.

Example:

use async_trait::async_trait;
use slatedb::{Db, Error};
use slatedb::db_cache::{DbCache, CachedEntry, CachedKey};
use slatedb::object_store::memory::InMemory;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

struct MyCache {
    inner: Mutex<MyCacheInner>,
}

struct MyCacheInner {
    data: HashMap<CachedKey, CachedEntry>,
    usage: u64,
    capacity: u64
}

impl MyCache {
    pub fn new(capacity: u64) -> Self {
        Self {
            inner: Mutex::new(
                MyCacheInner{
                    data: HashMap::new(),
                    usage: 0,
                    capacity,
                }
            )
        }
    }
}

#[async_trait]
impl DbCache for MyCache {
    async fn get_block(&self, key: &CachedKey) -> Result<Option<CachedEntry>, Error> {
        let guard = self.inner.lock().unwrap();
        Ok(guard.data.get(key).cloned())
    }

    async fn get_index(&self, key: &CachedKey) -> Result<Option<CachedEntry>, Error> {
        let guard = self.inner.lock().unwrap();
        Ok(guard.data.get(key).cloned())
    }

    async fn get_filter(&self, key: &CachedKey) -> Result<Option<CachedEntry>, Error> {
        let guard = self.inner.lock().unwrap();
        Ok(guard.data.get(key).cloned())
    }

    async fn get_stats(&self, key: &CachedKey) -> Result<Option<CachedEntry>, Error> {
        let guard = self.inner.lock().unwrap();
        Ok(guard.data.get(key).cloned())
    }

    async fn insert(&self, key: CachedKey, value: CachedEntry) {
        let mut guard = self.inner.lock().unwrap();
        guard.usage += value.size() as u64;
        if let Some(v) = guard.data.insert(key, value) {
            guard.usage -= v.size() as u64;
        }
    }

    async fn remove(&self, key: &CachedKey) {
        let mut guard = self.inner.lock().unwrap();
        if let Some(v) = guard.data.remove(key) {
            guard.usage -= v.size() as u64;
        }
    }

    fn entry_count(&self) -> u64 {
        let mut guard = self.inner.lock().unwrap();
        guard.capacity
    }
}

#[::tokio::main]
async fn main() {
    let object_store = Arc::new(InMemory::new());
    let cache = Arc::new(MyCache::new(128u64 * 1024 * 1024));
    let db = Db::builder("/path/to/db", object_store)
        .with_db_cache(cache)
        .build()
        .await;
}

Required Methods§

Source

fn get_block<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 CachedKey, ) -> Pin<Box<dyn Future<Output = Result<Option<CachedEntry>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Source

fn get_index<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 CachedKey, ) -> Pin<Box<dyn Future<Output = Result<Option<CachedEntry>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Source

fn get_filter<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 CachedKey, ) -> Pin<Box<dyn Future<Output = Result<Option<CachedEntry>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Source

fn get_stats<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 CachedKey, ) -> Pin<Box<dyn Future<Output = Result<Option<CachedEntry>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Source

fn insert<'life0, 'async_trait>( &'life0 self, key: CachedKey, value: CachedEntry, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Source

fn remove<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 CachedKey, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Source

fn entry_count(&self) -> u64

Provided Methods§

Source

fn close<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Gracefully close the cache, flushing any in-memory state to disk.

Implementations backed by hybrid (memory + disk) caches should use this to ensure cached entries survive process restarts. The default implementation is a no-op.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§