@@ -65,6 +65,7 @@ impl ScopeManager {
6565 . filter ( |( _, scope) | scope. loaded ( ) )
6666 . map ( |( name, _) | name. clone ( ) )
6767 . collect :: < HashSet < _ > > ( ) ,
68+ self . root_options . expire ,
6869 ) ) ) ;
6970 Ok ( ( ) )
7071 }
@@ -99,25 +100,37 @@ impl ScopeManager {
99100 Ok ( rx)
100101 }
101102
103+ async fn clear_scope ( & self , name : & str ) {
104+ self
105+ . scopes
106+ . lock ( )
107+ . await
108+ . get_mut ( name)
109+ . expect ( "should have scope" )
110+ . clear ( ) ;
111+ }
112+
102113 pub async fn load ( & self , name : & ' static str ) -> Result < StorageContent > {
114+ self
115+ . scopes
116+ . lock ( )
117+ . await
118+ . entry ( name. to_string ( ) )
119+ . or_insert_with ( || PackScope :: new ( self . strategy . get_path ( name) , self . pack_options . clone ( ) ) ) ;
120+
121+ // only check lock file and root meta for the first time
103122 if matches ! ( * self . root_meta. lock( ) . await , RootMetaState :: Pending ) {
104- let loaded = self . strategy . read_root_meta ( ) . await ?;
105- if let Some ( meta) = & loaded {
106- for scope_name in meta. scopes . clone ( ) {
107- self
108- . scopes
109- . lock ( )
110- . await
111- . entry ( scope_name. clone ( ) )
112- . or_insert_with ( || {
113- PackScope :: new (
114- self . strategy . get_path ( & scope_name) ,
115- self . pack_options . clone ( ) ,
116- )
117- } ) ;
123+ match self . strategy . before_load ( ) . await {
124+ Ok ( ( ) ) => {
125+ let loaded = self . strategy . read_root_meta ( ) . await ?;
126+ * self . root_meta . lock ( ) . await = RootMetaState :: Value ( loaded) ;
127+ }
128+ Err ( err) => {
129+ * self . root_meta . lock ( ) . await = RootMetaState :: Value ( None ) ;
130+ self . clear_scope ( name) . await ;
131+ return Err ( err) ;
118132 }
119133 }
120- * self . root_meta . lock ( ) . await = RootMetaState :: Value ( loaded) ;
121134 }
122135
123136 match self . validate_scope ( name) . await {
@@ -131,37 +144,18 @@ impl ScopeManager {
131144 }
132145 // create empty scope if not exists
133146 ValidateResult :: NotExists => {
134- self
135- . scopes
136- . lock ( )
137- . await
138- . entry ( name. to_string ( ) )
139- . or_insert_with ( || {
140- PackScope :: empty ( self . strategy . get_path ( name) , self . pack_options . clone ( ) )
141- } ) ;
147+ self . clear_scope ( name) . await ;
142148 Ok ( vec ! [ ] )
143149 }
144150 // clear scope if invalid
145151 ValidateResult :: Invalid ( _) => {
146- self
147- . scopes
148- . lock ( )
149- . await
150- . get_mut ( name)
151- . expect ( "should have scope" )
152- . clear ( ) ;
152+ self . clear_scope ( name) . await ;
153153 Err ( error ! ( validated. to_string( ) ) )
154154 }
155155 } ,
156156 Err ( e) => {
157157 // clear scope if error
158- self
159- . scopes
160- . lock ( )
161- . await
162- . get_mut ( name)
163- . expect ( "should have scope" )
164- . clear ( ) ;
158+ self . clear_scope ( name) . await ;
165159 Err ( Error :: from ( e) )
166160 }
167161 }
@@ -180,10 +174,7 @@ impl ScopeManager {
180174 } ;
181175
182176 // scope exists, validate it
183- let validated = self
184- . strategy
185- . validate_root ( root_meta, self . root_options . expire )
186- . await ?;
177+ let validated = self . strategy . validate_root ( root_meta) . await ?;
187178 if validated. is_valid ( ) {
188179 self . strategy . ensure_meta ( scope) . await ?;
189180 let validated = self . strategy . validate_meta ( scope) . await ?;
@@ -272,6 +263,8 @@ async fn save_scopes(
272263 . into_iter ( )
273264 . collect_vec ( ) ;
274265
266+ strategy. write_root_meta ( root_meta) . await ?;
267+
275268 join_all (
276269 scopes
277270 . values ( )
@@ -291,8 +284,6 @@ async fn save_scopes(
291284 . into_iter ( )
292285 . collect :: < Result < Vec < _ > > > ( ) ?;
293286
294- strategy. write_root_meta ( root_meta) . await ?;
295-
296287 for ( _, scope) in scopes. iter_mut ( ) {
297288 strategy. after_all ( scope) ?;
298289 }
0 commit comments