Skip to content

Commit 72bf9f4

Browse files
authored
Merge pull request #6 from kylebarron/kyle/fix-transform-list-dir
Use `list_with_delimiter` in `list_dir` method
2 parents 65f2db4 + ebf7be1 commit 72bf9f4

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ test = [
8686
"mypy",
8787
"hypothesis",
8888
"universal-pathlib",
89-
"obstore==0.3.0",
9089
]
9190
optional = ["rich", "universal-pathlib"]
9291
docs = [
@@ -213,6 +212,7 @@ dependencies = [
213212
'universal_pathlib @ git+https://github.com/fsspec/universal_pathlib',
214213
'typing_extensions @ git+https://github.com/python/typing_extensions',
215214
'donfig @ git+https://github.com/pytroll/donfig',
215+
"obstore @ git+https://github.com/developmentseed/obstore@main#subdirectory=obstore",
216216
# test deps
217217
'hypothesis',
218218
'pytest',

src/zarr/storage/object_store.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from collections.abc import AsyncGenerator, Coroutine, Iterable
2323
from typing import Any
2424

25-
from obstore import ListStream, ObjectMeta, OffsetRange, SuffixRange
25+
from obstore import ListResult, ListStream, ObjectMeta, OffsetRange, SuffixRange
2626
from obstore.store import ObjectStore as _ObjectStore
2727

2828
from zarr.core.buffer import Buffer, BufferPrototype
@@ -168,8 +168,8 @@ def list_prefix(self, prefix: str) -> AsyncGenerator[str, None]:
168168
return _transform_list(objects)
169169

170170
def list_dir(self, prefix: str) -> AsyncGenerator[str, None]:
171-
objects: ListStream[list[ObjectMeta]] = obs.list(self.store, prefix=prefix)
172-
return _transform_list_dir(objects, prefix)
171+
coroutine = obs.list_with_delimiter_async(self.store, prefix=prefix)
172+
return _transform_list_dir(coroutine, prefix)
173173

174174

175175
async def _transform_list(
@@ -180,19 +180,23 @@ async def _transform_list(
180180
yield item["path"]
181181

182182

183+
# ['zarr.json', 'c/0', 'c/1'] -> ['zarr.json']
184+
# ['zarr.json', 'c/0', 'c/1'] -> ['zarr.json, 'c']
183185
async def _transform_list_dir(
184-
list_stream: AsyncGenerator[list[ObjectMeta], None], prefix: str
186+
list_result_coroutine: Coroutine[Any, Any, ListResult], prefix: str
185187
) -> AsyncGenerator[str, None]:
188+
"""
189+
Transform the result of list_with_delimiter into an async generator of prefixes and paths.
190+
"""
191+
list_result = await list_result_coroutine
192+
186193
# We assume that the underlying object-store implementation correctly handles the
187194
# prefix, so we don't double-check that the returned results actually start with the
188195
# given prefix.
189-
prefix_len = len(prefix) + 1 # If one is not added to the length, all items will contain "/"
190-
async for batch in list_stream:
191-
for item in batch:
192-
# Yield this item if "/" does not exist after the prefix
193-
item_path = item["path"][prefix_len:]
194-
if "/" not in item_path:
195-
yield item_path
196+
prefixes = list_result["common_prefixes"]
197+
objects = [obj["path"].lstrip(prefix) for obj in list_result["objects"]]
198+
for item in prefixes + objects:
199+
yield item
196200

197201

198202
class _BoundedRequest(TypedDict):

0 commit comments

Comments
 (0)