use std::any::Any;
use std::fmt;
use std::fmt::Debug;
use vortex_session::SessionExt;
use vortex_session::SessionVar;
use crate::footer::Footer;
pub(super) struct MultiFileSession {
footer_cache: moka::sync::Cache<String, Footer>,
}
impl Default for MultiFileSession {
fn default() -> Self {
Self {
footer_cache: moka::sync::Cache::builder()
.max_capacity(100 * 1024) .weigher(|_k, footer: &Footer| {
footer
.approx_byte_size()
.and_then(|bytes| u32::try_from(bytes / 1024).ok())
.unwrap_or(10)
})
.build(),
}
}
}
impl Debug for MultiFileSession {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MultiFileSession")
.field("footer_cache_entry_count", &self.footer_cache.entry_count())
.finish()
}
}
impl MultiFileSession {
pub fn get_footer(&self, path: &str) -> Option<Footer> {
self.footer_cache.get(path)
}
pub fn put_footer(&self, path: &str, footer: Footer) {
self.footer_cache.insert(path.to_string(), footer);
}
}
impl SessionVar for MultiFileSession {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
pub(super) trait MultiFileSessionExt: SessionExt {
fn multi_file(&self) -> vortex_session::Ref<'_, MultiFileSession> {
self.get::<MultiFileSession>()
}
}
impl<S: SessionExt> MultiFileSessionExt for S {}