-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from LDeakin/array_sharded_ext_traits
Add array extension traits to simplify working with sharded arrays
- Loading branch information
Showing
7 changed files
with
778 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use super::{codec::ShardingCodecConfiguration, Array, ChunkGrid, ChunkShape}; | ||
|
||
/// An [`Array`] extension trait to simplify working with arrays using the `sharding_indexed` codec. | ||
pub trait ArrayShardedExt { | ||
/// Returns true if the array to bytes codec of the array is `sharding_indexed`. | ||
fn is_sharded(&self) -> bool; | ||
|
||
/// Return the inner chunk shape. | ||
/// | ||
/// Returns [`None`] for an unsharded array. | ||
fn inner_chunk_shape(&self) -> Option<ChunkShape>; | ||
|
||
/// Retrieve the inner chunk grid. | ||
/// | ||
/// Returns the normal chunk grid for an unsharded array. | ||
fn inner_chunk_grid(&self) -> ChunkGrid; | ||
} | ||
|
||
impl<TStorage: ?Sized> ArrayShardedExt for Array<TStorage> { | ||
fn is_sharded(&self) -> bool { | ||
self.codecs | ||
.array_to_bytes_codec() | ||
.create_metadata() | ||
.expect("the array to bytes codec should have metadata") | ||
.name() // TODO: Add codec::identifier()? | ||
== super::codec::array_to_bytes::sharding::IDENTIFIER | ||
} | ||
|
||
fn inner_chunk_shape(&self) -> Option<ChunkShape> { | ||
let codec_metadata = self | ||
.codecs | ||
.array_to_bytes_codec() | ||
.create_metadata() | ||
.expect("the array to bytes codec should have metadata"); | ||
if let Ok(ShardingCodecConfiguration::V1(sharding_configuration)) = | ||
codec_metadata.to_configuration() | ||
{ | ||
Some(sharding_configuration.chunk_shape) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn inner_chunk_grid(&self) -> ChunkGrid { | ||
if let Some(inner_chunk_shape) = self.inner_chunk_shape() { | ||
ChunkGrid::new(crate::array::chunk_grid::RegularChunkGrid::new( | ||
inner_chunk_shape, | ||
)) | ||
} else { | ||
self.chunk_grid().clone() | ||
} | ||
} | ||
} |
Oops, something went wrong.