@@ -277,28 +277,92 @@ impl NodeMetadata {
277277 }
278278 }
279279
280- /// Capture metadata information by either loading it from staging or starting fresh
281- pub fn load (
282- options : & Options ,
283- storage : & dyn ObjectStorageProvider ,
284- node_type : NodeType ,
285- ) -> Arc < Self > {
286- let staging_path = options. staging_dir ( ) ;
280+ pub async fn load_node_metadata ( node_type : NodeType ) -> anyhow:: Result < Self > {
281+ let staging_path = PARSEABLE . options . staging_dir ( ) ;
287282 let node_type_str = node_type. as_str ( ) ;
288283
289284 // Attempt to load metadata from staging
290- if let Some ( mut meta) = Self :: load_from_staging ( staging_path, node_type_str, options) {
291- Self :: update_metadata ( & mut meta, options, node_type) ;
292- meta. put_on_disk ( staging_path)
293- . expect ( "Couldn't write updated metadata to disk" ) ;
294- return Arc :: new ( meta) ;
285+ if let Some ( meta) = Self :: load_from_staging ( staging_path, node_type_str, & PARSEABLE . options )
286+ {
287+ return Self :: process_and_store_metadata ( meta, staging_path, node_type) . await ;
288+ }
289+
290+ // Attempt to load metadata from storage
291+ let storage_metas = Self :: load_from_storage ( node_type_str. to_string ( ) ) . await ;
292+ let url = PARSEABLE . options . get_url ( node_type. to_mode ( ) ) ;
293+ let port = url. port ( ) . unwrap_or ( 80 ) . to_string ( ) ;
294+ let url = url. to_string ( ) ;
295+
296+ for storage_meta in storage_metas {
297+ if storage_meta. domain_name == url && storage_meta. port == port {
298+ return Self :: process_and_store_metadata ( storage_meta, staging_path, node_type)
299+ . await ;
300+ }
295301 }
296302
297- // If no metadata is found in staging, create a new one
298- let meta = Self :: create_new_metadata ( options, storage, node_type) ;
303+ // If no metadata is found, create a new one
304+ let meta = Self :: create_new_metadata ( & PARSEABLE . options , & * PARSEABLE . storage , node_type) ;
305+ Self :: store_new_metadata ( meta, staging_path) . await
306+ }
307+
308+ /// Process and store metadata
309+ async fn process_and_store_metadata (
310+ mut meta : Self ,
311+ staging_path : & Path ,
312+ node_type : NodeType ,
313+ ) -> anyhow:: Result < Self > {
314+ Self :: update_metadata ( & mut meta, & PARSEABLE . options , node_type) ;
315+ meta. put_on_disk ( staging_path)
316+ . expect ( "Couldn't write updated metadata to disk" ) ;
317+
318+ let path = meta. file_path ( ) ;
319+ let resource = serde_json:: to_vec ( & meta) ?. into ( ) ;
320+ let store = PARSEABLE . storage . get_object_store ( ) ;
321+ store. put_object ( & path, resource) . await ?;
322+
323+ Ok ( meta)
324+ }
325+
326+ /// Store new metadata
327+ async fn store_new_metadata ( meta : Self , staging_path : & Path ) -> anyhow:: Result < Self > {
299328 meta. put_on_disk ( staging_path)
300329 . expect ( "Couldn't write new metadata to disk" ) ;
301- Arc :: new ( meta)
330+
331+ let path = meta. file_path ( ) ;
332+ let resource = serde_json:: to_vec ( & meta) ?. into ( ) ;
333+ let store = PARSEABLE . storage . get_object_store ( ) ;
334+ store. put_object ( & path, resource) . await ?;
335+
336+ Ok ( meta)
337+ }
338+
339+ async fn load_from_storage ( node_type : String ) -> Vec < NodeMetadata > {
340+ let path = RelativePathBuf :: from ( PARSEABLE_ROOT_DIRECTORY ) ;
341+ let glob_storage = PARSEABLE . storage . get_object_store ( ) ;
342+ let obs = glob_storage
343+ . get_objects (
344+ Some ( & path) ,
345+ Box :: new ( {
346+ let node_type = node_type. clone ( ) ;
347+ move |file_name| file_name. contains ( & node_type)
348+ } ) ,
349+ )
350+ . await ;
351+
352+ let mut metadata = vec ! [ ] ;
353+ if let Ok ( obs) = obs {
354+ for object in obs {
355+ //convert to NodeMetadata
356+ match serde_json:: from_slice :: < NodeMetadata > ( & object) {
357+ Ok ( node_metadata) => metadata. push ( node_metadata) ,
358+ Err ( e) => error ! ( "Failed to deserialize NodeMetadata: {:?}" , e) ,
359+ }
360+ }
361+ } else {
362+ error ! ( "Couldn't read from storage" ) ;
363+ }
364+ // Return the metadata
365+ metadata
302366 }
303367
304368 /// Load metadata from the staging directory
@@ -467,30 +531,6 @@ impl NodeMetadata {
467531 Ok ( metadata)
468532 }
469533
470- pub async fn migrate ( & self ) -> anyhow:: Result < Option < Self > > {
471- let imp = self . file_path ( ) ;
472- let bytes = match PARSEABLE . storage . get_object_store ( ) . get_object ( & imp) . await {
473- Ok ( bytes) => bytes,
474- Err ( _) => {
475- return Ok ( None ) ;
476- }
477- } ;
478-
479- let mut resource = Self :: from_bytes ( & bytes, PARSEABLE . options . flight_port ) ?;
480- resource. node_type . clone_from ( & self . node_type ) ;
481- let bytes = Bytes :: from ( serde_json:: to_vec ( & resource) ?) ;
482-
483- resource. put_on_disk ( PARSEABLE . options . staging_dir ( ) ) ?;
484-
485- PARSEABLE
486- . storage
487- . get_object_store ( )
488- . put_object ( & imp, bytes)
489- . await ?;
490-
491- Ok ( Some ( resource) )
492- }
493-
494534 /// Puts the node info into the staging.
495535 ///
496536 /// This function takes the node info as a parameter and stores it in staging.
0 commit comments