Skip to content

Commit c39e03c

Browse files
committed
Use prefix
1 parent 384d323 commit c39e03c

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/zarr/abc/store.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,9 @@ async def getsize(self, key: str) -> int:
417417
raise FileNotFoundError(key)
418418
return len(value)
419419

420-
async def getsize_dir(self, prefix: str) -> int:
420+
async def getsize_prefix(self, prefix: str) -> int:
421421
"""
422-
Return the size, in bytes, of all values in a directory.
422+
Return the size, in bytes, of all values under a prefix.
423423
424424
This will include just values whose keys start with ``prefix`` and
425425
do not contain the character ``/`` after the given prefix.
@@ -440,7 +440,7 @@ async def getsize_dir(self, prefix: str) -> int:
440440
listing all the keys in a directory and calling :meth:`Store.getsize`
441441
on each.
442442
"""
443-
keys = [x async for x in self.list_dir(prefix)]
443+
keys = [x async for x in self.list_prefix(prefix)]
444444
sizes = await gather(*[self.getsize(key) for key in keys])
445445
return sum(sizes)
446446

src/zarr/core/array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ def nchunks_initialized(self) -> int:
844844
return nchunks_initialized(self)
845845

846846
async def nbytes_stored(self) -> int:
847-
return await self.store_path.store.getsize_dir(self.store_path.path)
847+
return await self.store_path.store.getsize_prefix(self.store_path.path)
848848

849849
def _iter_chunk_coords(
850850
self, *, origin: Sequence[int] | None = None, selection_shape: Sequence[int] | None = None

tests/test_array.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,28 @@ def test_chunks_initialized(test_cls: type[Array] | type[AsyncArray[Any]]) -> No
371371
assert observed == expected
372372

373373

374-
def test_nbytes_stored():
374+
def test_nbytes_stored() -> None:
375375
arr = zarr.create(shape=(100,), chunks=(10,), dtype="i4")
376376
result = arr.nbytes_stored()
377-
assert result == 366 # the size of the metadata document. This is a fragile test
377+
assert result == 366 # the size of the metadata document. This is a fragile test.
378378
arr[:50] = 1
379379
result = arr.nbytes_stored()
380-
assert result == 366
380+
assert result == 566 # the size with 5 chunks filled.
381+
arr[50:] = 2
382+
result = arr.nbytes_stored()
383+
assert result == 766 # the size with all chunks filled.
384+
385+
386+
async def test_nbytes_stored_async() -> None:
387+
arr = await zarr.api.asynchronous.create(shape=(100,), chunks=(10,), dtype="i4")
388+
result = await arr.nbytes_stored()
389+
assert result == 366 # the size of the metadata document. This is a fragile test.
390+
await arr.setitem(slice(50), 1)
391+
result = await arr.nbytes_stored()
392+
assert result == 566 # the size with 5 chunks filled.
393+
await arr.setitem(slice(50, 100), 2)
394+
result = await arr.nbytes_stored()
395+
assert result == 766 # the size with all chunks filled.
381396

382397

383398
def test_default_fill_values() -> None:

0 commit comments

Comments
 (0)