spec_stream_cache/
snapshot.rs1use crate::allocator::BlockAllocator;
2use crate::error::CacheError;
3use crate::manager::PagedKvCacheManager;
4use serde::{Deserialize, Serialize};
5use spec_stream_core::TokenId;
6
7#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9pub struct PrefixSnapshotEntry {
10 pub hash: u64,
12 pub tokens: Vec<TokenId>,
14}
15
16#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
18pub struct PrefixCacheSnapshot {
19 pub block_size: usize,
21 pub entries: Vec<PrefixSnapshotEntry>,
23}
24
25impl PrefixCacheSnapshot {
26 pub fn new(block_size: usize) -> Self {
27 Self {
28 block_size,
29 entries: Vec::new(),
30 }
31 }
32
33 pub fn to_json(&self) -> Result<String, CacheError> {
35 serde_json::to_string_pretty(self)
36 .map_err(|e| CacheError::SerializationError(e.to_string()))
37 }
38
39 pub fn from_json(json_str: &str) -> Result<Self, CacheError> {
41 serde_json::from_str(json_str).map_err(|e| CacheError::SerializationError(e.to_string()))
42 }
43}
44
45impl BlockAllocator {
46 pub fn export_prefix_snapshot(&self) -> PrefixCacheSnapshot {
48 let mut snapshot = PrefixCacheSnapshot::new(self.block_size);
49
50 for (&hash, &block_id) in &self.prefix_cache {
51 if let Some(block) = self.blocks.get(block_id) {
52 snapshot.entries.push(PrefixSnapshotEntry {
53 hash,
54 tokens: block.tokens.clone(),
55 });
56 }
57 }
58
59 snapshot
60 }
61
62 pub fn import_prefix_snapshot(
65 &mut self,
66 snapshot: &PrefixCacheSnapshot,
67 ) -> Result<usize, CacheError> {
68 let mut imported = 0;
69
70 for entry in &snapshot.entries {
71 if !self.prefix_cache.contains_key(&entry.hash) && !self.free_list.is_empty() {
73 let block_id = self.allocate()?;
74 {
75 let block = self.get_block_mut(block_id)?;
76 block.tokens = entry.tokens.clone();
77 block.num_tokens = entry.tokens.len();
78 block.prefix_hash = Some(entry.hash);
79 block.ref_count = 1;
81 }
82 self.prefix_cache.insert(entry.hash, block_id);
83 imported += 1;
84 }
85 }
86
87 Ok(imported)
88 }
89}
90
91impl PagedKvCacheManager {
92 pub fn export_snapshot(&self) -> PrefixCacheSnapshot {
94 self.allocator.lock().export_prefix_snapshot()
95 }
96
97 pub fn import_snapshot(&self, snapshot: &PrefixCacheSnapshot) -> Result<usize, CacheError> {
99 self.allocator.lock().import_prefix_snapshot(snapshot)
100 }
101
102 pub fn save_snapshot_json(&self) -> Result<String, CacheError> {
104 self.export_snapshot().to_json()
105 }
106
107 pub fn load_snapshot_json(&self, json_str: &str) -> Result<usize, CacheError> {
109 let snapshot = PrefixCacheSnapshot::from_json(json_str)?;
110 self.import_snapshot(&snapshot)
111 }
112}
113
114#[cfg(test)]
115mod tests {
116 use super::*;
117
118 #[test]
119 fn test_prefix_cache_snapshot_roundtrip() {
120 let block_size = 4;
121 let mut allocator = BlockAllocator::new(10, block_size);
122
123 let block_id = allocator.allocate().unwrap();
125 let tokens = vec![11, 22, 33, 44];
126 {
127 let block = allocator.get_block_mut(block_id).unwrap();
128 block.tokens = tokens.clone();
129 block.num_tokens = 4;
130 }
131 let hash = 0xDEADBEEFCAFE;
132 allocator.register_prefix(hash, block_id);
133
134 let snapshot = allocator.export_prefix_snapshot();
136 assert_eq!(snapshot.entries.len(), 1);
137 let json = snapshot.to_json().unwrap();
138
139 let mut new_allocator = BlockAllocator::new(10, block_size);
141 let loaded_snapshot = PrefixCacheSnapshot::from_json(&json).unwrap();
142 let imported = new_allocator
143 .import_prefix_snapshot(&loaded_snapshot)
144 .unwrap();
145
146 assert_eq!(imported, 1);
147 assert_eq!(new_allocator.num_prefix_entries(), 1);
148 assert!(new_allocator.find_prefix(hash).is_some());
149 }
150}