Skip to content

Commit 9cbc7b0

Browse files
slowjazzd-v-b
andauthored
obstore implementations for .getsize and .getsize_prefix (#3227)
* obstore implementations for .getsize and .getsize_prefix * rm comment * rm typehint --------- Co-authored-by: Davis Bennett <davis.v.bennett@gmail.com>
1 parent 0019733 commit 9cbc7b0

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

changes/3227.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add lightweight implementations of .getsize() and .getsize_prefix() for ObjectStore.

src/zarr/storage/_obstore.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -212,19 +212,21 @@ def supports_listing(self) -> bool:
212212
# docstring inherited
213213
return True
214214

215-
def list(self) -> AsyncGenerator[str, None]:
216-
# docstring inherited
215+
async def _list(self, prefix: str | None = None) -> AsyncGenerator[ObjectMeta, None]:
217216
import obstore as obs
218217

219-
objects: ListStream[Sequence[ObjectMeta]] = obs.list(self.store)
220-
return _transform_list(objects)
218+
objects: ListStream[Sequence[ObjectMeta]] = obs.list(self.store, prefix=prefix)
219+
async for batch in objects:
220+
for item in batch:
221+
yield item
221222

222-
def list_prefix(self, prefix: str) -> AsyncGenerator[str, None]:
223+
def list(self) -> AsyncGenerator[str, None]:
223224
# docstring inherited
224-
import obstore as obs
225+
return (obj["path"] async for obj in self._list())
225226

226-
objects: ListStream[Sequence[ObjectMeta]] = obs.list(self.store, prefix=prefix)
227-
return _transform_list(objects)
227+
def list_prefix(self, prefix: str) -> AsyncGenerator[str, None]:
228+
# docstring inherited
229+
return (obj["path"] async for obj in self._list(prefix))
228230

229231
def list_dir(self, prefix: str) -> AsyncGenerator[str, None]:
230232
# docstring inherited
@@ -233,21 +235,21 @@ def list_dir(self, prefix: str) -> AsyncGenerator[str, None]:
233235
coroutine = obs.list_with_delimiter_async(self.store, prefix=prefix)
234236
return _transform_list_dir(coroutine, prefix)
235237

238+
async def getsize(self, key: str) -> int:
239+
# docstring inherited
240+
import obstore as obs
236241

237-
async def _transform_list(
238-
list_stream: ListStream[Sequence[ObjectMeta]],
239-
) -> AsyncGenerator[str, None]:
240-
"""
241-
Transform the result of list into an async generator of paths.
242-
"""
243-
async for batch in list_stream:
244-
for item in batch:
245-
yield item["path"]
242+
resp = await obs.head_async(self.store, key)
243+
return resp["size"]
244+
245+
async def getsize_prefix(self, prefix: str) -> int:
246+
# docstring inherited
247+
sizes = [obj["size"] async for obj in self._list(prefix=prefix)]
248+
return sum(sizes)
246249

247250

248251
async def _transform_list_dir(
249-
list_result_coroutine: Coroutine[Any, Any, ListResult[Sequence[ObjectMeta]]],
250-
prefix: str,
252+
list_result_coroutine: Coroutine[Any, Any, ListResult[Sequence[ObjectMeta]]], prefix: str
251253
) -> AsyncGenerator[str, None]:
252254
"""
253255
Transform the result of list_with_delimiter into an async generator of paths.

tests/test_store/test_object.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,21 @@ def test_store_init_raises(self) -> None:
7575
with pytest.raises(TypeError):
7676
ObjectStore("path/to/store")
7777

78+
async def test_store_getsize(self, store: ObjectStore) -> None:
79+
buf = cpu.Buffer.from_bytes(b"\x01\x02\x03\x04")
80+
await self.set(store, "key", buf)
81+
size = await store.getsize("key")
82+
assert size == len(buf)
83+
84+
async def test_store_getsize_prefix(self, store: ObjectStore) -> None:
85+
buf = cpu.Buffer.from_bytes(b"\x01\x02\x03\x04")
86+
await self.set(store, "c/key1/0", buf)
87+
await self.set(store, "c/key2/0", buf)
88+
size = await store.getsize_prefix("c/key1")
89+
assert size == len(buf)
90+
total_size = await store.getsize_prefix("c")
91+
assert total_size == len(buf) * 2
92+
7893

7994
@pytest.mark.slow_hypothesis
8095
def test_zarr_hierarchy():

0 commit comments

Comments
 (0)