gemachain_runtime/
snapshot_archive_info.rs

1//! Information about snapshot archives
2
3use crate::snapshot_utils::{self, ArchiveFormat, Result};
4use gemachain_sdk::{clock::Slot, hash::Hash};
5use std::{cmp::Ordering, path::PathBuf};
6
7/// Trait to query the snapshot archive information
8pub trait SnapshotArchiveInfoGetter {
9    fn snapshot_archive_info(&self) -> &SnapshotArchiveInfo;
10
11    fn path(&self) -> &PathBuf {
12        &self.snapshot_archive_info().path
13    }
14
15    fn slot(&self) -> Slot {
16        self.snapshot_archive_info().slot
17    }
18
19    fn hash(&self) -> &Hash {
20        &self.snapshot_archive_info().hash
21    }
22
23    fn archive_format(&self) -> ArchiveFormat {
24        self.snapshot_archive_info().archive_format
25    }
26}
27
28/// Common information about a snapshot archive
29#[derive(PartialEq, Eq, Debug, Clone)]
30pub struct SnapshotArchiveInfo {
31    /// Path to the snapshot archive file
32    pub path: PathBuf,
33
34    /// Slot that the snapshot was made
35    pub slot: Slot,
36
37    /// Hash of the accounts at this slot
38    pub hash: Hash,
39
40    /// Archive format for the snapshot file
41    pub archive_format: ArchiveFormat,
42}
43
44/// Information about a full snapshot archive: its path, slot, hash, and archive format
45#[derive(PartialEq, Eq, Debug, Clone)]
46pub struct FullSnapshotArchiveInfo(SnapshotArchiveInfo);
47
48impl FullSnapshotArchiveInfo {
49    /// Parse the path to a full snapshot archive and return a new `FullSnapshotArchiveInfo`
50    pub fn new_from_path(path: PathBuf) -> Result<Self> {
51        let filename = snapshot_utils::path_to_file_name_str(path.as_path())?;
52        let (slot, hash, archive_format) =
53            snapshot_utils::parse_full_snapshot_archive_filename(filename)?;
54
55        Ok(Self::new(SnapshotArchiveInfo {
56            path,
57            slot,
58            hash,
59            archive_format,
60        }))
61    }
62
63    pub(crate) fn new(snapshot_archive_info: SnapshotArchiveInfo) -> Self {
64        Self(snapshot_archive_info)
65    }
66}
67
68impl SnapshotArchiveInfoGetter for FullSnapshotArchiveInfo {
69    fn snapshot_archive_info(&self) -> &SnapshotArchiveInfo {
70        &self.0
71    }
72}
73
74impl PartialOrd for FullSnapshotArchiveInfo {
75    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
76        Some(self.cmp(other))
77    }
78}
79
80// Order `FullSnapshotArchiveInfo` by slot (ascending), which practially is sorting chronologically
81impl Ord for FullSnapshotArchiveInfo {
82    fn cmp(&self, other: &Self) -> Ordering {
83        self.slot().cmp(&other.slot())
84    }
85}
86
87/// Information about an incremental snapshot archive: its path, slot, base slot, hash, and archive format
88#[derive(PartialEq, Eq, Debug, Clone)]
89pub struct IncrementalSnapshotArchiveInfo {
90    /// The slot that the incremental snapshot was based from.  This is the same as the full
91    /// snapshot slot used when making the incremental snapshot.
92    base_slot: Slot,
93
94    /// Use the `SnapshotArchiveInfo` struct for the common fields: path, slot, hash, and
95    /// archive_format, but as they pertain to the incremental snapshot.
96    inner: SnapshotArchiveInfo,
97}
98
99impl IncrementalSnapshotArchiveInfo {
100    /// Parse the path to an incremental snapshot archive and return a new `IncrementalSnapshotArchiveInfo`
101    pub fn new_from_path(path: PathBuf) -> Result<Self> {
102        let filename = snapshot_utils::path_to_file_name_str(path.as_path())?;
103        let (base_slot, slot, hash, archive_format) =
104            snapshot_utils::parse_incremental_snapshot_archive_filename(filename)?;
105
106        Ok(Self::new(
107            base_slot,
108            SnapshotArchiveInfo {
109                path,
110                slot,
111                hash,
112                archive_format,
113            },
114        ))
115    }
116
117    pub(crate) fn new(base_slot: Slot, snapshot_archive_info: SnapshotArchiveInfo) -> Self {
118        Self {
119            base_slot,
120            inner: snapshot_archive_info,
121        }
122    }
123
124    pub fn base_slot(&self) -> Slot {
125        self.base_slot
126    }
127}
128
129impl SnapshotArchiveInfoGetter for IncrementalSnapshotArchiveInfo {
130    fn snapshot_archive_info(&self) -> &SnapshotArchiveInfo {
131        &self.inner
132    }
133}
134
135impl PartialOrd for IncrementalSnapshotArchiveInfo {
136    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
137        Some(self.cmp(other))
138    }
139}
140
141// Order `IncrementalSnapshotArchiveInfo` by base slot (ascending), then slot (ascending), which
142// practially is sorting chronologically
143impl Ord for IncrementalSnapshotArchiveInfo {
144    fn cmp(&self, other: &Self) -> Ordering {
145        self.base_slot()
146            .cmp(&other.base_slot())
147            .then(self.slot().cmp(&other.slot()))
148    }
149}