Skip to content

Commit

Permalink
fix: support for Zarr V2 zstd encoded data created with numcodecs
Browse files Browse the repository at this point in the history
… < 0.13 (#121)
  • Loading branch information
LDeakin authored Jan 5, 2025
1 parent 15187a0 commit 8f508b0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
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))
}
}
}
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 @@ use crate::{
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 @@ pub fn array_metadata_v2_to_v3(
&configuration,
)?);
}
crate::v3::array::codec::zstd::IDENTIFIER => {
let zstd = serde_json::from_value::<ZstdCodecConfigurationNumCodecs>(
serde_json::to_value(compressor.configuration())?,
)?;
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

0 comments on commit 8f508b0

Please sign in to comment.