mod entry;
mod key;
mod lookup;
mod weight;
use anyhow::Result;
#[cfg(feature = "jwks")]
pub(crate) use entry::CachedJwks;
pub(crate) use entry::Entry;
pub(crate) use lookup::Lookup;
use quick_cache::sync::DefaultLifecycle;
use quick_cache::{DefaultHashBuilder, OptionsBuilder};
use uuid::Uuid;
use crate::catalog::{DatabaseId, NamespaceId};
use crate::val::TableName;
pub(crate) type Cache = quick_cache::sync::Cache<key::Key, Entry, weight::Weight>;
pub struct DatastoreCache {
cache: Cache,
}
impl DatastoreCache {
pub(in crate::kvs) fn new(size: usize) -> Self {
let options = OptionsBuilder::new()
.estimated_items_capacity(size)
.weight_capacity(size as u64)
.build()
.expect("valid datastore cache options");
let cache = Cache::with_options(
options,
weight::Weight,
DefaultHashBuilder::default(),
DefaultLifecycle::default(),
);
Self {
cache,
}
}
pub(crate) fn get(&self, lookup: &Lookup) -> Option<Entry> {
self.cache.get(lookup)
}
pub(crate) fn insert(&self, lookup: Lookup, entry: Entry) {
self.cache.insert(lookup.into(), entry);
}
pub(crate) fn clear(&self) {
self.cache.clear();
}
pub(crate) fn set_live_queries_version(&self, ns: NamespaceId, db: DatabaseId, tb: &TableName) {
let key = Lookup::Lvv(ns, db, tb);
self.insert(key, Entry::Lvv(Uuid::now_v7()));
}
pub fn get_live_queries_version(
&self,
ns: NamespaceId,
db: DatabaseId,
tb: &TableName,
) -> Result<Uuid> {
let key = Lookup::Lvv(ns, db, tb);
let version = match self.get(&key) {
Some(val) => val.try_info_lvv()?,
None => {
let version = Uuid::now_v7();
let val = Entry::Lvv(version);
self.insert(key, val);
version
}
};
Ok(version)
}
}