use itertools::Itertools;
use zarrs_chunk_key_encoding::{ChunkKeyEncoding, ChunkKeyEncodingPlugin, ChunkKeyEncodingTraits};
use zarrs_metadata::v3::MetadataV3;
use zarrs_metadata::{ChunkKeySeparator, Configuration};
pub use zarrs_metadata_ext::chunk_key_encoding::default::DefaultChunkKeyEncodingConfiguration;
use zarrs_plugin::PluginCreateError;
use zarrs_storage::StoreKey;
zarrs_plugin::impl_extension_aliases!(DefaultChunkKeyEncoding, v3: "default");
inventory::submit! {
ChunkKeyEncodingPlugin::new::<DefaultChunkKeyEncoding>()
}
#[derive(Debug, Clone)]
pub struct DefaultChunkKeyEncoding {
separator: ChunkKeySeparator,
}
impl DefaultChunkKeyEncoding {
#[must_use]
pub const fn new(separator: ChunkKeySeparator) -> Self {
Self { separator }
}
#[must_use]
pub const fn new_dot() -> Self {
Self {
separator: ChunkKeySeparator::Dot,
}
}
#[must_use]
pub const fn new_slash() -> Self {
Self {
separator: ChunkKeySeparator::Slash,
}
}
}
impl Default for DefaultChunkKeyEncoding {
fn default() -> Self {
Self {
separator: ChunkKeySeparator::Slash,
}
}
}
impl ChunkKeyEncodingTraits for DefaultChunkKeyEncoding {
fn create(metadata: &MetadataV3) -> Result<ChunkKeyEncoding, PluginCreateError> {
let configuration: DefaultChunkKeyEncodingConfiguration =
metadata.to_typed_configuration()?;
let default = DefaultChunkKeyEncoding::new(configuration.separator);
Ok(default.into())
}
fn configuration(&self) -> Configuration {
DefaultChunkKeyEncodingConfiguration {
separator: self.separator,
}
.into()
}
fn encode(&self, chunk_grid_indices: &[u64]) -> StoreKey {
const PREFIX: &str = "c";
let key = if chunk_grid_indices.is_empty() {
PREFIX.to_string()
} else {
let mut separator_str: [u8; 4] = [0; 4];
let separator_char: char = self.separator.into();
let separator_str: &str = separator_char.encode_utf8(&mut separator_str);
let mut buffers = vec![itoa::Buffer::new(); chunk_grid_indices.len()];
let iter = chunk_grid_indices
.iter()
.zip(&mut buffers)
.map(|(&n, buffer)| buffer.format(n));
#[allow(clippy::let_and_return)]
let out = [PREFIX].into_iter().chain(iter).join(separator_str);
out
};
unsafe { StoreKey::new_unchecked(key) }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::node::{NodePath, data_key};
#[test]
fn slash_nd() {
let chunk_key_encoding: ChunkKeyEncoding = DefaultChunkKeyEncoding::new_slash().into();
let key = data_key(&NodePath::root(), &chunk_key_encoding.encode(&[1, 23, 45]));
assert_eq!(key, StoreKey::new("c/1/23/45").unwrap());
}
#[test]
fn dot_nd() {
let chunk_key_encoding: ChunkKeyEncoding = DefaultChunkKeyEncoding::new_dot().into();
let key = data_key(&NodePath::root(), &chunk_key_encoding.encode(&[1, 23, 45]));
assert_eq!(key, StoreKey::new("c.1.23.45").unwrap());
}
#[test]
fn slash_scalar() {
let chunk_key_encoding: ChunkKeyEncoding = DefaultChunkKeyEncoding::new_slash().into();
let key = data_key(&NodePath::root(), &chunk_key_encoding.encode(&[]));
assert_eq!(key, StoreKey::new("c").unwrap());
}
#[test]
fn dot_scalar() {
let chunk_key_encoding: ChunkKeyEncoding = DefaultChunkKeyEncoding::new_dot().into();
let key = data_key(&NodePath::root(), &chunk_key_encoding.encode(&[]));
assert_eq!(key, StoreKey::new("c").unwrap());
}
}