Save a change to disk. The change is serialized, written to a temporary file, and then atomically renamed to its final path. The change is also added to the cache. # Arguments `change` - The change to save # Returns The hash of the saved change. # Errors Returns an error if: - The directory cannot be created - The file cannot be written - Serialization fails # Example ```rust,ignore let c
(&self, change: &Change)
| 302 | /// println!("Saved change: {}", hash.to_base32()); |
| 303 | /// ``` |
| 304 | pub fn save_change(&self, change: &Change) -> ChangeStoreResult<Hash> { |
| 305 | // Create a temporary file in the changes directory |
| 306 | let temp_file = tempfile::NamedTempFile::new_in(&self.changes_dir)?; |
| 307 | |
| 308 | // Serialize the change and get its hash |
| 309 | let hash = { |
| 310 | let mut writer = BufWriter::new(&temp_file); |
| 311 | change.serialize(&mut writer)? |
| 312 | }; |
| 313 | |
| 314 | // Ensure the target directory exists |
| 315 | let target_path = self.change_path(&hash); |
| 316 | if let Some(parent) = target_path.parent() { |
| 317 | fs::create_dir_all(parent)?; |
| 318 | } |
| 319 | |
| 320 | // Atomically move to the final location |
| 321 | temp_file.persist(&target_path)?; |
| 322 | |
| 323 | // Add to cache |
| 324 | if let Ok(mut cache) = self.cache.write() { |
| 325 | cache.insert(hash, change.clone()); |
| 326 | } |
| 327 | |
| 328 | log::debug!( |
| 329 | "Saved change {} to {}", |
| 330 | hash.to_base32(), |
| 331 | target_path.display() |
| 332 | ); |
| 333 | |
| 334 | Ok(hash) |
| 335 | } |
| 336 | |
| 337 | /// Load a change from disk. |
| 338 | /// |