Skip to content

Commit

Permalink
fix: cleanup unnecessary lifetime constraints in partial decoders (#106)
Browse files Browse the repository at this point in the history
* fix: cleanup unnecessary lifetime constraints in partial decoders

* refactor: make `{Array,Bytes}PartialDecoderCache` private

* fix: remove BytesPartialDecoderCache lifetime constraint

* fix changelog
  • Loading branch information
LDeakin authored Dec 31, 2024
1 parent 9bb5fea commit 965f8a2
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 89 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

### Changed
- **Breaking**: Seal `Array` extension traits: `ArraySharded[Readable]Ext` and `ArrayChunkCacheExt`
- **Breaking**: Make `{Array,Bytes}PartialDecoderCache` private

### Fixed
- Cleanup unnecessary lifetime constraints in partial decoders

## [0.18.3] - 2024-12-30

Expand Down
6 changes: 3 additions & 3 deletions zarrs/src/array/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//!
//! A [`CodecChain`] represents a codec sequence consisting of any number of array to array and bytes to bytes codecs, and one array to bytes codec.
//! A codec chain is itself an array to bytes codec.
//! A [`ArrayPartialDecoderCache`] or [`BytesPartialDecoderCache`] may be inserted into a codec chain to optimise partial decoding where appropriate.
//! A cache may be inserted into a codec chain to optimise partial decoding where appropriate.
//!
//! See <https://zarr-specs.readthedocs.io/en/latest/v3/core/v3.0.html#id18>.
Expand Down Expand Up @@ -59,8 +59,8 @@ use thiserror::Error;

mod array_partial_decoder_cache;
mod bytes_partial_decoder_cache;
pub use array_partial_decoder_cache::ArrayPartialDecoderCache;
pub use bytes_partial_decoder_cache::BytesPartialDecoderCache;
pub(crate) use array_partial_decoder_cache::ArrayPartialDecoderCache;
pub(crate) use bytes_partial_decoder_cache::BytesPartialDecoderCache;

mod byte_interval_partial_decoder;
pub use byte_interval_partial_decoder::ByteIntervalPartialDecoder;
Expand Down
16 changes: 8 additions & 8 deletions zarrs/src/array/codec/array_partial_decoder_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ use super::{ArrayPartialDecoderTraits, ArraySubset, CodecError, CodecOptions};
use super::AsyncArrayPartialDecoderTraits;

/// A cache for an [`ArrayPartialDecoderTraits`] partial decoder.
pub struct ArrayPartialDecoderCache<'a> {
pub(crate) struct ArrayPartialDecoderCache {
decoded_representation: ChunkRepresentation,
cache: ArrayBytes<'a>,
cache: ArrayBytes<'static>,
}

impl<'a> ArrayPartialDecoderCache<'a> {
impl ArrayPartialDecoderCache {
/// Create a new partial decoder cache.
///
/// # Errors
/// Returns a [`CodecError`] if initialisation of the partial decoder fails.
pub fn new(
pub(crate) fn new(
input_handle: &dyn ArrayPartialDecoderTraits,
decoded_representation: ChunkRepresentation,
options: &CodecOptions,
Expand All @@ -43,11 +43,11 @@ impl<'a> ArrayPartialDecoderCache<'a> {
///
/// # Errors
/// Returns a [`CodecError`] if initialisation of the partial decoder fails.
pub async fn async_new(
pub(crate) async fn async_new(
input_handle: &dyn AsyncArrayPartialDecoderTraits,
decoded_representation: ChunkRepresentation,
options: &CodecOptions,
) -> Result<ArrayPartialDecoderCache<'a>, CodecError> {
) -> Result<ArrayPartialDecoderCache, CodecError> {
let bytes = input_handle
.partial_decode(
&[ArraySubset::new_with_shape(
Expand All @@ -65,7 +65,7 @@ impl<'a> ArrayPartialDecoderCache<'a> {
}
}

impl ArrayPartialDecoderTraits for ArrayPartialDecoderCache<'_> {
impl ArrayPartialDecoderTraits for ArrayPartialDecoderCache {
fn data_type(&self) -> &DataType {
self.decoded_representation.data_type()
}
Expand All @@ -90,7 +90,7 @@ impl ArrayPartialDecoderTraits for ArrayPartialDecoderCache<'_> {

#[cfg(feature = "async")]
#[async_trait::async_trait]
impl AsyncArrayPartialDecoderTraits for ArrayPartialDecoderCache<'_> {
impl AsyncArrayPartialDecoderTraits for ArrayPartialDecoderCache {
fn data_type(&self) -> &DataType {
self.decoded_representation.data_type()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ use crate::array::codec::{AsyncArrayPartialDecoderTraits, AsyncBytesPartialDecod
use super::{reverse_endianness, Endianness};

/// Partial decoder for the `bytes` codec.
pub(crate) struct BytesPartialDecoder<'a> {
input_handle: Arc<dyn BytesPartialDecoderTraits + 'a>,
pub(crate) struct BytesPartialDecoder {
input_handle: Arc<dyn BytesPartialDecoderTraits>,
decoded_representation: ChunkRepresentation,
endian: Option<Endianness>,
}

impl<'a> BytesPartialDecoder<'a> {
impl BytesPartialDecoder {
/// Create a new partial decoder for the `bytes` codec.
pub(crate) fn new(
input_handle: Arc<dyn BytesPartialDecoderTraits + 'a>,
input_handle: Arc<dyn BytesPartialDecoderTraits>,
decoded_representation: ChunkRepresentation,
endian: Option<Endianness>,
) -> Self {
Expand All @@ -38,7 +38,7 @@ impl<'a> BytesPartialDecoder<'a> {
}
}

impl ArrayPartialDecoderTraits for BytesPartialDecoder<'_> {
impl ArrayPartialDecoderTraits for BytesPartialDecoder {
fn data_type(&self) -> &DataType {
self.decoded_representation.data_type()
}
Expand Down
2 changes: 1 addition & 1 deletion zarrs/src/array/codec/array_to_bytes/codec_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::array::codec::{AsyncArrayPartialDecoderTraits, AsyncBytesPartialDecod

/// A codec chain is a sequence of array to array, a bytes to bytes, and a sequence of array to bytes codecs.
///
/// A codec chain partial decoder may insert a cache: [`ArrayPartialDecoderCache`] or [`BytesPartialDecoderCache`].
/// A codec chain partial decoder may insert a cache.
/// For example, the output of the `blosc`/`gzip` codecs should be cached since they read and decode an entire chunk.
/// If decoding (i.e. going backwards through a codec chain), then a cache may be inserted
/// - following the last codec with [`partial_decoder_decodes_all`](crate::array::codec::CodecTraits::partial_decoder_decodes_all) true, or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ use crate::array::{
use crate::array::codec::{AsyncArrayPartialDecoderTraits, AsyncBytesPartialDecoderTraits};

/// Partial decoder for the `bytes` codec.
pub(crate) struct PcodecPartialDecoder<'a> {
input_handle: Arc<dyn BytesPartialDecoderTraits + 'a>,
pub(crate) struct PcodecPartialDecoder {
input_handle: Arc<dyn BytesPartialDecoderTraits>,
decoded_representation: ChunkRepresentation,
}

impl<'a> PcodecPartialDecoder<'a> {
impl PcodecPartialDecoder {
/// Create a new partial decoder for the `bytes` codec.
pub(crate) fn new(
input_handle: Arc<dyn BytesPartialDecoderTraits + 'a>,
input_handle: Arc<dyn BytesPartialDecoderTraits>,
decoded_representation: ChunkRepresentation,
) -> Self {
Self {
Expand Down Expand Up @@ -101,7 +101,7 @@ fn do_partial_decode<'a>(
Ok(decoded_bytes)
}

impl ArrayPartialDecoderTraits for PcodecPartialDecoder<'_> {
impl ArrayPartialDecoderTraits for PcodecPartialDecoder {
fn data_type(&self) -> &DataType {
self.decoded_representation.data_type()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ use crate::{
use crate::array::codec::{AsyncArrayPartialDecoderTraits, AsyncBytesPartialDecoderTraits};

/// Partial decoder for the `bytes` codec.
pub(crate) struct VlenPartialDecoder<'a> {
input_handle: Arc<dyn BytesPartialDecoderTraits + 'a>,
pub(crate) struct VlenPartialDecoder {
input_handle: Arc<dyn BytesPartialDecoderTraits>,
decoded_representation: ChunkRepresentation,
index_codecs: Arc<CodecChain>,
data_codecs: Arc<CodecChain>,
index_data_type: VlenIndexDataType,
}

impl<'a> VlenPartialDecoder<'a> {
impl VlenPartialDecoder {
/// Create a new partial decoder for the `bytes` codec.
pub(crate) fn new(
input_handle: Arc<dyn BytesPartialDecoderTraits + 'a>,
input_handle: Arc<dyn BytesPartialDecoderTraits>,
decoded_representation: ChunkRepresentation,
index_codecs: Arc<CodecChain>,
data_codecs: Arc<CodecChain>,
Expand Down Expand Up @@ -95,7 +95,7 @@ fn decode_vlen_bytes<'a>(
}
}

impl ArrayPartialDecoderTraits for VlenPartialDecoder<'_> {
impl ArrayPartialDecoderTraits for VlenPartialDecoder {
fn data_type(&self) -> &DataType {
self.decoded_representation.data_type()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ use crate::array::{
use crate::array::codec::{AsyncArrayPartialDecoderTraits, AsyncBytesPartialDecoderTraits};

/// Partial decoder for the `bytes` codec.
pub(crate) struct VlenV2PartialDecoder<'a> {
input_handle: Arc<dyn BytesPartialDecoderTraits + 'a>,
pub(crate) struct VlenV2PartialDecoder {
input_handle: Arc<dyn BytesPartialDecoderTraits>,
decoded_representation: ChunkRepresentation,
}

impl<'a> VlenV2PartialDecoder<'a> {
impl VlenV2PartialDecoder {
/// Create a new partial decoder for the `bytes` codec.
pub(crate) fn new(
input_handle: Arc<dyn BytesPartialDecoderTraits + 'a>,
input_handle: Arc<dyn BytesPartialDecoderTraits>,
decoded_representation: ChunkRepresentation,
) -> Self {
Self {
Expand Down Expand Up @@ -54,7 +54,7 @@ fn decode_vlen_bytes<'a>(
}
}

impl ArrayPartialDecoderTraits for VlenV2PartialDecoder<'_> {
impl ArrayPartialDecoderTraits for VlenV2PartialDecoder {
fn data_type(&self) -> &DataType {
self.decoded_representation.data_type()
}
Expand Down
10 changes: 5 additions & 5 deletions zarrs/src/array/codec/array_to_bytes/zfp/zfp_partial_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ use crate::array::codec::{AsyncArrayPartialDecoderTraits, AsyncBytesPartialDecod
use super::{zarr_to_zfp_data_type, zfp_decode, ZfpMode};

/// Partial decoder for the `zfp` codec.
pub(crate) struct ZfpPartialDecoder<'a> {
input_handle: Arc<dyn BytesPartialDecoderTraits + 'a>,
pub(crate) struct ZfpPartialDecoder {
input_handle: Arc<dyn BytesPartialDecoderTraits>,
decoded_representation: ChunkRepresentation,
mode: ZfpMode,
write_header: bool,
}

impl<'a> ZfpPartialDecoder<'a> {
impl ZfpPartialDecoder {
/// Create a new partial decoder for the `zfp` codec.
pub(crate) fn new(
input_handle: Arc<dyn BytesPartialDecoderTraits + 'a>,
input_handle: Arc<dyn BytesPartialDecoderTraits>,
decoded_representation: &ChunkRepresentation,
mode: ZfpMode,
write_header: bool,
Expand All @@ -48,7 +48,7 @@ impl<'a> ZfpPartialDecoder<'a> {
}
}

impl ArrayPartialDecoderTraits for ZfpPartialDecoder<'_> {
impl ArrayPartialDecoderTraits for ZfpPartialDecoder {
fn data_type(&self) -> &DataType {
self.decoded_representation.data_type()
}
Expand Down
27 changes: 10 additions & 17 deletions zarrs/src/array/codec/bytes_partial_decoder_cache.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! A cache for partial decoders.
use std::{borrow::Cow, marker::PhantomData};
use std::borrow::Cow;

use crate::{
array::RawBytes,
Expand All @@ -13,50 +13,43 @@ use super::{BytesPartialDecoderTraits, CodecError, CodecOptions};
use super::AsyncBytesPartialDecoderTraits;

/// A cache for a [`BytesPartialDecoderTraits`] partial decoder.
pub struct BytesPartialDecoderCache<'a> {
pub(crate) struct BytesPartialDecoderCache {
cache: Option<Vec<u8>>,
phantom: PhantomData<&'a ()>,
}

impl<'a> BytesPartialDecoderCache<'a> {
impl BytesPartialDecoderCache {
/// Create a new partial decoder cache.
///
/// # Errors
/// Returns a [`CodecError`] if caching fails.
pub fn new(
pub(crate) fn new(
input_handle: &dyn BytesPartialDecoderTraits,
options: &CodecOptions,
) -> Result<Self, CodecError> {
let cache = input_handle
.partial_decode(&[ByteRange::FromStart(0, None)], options)?
.map(|mut bytes| bytes.remove(0).into_owned());
Ok(Self {
cache,
phantom: PhantomData,
})
Ok(Self { cache })
}

#[cfg(feature = "async")]
/// Create a new asynchronous partial decoder cache.
///
/// # Errors
/// Returns a [`CodecError`] if caching fails.
pub async fn async_new(
pub(crate) async fn async_new(
input_handle: &dyn AsyncBytesPartialDecoderTraits,
options: &CodecOptions,
) -> Result<BytesPartialDecoderCache<'a>, CodecError> {
) -> Result<BytesPartialDecoderCache, CodecError> {
let cache = input_handle
.partial_decode(&[ByteRange::FromStart(0, None)], options)
.await?
.map(|mut bytes| bytes.remove(0).into_owned());
Ok(Self {
cache,
phantom: PhantomData,
})
Ok(Self { cache })
}
}

impl BytesPartialDecoderTraits for BytesPartialDecoderCache<'_> {
impl BytesPartialDecoderTraits for BytesPartialDecoderCache {
fn partial_decode(
&self,
decoded_regions: &[ByteRange],
Expand All @@ -77,7 +70,7 @@ impl BytesPartialDecoderTraits for BytesPartialDecoderCache<'_> {

#[cfg(feature = "async")]
#[async_trait::async_trait]
impl AsyncBytesPartialDecoderTraits for BytesPartialDecoderCache<'_> {
impl AsyncBytesPartialDecoderTraits for BytesPartialDecoderCache {
async fn partial_decode(
&self,
decoded_regions: &[ByteRange],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ use crate::array::codec::AsyncBytesPartialDecoderTraits;
use super::{blosc_decompress_bytes_partial, blosc_typesize, blosc_validate};

/// Partial decoder for the `blosc` codec.
pub(crate) struct BloscPartialDecoder<'a> {
input_handle: Arc<dyn BytesPartialDecoderTraits + 'a>,
pub(crate) struct BloscPartialDecoder {
input_handle: Arc<dyn BytesPartialDecoderTraits>,
}

impl<'a> BloscPartialDecoder<'a> {
pub(crate) fn new(input_handle: Arc<dyn BytesPartialDecoderTraits + 'a>) -> Self {
impl BloscPartialDecoder {
pub(crate) fn new(input_handle: Arc<dyn BytesPartialDecoderTraits>) -> Self {
Self { input_handle }
}
}

impl BytesPartialDecoderTraits for BloscPartialDecoder<'_> {
impl BytesPartialDecoderTraits for BloscPartialDecoder {
fn partial_decode(
&self,
decoded_regions: &[ByteRange],
Expand Down
10 changes: 5 additions & 5 deletions zarrs/src/array/codec/bytes_to_bytes/bz2/bz2_partial_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ use crate::{
use crate::array::codec::AsyncBytesPartialDecoderTraits;

/// Partial decoder for the `bz2` codec.
pub(crate) struct Bz2PartialDecoder<'a> {
input_handle: Arc<dyn BytesPartialDecoderTraits + 'a>,
pub(crate) struct Bz2PartialDecoder {
input_handle: Arc<dyn BytesPartialDecoderTraits>,
}

impl<'a> Bz2PartialDecoder<'a> {
pub(crate) fn new(input_handle: Arc<dyn BytesPartialDecoderTraits + 'a>) -> Self {
impl Bz2PartialDecoder {
pub(crate) fn new(input_handle: Arc<dyn BytesPartialDecoderTraits>) -> Self {
Self { input_handle }
}
}

impl BytesPartialDecoderTraits for Bz2PartialDecoder<'_> {
impl BytesPartialDecoderTraits for Bz2PartialDecoder {
fn partial_decode(
&self,
decoded_regions: &[ByteRange],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ use crate::array::codec::AsyncBytesPartialDecoderTraits;
use super::CHECKSUM_SIZE;

/// Partial decoder for the `crc32c` (CRC32C checksum) codec.
pub(crate) struct Crc32cPartialDecoder<'a> {
input_handle: Arc<dyn BytesPartialDecoderTraits + 'a>,
pub(crate) struct Crc32cPartialDecoder {
input_handle: Arc<dyn BytesPartialDecoderTraits>,
}

impl<'a> Crc32cPartialDecoder<'a> {
impl Crc32cPartialDecoder {
/// Create a new partial decoder for the `crc32c` codec.
pub(crate) fn new(input_handle: Arc<dyn BytesPartialDecoderTraits + 'a>) -> Self {
pub(crate) fn new(input_handle: Arc<dyn BytesPartialDecoderTraits>) -> Self {
Self { input_handle }
}
}

impl BytesPartialDecoderTraits for Crc32cPartialDecoder<'_> {
impl BytesPartialDecoderTraits for Crc32cPartialDecoder {
fn partial_decode(
&self,
decoded_regions: &[ByteRange],
Expand Down
Loading

0 comments on commit 965f8a2

Please sign in to comment.