krishvishal commented on code in PR #2916:
URL: https://github.com/apache/iggy/pull/2916#discussion_r2935583979
##########
core/metadata/src/impls/metadata.rs:
##########
@@ -52,6 +53,47 @@ impl IggySnapshot {
pub const fn snapshot(&self) -> &MetadataSnapshot {
&self.snapshot
}
+
+ /// Persist the snapshot to disk.
+ ///
+ /// # Errors
+ /// Returns `SnapshotError` if serialization or I/O fails.
+ pub fn persist(&self, path: &Path) -> Result<(), SnapshotError> {
+ use std::fs;
+ use std::io::Write;
+
+ let encoded = self.encode()?;
+
+ let tmp_path = path.with_extension("bin.tmp");
+
+ let mut file = fs::File::create(&tmp_path)?;
+ file.write_all(&encoded)?;
+ file.sync_all()?;
+ drop(file);
+
+ fs::rename(&tmp_path, path)?;
+
+ // Fsync the parent directory to ensure the rename is durable.
+ if let Some(parent) = path.parent() {
+ let dir = fs::File::open(parent)?;
+ dir.sync_all()?;
+ }
+
+ Ok(())
+ }
+
+ /// Load a snapshot from disk.
+ ///
+ /// # Errors
+ /// Returns `SnapshotError` if the file cannot be read or deserialization
fails.
+ pub fn load(path: &Path) -> Result<Self, SnapshotError> {
+ let data = std::fs::read(path)?;
+
+ // TODO: when checksum is added we need to check
Review Comment:
Done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]