Skip to content

Commit

Permalink
Add Group::[set_]consolidated_metadata()
Browse files Browse the repository at this point in the history
  • Loading branch information
LDeakin committed Aug 24, 2024
1 parent 9624500 commit a6ee022
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
30 changes: 28 additions & 2 deletions src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ use thiserror::Error;

use crate::{
metadata::{
group_metadata_v2_to_v3, v3::UnsupportedAdditionalFieldError, AdditionalFields,
GroupMetadataV2, MetadataConvertVersion, MetadataEraseVersion, MetadataRetrieveVersion,
group_metadata_v2_to_v3,
v3::{group::ConsolidatedMetadata, UnsupportedAdditionalFieldError},
AdditionalFields, GroupMetadataV2, MetadataConvertVersion, MetadataEraseVersion,
MetadataRetrieveVersion,
},
node::{NodePath, NodePathError},
storage::{
Expand Down Expand Up @@ -145,6 +147,30 @@ impl<TStorage: ?Sized> Group<TStorage> {
(GM::V2(metadata), V::V3) => GM::V3(group_metadata_v2_to_v3(&metadata)),
}
}

/// Get the consolidated metadata. Returns [`None`] if `consolidated_metadata` is absent.
///
/// Consolidated metadata is not currently supported for Zarr V2 groups.
#[must_use]
pub fn consolidated_metadata(&self) -> Option<&ConsolidatedMetadata> {
if let GroupMetadata::V3(group_metadata) = &self.metadata {
group_metadata.consolidated_metadata.as_ref()
} else {
None
}
}

/// Set the consolidated metadata.
///
/// Consolidated metadata is not currently supported for Zarr V2 groups, and this function is a no-op.
pub fn set_consolidated_metadata(
&mut self,
consolidated_metadata: Option<ConsolidatedMetadata>,
) {
if let GroupMetadata::V3(group_metadata) = &mut self.metadata {
group_metadata.consolidated_metadata = consolidated_metadata;
}
}
}

impl<TStorage: ?Sized + ReadableStorageTraits> Group<TStorage> {
Expand Down
14 changes: 4 additions & 10 deletions src/metadata/v3/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub struct GroupMetadataV3 {
#[serde(default, skip_serializing_if = "serde_json::Map::is_empty")]
pub attributes: serde_json::Map<String, serde_json::Value>,
/// Consolidated metadata.
#[serde(default, skip_serializing_if = "ConsolidatedMetadata::is_empty")]
pub consolidated_metadata: ConsolidatedMetadata,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub consolidated_metadata: Option<ConsolidatedMetadata>,
/// Additional fields.
#[serde(flatten)]
pub additional_fields: AdditionalFields,
Expand All @@ -58,7 +58,7 @@ impl GroupMetadataV3 {
node_type: monostate::MustBe!("group"),
attributes,
additional_fields,
consolidated_metadata: ConsolidatedMetadata::default(),
consolidated_metadata: None,
}
}
}
Expand Down Expand Up @@ -88,13 +88,6 @@ impl Default for ConsolidatedMetadata {
}
}

impl ConsolidatedMetadata {
#[must_use]
fn is_empty(&self) -> bool {
self.metadata.is_empty()
}
}

/// The "kind" of consolidated metadata.
#[non_exhaustive]
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Debug, Display)]
Expand Down Expand Up @@ -137,6 +130,7 @@ mod tests {
assert_eq!(
group_metadata
.consolidated_metadata
.unwrap()
.metadata
.get(&NodePath::new("/subgroup").unwrap())
.unwrap(),
Expand Down
10 changes: 10 additions & 0 deletions tests/hierarchy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::sync::Arc;

use zarrs::{
group::Group,
metadata::v3::group::ConsolidatedMetadata,
node::{Node, NodePath},
storage::store::FilesystemStore,
};
Expand Down Expand Up @@ -43,4 +45,12 @@ fn consolidated_metadata() {
let actual = Node::open(&store, node_path).unwrap();
assert_eq!(consolidated, actual.metadata());
}

let mut group = Group::open(store, "/").unwrap();
assert!(group.consolidated_metadata().is_none());
group.set_consolidated_metadata(Some(ConsolidatedMetadata {
metadata: consolidated_metadata,
..Default::default()
}));
assert!(group.consolidated_metadata().is_some());
}

0 comments on commit a6ee022

Please sign in to comment.