-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: make
VLenV2Codec
private, add `VLen{Array,Bytes,Utf8}Cod…
…ec` (#119)
- Loading branch information
Showing
20 changed files
with
256 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
//! The `vlen-array` array to bytes codec. | ||
use crate::array::codec::array_to_bytes::vlen_v2::vlen_v2_macros; | ||
|
||
vlen_v2_macros::vlen_v2_module!(vlen_array, vlen_array_codec, VlenArrayCodec); |
3 changes: 3 additions & 0 deletions
3
zarrs/src/array/codec/array_to_bytes/vlen_array/vlen_array_codec.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
use crate::array::codec::array_to_bytes::vlen_v2::vlen_v2_macros; | ||
|
||
vlen_v2_macros::vlen_v2_codec!(VlenArrayCodec, "vlen-array"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
//! The `vlen-bytes` array to bytes codec. | ||
use crate::array::codec::array_to_bytes::vlen_v2::vlen_v2_macros; | ||
|
||
vlen_v2_macros::vlen_v2_module!(vlen_bytes, vlen_bytes_codec, VlenBytesCodec); |
3 changes: 3 additions & 0 deletions
3
zarrs/src/array/codec/array_to_bytes/vlen_bytes/vlen_bytes_codec.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
use crate::array::codec::array_to_bytes::vlen_v2::vlen_v2_macros; | ||
|
||
vlen_v2_macros::vlen_v2_codec!(VlenBytesCodec, "vlen-bytes"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
//! The `vlen-utf8` array to bytes codec. | ||
use crate::array::codec::array_to_bytes::vlen_v2::vlen_v2_macros; | ||
|
||
vlen_v2_macros::vlen_v2_module!(vlen_utf8, vlen_utf8_codec, VlenUtf8Codec); |
3 changes: 3 additions & 0 deletions
3
zarrs/src/array/codec/array_to_bytes/vlen_utf8/vlen_utf8_codec.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
use crate::array::codec::array_to_bytes::vlen_v2::vlen_v2_macros; | ||
|
||
vlen_v2_macros::vlen_v2_codec!(VlenUtf8Codec, "vlen-utf8"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
zarrs/src/array/codec/array_to_bytes/vlen_v2/vlen_v2_macros.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
macro_rules! vlen_v2_module { | ||
($module:ident, $module_codec:ident, $struct:ident) => { | ||
mod $module_codec; | ||
|
||
use std::sync::Arc; | ||
|
||
pub use $module::IDENTIFIER; | ||
|
||
pub use $module_codec::$struct; | ||
|
||
use crate::{ | ||
array::codec::{Codec, CodecPlugin}, | ||
metadata::v2::array::codec::$module, | ||
metadata::v3::MetadataV3, | ||
plugin::{PluginCreateError, PluginMetadataInvalidError}, | ||
}; | ||
|
||
// Register the codec. | ||
inventory::submit! { | ||
CodecPlugin::new(IDENTIFIER, is_name, create_codec) | ||
} | ||
|
||
fn is_name(name: &str) -> bool { | ||
name.eq(IDENTIFIER) | ||
} | ||
|
||
fn create_codec(metadata: &MetadataV3) -> Result<Codec, PluginCreateError> { | ||
if metadata.configuration_is_none_or_empty() { | ||
let codec = Arc::new($struct::new()); | ||
Ok(Codec::ArrayToBytes(codec)) | ||
} else { | ||
Err(PluginMetadataInvalidError::new(IDENTIFIER, "codec", metadata.clone()).into()) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! vlen_v2_codec { | ||
($struct:ident,$identifier:expr) => { | ||
use std::sync::Arc; | ||
|
||
use zarrs_metadata::v3::MetadataV3; | ||
|
||
use crate::array::{ | ||
codec::{ | ||
array_to_bytes::vlen_v2::VlenV2Codec, ArrayPartialDecoderTraits, | ||
ArrayPartialEncoderTraits, ArrayToBytesCodecTraits, BytesPartialDecoderTraits, | ||
BytesPartialEncoderTraits, CodecError, CodecOptions, CodecTraits, | ||
}, | ||
ArrayBytes, ArrayCodecTraits, ArrayMetadataOptions, BytesRepresentation, | ||
ChunkRepresentation, RawBytes, RecommendedConcurrency, | ||
}; | ||
|
||
#[cfg(feature = "async")] | ||
use crate::array::codec::{AsyncArrayPartialDecoderTraits, AsyncBytesPartialDecoderTraits}; | ||
|
||
/// The `$identifier` codec implementation. | ||
#[derive(Debug, Clone)] | ||
pub struct $struct { | ||
inner: Arc<VlenV2Codec>, | ||
} | ||
|
||
impl $struct { | ||
/// Create a new `$identifier` codec. | ||
#[must_use] | ||
pub fn new() -> Self { | ||
Self { | ||
inner: Arc::new(VlenV2Codec::new($identifier.to_string())), | ||
} | ||
} | ||
} | ||
|
||
impl Default for $struct { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl CodecTraits for $struct { | ||
fn create_metadata_opt(&self, options: &ArrayMetadataOptions) -> Option<MetadataV3> { | ||
self.inner.create_metadata_opt(options) | ||
} | ||
|
||
fn partial_decoder_should_cache_input(&self) -> bool { | ||
self.inner.partial_decoder_should_cache_input() | ||
} | ||
|
||
fn partial_decoder_decodes_all(&self) -> bool { | ||
self.inner.partial_decoder_decodes_all() | ||
} | ||
} | ||
|
||
impl ArrayCodecTraits for $struct { | ||
fn recommended_concurrency( | ||
&self, | ||
decoded_representation: &ChunkRepresentation, | ||
) -> Result<RecommendedConcurrency, CodecError> { | ||
self.inner.recommended_concurrency(decoded_representation) | ||
} | ||
} | ||
|
||
#[cfg_attr(feature = "async", async_trait::async_trait)] | ||
impl ArrayToBytesCodecTraits for $struct { | ||
fn dynamic(self: Arc<Self>) -> Arc<dyn ArrayToBytesCodecTraits> { | ||
self as Arc<dyn ArrayToBytesCodecTraits> | ||
} | ||
|
||
fn encode<'a>( | ||
&self, | ||
bytes: ArrayBytes<'a>, | ||
decoded_representation: &ChunkRepresentation, | ||
options: &CodecOptions, | ||
) -> Result<RawBytes<'a>, CodecError> { | ||
self.inner.encode(bytes, decoded_representation, options) | ||
} | ||
|
||
fn decode<'a>( | ||
&self, | ||
bytes: RawBytes<'a>, | ||
decoded_representation: &ChunkRepresentation, | ||
options: &CodecOptions, | ||
) -> Result<ArrayBytes<'a>, CodecError> { | ||
self.inner.decode(bytes, decoded_representation, options) | ||
} | ||
|
||
fn partial_decoder( | ||
self: Arc<Self>, | ||
input_handle: Arc<dyn BytesPartialDecoderTraits>, | ||
decoded_representation: &ChunkRepresentation, | ||
options: &CodecOptions, | ||
) -> Result<Arc<dyn ArrayPartialDecoderTraits>, CodecError> { | ||
self.inner | ||
.clone() | ||
.partial_decoder(input_handle, decoded_representation, options) | ||
} | ||
|
||
fn partial_encoder( | ||
self: Arc<Self>, | ||
input_handle: Arc<dyn BytesPartialDecoderTraits>, | ||
output_handle: Arc<dyn BytesPartialEncoderTraits>, | ||
decoded_representation: &ChunkRepresentation, | ||
options: &CodecOptions, | ||
) -> Result<Arc<dyn ArrayPartialEncoderTraits>, CodecError> { | ||
self.inner.clone().partial_encoder( | ||
input_handle, | ||
output_handle, | ||
decoded_representation, | ||
options, | ||
) | ||
} | ||
|
||
#[cfg(feature = "async")] | ||
async fn async_partial_decoder( | ||
self: Arc<Self>, | ||
input_handle: Arc<dyn AsyncBytesPartialDecoderTraits>, | ||
decoded_representation: &ChunkRepresentation, | ||
options: &CodecOptions, | ||
) -> Result<Arc<dyn AsyncArrayPartialDecoderTraits>, CodecError> { | ||
self.inner | ||
.clone() | ||
.async_partial_decoder(input_handle, decoded_representation, options) | ||
.await | ||
} | ||
|
||
fn compute_encoded_size( | ||
&self, | ||
decoded_representation: &ChunkRepresentation, | ||
) -> Result<BytesRepresentation, CodecError> { | ||
self.inner.compute_encoded_size(decoded_representation) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
pub(crate) use vlen_v2_codec; | ||
pub(crate) use vlen_v2_module; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.