@@ -8,14 +8,15 @@ use std::path::{Path, PathBuf};
88use serde:: Deserialize ;
99use url:: Url ;
1010
11- const DEFAULT_FULL_PATH : & str = "./snapshots/mainnet/latest-full_snapshot.bin" ;
12- const DEFAULT_DOWNLOAD_URLS : Vec < DownloadUrls > = Vec :: new ( ) ;
11+ const DEFAULT_ENABLED : bool = false ;
1312const DEFAULT_DEPTH : u32 = 50 ;
1413const DEFAULT_INTERVAL_SYNCED : u32 = 50 ;
1514const DEFAULT_INTERVAL_UNSYNCED : u32 = 1000 ;
15+ const DEFAULT_DOWNLOAD_URLS : Vec < DownloadUrls > = Vec :: new ( ) ;
16+ const DEFAULT_FULL_PATH : & str = "./snapshots/mainnet/latest-full_snapshot.bin" ;
1617
1718/// Contains URLs to download the full and delta snapshot files.
18- #[ derive( Clone , Deserialize , PartialEq ) ]
19+ #[ derive( Clone , Debug , Deserialize , PartialEq ) ]
1920pub struct DownloadUrls {
2021 full : Url ,
2122 delta : Url ,
@@ -33,66 +34,100 @@ impl DownloadUrls {
3334 }
3435}
3536
36- /// Builder for a ` SnapshotConfig`.
37- #[ derive( Default , Deserialize , PartialEq ) ]
37+ /// Builder for a [`SnapshottingConfig`] which is part of the [` SnapshotConfig`] .
38+ #[ derive( Default , Debug , Deserialize , PartialEq ) ]
3839#[ must_use]
39- pub struct SnapshotConfigBuilder {
40- #[ serde( alias = "fullPath" ) ]
41- full_path : Option < PathBuf > ,
42- #[ serde( alias = "deltaPath" ) ]
43- delta_path : Option < PathBuf > ,
44- #[ serde( alias = "downloadUrls" ) ]
45- download_urls : Option < Vec < DownloadUrls > > ,
40+ pub struct SnapshottingConfigBuilder {
41+ enabled : Option < bool > ,
4642 depth : Option < u32 > ,
4743 #[ serde( alias = "intervalSynced" ) ]
4844 interval_synced : Option < u32 > ,
4945 #[ serde( alias = "intervalUnsynced" ) ]
5046 interval_unsynced : Option < u32 > ,
5147}
5248
49+ impl SnapshottingConfigBuilder {
50+ /// Sets whether snapshotting is enabled.
51+ pub fn enabled ( mut self , enabled : bool ) -> Self {
52+ self . enabled . replace ( enabled) ;
53+ self
54+ }
55+
56+ /// Sets the snapshotting depth.
57+ pub fn depth ( mut self , depth : u32 ) -> Self {
58+ self . depth . replace ( depth) ;
59+ self
60+ }
61+
62+ /// Sets the snapshotting interval for a synced node.
63+ pub fn interval_synced ( mut self , interval_synced : u32 ) -> Self {
64+ self . interval_synced . replace ( interval_synced) ;
65+ self
66+ }
67+
68+ /// Sets the snapshotting interval for an unsynced node.
69+ pub fn interval_unsynced ( mut self , interval_unsynced : u32 ) -> Self {
70+ self . interval_unsynced . replace ( interval_unsynced) ;
71+ self
72+ }
73+
74+ /// Produces a [`SnapshottingConfig`] from this builder.
75+ #[ must_use]
76+ pub fn finish ( self ) -> SnapshottingConfig {
77+ SnapshottingConfig {
78+ enabled : self . enabled . unwrap_or ( DEFAULT_ENABLED ) ,
79+ depth : self . depth . unwrap_or ( DEFAULT_DEPTH ) ,
80+ interval_synced : self . interval_synced . unwrap_or ( DEFAULT_INTERVAL_SYNCED ) ,
81+ interval_unsynced : self . interval_unsynced . unwrap_or ( DEFAULT_INTERVAL_UNSYNCED ) ,
82+ }
83+ }
84+ }
85+
86+ /// Builder for a [`SnapshotConfig`] that can also be deserialized from some source.
87+ #[ derive( Default , Debug , Deserialize , PartialEq ) ]
88+ #[ must_use]
89+ pub struct SnapshotConfigBuilder {
90+ #[ serde( alias = "downloadUrls" ) ]
91+ download_urls : Option < Vec < DownloadUrls > > ,
92+ #[ serde( alias = "fullPath" ) ]
93+ full_path : Option < PathBuf > ,
94+ #[ serde( alias = "deltaPath" ) ]
95+ delta_path : Option < PathBuf > ,
96+ #[ serde( alias = "create" ) ]
97+ snapshotting : Option < SnapshottingConfigBuilder > ,
98+ }
99+
53100impl SnapshotConfigBuilder {
54- /// Creates a new `SnapshotConfigBuilder` .
101+ /// Creates a new builder .
55102 pub fn new ( ) -> Self {
56103 Self :: default ( )
57104 }
58105
59- /// Sets the full path of the `SnapshotConfigBuilder` .
106+ /// Sets the path of full snapshots .
60107 pub fn full_path ( mut self , full_path : PathBuf ) -> Self {
61108 self . full_path . replace ( full_path) ;
62109 self
63110 }
64111
65- /// Sets the delta path of the `SnapshotConfigBuilder` .
112+ /// Sets the path of delta snapshots .
66113 pub fn delta_path ( mut self , delta_path : PathBuf ) -> Self {
67114 self . delta_path . replace ( delta_path) ;
68115 self
69116 }
70117
71- /// Sets the download URLs of the `SnapshotConfigBuilder` .
118+ /// Sets the URLs for downloading remotely produced snapshots .
72119 pub fn download_urls ( mut self , download_urls : Vec < DownloadUrls > ) -> Self {
73120 self . download_urls . replace ( download_urls) ;
74121 self
75122 }
76123
77- /// Sets the depth of the `SnapshotConfigBuilder`.
78- pub fn depth ( mut self , depth : u32 ) -> Self {
79- self . depth . replace ( depth) ;
80- self
81- }
82-
83- /// Sets the synced interval of the `SnapshotConfigBuilder`.
84- pub fn interval_synced ( mut self , interval_synced : u32 ) -> Self {
85- self . interval_synced . replace ( interval_synced) ;
86- self
87- }
88-
89- /// Sets the unsynced interval of the `SnapshotConfigBuilder`.
90- pub fn interval_unsynced ( mut self , interval_unsynced : u32 ) -> Self {
91- self . interval_unsynced . replace ( interval_unsynced) ;
124+ /// Sets the `[SnapshottingConfigBuilder`].
125+ pub fn snapshotting ( mut self , builder : SnapshottingConfigBuilder ) -> Self {
126+ self . snapshotting . replace ( builder) ;
92127 self
93128 }
94129
95- /// Finishes the `SnapshotConfigBuilder` into a `SnapshotConfig` .
130+ /// Produces a [`SnapshotConfig`] from this builder .
96131 #[ must_use]
97132 pub fn finish ( self ) -> SnapshotConfig {
98133 SnapshotConfig {
@@ -101,22 +136,18 @@ impl SnapshotConfigBuilder {
101136 . unwrap_or_else ( || PathBuf :: from ( DEFAULT_FULL_PATH . to_string ( ) ) ) ,
102137 delta_path : self . delta_path ,
103138 download_urls : self . download_urls . unwrap_or ( DEFAULT_DOWNLOAD_URLS ) ,
104- depth : self . depth . unwrap_or ( DEFAULT_DEPTH ) ,
105- interval_synced : self . interval_synced . unwrap_or ( DEFAULT_INTERVAL_SYNCED ) ,
106- interval_unsynced : self . interval_unsynced . unwrap_or ( DEFAULT_INTERVAL_UNSYNCED ) ,
139+ snapshotting : self . snapshotting . unwrap_or_default ( ) . finish ( ) ,
107140 }
108141 }
109142}
110143
111- /// A snapshot configuration.
112- #[ derive( Clone ) ]
144+ /// The configuration of downloading and creating snapshots .
145+ #[ derive( Clone , Debug ) ]
113146pub struct SnapshotConfig {
114147 full_path : PathBuf ,
115148 delta_path : Option < PathBuf > ,
116149 download_urls : Vec < DownloadUrls > ,
117- depth : u32 ,
118- interval_synced : u32 ,
119- interval_unsynced : u32 ,
150+ snapshotting : SnapshottingConfig ,
120151}
121152
122153impl SnapshotConfig {
@@ -140,17 +171,38 @@ impl SnapshotConfig {
140171 & self . download_urls
141172 }
142173
143- /// Returns the depth of the `SnapshotConfig`.
174+ /// Returns the [`SnapshottingConfig`].
175+ pub fn snapshotting ( & self ) -> & SnapshottingConfig {
176+ & self . snapshotting
177+ }
178+ }
179+
180+ /// The configuration for creating snapshots.
181+ #[ derive( Clone , Debug ) ]
182+ pub struct SnapshottingConfig {
183+ enabled : bool ,
184+ depth : u32 ,
185+ interval_synced : u32 ,
186+ interval_unsynced : u32 ,
187+ }
188+
189+ impl SnapshottingConfig {
190+ /// Returns whether pruning based on milestone indexes is enabled.
191+ pub fn enabled ( & self ) -> bool {
192+ self . enabled
193+ }
194+
195+ /// Returns the snapshot depth.
144196 pub fn depth ( & self ) -> u32 {
145197 self . depth
146198 }
147199
148- /// Returns the synced interval of the `SnapshotConfig` .
200+ /// Returns the snapshot interval for a synced node .
149201 pub fn interval_synced ( & self ) -> u32 {
150202 self . interval_synced
151203 }
152204
153- /// Returns the unsynced interval of the `SnapshotConfig` .
205+ /// Returns the snapshot interval for an unsynced node .
154206 pub fn interval_unsynced ( & self ) -> u32 {
155207 self . interval_unsynced
156208 }
0 commit comments