@@ -193,8 +193,7 @@ impl CacheHashDataFile {
193193pub ( crate ) struct CacheHashData {
194194 cache_dir : PathBuf ,
195195 pre_existing_cache_files : Arc < Mutex < HashSet < PathBuf > > > ,
196- /// Decides which old cache files to delete. See `delete_old_cache_files()` for more info.
197- storages_start_slot : Option < Slot > ,
196+ deletion_policy : DeletionPolicy ,
198197 pub stats : Arc < CacheHashDataStats > ,
199198}
200199
@@ -206,15 +205,15 @@ impl Drop for CacheHashData {
206205}
207206
208207impl CacheHashData {
209- pub ( crate ) fn new ( cache_dir : PathBuf , storages_start_slot : Option < Slot > ) -> CacheHashData {
208+ pub ( crate ) fn new ( cache_dir : PathBuf , deletion_policy : DeletionPolicy ) -> CacheHashData {
210209 std:: fs:: create_dir_all ( & cache_dir) . unwrap_or_else ( |err| {
211210 panic ! ( "error creating cache dir {}: {err}" , cache_dir. display( ) )
212211 } ) ;
213212
214213 let result = CacheHashData {
215214 cache_dir,
216215 pre_existing_cache_files : Arc :: new ( Mutex :: new ( HashSet :: default ( ) ) ) ,
217- storages_start_slot ,
216+ deletion_policy ,
218217 stats : Arc :: default ( ) ,
219218 } ;
220219
@@ -229,21 +228,24 @@ impl CacheHashData {
229228 let mut old_cache_files =
230229 std:: mem:: take ( & mut * self . pre_existing_cache_files . lock ( ) . unwrap ( ) ) ;
231230
232- // If `storages_start_slot` is None, we're doing a full accounts hash calculation, and thus
233- // all unused cache files can be deleted.
234- // If `storages_start_slot` is Some, we're doing an incremental accounts hash calculation,
235- // and we only want to delete the unused cache files *that IAH considered*.
236- if let Some ( storages_start_slot) = self . storages_start_slot {
237- old_cache_files. retain ( |old_cache_file| {
238- let Some ( parsed_filename) = parse_filename ( old_cache_file) else {
239- // if parsing the cache filename fails, we *do* want to delete it
240- return true ;
241- } ;
242-
243- // if the old cache file is in the incremental accounts hash calculation range,
244- // then delete it
245- parsed_filename. slot_range_start >= storages_start_slot
246- } ) ;
231+ match self . deletion_policy {
232+ DeletionPolicy :: AllUnused => {
233+ // no additional work to do here; we will delete everything in `old_cache_files`
234+ }
235+ DeletionPolicy :: UnusedAtLeast ( storages_start_slot) => {
236+ // when calculating an incremental accounts hash, we only want to delete the unused
237+ // cache files *that IAH considered*
238+ old_cache_files. retain ( |old_cache_file| {
239+ let Some ( parsed_filename) = parse_filename ( old_cache_file) else {
240+ // if parsing the cache filename fails, we *do* want to delete it
241+ return true ;
242+ } ;
243+
244+ // if the old cache file is in the incremental accounts hash calculation range,
245+ // then delete it
246+ parsed_filename. slot_range_start >= storages_start_slot
247+ } ) ;
248+ }
247249 }
248250
249251 if !old_cache_files. is_empty ( ) {
@@ -410,6 +412,19 @@ fn parse_filename(cache_filename: impl AsRef<Path>) -> Option<ParsedFilename> {
410412 } )
411413}
412414
415+ /// Decides which old cache files to delete
416+ ///
417+ /// See `delete_old_cache_files()` for more info.
418+ #[ derive( Debug , Copy , Clone , Eq , PartialEq ) ]
419+ pub enum DeletionPolicy {
420+ /// Delete *all* the unused cache files
421+ /// Should be used when calculating full accounts hash
422+ AllUnused ,
423+ /// Delete *only* the unused cache files with starting slot range *at least* this slot
424+ /// Should be used when calculating incremental accounts hash
425+ UnusedAtLeast ( Slot ) ,
426+ }
427+
413428#[ cfg( test) ]
414429mod tests {
415430 use { super :: * , crate :: accounts_hash:: AccountHash , rand:: Rng } ;
@@ -477,7 +492,8 @@ mod tests {
477492 data_this_pass. push ( this_bin_data) ;
478493 }
479494 }
480- let cache = CacheHashData :: new ( cache_dir. clone ( ) , None ) ;
495+ let cache =
496+ CacheHashData :: new ( cache_dir. clone ( ) , DeletionPolicy :: AllUnused ) ;
481497 let file_name = PathBuf :: from ( "test" ) ;
482498 cache. save ( & file_name, & data_this_pass) . unwrap ( ) ;
483499 cache. get_cache_files ( ) ;
0 commit comments