Skip to content

Commit

Permalink
sharding with empty inner chunk and index location start (#16)
Browse files Browse the repository at this point in the history
* test_sharding_with_empty_inner_chunk

* only update non empty chunks offset
  • Loading branch information
brokkoli71 authored Sep 28, 2024
1 parent 23cf523 commit 966bab4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
29 changes: 29 additions & 0 deletions tests/test_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -1082,3 +1082,32 @@ def test_update_attributes_group(store: Store):

g = Group.open(store / "update_attributes_group")
assert g.metadata.attributes["hello"] == "zarrita"


@pytest.mark.parametrize(
"index_location", [ShardingCodecIndexLocation.start, ShardingCodecIndexLocation.end]
)
def test_sharding_with_empty_inner_chunk(store: Store, index_location):
data = np.arange(0, 16 * 16, dtype="uint16").reshape((16, 16))
fill_value = 1
data[:4, :4] = fill_value

a = Array.create(
store / "sharding_with_empty_inner_chunk",
shape=(16, 16),
chunk_shape=(8, 8),
dtype=data.dtype,
fill_value=fill_value,
codecs=[
codecs.sharding_codec(
chunk_shape=(4, 4),
codecs=[
codecs.bytes_codec(),
codecs.blosc_codec(typesize=data.dtype.itemsize),
],
index_location=index_location,
)
],
)
a[:] = data
assert np.array_equal(a[:], data)
3 changes: 2 additions & 1 deletion zarrita/sharding.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ async def finalize(
) -> BytesLike:
index_bytes = await index_encoder(self.index)
if index_location == ShardingCodecIndexLocation.start:
self.index.offsets_and_lengths[..., 0] += len(index_bytes)
empty_chunks_mask = self.index.offsets_and_lengths[..., 0] == MAX_UINT_64
self.index.offsets_and_lengths[~empty_chunks_mask, 0] += len(index_bytes)
index_bytes = await index_encoder(
self.index
) # encode again with corrected offsets
Expand Down

0 comments on commit 966bab4

Please sign in to comment.