1- use crate :: general:: { handle, store} ;
2- use std:: path:: PathBuf ;
1+ use std:: {
2+ path:: PathBuf ,
3+ sync:: { atomic:: Ordering , Arc } ,
4+ } ;
35
4- use crate :: general :: store :: StateId ;
5- use crate :: RefreshMode ;
6- use std :: sync :: atomic :: Ordering ;
7- use std :: sync :: Arc ;
6+ use crate :: {
7+ general :: { handle , store , store :: StateId } ,
8+ RefreshMode ,
9+ } ;
810
911pub ( crate ) enum Outcome {
1012 /// Drop all data and fully replace it with `indices`.
1113 /// This happens if we have witnessed a generational change invalidating all of our ids and causing currently loaded
1214 /// indices and maps to be dropped.
13- Replace {
14- indices : Vec < handle:: IndexLookup > , // should probably be SmallVec to get around most allocations
15- loose_dbs : Arc < Vec < crate :: loose:: Store > > ,
16- marker : store:: SlotIndexMarker , // use to show where the caller left off last time
17- } ,
15+ Replace ( Snapshot ) ,
1816 /// Despite all values being full copies, indices are still compatible to what was before. This also means
1917 /// the caller can continue searching the added indices and loose-dbs.
2018 /// Or in other words, new indices were only added to the known list, and what was seen before is known not to have changed.
2119 /// Besides that, the full internal state can be replaced as with `Replace`.
22- ReplaceStable {
23- indices : Vec < handle :: IndexLookup > , // should probably be SmallVec to get around most allocations
24- loose_dbs : Arc < Vec < crate :: loose :: Store > > ,
25- marker : store :: SlotIndexMarker , // use to show where the caller left off last time
26- } ,
27- /// No new indices to look at, caller should give up
28- NoMoreIndices ,
20+ ReplaceStable ( Snapshot ) ,
21+ }
22+
23+ pub ( crate ) struct Snapshot {
24+ indices : Vec < handle :: IndexLookup > , // should probably be SmallVec to get around most allocations
25+ loose_dbs : Arc < Vec < crate :: loose :: Store > > ,
26+ marker : store :: SlotIndexMarker , // use to show where the caller left off last time
2927}
3028
3129impl super :: Store {
30+ /// If `None` is returned, there is new indices and the caller should give up. This is a possibility even if it's allowed to refresh
31+ /// as here might be no change to pick up.
3232 pub ( crate ) fn load_next_indices (
3333 & self ,
3434 refresh_mode : RefreshMode ,
3535 marker : Option < store:: SlotIndexMarker > ,
36- ) -> std:: io:: Result < Outcome > {
36+ ) -> std:: io:: Result < Option < Outcome > > {
3737 let index = self . index . load ( ) ;
3838 let state_id = index. state_id ( ) ;
39- if index. loose_dbs . is_empty ( ) {
39+ if ! index. is_initialized ( ) {
4040 // TODO: figure out what kind of refreshes we need. This one loads in the initial slot map, but I think this cost is paid
4141 // in full during instantiation.
4242 return self . consolidate_with_disk_state ( state_id) ;
4343 }
4444
45- Ok ( match marker {
45+ Ok ( Some ( match marker {
4646 Some ( marker) => {
4747 if marker. generation != index. generation {
4848 self . collect_replace_outcome ( false /*stable*/ )
@@ -51,19 +51,19 @@ impl super::Store {
5151
5252 // …and if that didn't yield anything new consider refreshing our disk state.
5353 match refresh_mode {
54- RefreshMode :: Never => Outcome :: NoMoreIndices ,
54+ RefreshMode :: Never => return Ok ( None ) ,
5555 RefreshMode :: AfterAllIndicesLoaded => return self . consolidate_with_disk_state ( state_id) ,
5656 }
5757 } else {
5858 self . collect_replace_outcome ( true /*stable*/ )
5959 }
6060 }
6161 None => self . collect_replace_outcome ( false /*stable*/ ) ,
62- } )
62+ } ) )
6363 }
6464
6565 /// refresh and possibly clear out our existing data structures, causing all pack ids to be invalidated.
66- fn consolidate_with_disk_state ( & self , seen : StateId ) -> std:: io:: Result < Outcome > {
66+ fn consolidate_with_disk_state ( & self , seen : StateId ) -> std:: io:: Result < Option < Outcome > > {
6767 let objects_directory = self . path . lock ( ) ;
6868 if seen != self . index . load ( ) . state_id ( ) {
6969 todo ! ( "return …" )
@@ -85,7 +85,7 @@ impl super::Store {
8585 self . num_handles_stable . load ( Ordering :: SeqCst ) == 0
8686 }
8787
88- fn collect_replace_outcome ( & self , is_stable : bool ) -> Outcome {
88+ pub ( crate ) fn collect_snapshot ( & self ) -> Snapshot {
8989 let index = self . index . load ( ) ;
9090 let indices = index
9191 . slot_indices
@@ -106,18 +106,19 @@ impl super::Store {
106106 } )
107107 . collect ( ) ;
108108
109+ Snapshot {
110+ indices,
111+ loose_dbs : Arc :: clone ( & index. loose_dbs ) ,
112+ marker : index. marker ( ) ,
113+ }
114+ }
115+
116+ fn collect_replace_outcome ( & self , is_stable : bool ) -> Outcome {
117+ let snapshot = self . collect_snapshot ( ) ;
109118 if is_stable {
110- Outcome :: ReplaceStable {
111- indices,
112- loose_dbs : Arc :: clone ( & index. loose_dbs ) ,
113- marker : index. marker ( ) ,
114- }
119+ Outcome :: ReplaceStable ( snapshot)
115120 } else {
116- Outcome :: Replace {
117- indices,
118- loose_dbs : Arc :: clone ( & index. loose_dbs ) ,
119- marker : index. marker ( ) ,
120- }
121+ Outcome :: Replace ( snapshot)
121122 }
122123 }
123124}
0 commit comments