Skip to content

Commit

Permalink
Node::{new_with_store->new} and Node::{new->new_with_metadata}
Browse files Browse the repository at this point in the history
  • Loading branch information
LDeakin committed Jan 21, 2024
1 parent 7b44ade commit 559e4ce
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 22 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- Tests for `ByteRange`, `BytesRepresentation`, `StorePrefix`, `StoreKey`, `ArrayBuilder`, `ArraySubset`, `GroupBuilder`, `Group`, `NodeName`, `NodePath`
- Tests for `ByteRange`, `BytesRepresentation`, `StorePrefix`, `StoreKey`, `ArrayBuilder`, `ArraySubset`, `GroupBuilder`, `Group`, `NodeName`, `NodePath`, `Node`
- `array_subset::IncompatibleStartEndIndicesError`

### Changed
Expand All @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **Breaking**: `ArraySubset::new_with_start_end_{inc,exc}` now return `IncompatibleStartEndIndicesError` instead of `IncompatibleDimensionalityError`
- It is now an error if any element of `end` is less than `start`
- Remove `#[must_use]` from `GroupBuilder::{attributes,additional_fields}`
- **Breaking**: Rename `Node::new_with_store` to `Node::new`, and `Node::new` to `Node::new_with_metadata` for consistency with `Array`/`Group`

### Removed
- **Breaking**: Remove `StorePrefixError::new`, deprecated since `v0.7.3`
Expand Down
2 changes: 1 addition & 1 deletion examples/array_write_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fn array_write_read() -> Result<(), Box<dyn std::error::Error>> {
println!("retrieve_array_subset [2..6, 3..5]:\n{data_subset:+4.1}\n");

// Show the hierarchy
let node = Node::new_with_store(&*store, "/").unwrap();
let node = Node::new(&*store, "/").unwrap();
let tree = node.hierarchy_tree();
println!("hierarchy_tree:\n{}", tree);

Expand Down
2 changes: 1 addition & 1 deletion examples/async_array_write_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ async fn async_array_write_read() -> Result<(), Box<dyn std::error::Error>> {
println!("async_retrieve_array_subset [2..6, 3..5]:\n{data_subset:+4.1}\n");

// Show the hierarchy
let node = Node::async_new_with_store(&*store, "/").await.unwrap();
let node = Node::async_new(&*store, "/").await.unwrap();
let tree = node.hierarchy_tree();
println!("hierarchy_tree:\n{}", tree);

Expand Down
2 changes: 1 addition & 1 deletion examples/rectangular_array_write_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fn rectangular_array_write_read() -> Result<(), Box<dyn std::error::Error>> {
println!("The middle 4x2 subset is:\n{:?}\n", data_4x2);

// Show the hierarchy
let node = Node::new_with_store(&*store, "/").unwrap();
let node = Node::new(&*store, "/").unwrap();
let tree = node.hierarchy_tree();
println!("The zarr hierarchy tree is:\n{}", tree);

Expand Down
2 changes: 1 addition & 1 deletion examples/sharded_array_write_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ fn sharded_array_write_read() -> Result<(), Box<dyn std::error::Error>> {
}

// Show the hierarchy
let node = Node::new_with_store(&*store_readable_listable, "/").unwrap();
let node = Node::new(&*store_readable_listable, "/").unwrap();
let tree = node.hierarchy_tree();
println!("The zarr hierarchy tree is:\n{}", tree);

Expand Down
2 changes: 1 addition & 1 deletion examples/zip_array_write_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ fn zip_array_write_read() -> Result<(), Box<dyn std::error::Error>> {
read_array_from_store(array)?;

// Show the hierarchy
let node = Node::new_with_store(&*store, "/").unwrap();
let node = Node::new(&*store, "/").unwrap();
let tree = node.hierarchy_tree();
println!("The zarr hierarchy tree is:\n{}", tree);

Expand Down
47 changes: 35 additions & 12 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,12 @@ pub enum NodeCreateError {
}

impl Node {
/// Create a new node at `path` with `metadata` and `children`.
#[must_use]
pub fn new(path: NodePath, metadata: NodeMetadata, children: Vec<Self>) -> Self {
Self {
path,
metadata,
children,
}
}

/// Create a new node at `path` and read metadata and children from `storage`.
///
/// # Errors
///
/// Returns [`NodeCreateError`] if metadata is invalid or there is a failure to list child nodes.
pub fn new_with_store<TStorage: ?Sized + ReadableStorageTraits + ListableStorageTraits>(
pub fn new<TStorage: ?Sized + ReadableStorageTraits + ListableStorageTraits>(
storage: &TStorage,
path: &str,
) -> Result<Self, NodeCreateError> {
Expand Down Expand Up @@ -102,7 +92,7 @@ impl Node {
/// # Errors
///
/// Returns [`NodeCreateError`] if metadata is invalid or there is a failure to list child nodes.
pub async fn async_new_with_store<
pub async fn async_new<
TStorage: ?Sized + AsyncReadableStorageTraits + AsyncListableStorageTraits,
>(
storage: &TStorage,
Expand All @@ -127,6 +117,16 @@ impl Node {
Ok(node)
}

/// Create a new node at `path` with `metadata` and `children`.
#[must_use]
pub fn new_with_metadata(path: NodePath, metadata: NodeMetadata, children: Vec<Self>) -> Self {
Self {
path,
metadata,
children,
}
}

/// Indicates if a node is the root.
#[must_use]
pub fn is_root(&self) -> bool {
Expand Down Expand Up @@ -187,6 +187,8 @@ impl Node {

#[cfg(test)]
mod tests {
use crate::{group::GroupMetadata, storage::store::MemoryStore};

use super::*;

#[test]
Expand Down Expand Up @@ -259,4 +261,25 @@ mod tests {
}"#;
serde_json::from_str::<NodeMetadata>(JSON_GROUP).unwrap();
}

#[test]
fn node_default() {
let store = std::sync::Arc::new(MemoryStore::new());
let node_path = "/node";
let node = Node::new(&*store, node_path).unwrap();
assert_eq!(
node.metadata,
NodeMetadata::Group(GroupMetadata::V3(GroupMetadataV3::default()))
);
}

#[test]
fn node_root() {
let node = Node::new_with_metadata(
NodePath::root(),
NodeMetadata::Group(GroupMetadata::V3(GroupMetadataV3::default())),
vec![],
);
assert!(node.is_root());
}
}
2 changes: 1 addition & 1 deletion src/node/node_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{array::ArrayMetadata, group::GroupMetadata};

/// Node metadata ([`ArrayMetadata`] or [`GroupMetadata`]).
#[derive(serde::Serialize, serde::Deserialize, Debug)]
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)]
#[serde(untagged)]
pub enum NodeMetadata {
/// Array metadata.
Expand Down
2 changes: 1 addition & 1 deletion src/storage/storage_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ pub async fn async_get_child_nodes<
NodeMetadata::Array(_) => Vec::default(),
NodeMetadata::Group(_) => async_get_child_nodes(storage, &path).await?,
};
nodes.push(Node::new(path, child_metadata, children));
nodes.push(Node::new_with_metadata(path, child_metadata, children));
}
Ok(nodes)
}
Expand Down
2 changes: 1 addition & 1 deletion src/storage/storage_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ pub fn get_child_nodes<TStorage: ?Sized + ReadableStorageTraits + ListableStorag
NodeMetadata::Array(_) => Vec::default(),
NodeMetadata::Group(_) => get_child_nodes(storage, &path)?,
};
nodes.push(Node::new(path, child_metadata, children));
nodes.push(Node::new_with_metadata(path, child_metadata, children));
}
Ok(nodes)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn hierarchy_tree() {
let store = FilesystemStore::new("./tests/data/hierarchy.zarr")
.unwrap()
.sorted();
let node = Node::new_with_store(&store, "/").unwrap();
let node = Node::new(&store, "/").unwrap();
let tree = node.hierarchy_tree();
println!("{:?}", tree);
assert_eq!(
Expand Down

0 comments on commit 559e4ce

Please sign in to comment.