Skip to content

Commit

Permalink
Blosc codec config allow deserialization with missing typesize/`shu…
Browse files Browse the repository at this point in the history
…ffle`
  • Loading branch information
LDeakin committed Mar 5, 2024
1 parent a92ff2d commit 854971c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- Fixed various errors in storage docs
- Blosc codec config allow deserialization with missing `typesize`/`shuffle`

## [0.12.2] - 2024-02-26

Expand Down
3 changes: 2 additions & 1 deletion src/array/codec/bytes_to_bytes/blosc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ impl<'de> serde::Deserialize<'de> for BloscCompressionLevel {
}

/// The `blosc` shuffle mode.
#[derive(Serialize, Deserialize, Copy, Clone, Debug, Eq, PartialEq)]
#[derive(Serialize, Deserialize, Copy, Clone, Debug, Eq, PartialEq, Default)]
#[serde(rename_all = "lowercase")]
#[repr(u32)]
pub enum BloscShuffleMode {
/// No shuffling.
#[default]
NoShuffle = BLOSC_NOSHUFFLE,
/// Byte-wise shuffling.
Shuffle = BLOSC_SHUFFLE,
Expand Down
2 changes: 1 addition & 1 deletion src/array/codec/bytes_to_bytes/blosc/blosc_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct BloscCodec {
impl BloscCodec {
/// Create a new `blosc` codec.
///
/// The block size is chosen automatically if `blocksize` is none.
/// The block size is chosen automatically if `blocksize` is none or zero.
/// `typesize` must be a positive integer if shuffling is enabled.
///
/// # Errors
Expand Down
55 changes: 54 additions & 1 deletion src/array/codec/bytes_to_bytes/blosc/blosc_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@ pub struct BloscCodecConfigurationV1 {
/// The compression level.
pub clevel: BloscCompressionLevel,
/// The shuffle mode.
///
/// Defaults to noshuffle if unspecified.
#[serde(default)]
pub shuffle: BloscShuffleMode,
/// The type size in bytes.
///
/// Required unless shuffle is "noshuffle", in which case the value is ignored.
// FIXME: Change to option on major release
#[serde(default, skip_serializing_if = "usize_is_zero")]
pub typesize: usize,
/// The compression block size. Automatically determined if [`None`].
/// The compression block size. Automatically determined if [`None`] or 0.
// FIXME: Change to usize on major version change
pub blocksize: Option<usize>,
}

fn usize_is_zero(v: &usize) -> bool {
v == &0
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -62,6 +74,47 @@ mod tests {
.unwrap();
}

#[test]
fn codec_blosc_invalid_no_typesize() {
serde_json::from_str::<BloscCodecConfiguration>(
r#"
{
"cname": "lz4",
"clevel": 4,
"shuffle": "bitshuffle",
"blocksize": 0
}"#,
)
.unwrap();
}

#[test]
fn codec_blosc_valid_no_shuffle() {
serde_json::from_str::<BloscCodecConfiguration>(
r#"
{
"cname": "lz4",
"clevel": 4,
"blocksize": 0
}"#,
)
.unwrap();
}

#[test]
fn codec_blosc_valid_no_typesize() {
serde_json::from_str::<BloscCodecConfiguration>(
r#"
{
"cname": "lz4",
"clevel": 4,
"shuffle": "shuffle",
"blocksize": 0
}"#,
)
.unwrap();
}

#[test]
fn codec_blosc_invalid_clevel() {
let json = r#"
Expand Down

0 comments on commit 854971c

Please sign in to comment.