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)
| 408 | /// println!("Message: {}", change.hashed.header.message); |
| 409 | /// ``` |
| 410 | pub fn load_change(&self, hash: &Hash) -> ChangeStoreResult<Change> { |
| 411 | // Check cache first |
| 412 | { |
| 413 | if let Ok(mut cache) = self.cache.write() { |
| 414 | if let Some(change) = cache.get(hash) { |
| 415 | log::trace!("Cache hit for change {}", hash.to_base32()); |
| 416 | return Ok(change.clone()); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | // Load from disk |
| 422 | let path = self.change_path(hash); |
| 423 | log::debug!( |
| 424 | "Loading change {} from {}", |
| 425 | hash.to_base32(), |
| 426 | path.display() |
| 427 | ); |
| 428 | |
| 429 | if !path.exists() { |
| 430 | return Err(ChangeStoreError::NotFound { |
| 431 | hash: hash.to_base32(), |
| 432 | }); |
| 433 | } |
| 434 | |
| 435 | let file = File::open(&path)?; |
| 436 | let mut reader = BufReader::new(file); |
| 437 | |
| 438 | let (change, computed_hash) = Change::deserialize(&mut reader)?; |
| 439 | |
| 440 | // Verify the hash |
| 441 | if computed_hash != *hash { |
| 442 | return Err(ChangeStoreError::HashMismatch { |
| 443 | expected: hash.to_base32(), |
| 444 | computed: computed_hash.to_base32(), |
| 445 | }); |
| 446 | } |
| 447 | |
| 448 | // Add to cache |
| 449 | if let Ok(mut cache) = self.cache.write() { |
| 450 | cache.insert(*hash, change.clone()); |
| 451 | } |
| 452 | |
| 453 | Ok(change) |
| 454 | } |
| 455 | |
| 456 | /// Read the current source even on an operation-local cache hit. Identical |
| 457 | /// bytes reuse the immutable checked decode; changed notes, corruption, |