MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / load_change

Method load_change

atomic-repository/src/changestore/mod.rs:360–404  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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 ///

Callers 15

run_from_viewFunction · 0.45
run_tagFunction · 0.45
run_previewFunction · 0.45
execute_reviseMethod · 0.45
synthesize_messageMethod · 0.45
get_imported_shasMethod · 0.45
load_change_messageFunction · 0.45
load_change_dataFunction · 0.45
show_change_diffMethod · 0.45
format_defaultMethod · 0.45
runMethod · 0.45
runMethod · 0.45

Calls 8

deserializeFunction · 0.85
getMethod · 0.65
writeMethod · 0.45
cloneMethod · 0.45
change_pathMethod · 0.45
existsMethod · 0.45
to_base32Method · 0.45
insertMethod · 0.45