gemachain_runtime/
snapshot_archive_info.rs1use crate::snapshot_utils::{self, ArchiveFormat, Result};
4use gemachain_sdk::{clock::Slot, hash::Hash};
5use std::{cmp::Ordering, path::PathBuf};
6
7pub 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#[derive(PartialEq, Eq, Debug, Clone)]
30pub struct SnapshotArchiveInfo {
31 pub path: PathBuf,
33
34 pub slot: Slot,
36
37 pub hash: Hash,
39
40 pub archive_format: ArchiveFormat,
42}
43
44#[derive(PartialEq, Eq, Debug, Clone)]
46pub struct FullSnapshotArchiveInfo(SnapshotArchiveInfo);
47
48impl FullSnapshotArchiveInfo {
49 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
80impl Ord for FullSnapshotArchiveInfo {
82 fn cmp(&self, other: &Self) -> Ordering {
83 self.slot().cmp(&other.slot())
84 }
85}
86
87#[derive(PartialEq, Eq, Debug, Clone)]
89pub struct IncrementalSnapshotArchiveInfo {
90 base_slot: Slot,
93
94 inner: SnapshotArchiveInfo,
97}
98
99impl IncrementalSnapshotArchiveInfo {
100 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
141impl 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}