diff --git a/CHANGELOG.md b/CHANGELOG.md index e62e1674..a1d9f2b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Add `From` for `ChunkKeyEncoding` + - Add chunk key encoding tests + +### Fixed + - Fixed chunk key encoding for 0 dimensional arrays with `default` and `v2` encoding ## [0.7.2] - 2023-12-17 diff --git a/src/array/chunk_key_encoding/default.rs b/src/array/chunk_key_encoding/default.rs index db6de464..4564b095 100644 --- a/src/array/chunk_key_encoding/default.rs +++ b/src/array/chunk_key_encoding/default.rs @@ -99,13 +99,63 @@ impl ChunkKeyEncodingTraits for DefaultChunkKeyEncoding { } fn encode(&self, chunk_grid_indices: &[u64]) -> StoreKey { - let key = "c".to_string() - + &self.separator.to_string() - + &chunk_grid_indices - .iter() - .map(std::string::ToString::to_string) - .collect::>() - .join(&self.separator.to_string()); + let mut key = "c".to_string(); + if !chunk_grid_indices.is_empty() { + key = key + + &self.separator.to_string() + + &chunk_grid_indices + .iter() + .map(std::string::ToString::to_string) + .collect::>() + .join(&self.separator.to_string()); + } unsafe { StoreKey::new_unchecked(key) } } } + +#[cfg(test)] +mod tests { + use crate::{node::NodePath, storage::data_key}; + + use super::*; + + #[test] + fn slash_nd() { + let key = data_key( + &NodePath::root(), + &[1, 23, 45], + &DefaultChunkKeyEncoding::new_slash().into(), + ); + assert_eq!(key, StoreKey::new("c/1/23/45").unwrap()); + } + + #[test] + fn dot_nd() { + let key = data_key( + &NodePath::root(), + &[1, 23, 45], + &DefaultChunkKeyEncoding::new_dot().into(), + ); + assert_eq!(key, StoreKey::new("c.1.23.45").unwrap()); + } + + #[test] + fn slash_scalar() { + let key = data_key( + &NodePath::root(), + &[], + &DefaultChunkKeyEncoding::new_slash().into(), + ); + assert_eq!(key, StoreKey::new("c").unwrap()); + } + + #[test] + fn dot_scalar() { + let key = data_key( + &NodePath::root(), + &[], + &DefaultChunkKeyEncoding::new_dot().into(), + ); + assert_eq!(key, StoreKey::new("c").unwrap()); + } +} diff --git a/src/array/chunk_key_encoding/v2.rs b/src/array/chunk_key_encoding/v2.rs index 41c9e823..af77d415 100644 --- a/src/array/chunk_key_encoding/v2.rs +++ b/src/array/chunk_key_encoding/v2.rs @@ -97,11 +97,62 @@ impl ChunkKeyEncodingTraits for V2ChunkKeyEncoding { } fn encode(&self, chunk_grid_indices: &[u64]) -> StoreKey { - let key = chunk_grid_indices - .iter() - .map(std::string::ToString::to_string) - .collect::>() - .join(&self.separator.to_string()); + let key = if chunk_grid_indices.is_empty() { + "0".to_string() + } else { + chunk_grid_indices + .iter() + .map(std::string::ToString::to_string) + .collect::>() + .join(&self.separator.to_string()) + }; unsafe { StoreKey::new_unchecked(key) } } } + +#[cfg(test)] +mod tests { + use crate::{node::NodePath, storage::data_key}; + + use super::*; + + #[test] + fn slash_nd() { + let key = data_key( + &NodePath::root(), + &[1, 23, 45], + &V2ChunkKeyEncoding::new_slash().into(), + ); + assert_eq!(key, StoreKey::new("1/23/45").unwrap()); + } + + #[test] + fn dot_nd() { + let key = data_key( + &NodePath::root(), + &[1, 23, 45], + &V2ChunkKeyEncoding::new_dot().into(), + ); + assert_eq!(key, StoreKey::new("1.23.45").unwrap()); + } + + #[test] + fn slash_scalar() { + let key = data_key( + &NodePath::root(), + &[], + &V2ChunkKeyEncoding::new_slash().into(), + ); + assert_eq!(key, StoreKey::new("0").unwrap()); + } + + #[test] + fn dot_scalar() { + let key = data_key( + &NodePath::root(), + &[], + &V2ChunkKeyEncoding::new_dot().into(), + ); + assert_eq!(key, StoreKey::new("0").unwrap()); + } +}