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

fix: support for Zarr V2 zstd encoded data created with numcodecs < 0.13 #121

Merged
merged 1 commit into from
Jan 5, 2025
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `ArrayShardedExt::is_exclusively_sharded`
- Add `ArrayShardedReadableExtCache::array_is_exclusively_sharded`
- Add `Vlen{Array,Bytes,Utf8}Codec`, replacing `VlenV2Codec`
- Add `ZstdCodecConfigurationNumCodecs`
- Adds support for Zarr V2 `zstd` encoded data created with `numcodecs` < 0.13

### Changed
- **Breaking**: Seal `Array` extension traits: `ArraySharded[Readable]Ext` and `ArrayChunkCacheExt`
Expand Down
4 changes: 2 additions & 2 deletions zarrs/tests/data/v3/array_zstd.zarr/zarr.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
{
"name": "zstd",
"configuration": {
"checksum": false,
"level": 5
"level": 5,
"checksum": false
}
}
],
Expand Down
39 changes: 39 additions & 0 deletions zarrs_metadata/src/v2/array/codec/zstd.rs
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
use derive_more::derive::{Display, From};
use serde::{Deserialize, Serialize};

pub use crate::v3::array::codec::zstd::ZstdCodecConfigurationV1;

use crate::v3::array::codec::zstd::{ZstdCodecConfiguration, ZstdCompressionLevel};

type ZstdCodecConfigurationNumCodecs0_13 = ZstdCodecConfigurationV1;

/// A wrapper to handle various versions of `zstd` codec configuration parameters.
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Debug, Display, From)]
#[serde(untagged)]
pub enum ZstdCodecConfigurationNumCodecs {
/// `numcodecs` version 0.13.
V0_13(ZstdCodecConfigurationNumCodecs0_13),
/// `numcodecs` version 0.1.
V0_1(ZstdCodecConfigurationNumCodecs0_1),
}

/// Configuration parameters for the `zstd` codec (`numcodecs` version 0.1).
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Debug, Display)]
#[serde(deny_unknown_fields)]
#[display("{}", serde_json::to_string(self).unwrap_or_default())]
pub struct ZstdCodecConfigurationNumCodecs0_1 {
/// The compression level.
pub level: ZstdCompressionLevel,
}

/// Convert [`ZstdCodecConfigurationNumCodecs`] to [`ZstdCodecConfiguration`].
#[must_use]
pub fn codec_zstd_v2_numcodecs_to_v3(
zstd: &ZstdCodecConfigurationNumCodecs,
) -> ZstdCodecConfiguration {
match zstd {
ZstdCodecConfigurationNumCodecs::V0_13(zstd) => ZstdCodecConfiguration::V1(zstd.clone()),
ZstdCodecConfigurationNumCodecs::V0_1(zstd) => {
ZstdCodecConfiguration::V1(ZstdCodecConfigurationV1::new(zstd.level.clone(), false))

Check warning on line 37 in zarrs_metadata/src/v2/array/codec/zstd.rs

View check run for this annotation

Codecov / codecov/patch

zarrs_metadata/src/v2/array/codec/zstd.rs#L36-L37

Added lines #L36 - L37 were not covered by tests
}
}
}
11 changes: 11 additions & 0 deletions zarrs_metadata/src/v2_to_v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
codec::{
blosc::{codec_blosc_v2_numcodecs_to_v3, BloscCodecConfigurationNumcodecs},
zfpy::{codec_zfpy_v2_numcodecs_to_v3, ZfpyCodecConfigurationNumcodecs},
zstd::{codec_zstd_v2_numcodecs_to_v3, ZstdCodecConfigurationNumCodecs},
},
data_type_metadata_v2_to_endianness, ArrayMetadataV2Order, DataTypeMetadataV2,
DataTypeMetadataV2InvalidEndiannessError, FillValueMetadataV2,
Expand Down Expand Up @@ -214,6 +215,16 @@
&configuration,
)?);
}
crate::v3::array::codec::zstd::IDENTIFIER => {
let zstd = serde_json::from_value::<ZstdCodecConfigurationNumCodecs>(
serde_json::to_value(compressor.configuration())?,
)?;

Check warning on line 221 in zarrs_metadata/src/v2_to_v3.rs

View check run for this annotation

Codecov / codecov/patch

zarrs_metadata/src/v2_to_v3.rs#L221

Added line #L221 was not covered by tests
let configuration = codec_zstd_v2_numcodecs_to_v3(&zstd);
codecs.push(MetadataV3::new_with_serializable_configuration(
crate::v3::array::codec::zstd::IDENTIFIER,
&configuration,
)?);
}
_ => codecs.push(MetadataV3::new_with_configuration(
compressor.id(),
compressor.configuration().clone(),
Expand Down
Loading