Skip to content

Commit b387aeb

Browse files
committed
use chunktransform
1 parent 2927797 commit b387aeb

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

src/zarr/abc/codec.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@ def _encode_sync(
8383
class SupportsChunkCodec(Protocol):
8484
"""Protocol for objects that can decode/encode whole chunks synchronously.
8585
86-
[`CodecChain`][zarr.core.codec_pipeline.CodecChain] satisfies this protocol.
86+
[`ChunkTransform`][zarr.core.codec_pipeline.ChunkTransform] satisfies this protocol.
8787
"""
8888

89+
array_spec: ArraySpec
90+
8991
def decode_chunk(self, chunk_bytes: Buffer) -> NDBuffer: ...
9092

9193
def encode_chunk(self, chunk_array: NDBuffer) -> Buffer | None: ...
@@ -316,7 +318,6 @@ def serialize(
316318
def prepare_read_sync(
317319
self,
318320
byte_getter: Any,
319-
chunk_spec: ArraySpec,
320321
chunk_selection: SelectorTuple,
321322
codec_chain: SupportsChunkCodec,
322323
) -> NDBuffer | None:
@@ -328,21 +329,19 @@ def prepare_read_sync(
328329
byte_getter : Any
329330
An object supporting ``get_sync`` (e.g.
330331
[`StorePath`][zarr.storage._common.StorePath]).
331-
chunk_spec : ArraySpec
332-
The [`ArraySpec`][zarr.core.array_spec.ArraySpec] for the chunk.
333332
chunk_selection : SelectorTuple
334333
Selection within the decoded chunk array.
335334
codec_chain : SupportsChunkCodec
336335
The [`SupportsChunkCodec`][zarr.abc.codec.SupportsChunkCodec] used to
337-
decode the chunk.
336+
decode the chunk. Must carry an ``array_spec`` attribute.
338337
339338
Returns
340339
-------
341340
NDBuffer or None
342341
The decoded chunk data at *chunk_selection*, or ``None`` if the
343342
chunk does not exist in the store.
344343
"""
345-
raw = byte_getter.get_sync(prototype=chunk_spec.prototype)
344+
raw = byte_getter.get_sync(prototype=codec_chain.array_spec.prototype)
346345
if raw is None:
347346
return None
348347
chunk_array = codec_chain.decode_chunk(raw)
@@ -351,7 +350,7 @@ def prepare_read_sync(
351350
def prepare_write_sync(
352351
self,
353352
byte_setter: Any,
354-
chunk_spec: ArraySpec,
353+
codec_chain: SupportsChunkCodec,
355354
chunk_selection: SelectorTuple,
356355
out_selection: SelectorTuple,
357356
replace: bool,
@@ -367,8 +366,9 @@ def prepare_write_sync(
367366
byte_setter : Any
368367
An object supporting ``get_sync`` and ``set_sync`` (e.g.
369368
[`StorePath`][zarr.storage._common.StorePath]).
370-
chunk_spec : ArraySpec
371-
The [`ArraySpec`][zarr.core.array_spec.ArraySpec] for the chunk.
369+
codec_chain : SupportsChunkCodec
370+
The [`SupportsChunkCodec`][zarr.abc.codec.SupportsChunkCodec]
371+
carrying the ``array_spec`` for the chunk.
372372
chunk_selection : SelectorTuple
373373
Selection within the chunk being written.
374374
out_selection : SelectorTuple
@@ -384,6 +384,7 @@ def prepare_write_sync(
384384
A [`PreparedWrite`][zarr.abc.codec.PreparedWrite] carrying the
385385
deserialized chunk data and selection metadata.
386386
"""
387+
chunk_spec = codec_chain.array_spec
387388
existing: Buffer | None = None
388389
if not replace:
389390
existing = byte_setter.get_sync(prototype=chunk_spec.prototype)
@@ -403,7 +404,7 @@ def prepare_write_sync(
403404
def finalize_write_sync(
404405
self,
405406
prepared: PreparedWrite,
406-
chunk_spec: ArraySpec,
407+
codec_chain: SupportsChunkCodec,
407408
byte_setter: Any,
408409
) -> None:
409410
"""Serialize the prepared chunk data and write it to the store.
@@ -416,13 +417,14 @@ def finalize_write_sync(
416417
prepared : PreparedWrite
417418
The [`PreparedWrite`][zarr.abc.codec.PreparedWrite] returned by
418419
[`prepare_write_sync`][zarr.abc.codec.ArrayBytesCodec.prepare_write_sync].
419-
chunk_spec : ArraySpec
420-
The [`ArraySpec`][zarr.core.array_spec.ArraySpec] for the chunk.
420+
codec_chain : SupportsChunkCodec
421+
The [`SupportsChunkCodec`][zarr.abc.codec.SupportsChunkCodec]
422+
carrying the ``array_spec`` for the chunk.
421423
byte_setter : Any
422424
An object supporting ``set_sync`` and ``delete_sync`` (e.g.
423425
[`StorePath`][zarr.storage._common.StorePath]).
424426
"""
425-
blob = self.serialize(prepared.chunk_dict, chunk_spec)
427+
blob = self.serialize(prepared.chunk_dict, codec_chain.array_spec)
426428
if blob is None:
427429
byte_setter.delete_sync()
428430
else:
@@ -435,7 +437,6 @@ def finalize_write_sync(
435437
async def prepare_read(
436438
self,
437439
byte_getter: Any,
438-
chunk_spec: ArraySpec,
439440
chunk_selection: SelectorTuple,
440441
codec_chain: SupportsChunkCodec,
441442
) -> NDBuffer | None:
@@ -447,21 +448,19 @@ async def prepare_read(
447448
byte_getter : Any
448449
An object supporting ``get`` (e.g.
449450
[`StorePath`][zarr.storage._common.StorePath]).
450-
chunk_spec : ArraySpec
451-
The [`ArraySpec`][zarr.core.array_spec.ArraySpec] for the chunk.
452451
chunk_selection : SelectorTuple
453452
Selection within the decoded chunk array.
454453
codec_chain : SupportsChunkCodec
455454
The [`SupportsChunkCodec`][zarr.abc.codec.SupportsChunkCodec] used to
456-
decode the chunk.
455+
decode the chunk. Must carry an ``array_spec`` attribute.
457456
458457
Returns
459458
-------
460459
NDBuffer or None
461460
The decoded chunk data at *chunk_selection*, or ``None`` if the
462461
chunk does not exist in the store.
463462
"""
464-
raw = await byte_getter.get(prototype=chunk_spec.prototype)
463+
raw = await byte_getter.get(prototype=codec_chain.array_spec.prototype)
465464
if raw is None:
466465
return None
467466
chunk_array = codec_chain.decode_chunk(raw)
@@ -470,7 +469,7 @@ async def prepare_read(
470469
async def prepare_write(
471470
self,
472471
byte_setter: Any,
473-
chunk_spec: ArraySpec,
472+
codec_chain: SupportsChunkCodec,
474473
chunk_selection: SelectorTuple,
475474
out_selection: SelectorTuple,
476475
replace: bool,
@@ -483,8 +482,9 @@ async def prepare_write(
483482
byte_setter : Any
484483
An object supporting ``get`` and ``set`` (e.g.
485484
[`StorePath`][zarr.storage._common.StorePath]).
486-
chunk_spec : ArraySpec
487-
The [`ArraySpec`][zarr.core.array_spec.ArraySpec] for the chunk.
485+
codec_chain : SupportsChunkCodec
486+
The [`SupportsChunkCodec`][zarr.abc.codec.SupportsChunkCodec]
487+
carrying the ``array_spec`` for the chunk.
488488
chunk_selection : SelectorTuple
489489
Selection within the chunk being written.
490490
out_selection : SelectorTuple
@@ -500,6 +500,7 @@ async def prepare_write(
500500
A [`PreparedWrite`][zarr.abc.codec.PreparedWrite] carrying the
501501
deserialized chunk data and selection metadata.
502502
"""
503+
chunk_spec = codec_chain.array_spec
503504
existing: Buffer | None = None
504505
if not replace:
505506
existing = await byte_setter.get(prototype=chunk_spec.prototype)
@@ -519,7 +520,7 @@ async def prepare_write(
519520
async def finalize_write(
520521
self,
521522
prepared: PreparedWrite,
522-
chunk_spec: ArraySpec,
523+
codec_chain: SupportsChunkCodec,
523524
byte_setter: Any,
524525
) -> None:
525526
"""Async variant of
@@ -530,13 +531,14 @@ async def finalize_write(
530531
prepared : PreparedWrite
531532
The [`PreparedWrite`][zarr.abc.codec.PreparedWrite] returned by
532533
[`prepare_write`][zarr.abc.codec.ArrayBytesCodec.prepare_write].
533-
chunk_spec : ArraySpec
534-
The [`ArraySpec`][zarr.core.array_spec.ArraySpec] for the chunk.
534+
codec_chain : SupportsChunkCodec
535+
The [`SupportsChunkCodec`][zarr.abc.codec.SupportsChunkCodec]
536+
carrying the ``array_spec`` for the chunk.
535537
byte_setter : Any
536538
An object supporting ``set`` and ``delete`` (e.g.
537539
[`StorePath`][zarr.storage._common.StorePath]).
538540
"""
539-
blob = self.serialize(prepared.chunk_dict, chunk_spec)
541+
blob = self.serialize(prepared.chunk_dict, codec_chain.array_spec)
540542
if blob is None:
541543
await byte_setter.delete()
542544
else:

0 commit comments

Comments
 (0)