Load a change from disk. If the change is in the cache, it's returned directly. Otherwise, it's loaded from disk, verified, and added to the cache. # Arguments `hash` - The hash of the change to load # Errors Returns an error if: - The change doesn't exist (`NotFound`) - The file is corrupted (`HashMismatch`) - Deserialization fails (`Serialization`) - An I/O error occurs (`Io`) # Example `
(&self, hash: &Hash)
| 358 | /// println!("Message: {}", change.hashed.header.message); |
| 359 | /// ``` |
| 360 | pub fn load_change(&self, hash: &Hash) -> ChangeStoreResult<Change> { |
| 361 | // Check cache first |
| 362 | { |
| 363 | if let Ok(mut cache) = self.cache.write() { |
| 364 | if let Some(change) = cache.get(hash) { |
| 365 | log::trace!("Cache hit for change {}", hash.to_base32()); |
| 366 | return Ok(change.clone()); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | // Load from disk |
| 372 | let path = self.change_path(hash); |
| 373 | log::debug!( |
| 374 | "Loading change {} from {}", |
| 375 | hash.to_base32(), |
| 376 | path.display() |
| 377 | ); |
| 378 | |
| 379 | if !path.exists() { |
| 380 | return Err(ChangeStoreError::NotFound { |
| 381 | hash: hash.to_base32(), |
| 382 | }); |
| 383 | } |
| 384 | |
| 385 | let file = File::open(&path)?; |
| 386 | let mut reader = BufReader::new(file); |
| 387 | |
| 388 | let (change, computed_hash) = Change::deserialize(&mut reader)?; |
| 389 | |
| 390 | // Verify the hash |
| 391 | if computed_hash != *hash { |
| 392 | return Err(ChangeStoreError::HashMismatch { |
| 393 | expected: hash.to_base32(), |
| 394 | computed: computed_hash.to_base32(), |
| 395 | }); |
| 396 | } |
| 397 | |
| 398 | // Add to cache |
| 399 | if let Ok(mut cache) = self.cache.write() { |
| 400 | cache.insert(*hash, change.clone()); |
| 401 | } |
| 402 | |
| 403 | Ok(change) |
| 404 | } |
| 405 | |
| 406 | /// Copy a content span from a change without cloning the full `Change`. |
| 407 | /// |