|
16 | 16 | from zarr.core.config import config |
17 | 17 |
|
18 | 18 | if TYPE_CHECKING: |
19 | | - from collections.abc import AsyncGenerator, Coroutine, Iterable |
| 19 | + from collections.abc import AsyncGenerator, Coroutine, Iterable, Sequence |
20 | 20 | from typing import Any |
21 | 21 |
|
22 | 22 | from obstore import ListResult, ListStream, ObjectMeta, OffsetRange, SuffixRange |
@@ -212,41 +212,48 @@ def supports_listing(self) -> bool: |
212 | 212 | # docstring inherited |
213 | 213 | return True |
214 | 214 |
|
215 | | - def list(self) -> AsyncGenerator[str, None]: |
216 | | - # docstring inherited |
| 215 | + async def _list(self, prefix: str | None = None) -> AsyncGenerator[ObjectMeta, None]: |
217 | 216 | import obstore as obs |
218 | 217 |
|
219 | | - objects: ListStream[list[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 |
221 | 222 |
|
222 | | - def list_prefix(self, prefix: str) -> AsyncGenerator[str, None]: |
| 223 | + # return (obj async for obj in _transform_list(objects)) |
| 224 | + |
| 225 | + def list(self) -> AsyncGenerator[str, None]: |
223 | 226 | # docstring inherited |
224 | | - import obstore as obs |
| 227 | + return (obj["path"] async for obj in self._list()) |
225 | 228 |
|
226 | | - objects: ListStream[list[ObjectMeta]] = obs.list(self.store, prefix=prefix) |
227 | | - return _transform_list(objects) |
| 229 | + def list_prefix(self, prefix: str) -> AsyncGenerator[str, None]: |
| 230 | + # docstring inherited |
| 231 | + return (obj["path"] async for obj in self._list(prefix)) |
228 | 232 |
|
229 | 233 | def list_dir(self, prefix: str) -> AsyncGenerator[str, None]: |
230 | 234 | # docstring inherited |
231 | 235 | import obstore as obs |
232 | 236 |
|
233 | | - coroutine = obs.list_with_delimiter_async(self.store, prefix=prefix) |
| 237 | + coroutine: Coroutine[Any, Any, ListResult[Sequence[ObjectMeta]]] = ( |
| 238 | + obs.list_with_delimiter_async(self.store, prefix=prefix) |
| 239 | + ) |
234 | 240 | return _transform_list_dir(coroutine, prefix) |
235 | 241 |
|
| 242 | + async def getsize(self, key: str) -> int: |
| 243 | + # docstring inherited |
| 244 | + import obstore as obs |
236 | 245 |
|
237 | | -async def _transform_list( |
238 | | - list_stream: ListStream[list[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"] |
| 246 | + resp = await obs.head_async(self.store, key) |
| 247 | + return resp["size"] |
| 248 | + |
| 249 | + async def getsize_prefix(self, prefix: str) -> int: |
| 250 | + # docstring inherited |
| 251 | + sizes = [obj["size"] async for obj in self._list(prefix=prefix)] |
| 252 | + return sum(sizes) |
246 | 253 |
|
247 | 254 |
|
248 | 255 | async def _transform_list_dir( |
249 | | - list_result_coroutine: Coroutine[Any, Any, ListResult[list[ObjectMeta]]], prefix: str |
| 256 | + list_result_coroutine: Coroutine[Any, Any, ListResult[Sequence[ObjectMeta]]], prefix: str |
250 | 257 | ) -> AsyncGenerator[str, None]: |
251 | 258 | """ |
252 | 259 | Transform the result of list_with_delimiter into an async generator of paths. |
|
0 commit comments