Skip to content

Commit 35de1ef

Browse files
kimgefjon
andauthored
snapshot: pub access to snapshot file + object repo (#1709)
Signed-off-by: Kim Altintop <kim@eagain.io> Co-authored-by: Phoebe Goldman <phoebe@clockworklabs.io>
1 parent 214eb7e commit 35de1ef

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

crates/snapshot/src/lib.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl Snapshot {
303303
/// - `path` does not refer to a readable file.
304304
/// - The file at `path` is corrupted,
305305
/// as detected by comparing the hash of its bytes to a hash recorded in the file.
306-
fn read_from_file(path: &Path) -> Result<Self, SnapshotError> {
306+
pub fn read_from_file(path: &Path) -> Result<Self, SnapshotError> {
307307
let err_read_object = |cause| SnapshotError::ReadObject {
308308
ty: ObjectType::Snapshot,
309309
source_repo: path.to_path_buf(),
@@ -562,17 +562,53 @@ impl SnapshotRepository {
562562
}
563563
}
564564

565-
fn snapshot_dir_path(&self, tx_offset: TxOffset) -> PathBuf {
565+
/// Get the path to the directory which would contain the snapshot of transaction `tx_offset`.
566+
///
567+
/// The directory may not exist if no snapshot has been taken of `tx_offset`.
568+
///
569+
/// The directory may exist but be locked or incomplete
570+
/// if a file with the same name and the extension `.lock` exists.
571+
/// In this case, callers should treat the snapshot as if it did not exist.
572+
///
573+
/// Use `[Self::all_snapshots]` to get `tx_offsets` which will return valid extant paths.
574+
/// `[Self::all_snapshots]` will never return a `tx_offset` for a locked or incomplete snapshot.
575+
/// `[Self::all_snapshots]` does not validate the contents of snapshots,
576+
/// so it may return a `tx_offset` whose snapshot is corrupted.
577+
///
578+
/// Any mutations to any files contained in the returned directory
579+
/// will likely corrupt the snapshot,
580+
/// causing attempts to reconstruct it to fail.
581+
pub fn snapshot_dir_path(&self, tx_offset: TxOffset) -> PathBuf {
566582
let dir_name = format!("{tx_offset:0>20}.{SNAPSHOT_DIR_EXT}");
567583
self.root.join(dir_name)
568584
}
569585

570-
fn snapshot_file_path(tx_offset: TxOffset, snapshot_dir: &Path) -> PathBuf {
586+
/// Given `snapshot_dir` as the result of `self.snapshot_dir_path(tx_offset)`,
587+
/// get the path to the root snapshot file, which contains a serialized [`Snapshot`].
588+
///
589+
/// This method does not validate that the `snapshot_dir` exists or is valid,
590+
/// so the returned snapshot file path may be nonexistant or refer to a locked or incomplete snapshot file.
591+
/// This method also does not check if the snapshot file, or any other part of the snapshot, is corrupted.
592+
/// Consumers should verify the hashes stored in the snapshot file and object repository.
593+
///
594+
/// Any mutations to the snapshot file will likely render the snapshot corrupted,
595+
/// causing future attempts to reconstruct it to fail.
596+
pub fn snapshot_file_path(tx_offset: TxOffset, snapshot_dir: &Path) -> PathBuf {
571597
let file_name = format!("{tx_offset:0>20}.{SNAPSHOT_FILE_EXT}");
572598
snapshot_dir.join(file_name)
573599
}
574600

575-
fn object_repo(snapshot_dir: &Path) -> Result<DirTrie, std::io::Error> {
601+
/// Given `snapshot_dir` as the result of [`Self::snapshot_dir_path`],
602+
/// get the [`DirTrie`] which contains serialized objects (pages and large blobs)
603+
/// referenced by the [`Snapshot`] contained in the [`Self::snapshot_file_path`].
604+
///
605+
/// Consequences are unspecified if this method is called from outside this crate
606+
/// on a non-existent, locked or incomplete `snapshot_dir`.
607+
///
608+
/// Any mutations to the returned [`DirTrie`] or its contents
609+
/// will likely render the snapshot corrupted,
610+
/// causing future attempts to reconstruct it to fail.
611+
pub fn object_repo(snapshot_dir: &Path) -> Result<DirTrie, std::io::Error> {
576612
DirTrie::open(snapshot_dir.join("objects"))
577613
}
578614

0 commit comments

Comments
 (0)