use std::{borrow::Cow, ops::Deref, sync::Arc};
use deepsize::{Context, DeepSizeOf};
use lance_core::cache::{CacheKey, LanceCache};
use lance_index::frag_reuse::FragReuseIndex;
use lance_table::format::IndexMetadata;
use uuid::Uuid;
pub struct GlobalIndexCache(pub(super) LanceCache);
impl GlobalIndexCache {
pub fn for_dataset(&self, uri: &str) -> DSIndexCache {
DSIndexCache(self.0.with_key_prefix(uri))
}
}
impl Clone for GlobalIndexCache {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl Deref for GlobalIndexCache {
type Target = LanceCache;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DeepSizeOf for GlobalIndexCache {
fn deep_size_of_children(&self, context: &mut Context) -> usize {
self.0.deep_size_of_children(context)
}
}
pub struct DSIndexCache(pub(crate) LanceCache);
impl Deref for DSIndexCache {
type Target = LanceCache;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DSIndexCache {
pub fn for_index(&self, uuid: &str, fri_uuid: Option<&Uuid>) -> LanceCache {
if let Some(fri_uuid) = fri_uuid {
let cache_key = format!("{}-{}", uuid, fri_uuid);
self.0.with_key_prefix(&cache_key)
} else {
self.0.with_key_prefix(uuid)
}
}
}
#[derive(Debug)]
pub struct FragReuseIndexKey<'a> {
pub uuid: &'a str,
}
impl CacheKey for FragReuseIndexKey<'_> {
type ValueType = FragReuseIndex;
fn key(&self) -> Cow<'_, str> {
Cow::Owned(format!("frag_reuse/{}", self.uuid))
}
}
#[derive(Debug)]
pub struct IndexMetadataKey {
pub version: u64,
}
impl CacheKey for IndexMetadataKey {
type ValueType = Vec<IndexMetadata>;
fn key(&self) -> Cow<'_, str> {
Cow::Owned(self.version.to_string())
}
}
pub struct ProstAny(pub Arc<prost_types::Any>);
impl DeepSizeOf for ProstAny {
fn deep_size_of_children(&self, context: &mut Context) -> usize {
self.0.type_url.deep_size_of_children(context) + self.0.value.deep_size_of_children(context)
}
}
#[derive(Debug)]
pub struct ScalarIndexDetailsKey<'a> {
pub uuid: &'a str,
}
impl CacheKey for ScalarIndexDetailsKey<'_> {
type ValueType = ProstAny;
fn key(&self) -> Cow<'_, str> {
Cow::Owned(format!("type/{}", self.uuid))
}
}