@@ -117,7 +117,7 @@ use crate::{
117117 BaoFileStorage , BaoFileStorageSubscriber , CompleteStorage , DataReader ,
118118 OutboardReader ,
119119 } ,
120- util:: entity_manager,
120+ util:: entity_manager:: { self , EntityState } ,
121121 } ,
122122 util:: { BaoTreeSender , FixedSize , MemOrFile , ValueOrPoisioned } ,
123123 Hash , IROH_BLOCK_SIZE ,
@@ -211,12 +211,12 @@ impl TaskContext {
211211 }
212212}
213213
214- impl entity_manager:: Params for HashContext {
215- type EntityId = Hash ;
214+ impl entity_manager:: EntityState for HashContext {
215+ type Id = Hash ;
216216
217217 type GlobalState = Arc < TaskContext > ;
218218
219- fn id ( & self ) -> & Self :: EntityId {
219+ fn id ( & self ) -> & Self :: Id {
220220 & self . id
221221 }
222222
@@ -228,24 +228,41 @@ impl entity_manager::Params for HashContext {
228228 self . state . sender_count ( ) + self . state . receiver_count ( )
229229 }
230230
231- fn new ( id : & Self :: EntityId , global : & Self :: GlobalState ) -> Self {
231+ fn new ( id : & Self :: Id , global : & Self :: GlobalState ) -> Self {
232232 Self {
233233 id : * id,
234234 global : global. clone ( ) ,
235235 state : BaoFileHandle :: default ( ) ,
236236 }
237237 }
238238
239- fn reset ( & mut self , id : & Self :: EntityId , global : & Self :: GlobalState ) {
239+ fn reset ( & mut self , id : & Self :: Id , global : & Self :: GlobalState ) {
240240 self . id = * id;
241241 self . global = global. clone ( ) ;
242242 // this is identical to self.state = BaoFileHandle::default(),
243243 // but does not allocate a new handle.
244244 self . state . send_replace ( BaoFileStorage :: Initial ) ;
245245 }
246246
247+ #[ instrument( skip_all, fields( hash = %self . id. fmt_short( ) ) ) ]
247248 async fn on_shutdown ( & self , _cause : entity_manager:: ShutdownCause ) {
248- self . persist ( ) . await ;
249+ self . state . send_if_modified ( |guard| {
250+ let hash = & self . id ;
251+ let BaoFileStorage :: Partial ( fs) = guard. take ( ) else {
252+ return false ;
253+ } ;
254+ let path = self . global . options . path . bitfield_path ( hash) ;
255+ trace ! ( "writing bitfield for hash {} to {}" , hash, path. display( ) ) ;
256+ if let Err ( cause) = fs. sync_all ( & path) {
257+ error ! (
258+ "failed to write bitfield for {} at {}: {:?}" ,
259+ hash,
260+ path. display( ) ,
261+ cause
262+ ) ;
263+ }
264+ false
265+ } ) ;
249266 }
250267}
251268
@@ -878,7 +895,7 @@ async fn handle_batch_impl(cmd: BatchMsg, id: Scope, scope: &Arc<TempTagScope>)
878895}
879896
880897/// The minimal API you need to implement for an entity for a store to work.
881- trait EntityApi {
898+ trait EntityApi : EntityState {
882899 /// Import from a stream of n0 bao encoded data.
883900 async fn import_bao ( & self , cmd : ImportBaoMsg ) ;
884901 /// Finish an import from a local file or memory.
@@ -891,8 +908,6 @@ trait EntityApi {
891908 async fn export_bao ( & self , cmd : ExportBaoMsg ) ;
892909 /// Export the entry to a local file.
893910 async fn export_path ( & self , cmd : ExportPathMsg ) ;
894- /// Persist the entry at the end of its lifecycle.
895- async fn persist ( & self ) ;
896911}
897912
898913/// A more opinionated API that can be used as a helper to save implementation
@@ -1000,27 +1015,6 @@ impl EntityApi for HashContext {
10001015 } ;
10011016 cmd. tx . send ( res) . await . ok ( ) ;
10021017 }
1003-
1004- #[ instrument( skip_all, fields( hash = %self . id. fmt_short( ) ) ) ]
1005- async fn persist ( & self ) {
1006- self . state . send_if_modified ( |guard| {
1007- let hash = & self . id ;
1008- let BaoFileStorage :: Partial ( fs) = guard. take ( ) else {
1009- return false ;
1010- } ;
1011- let path = self . global . options . path . bitfield_path ( hash) ;
1012- trace ! ( "writing bitfield for hash {} to {}" , hash, path. display( ) ) ;
1013- if let Err ( cause) = fs. sync_all ( & path) {
1014- error ! (
1015- "failed to write bitfield for {} at {}: {:?}" ,
1016- hash,
1017- path. display( ) ,
1018- cause
1019- ) ;
1020- }
1021- false
1022- } ) ;
1023- }
10241018}
10251019
10261020async fn finish_import_impl ( ctx : & HashContext , import_data : ImportEntry ) -> io:: Result < ( ) > {
0 commit comments