Serialize to bytes for checkpoint persistence.
(&self)
| 142 | |
| 143 | /// Serialize to bytes for checkpoint persistence. |
| 144 | pub fn checkpoint_to_bytes(&self) -> Vec<u8> { |
| 145 | let snapshot = SparseIndexSnapshot { |
| 146 | next_id: self.next_id, |
| 147 | documents: self |
| 148 | .doc_id_reverse |
| 149 | .iter() |
| 150 | .map(|(&id, name)| { |
| 151 | let dims = self.doc_dims.get(&id).cloned().unwrap_or_default(); |
| 152 | let entries: Vec<(u32, f32)> = dims |
| 153 | .iter() |
| 154 | .filter_map(|&dim| { |
| 155 | self.postings.get(&dim).and_then(|list| { |
| 156 | list.iter() |
| 157 | .find(|&&(doc, _)| doc == id) |
| 158 | .map(|&(_, w)| (dim, w)) |
| 159 | }) |
| 160 | }) |
| 161 | .collect(); |
| 162 | SparseDocSnapshot { |
| 163 | internal_id: id, |
| 164 | doc_id: name.clone(), |
| 165 | entries, |
| 166 | } |
| 167 | }) |
| 168 | .collect(), |
| 169 | }; |
| 170 | zerompk::to_msgpack_vec(&snapshot).unwrap_or_default() |
| 171 | } |
| 172 | |
| 173 | /// Restore from checkpoint bytes. |
| 174 | pub fn from_checkpoint(bytes: &[u8]) -> Option<Self> { |