Skip to content

Commit aa38698

Browse files
committed
v2codec supports sync
1 parent 0766289 commit aa38698

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

src/zarr/codecs/_v2.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@ class V2Codec(ArrayBytesCodec):
2323

2424
is_fixed_size = False
2525

26-
async def _decode_single(
26+
def _decode_sync(
2727
self,
2828
chunk_bytes: Buffer,
2929
chunk_spec: ArraySpec,
3030
) -> NDBuffer:
3131
cdata = chunk_bytes.as_array_like()
3232
# decompress
3333
if self.compressor:
34-
chunk = await asyncio.to_thread(self.compressor.decode, cdata)
34+
chunk = self.compressor.decode(cdata)
3535
else:
3636
chunk = cdata
3737

3838
# apply filters
3939
if self.filters:
4040
for f in reversed(self.filters):
41-
chunk = await asyncio.to_thread(f.decode, chunk)
41+
chunk = f.decode(chunk)
4242

4343
# view as numpy array with correct dtype
4444
chunk = ensure_ndarray_like(chunk)
@@ -70,7 +70,7 @@ async def _decode_single(
7070

7171
return get_ndbuffer_class().from_ndarray_like(chunk)
7272

73-
async def _encode_single(
73+
def _encode_sync(
7474
self,
7575
chunk_array: NDBuffer,
7676
chunk_spec: ArraySpec,
@@ -83,18 +83,32 @@ async def _encode_single(
8383
# apply filters
8484
if self.filters:
8585
for f in self.filters:
86-
chunk = await asyncio.to_thread(f.encode, chunk)
86+
chunk = f.encode(chunk)
8787
# check object encoding
8888
if ensure_ndarray_like(chunk).dtype == object:
8989
raise RuntimeError("cannot write object array without object codec")
9090

9191
# compress
9292
if self.compressor:
93-
cdata = await asyncio.to_thread(self.compressor.encode, chunk)
93+
cdata = self.compressor.encode(chunk)
9494
else:
9595
cdata = chunk
9696
cdata = ensure_bytes(cdata)
9797
return chunk_spec.prototype.buffer.from_bytes(cdata)
9898

99+
async def _decode_single(
100+
self,
101+
chunk_bytes: Buffer,
102+
chunk_spec: ArraySpec,
103+
) -> NDBuffer:
104+
return await asyncio.to_thread(self._decode_sync, chunk_bytes, chunk_spec)
105+
106+
async def _encode_single(
107+
self,
108+
chunk_array: NDBuffer,
109+
chunk_spec: ArraySpec,
110+
) -> Buffer | None:
111+
return await asyncio.to_thread(self._encode_sync, chunk_array, chunk_spec)
112+
99113
def compute_encoded_size(self, _input_byte_length: int, _chunk_spec: ArraySpec) -> int:
100114
raise NotImplementedError

0 commit comments

Comments
 (0)