use std::future::Future;
use std::pin::Pin;
#[cfg(all(feature = "compression", not(target_arch = "wasm32")))]
use std::sync::Arc;
#[cfg(all(feature = "compression", not(target_arch = "wasm32")))]
use pjson_rs_domain::value_objects::SessionId;
#[cfg(all(feature = "compression", not(target_arch = "wasm32")))]
use crate::compression::zstd::ZstdDictionary;
use crate::Result;
pub type DictionaryFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T>> + Send + 'a>>;
pub trait DictionaryStore: Send + Sync {
#[cfg(all(feature = "compression", not(target_arch = "wasm32")))]
fn get_dictionary<'a>(
&'a self,
session_id: SessionId,
) -> DictionaryFuture<'a, Option<Arc<ZstdDictionary>>>;
#[cfg(all(feature = "compression", not(target_arch = "wasm32")))]
fn train_if_ready<'a>(
&'a self,
session_id: SessionId,
sample: Vec<u8>,
) -> DictionaryFuture<'a, ()>;
}
#[derive(Debug, Default, Clone)]
pub struct NoopDictionaryStore;
impl DictionaryStore for NoopDictionaryStore {
#[cfg(all(feature = "compression", not(target_arch = "wasm32")))]
fn get_dictionary<'a>(
&'a self,
_: SessionId,
) -> DictionaryFuture<'a, Option<Arc<ZstdDictionary>>> {
Box::pin(async { Ok(None) })
}
#[cfg(all(feature = "compression", not(target_arch = "wasm32")))]
fn train_if_ready<'a>(&'a self, _: SessionId, _: Vec<u8>) -> DictionaryFuture<'a, ()> {
Box::pin(async { Ok(()) })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(all(feature = "compression", not(target_arch = "wasm32")))]
#[tokio::test]
async fn test_noop_get_dictionary_returns_none() {
let store = NoopDictionaryStore;
let sid = SessionId::new();
let result = store.get_dictionary(sid).await.unwrap();
assert!(result.is_none());
}
#[cfg(all(feature = "compression", not(target_arch = "wasm32")))]
#[tokio::test]
async fn test_noop_train_if_ready_is_ok() {
let store = NoopDictionaryStore;
let sid = SessionId::new();
let result = store.train_if_ready(sid, b"sample data".to_vec()).await;
assert!(result.is_ok());
}
#[test]
fn test_noop_is_send_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<NoopDictionaryStore>();
}
}