From 54251a7c9fdf08bced132be3851207917cbc36ab Mon Sep 17 00:00:00 2001 From: =?utf8?q?=E5=BF=97=E5=AE=87?= Date: Thu, 6 Mar 2025 11:41:03 +1100 Subject: [PATCH] docs(file_store): Show how to overwrite original file during recovery --- crates/file_store/src/store.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/crates/file_store/src/store.rs b/crates/file_store/src/store.rs index cad61af1..d870e530 100644 --- a/crates/file_store/src/store.rs +++ b/crates/file_store/src/store.rs @@ -109,18 +109,24 @@ where /// # let new_len = file.seek(SeekFrom::End(-2))?; /// # file.set_len(new_len)?; /// - /// let mut new_store = match Store::::load(&MAGIC_BYTES, &file_path) { + /// let (mut new_store, _aggregate_changeset) = + /// match Store::::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} "), -- 2.49.0