Skip to content

Commit

Permalink
Fixed chunk key encoding for 0 dimensional arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
LDeakin committed Dec 21, 2023
1 parent 62857ba commit 6373dec
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- Add `From<ChunkKeyEncodingTraits>` 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

Expand Down
64 changes: 57 additions & 7 deletions src/array/chunk_key_encoding/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<String>>()
.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::<Vec<String>>()
.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());
}
}
61 changes: 56 additions & 5 deletions src/array/chunk_key_encoding/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<String>>()
.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::<Vec<String>>()
.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());
}
}

0 comments on commit 6373dec

Please sign in to comment.