zarrs 0.23.9

A library for the Zarr storage format for multidimensional arrays and metadata
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Zarr hierarchies.
//!
//! A Zarr hierarchy is a tree structure, where each node in the tree is either a [`Group`] or an [`Array`].
//!
//! A [`Hierarchy`] holds a mapping of [`NodePath`]s to [`NodeMetadata`].
//!
//! The [`Hierarchy::tree`] function can be used to create a string representation of the hierarchy.
//!
//! See <https://zarr-specs.readthedocs.io/en/latest/v3/core/index.html#hierarchy>.

use std::collections::BTreeMap;
use std::sync::Arc;

use crate::array::{Array, ArrayMetadata};
use crate::config::MetadataRetrieveVersion;
use crate::group::Group;
use crate::node::get_all_nodes_of;
pub use crate::node::{Node, NodeCreateError, NodePath, NodePathError};
#[cfg(feature = "async")]
use crate::{
    node::async_get_all_nodes_of,
    storage::{AsyncListableStorageTraits, AsyncReadableStorageTraits},
};
pub use zarrs_metadata::NodeMetadata;
use zarrs_storage::{ListableStorageTraits, ReadableStorageTraits};

/// A Zarr hierarchy.
#[derive(Debug, Clone)]
pub struct Hierarchy(BTreeMap<NodePath, NodeMetadata>);

impl std::fmt::Display for Hierarchy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.tree())
    }
}

/// A hierarchy creation error.
pub type HierarchyCreateError = NodeCreateError;

impl Hierarchy {
    /// Create a new, empty hierarchy.
    fn new() -> Self {
        Hierarchy(BTreeMap::new())
    }

    /// Open a hierarchy at `path` and read metadata and children from `storage` with default [`MetadataRetrieveVersion`].
    ///
    /// # Errors
    /// Returns [`HierarchyCreateError`] if metadata is invalid or there is a failure to list child nodes.
    pub fn open<TStorage: ?Sized + ReadableStorageTraits + ListableStorageTraits>(
        storage: &Arc<TStorage>,
        path: &str,
    ) -> Result<Self, HierarchyCreateError> {
        Self::open_opt(storage, path, &MetadataRetrieveVersion::Default)
    }

    /// Open a hierarchy at a `path` and read metadata and children from `storage` with non-default [`MetadataRetrieveVersion`].
    ///
    /// # Errors
    /// Returns [`HierarchyCreateError`] if metadata is invalid or there is a failure to list child nodes.
    pub fn open_opt<TStorage: ?Sized + ReadableStorageTraits + ListableStorageTraits>(
        storage: &Arc<TStorage>,
        path: &str,
        version: &MetadataRetrieveVersion,
    ) -> Result<Self, HierarchyCreateError> {
        let node_path = NodePath::try_from(path)?;
        let node_metadata = Node::get_metadata(storage, &node_path, version)?;
        let mut hierarchy = Hierarchy::new();

        let nodes = match node_metadata {
            NodeMetadata::Array(_) => Vec::default(),
            // TODO: Add consolidated metadata support
            NodeMetadata::Group(_) => get_all_nodes_of(storage, &node_path, version)?,
        };

        hierarchy.0.insert(node_path, node_metadata);
        hierarchy.0.extend(nodes);

        Ok(hierarchy)
    }

    #[cfg(feature = "async")]
    /// Asynchronously open a hierarchy at `path` and read metadata and children from `storage` with default [`MetadataRetrieveVersion`].
    ///
    /// # Errors
    /// Returns [`HierarchyCreateError`] if metadata is invalid or there is a failure to list child nodes.
    pub async fn async_open<
        TStorage: ?Sized + AsyncReadableStorageTraits + AsyncListableStorageTraits,
    >(
        storage: &Arc<TStorage>,
        path: &str,
    ) -> Result<Self, HierarchyCreateError> {
        Self::async_open_opt(storage, path, &MetadataRetrieveVersion::Default).await
    }

    #[cfg(feature = "async")]
    /// Asynchronously open a hierarchy at a `path` and read metadata and children from `storage` with non-default [`MetadataRetrieveVersion`].
    ///
    /// # Errors
    /// Returns [`HierarchyCreateError`] if metadata is invalid or there is a failure to list child nodes.
    pub async fn async_open_opt<
        TStorage: ?Sized + AsyncReadableStorageTraits + AsyncListableStorageTraits,
    >(
        storage: &Arc<TStorage>,
        path: &str,
        version: &MetadataRetrieveVersion,
    ) -> Result<Self, HierarchyCreateError> {
        let node_path = NodePath::try_from(path)?;
        let node_metadata = Node::async_get_metadata(storage, &node_path, version).await?;
        let mut hierarchy = Hierarchy::new();

        let nodes = match node_metadata {
            NodeMetadata::Array(_) => Vec::default(),
            // TODO: Add consolidated metadata support
            NodeMetadata::Group(_) => async_get_all_nodes_of(storage, &node_path, version).await?,
        };

        hierarchy.0.insert(node_path, node_metadata);
        hierarchy.0.extend(nodes);

        Ok(hierarchy)
    }

    /// Convenience method to create a `Hierarchy` from a `Group` with synchronous storage.
    ///
    /// # Errors
    /// Returns [`HierarchyCreateError`] if group metadata is invalid or there is a failure to list child nodes.
    pub fn try_from_group<TStorage: ?Sized + ReadableStorageTraits + ListableStorageTraits>(
        group: &Group<TStorage>,
    ) -> Result<Self, HierarchyCreateError> {
        let mut hierarchy = Hierarchy::new();
        hierarchy.0.insert(
            group.path().clone(),
            NodeMetadata::Group(group.metadata().clone()),
        );
        hierarchy.0.extend(get_all_nodes_of(
            &group.storage(),
            group.path(),
            &MetadataRetrieveVersion::Default,
        )?);
        Ok(hierarchy)
    }

    #[cfg(feature = "async")]
    /// Convenience method to create a `Hierarchy` from a Group with asynchronous storage.
    ///
    /// # Errors
    /// Returns [`HierarchyCreateError`] if group metadata is invalid or there is a failure to list child nodes.
    pub async fn try_from_async_group<
        TStorage: ?Sized + AsyncReadableStorageTraits + AsyncListableStorageTraits,
    >(
        group: &Group<TStorage>,
    ) -> Result<Hierarchy, HierarchyCreateError> {
        let mut hierarchy = Hierarchy::new();
        hierarchy.0.insert(
            group.path().clone(),
            NodeMetadata::Group(group.metadata().clone()),
        );
        hierarchy.0.extend(
            async_get_all_nodes_of(
                &group.storage(),
                group.path(),
                &MetadataRetrieveVersion::Default,
            )
            .await?,
        );
        Ok(hierarchy)
    }

    // /// Insert a node into the hierarchy.
    // pub fn insert(&mut self, path: NodePath, metadata: NodeMetadata) -> Option<NodeMetadata> {
    //     self.0.insert(path, metadata)
    // }

    /// Create a string representation of the hierarchy starting from the root.
    #[must_use]
    pub fn tree(&self) -> String {
        self.tree_of(&NodePath::root())
    }

    /// Create a string representation of the hierarchy starting from `path`.
    #[must_use]
    pub fn tree_of(&self, path: &NodePath) -> String {
        fn print_metadata(name: &str, string: &mut String, metadata: &NodeMetadata) {
            match metadata {
                NodeMetadata::Array(array_metadata) => {
                    let s = match array_metadata {
                        ArrayMetadata::V3(array_metadata) => {
                            format!(
                                "{} {:?} {}",
                                name, array_metadata.shape, array_metadata.data_type
                            )
                        }
                        ArrayMetadata::V2(array_metadata) => {
                            format!(
                                "{} {:?} {:?}",
                                name, array_metadata.shape, array_metadata.dtype
                            )
                        }
                    };
                    string.push_str(&s);
                }
                NodeMetadata::Group(_) => {
                    string.push_str(name);
                }
            }
            string.push('\n');
        }

        let mut s = String::from(path.as_str());
        s.push('\n');

        let prefix = path.as_str();
        let depth = path.as_path().components().count();

        for node in self
            .0
            .iter()
            .filter(|(path, _)| path.as_str().starts_with(prefix) && !path.as_str().eq(prefix))
            .map(|(p, md)| Node::new_with_metadata(p.clone(), md.clone(), vec![]))
        {
            let depth = node
                .path()
                .as_path()
                .components()
                .count()
                .saturating_sub(depth);

            s.push_str(&" ".repeat(depth * 2));
            print_metadata(node.name().as_str(), &mut s, node.metadata());
        }
        s
    }
}

// impl Extend<(NodePath, NodeMetadata)> for Hierarchy {
//     fn extend<T: IntoIterator<Item = (NodePath, NodeMetadata)>>(&mut self, iter: T) {
//         for (path, metadata) in iter {
//             self.insert(path, metadata);
//         }
//     }
// }

impl<TStorage: ?Sized> TryFrom<&Array<TStorage>> for Hierarchy {
    type Error = HierarchyCreateError;
    fn try_from(array: &Array<TStorage>) -> Result<Self, Self::Error> {
        let mut hierarchy = Hierarchy::new();
        hierarchy.0.insert(
            array.path().clone(),
            NodeMetadata::Array(array.metadata().clone()),
        );
        Ok(hierarchy)
    }
}

impl<TStorage: ?Sized> TryFrom<Array<TStorage>> for Hierarchy {
    type Error = HierarchyCreateError;
    fn try_from(array: Array<TStorage>) -> Result<Self, Self::Error> {
        (&array).try_into()
    }
}

#[cfg(test)]
mod tests {
    use std::num::NonZeroU64;

    use super::*;
    use crate::array::ArrayBuilder;
    use crate::group::GroupBuilder;
    use zarrs_metadata::GroupMetadata;
    use zarrs_metadata::v2::{ArrayMetadataV2, GroupMetadataV2};
    use zarrs_metadata::v3::GroupMetadataV3;
    #[cfg(feature = "async")]
    use zarrs_storage::AsyncReadableWritableListableStorageTraits;
    use zarrs_storage::store::MemoryStore;
    use zarrs_storage::{StoreKey, WritableStorageTraits};

    const EXPECTED_TREE: &str = "/\n  array [10, 10] float32\n  group\n    array [10, 10] float32\n    subgroup\n      mysubarray [10, 10] float32\n";

    fn helper_create_dataset(store: &Arc<MemoryStore>) -> Group<MemoryStore> {
        let group_builder = GroupBuilder::default();

        let root = group_builder
            .build(store.clone(), NodePath::root().as_str())
            .unwrap();
        let group = group_builder.build(store.clone(), "/group").unwrap();
        let array_builder = ArrayBuilder::new(
            vec![10, 10],
            vec![5, 5],
            crate::array::data_type::float32(),
            0.0f32,
        );

        let array = array_builder.build(store.clone(), "/array").unwrap();
        let group_array = array_builder.build(store.clone(), "/group/array").unwrap();
        let subgroup = group_builder
            .build(store.clone(), "/group/subgroup")
            .unwrap();
        let subgroup_array = array_builder
            .build(store.clone(), "/group/subgroup/mysubarray")
            .unwrap();

        root.store_metadata().unwrap();
        array.store_metadata().unwrap();
        group.store_metadata().unwrap();
        group_array.store_metadata().unwrap();
        subgroup.store_metadata().unwrap();
        subgroup_array.store_metadata().unwrap();

        root
    }

    #[test]
    fn hierarchy_try_from_array() {
        let store = Arc::new(MemoryStore::new());
        let array_builder =
            ArrayBuilder::new(vec![1], vec![1], crate::array::data_type::float32(), 0.0f32);

        let array = array_builder
            .build(store, "/store/of/data.zarr/path/to/an/array")
            .expect("Faulty test array");

        let hierarchy = Hierarchy::try_from(&array).unwrap();
        assert_eq!(hierarchy.0.len(), 1);
        let hierarchy = Hierarchy::try_from(array).unwrap();
        assert_eq!(hierarchy.0.len(), 1);
    }

    #[cfg(feature = "async")]
    #[test]
    fn hierarchy_try_from_async_array() {
        let store = std::sync::Arc::new(zarrs_object_store::AsyncObjectStore::new(
            object_store::memory::InMemory::new(),
        ));
        let array_builder =
            ArrayBuilder::new(vec![1], vec![1], crate::array::data_type::float32(), 0.0f32);

        let array = array_builder
            .build(store, "/store/of/data.zarr/path/to/an/array")
            .expect("Faulty test array");

        let hierarchy = Hierarchy::try_from(&array).unwrap();
        assert_eq!(hierarchy.0.len(), 1);
        let hierarchy = Hierarchy::try_from(array).unwrap();
        assert_eq!(hierarchy.0.len(), 1);
    }

    #[test]
    fn hierarchy_try_from_group() {
        let store = Arc::new(MemoryStore::new());
        let group = helper_create_dataset(&store);
        let hierarchy = Hierarchy::try_from_group(&group).unwrap();

        assert_eq!(hierarchy.0.len(), 6);
    }

    #[cfg(feature = "async")]
    #[tokio::test]
    async fn hierarchy_async_try_from_group() {
        let store = std::sync::Arc::new(zarrs_object_store::AsyncObjectStore::new(
            object_store::memory::InMemory::new(),
        ));
        let group_path = "/group";
        let group_builder = GroupBuilder::new();
        let group = group_builder.build(store.clone(), group_path).unwrap();

        let subgroup = group_builder
            .build(store.clone(), "/group/subgroup")
            .unwrap();

        let array = ArrayBuilder::new(
            vec![10, 10],
            vec![5, 5],
            crate::array::data_type::float32(),
            0.0f32,
        )
        .build(store.clone(), "/group/subgroup/array")
        .unwrap();

        group.async_store_metadata().await.unwrap();
        subgroup.async_store_metadata().await.unwrap();
        array.async_store_metadata().await.unwrap();

        let hierarchy = Hierarchy::try_from_async_group(&group).await;
        assert!(hierarchy.is_ok());
        let hierarchy = hierarchy.unwrap();
        assert!(
            "/\n  group\n    subgroup\n      array [10, 10] float32\n" == hierarchy.to_string()
        );
    }

    #[cfg(feature = "async")]
    #[tokio::test]
    async fn hierarchy_async_try_from_invalid_async_group() {
        let store = std::sync::Arc::new(zarrs_object_store::AsyncObjectStore::new(
            object_store::memory::InMemory::new(),
        ));

        let root = Group::new_with_metadata(
            store.clone(),
            "/",
            GroupMetadata::V3(GroupMetadataV3::default()),
        )
        .unwrap();

        assert!(Hierarchy::try_from_async_group(&root).await.is_ok());

        use zarrs_storage::AsyncWritableStorageTraits;
        // Inject fauly subgroup
        store
            .set(
                &StoreKey::new("subgroup/zarr.json").unwrap(),
                vec![0].into(),
            )
            .await
            .unwrap();

        assert!(Hierarchy::try_from_async_group(&root).await.is_err());
    }

    #[test]
    fn hierarchy_try_from_invalid_group() {
        let store = Arc::new(MemoryStore::new());

        let root = Group::new_with_metadata(
            store.clone(),
            "/",
            GroupMetadata::V3(GroupMetadataV3::default()),
        )
        .unwrap();

        assert!(Hierarchy::try_from_group(&root).is_ok());

        // Inject fauly subgroup
        store
            .set(
                &StoreKey::new("subgroup/zarr.json").unwrap(),
                vec![0].into(),
            )
            .unwrap();

        assert!(Hierarchy::try_from_group(&root).is_err());
    }

    #[test]
    fn hierarchy_tree_of() {
        let store = Arc::new(MemoryStore::new());

        let group = helper_create_dataset(&store);

        let hierarchy = Hierarchy::try_from_group(&group).unwrap();

        assert_eq!(
            "/group/subgroup\n  mysubarray [10, 10] float32\n",
            hierarchy.tree_of(&NodePath::try_from("/group/subgroup").unwrap())
        );
    }

    #[test]
    fn hierarchy_tree() {
        let store = Arc::new(MemoryStore::new());

        let group = helper_create_dataset(&store);

        let hierarchy = Hierarchy::try_from_group(&group).unwrap();

        assert_eq!(
            "/\n  array [10, 10] float32\n  group\n    array [10, 10] float32\n    subgroup\n      mysubarray [10, 10] float32\n",
            hierarchy.tree()
        );

        let store = Arc::new(MemoryStore::new());
        let groupv2 = Group::new_with_metadata(
            store.clone(),
            "/groupv2",
            GroupMetadata::V2(GroupMetadataV2::new()),
        )
        .expect("Unexpected issue when greating a Group for testing.");

        let arrayv2 = Array::new_with_metadata(
            store.clone(),
            "/groupv2/arrayv2",
            ArrayMetadata::V2(ArrayMetadataV2::new(
                vec![1],
                crate::array::ChunkShape::from(vec![NonZeroU64::new(1).unwrap()]),
                zarrs_metadata::v2::DataTypeMetadataV2::Simple("<f8".into()),
                zarrs_metadata::FillValueMetadata::from(f64::NAN),
                None,
                None,
            )),
        )
        .expect("Unexpected issue when creating a v2 Array for testing.");

        let _ = groupv2.store_metadata();
        let _ = arrayv2.store_metadata();

        let h = Hierarchy::try_from_group(&groupv2);
        assert!(h.is_ok());
        assert!("/\n  groupv2\n    arrayv2 [1] Simple(\"<f8\")\n" == h.unwrap().tree());
    }

    #[test]
    fn hierarchy_tree_empty() {
        let hierarchy = Hierarchy::new();
        let tree_str = hierarchy.tree();
        assert_eq!(tree_str, "/\n");
    }

    #[test]
    fn hierarchy_open() {
        let store: std::sync::Arc<MemoryStore> = std::sync::Arc::new(MemoryStore::new());

        let _group = helper_create_dataset(&store);

        // Open a group node
        let h = Hierarchy::open(&store, "/").unwrap();
        assert_eq!(EXPECTED_TREE, h.tree());

        // Open an array node
        let h = Hierarchy::open(&store, "/array").unwrap();
        assert_eq!("/\n  array [10, 10] float32\n", h.tree());
    }

    #[cfg(feature = "async")]
    async fn async_helper_create_dataset<
        AStore: ?Sized + AsyncReadableWritableListableStorageTraits + 'static,
    >(
        store: &Arc<AStore>,
    ) -> Group<AStore> {
        let group_builder = GroupBuilder::default();

        let root = group_builder
            .build(store.clone(), NodePath::root().as_str())
            .unwrap();
        let group = group_builder.build(store.clone(), "/group").unwrap();
        let array_builder = ArrayBuilder::new(
            vec![10, 10],
            vec![5, 5],
            crate::array::data_type::float32(),
            0.0f32,
        );

        let array = array_builder.build(store.clone(), "/array").unwrap();
        let group_array = array_builder.build(store.clone(), "/group/array").unwrap();
        let subgroup = group_builder
            .build(store.clone(), "/group/subgroup")
            .unwrap();
        let subgroup_array = array_builder
            .build(store.clone(), "/group/subgroup/mysubarray")
            .unwrap();

        root.async_store_metadata().await.unwrap();
        array.async_store_metadata().await.unwrap();
        group.async_store_metadata().await.unwrap();
        group_array.async_store_metadata().await.unwrap();
        subgroup.async_store_metadata().await.unwrap();
        subgroup_array.async_store_metadata().await.unwrap();

        root
    }

    #[cfg(feature = "async")]
    #[tokio::test]
    async fn hierarchy_async_open() {
        use zarrs_storage::AsyncReadableWritableListableStorage;

        let store: AsyncReadableWritableListableStorage = Arc::new(
            zarrs_object_store::AsyncObjectStore::new(object_store::memory::InMemory::new()),
        );

        let _group = async_helper_create_dataset(&store).await;

        // Open a Group node
        let h = Hierarchy::async_open(&store, "/").await;
        assert!(h.is_ok());
        assert_eq!(EXPECTED_TREE, h.unwrap().tree());

        // Open an Array node
        let h = Hierarchy::async_open(&store, "/array").await;
        assert!(h.is_ok());
        assert_eq!("/\n  array [10, 10] float32\n", h.unwrap().tree());
    }
}