@@ -24,119 +24,6 @@ pub trait Backend: Clone + Sync + Send + 'static {
2424 /// ```
2525 fn has_item_search ( & self ) -> bool ;
2626
27- /// Adds collections and items from hrefs.
28- ///
29- /// A default implementation is provided. If `auto_create_collections` is
30- /// true, then, _if_ there is no collection for one or more items, a
31- /// collection will be auto-created before adding the items. If
32- /// `follow_links` is true, then `item` links on collections will be
33- /// followed and added as well.
34- ///
35- /// # Examples
36- ///
37- /// ```
38- /// use stac_server::{MemoryBackend, Backend};
39- /// let mut backend = MemoryBackend::new();
40- /// # tokio_test::block_on(async {
41- /// backend.add_from_hrefs(vec![
42- /// "tests/data/collection.json".to_string(),
43- /// "tests/data/feature.geojson".to_string(),
44- /// ], false, false);
45- /// # })
46- /// ```
47- #[ cfg( feature = "tokio" ) ]
48- async fn add_from_hrefs (
49- & mut self ,
50- hrefs : Vec < String > ,
51- auto_create_collections : bool ,
52- follow_links : bool ,
53- ) -> Result < ( ) > {
54- use crate :: Error ;
55- use stac:: { Href , Links , Value } ;
56- use std:: collections:: { HashMap , HashSet } ;
57- use tokio:: task:: JoinSet ;
58-
59- let mut set = JoinSet :: new ( ) ;
60- for href in hrefs {
61- // TODO allow parquet
62- let _ = set. spawn ( async move { stac_async:: read :: < Value > ( href) . await } ) ;
63- }
64-
65- let mut items: HashMap < Option < String > , Vec < Item > > = HashMap :: new ( ) ;
66- let mut item_collection_ids = HashSet :: new ( ) ;
67- let mut add_item = |mut item : Item | {
68- item. remove_structural_links ( ) ;
69- if let Some ( collection) = item. collection . as_ref ( ) {
70- let collection = collection. clone ( ) ;
71- let _ = item_collection_ids. insert ( collection. clone ( ) ) ;
72- let _ = items. entry ( Some ( collection) ) . or_default ( ) . push ( item) ;
73- } else {
74- let _ = items. entry ( None ) . or_default ( ) . push ( item) ;
75- }
76- } ;
77- let mut item_set = JoinSet :: new ( ) ;
78- let mut collection_ids = HashSet :: new ( ) ;
79- while let Some ( result) = set. join_next ( ) . await {
80- let value = result??;
81- match value {
82- Value :: Item ( item) => add_item ( item) ,
83- Value :: Catalog ( catalog) => {
84- return Err ( Error :: Backend ( format ! (
85- "cannot add catalog with id={} to the backend" ,
86- catalog. id
87- ) ) )
88- }
89- Value :: Collection ( mut collection) => {
90- if follow_links {
91- // TODO we could maybe merge this with `remove_structural_links`
92- let href = collection
93- . href ( )
94- . expect ( "we read it, so it should have an href" )
95- . to_string ( ) ;
96- collection. make_relative_links_absolute ( href) ?;
97- for link in collection. iter_item_links ( ) {
98- let href = link. href . clone ( ) ;
99- let _ =
100- item_set. spawn ( async move { stac_async:: read :: < Item > ( href) . await } ) ;
101- }
102- }
103- collection. remove_structural_links ( ) ;
104- let _ = collection_ids. insert ( collection. id . clone ( ) ) ;
105- self . add_collection ( collection) . await ?
106- }
107- Value :: ItemCollection ( item_collection) => {
108- for item in item_collection. items {
109- add_item ( item)
110- }
111- }
112- }
113- }
114-
115- while let Some ( result) = item_set. join_next ( ) . await {
116- let item = result??;
117- add_item ( item) ;
118- }
119-
120- if auto_create_collections {
121- for id in item_collection_ids {
122- if !collection_ids. contains ( & id) {
123- let items = items
124- . get ( & Some ( id. clone ( ) ) ) // TODO can we get rid of this clone?
125- . expect ( "should have items for collection id" ) ;
126- let collection = Collection :: from_id_and_items ( id, items) ;
127- self . add_collection ( collection) . await ?;
128- }
129- }
130- }
131-
132- for ( _, items) in items {
133- for item in items {
134- self . add_item ( item) . await ?;
135- }
136- }
137- Ok ( ( ) )
138- }
139-
14027 /// Returns all collections.
14128 ///
14229 /// # Examples
@@ -251,28 +138,3 @@ pub trait Backend: Clone + Sync + Send + 'static {
251138 /// ```
252139 async fn search ( & self , search : Search ) -> Result < ItemCollection > ;
253140}
254-
255- #[ cfg( test) ]
256- mod tests {
257- #[ tokio:: test]
258- #[ cfg( feature = "tokio" ) ]
259- async fn auto_create_collection ( ) {
260- use super :: Backend ;
261- use crate :: MemoryBackend ;
262-
263- let mut backend = MemoryBackend :: new ( ) ;
264- backend
265- . add_from_hrefs (
266- vec ! [ "../spec-examples/v1.0.0/simple-item.json" . to_string( ) ] ,
267- true ,
268- false ,
269- )
270- . await
271- . unwrap ( ) ;
272- let _ = backend
273- . collection ( "simple-collection" )
274- . await
275- . unwrap ( )
276- . unwrap ( ) ;
277- }
278- }
0 commit comments