]> Untitled Git - bdk/commitdiff
docs(file_store): Show how to overwrite original file during recovery
author志宇 <hello@evanlinjin.me>
Thu, 6 Mar 2025 00:41:03 +0000 (11:41 +1100)
committer志宇 <hello@evanlinjin.me>
Thu, 6 Mar 2025 00:41:03 +0000 (11:41 +1100)
crates/file_store/src/store.rs

index cad61af1b801b8ee269a5e0c29e3cb7cb5e9e31c..d870e530a8ae8e2d9a61a60ea30a6e8b201c9686 100644 (file)
@@ -109,18 +109,24 @@ where
     /// # let new_len = file.seek(SeekFrom::End(-2))?;
     /// # file.set_len(new_len)?;
     ///
-    /// let mut new_store = match Store::<TestChangeSet>::load(&MAGIC_BYTES, &file_path) {
+    /// let (mut new_store, _aggregate_changeset) =
+    ///     match Store::<TestChangeSet>::load(&MAGIC_BYTES, &file_path) {
     /// #   Ok(_) => panic!("should have errored"),
-    ///     Ok((store, _aggregated_changeset)) => store,
-    ///     Err(StoreErrorWithDump { changeset, .. }) => {
-    ///         let new_file_path = file_path.with_extension("bkp");
-    ///         let mut new_store = Store::create(&MAGIC_BYTES, &new_file_path).unwrap();
-    ///         if let Some(aggregated_changeset) = changeset {
-    ///             new_store.append(&aggregated_changeset)?;
+    ///         Ok((store, changeset)) => (store, changeset),
+    ///         Err(StoreErrorWithDump { changeset, .. }) => {
+    ///             let new_file_path = file_path.with_extension("backup");
+    ///             let mut new_store =
+    ///                 Store::create(&MAGIC_BYTES, &new_file_path).expect("must create new file");
+    ///             if let Some(aggregated_changeset) = changeset {
+    ///                 new_store.append(&aggregated_changeset)?;
+    ///             }
+    ///             // The following will overwrite the original file. You will loose the corrupted
+    ///             // portion of the original file forever.
+    ///             drop(new_store);
+    ///             std::fs::rename(&new_file_path, &file_path)?;
+    ///             Store::load(&MAGIC_BYTES, &file_path).expect("must load new file")
     ///         }
-    ///         new_store
-    ///     }
-    /// };
+    ///     };
     /// #
     /// # assert_eq!(
     /// #     new_store.dump().expect("should dump changeset: {1, 2, 3} "),