Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when chunk coordinates are invalid #395

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion icechunk/src/format/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::metadata::{
use super::{
format_constants, manifest::ManifestRef, AttributesId, IcechunkFormatError,
IcechunkFormatVersion, IcechunkResult, ManifestId, NodeId, ObjectId, Path,
SnapshotId, TableOffset,
SnapshotId, TableOffset, ChunkIndices
};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
Expand Down Expand Up @@ -49,6 +49,55 @@ pub struct ZarrArrayMetadata {
pub dimension_names: Option<DimensionNames>,
}

impl ZarrArrayMetadata {

/// Returns an iterator over the maximum permitted chunk indices for the array.
///
/// This function calculates the maximum chunk indices based on the shape of the array
/// and the chunk shape.
///
/// # Returns
///
/// An iterator over the maximum permitted chunk indices.
fn max_chunk_indices_permitted(&self) -> impl Iterator<Item = u64> + '_ {
self.shape
.iter()
.zip(self.chunk_shape.0.iter())
.map(|(s, cs)| ((s + cs.get() - 1)) / cs.get() - 1)
}

/// Validates the provided chunk coordinates for the array.
///
/// This function checks if the provided chunk indices are valid for the array.
///
/// # Arguments
///
/// * `coord` - The chunk indices to validate.
///
/// # Returns
///
/// An `IcechunkResult` indicating whether the chunk coordinates are valid.
///
/// # Errors
///
/// Returns `IcechunkFormatError::ChunkCoordinatesNotFound` if the chunk coordinates are invalid.
pub fn valid_chunk_coord(&self, coord: &ChunkIndices) -> IcechunkResult<bool> {

let valid: bool = coord
.0
.iter()
.zip(self.max_chunk_indices_permitted())
.all(|(index, index_permitted)| *index <= index_permitted as u32);

if valid {
Ok(true)
} else {
Err(IcechunkFormatError::ChunkCoordinatesNotFound { coords: (coord.clone()) })
}

}
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum NodeData {
Array(ZarrArrayMetadata, Vec<ManifestRef>),
Expand Down
17 changes: 14 additions & 3 deletions icechunk/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,20 @@ impl Repository {
coord: ChunkIndices,
data: Option<ChunkPayload>,
) -> RepositoryResult<()> {
self.get_array(&path)
.await
.map(|node| self.change_set.set_chunk_ref(node.id, coord, data))

let node_snapshot = self.get_array(&path).await?;

if let NodeData::Array(zarr_metadata, _, ) = node_snapshot.node_data {
zarr_metadata.valid_chunk_coord(&coord)?;
self.change_set.set_chunk_ref(node_snapshot.id, coord, data);
Ok(())
} else {
Err(RepositoryError::NotAnArray {
node: node_snapshot,
message: "getting an array".to_string(),
})
}

}

pub async fn get_node(&self, path: &Path) -> RepositoryResult<NodeSnapshot> {
Expand Down