use crate::allocator::BlockAllocator;
use crate::error::CacheError;
use crate::manager::PagedKvCacheManager;
use serde::{Deserialize, Serialize};
use spec_stream_core::TokenId;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PrefixSnapshotEntry {
pub hash: u64,
pub tokens: Vec<TokenId>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PrefixCacheSnapshot {
pub block_size: usize,
pub entries: Vec<PrefixSnapshotEntry>,
}
impl PrefixCacheSnapshot {
pub fn new(block_size: usize) -> Self {
Self {
block_size,
entries: Vec::new(),
}
}
pub fn to_json(&self) -> Result<String, CacheError> {
serde_json::to_string_pretty(self)
.map_err(|e| CacheError::SerializationError(e.to_string()))
}
pub fn from_json(json_str: &str) -> Result<Self, CacheError> {
serde_json::from_str(json_str).map_err(|e| CacheError::SerializationError(e.to_string()))
}
}
impl BlockAllocator {
pub fn export_prefix_snapshot(&self) -> PrefixCacheSnapshot {
let mut snapshot = PrefixCacheSnapshot::new(self.block_size);
for (&hash, &block_id) in &self.prefix_cache {
if let Some(block) = self.blocks.get(block_id) {
snapshot.entries.push(PrefixSnapshotEntry {
hash,
tokens: block.tokens.clone(),
});
}
}
snapshot
}
pub fn import_prefix_snapshot(
&mut self,
snapshot: &PrefixCacheSnapshot,
) -> Result<usize, CacheError> {
let mut imported = 0;
for entry in &snapshot.entries {
if !self.prefix_cache.contains_key(&entry.hash) && !self.free_list.is_empty() {
let block_id = self.allocate()?;
{
let block = self.get_block_mut(block_id)?;
block.tokens = entry.tokens.clone();
block.num_tokens = entry.tokens.len();
block.prefix_hash = Some(entry.hash);
block.ref_count = 1;
}
self.prefix_cache.insert(entry.hash, block_id);
imported += 1;
}
}
Ok(imported)
}
}
impl PagedKvCacheManager {
pub fn export_snapshot(&self) -> PrefixCacheSnapshot {
self.allocator.lock().export_prefix_snapshot()
}
pub fn import_snapshot(&self, snapshot: &PrefixCacheSnapshot) -> Result<usize, CacheError> {
self.allocator.lock().import_prefix_snapshot(snapshot)
}
pub fn save_snapshot_json(&self) -> Result<String, CacheError> {
self.export_snapshot().to_json()
}
pub fn load_snapshot_json(&self, json_str: &str) -> Result<usize, CacheError> {
let snapshot = PrefixCacheSnapshot::from_json(json_str)?;
self.import_snapshot(&snapshot)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_prefix_cache_snapshot_roundtrip() {
let block_size = 4;
let mut allocator = BlockAllocator::new(10, block_size);
let block_id = allocator.allocate().unwrap();
let tokens = vec![11, 22, 33, 44];
{
let block = allocator.get_block_mut(block_id).unwrap();
block.tokens = tokens.clone();
block.num_tokens = 4;
}
let hash = 0xDEADBEEFCAFE;
allocator.register_prefix(hash, block_id);
let snapshot = allocator.export_prefix_snapshot();
assert_eq!(snapshot.entries.len(), 1);
let json = snapshot.to_json().unwrap();
let mut new_allocator = BlockAllocator::new(10, block_size);
let loaded_snapshot = PrefixCacheSnapshot::from_json(&json).unwrap();
let imported = new_allocator
.import_prefix_snapshot(&loaded_snapshot)
.unwrap();
assert_eq!(imported, 1);
assert_eq!(new_allocator.num_prefix_entries(), 1);
assert!(new_allocator.find_prefix(hash).is_some());
}
}