Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add array storage helpers #2065

Merged
merged 31 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ebbfbe0
implement store.list_prefix and store._set_dict
d-v-b Aug 3, 2024
da6083e
simplify string handling
d-v-b Aug 3, 2024
dc5fe47
add nchunks_initialized, and necessary additions for it
d-v-b Aug 2, 2024
b694b6e
rename _iter_chunks to _iter_chunk_coords
d-v-b Aug 2, 2024
6a27ca8
fix test name
d-v-b Aug 3, 2024
d15be9a
bring in correct store list_dir implementations
d-v-b Aug 3, 2024
ef34f25
Merge branch 'v3' of https://github.com/zarr-developers/zarr-python i…
d-v-b Aug 12, 2024
962ffed
bump numcodecs to dodge zstd exception
d-v-b Aug 12, 2024
5c98ab4
remove store._set_dict, and add _set_many and get_many instead
d-v-b Aug 12, 2024
9e64fa8
update deprecation warning template
d-v-b Aug 13, 2024
a4b4696
add a type annotation
d-v-b Aug 13, 2024
04b1d6a
refactor chunk iterators. they are not properties any more, just meth…
d-v-b Aug 13, 2024
12b3bc1
Merge branch 'v3' of https://github.com/zarr-developers/zarr-python i…
d-v-b Aug 13, 2024
3e2c656
Merge branch 'v3' of github.com:zarr-developers/zarr-python into add-…
d-v-b Sep 19, 2024
b7c1a56
_get_many returns tuple[str, buffer]
d-v-b Sep 19, 2024
44bed5c
stricter store types
d-v-b Sep 19, 2024
021d41e
Merge branch 'v3' of github.com:zarr-developers/zarr-python into add-…
d-v-b Sep 23, 2024
2db860b
fix types
d-v-b Sep 23, 2024
45f27b1
Merge branch 'v3' of github.com:zarr-developers/zarr-python into add-…
d-v-b Sep 23, 2024
78f22b9
Merge branch 'v3' of github.com:zarr-developers/zarr-python into add-…
d-v-b Sep 24, 2024
b5e08e8
lint
d-v-b Sep 24, 2024
43743e1
remove deprecation warnings
d-v-b Sep 24, 2024
f65a6e8
fix zip list_prefix
d-v-b Sep 24, 2024
df6f9a7
tests for nchunks_initialized, chunks_initialized; add selection_shap…
d-v-b Sep 24, 2024
e60cbe0
add nchunks test
d-v-b Sep 24, 2024
5c54449
fix docstrings
d-v-b Sep 24, 2024
e8598c6
fix docstring
d-v-b Sep 24, 2024
ae216e1
Merge branch 'v3' into add-array-storage-helpers
d-v-b Sep 24, 2024
768ab43
revert unnecessary changes to project config
d-v-b Sep 24, 2024
c953f21
Merge branch 'add-array-storage-helpers' of github.com:d-v-b/zarr-pyt…
d-v-b Sep 24, 2024
f0d61b2
Merge branch 'v3' of https://github.com/zarr-developers/zarr-python i…
d-v-b Sep 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions src/zarr/abc/store.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from asyncio import gather
from collections.abc import AsyncGenerator, Iterable
from typing import Any, NamedTuple, Protocol, runtime_checkable
from typing import TYPE_CHECKING, Any, NamedTuple, Protocol, runtime_checkable

if TYPE_CHECKING:
from collections.abc import AsyncGenerator, Iterable
from typing import Any, TypeAlias

from typing_extensions import Self
from typing_extensions import Self

from zarr.core.buffer import Buffer, BufferPrototype
from zarr.core.common import AccessModeLiteral, BytesLike
from zarr.core.buffer import Buffer, BufferPrototype
from zarr.core.common import AccessModeLiteral, BytesLike

__all__ = ["Store", "AccessMode", "ByteGetter", "ByteSetter", "set_or_delete"]

ByteRangeRequest: TypeAlias = tuple[int | None, int | None]


class AccessMode(NamedTuple):
str: AccessModeLiteral
Expand Down Expand Up @@ -94,14 +101,14 @@ async def get(
self,
key: str,
prototype: BufferPrototype,
byte_range: tuple[int | None, int | None] | None = None,
byte_range: ByteRangeRequest | None = None,
) -> Buffer | None:
"""Retrieve the value associated with a given key.

Parameters
----------
key : str
byte_range : tuple[int, Optional[int]], optional
byte_range : tuple[int | None, int | None], optional

Returns
-------
Expand All @@ -113,13 +120,13 @@ async def get(
async def get_partial_values(
self,
prototype: BufferPrototype,
key_ranges: list[tuple[str, tuple[int | None, int | None]]],
key_ranges: Iterable[tuple[str, ByteRangeRequest]],
) -> list[Buffer | None]:
"""Retrieve possibly partial values from given key_ranges.

Parameters
----------
key_ranges : list[tuple[str, tuple[int, int]]]
key_ranges : Iterable[tuple[str, tuple[int | None, int | None]]]
Ordered set of key, range pairs, a key may occur multiple times with different ranges

Returns
Expand Down Expand Up @@ -189,7 +196,9 @@ def supports_partial_writes(self) -> bool:
...

@abstractmethod
async def set_partial_values(self, key_start_values: list[tuple[str, int, BytesLike]]) -> None:
async def set_partial_values(
self, key_start_values: Iterable[tuple[str, int, BytesLike]]
) -> None:
"""Store values at a given key, starting at byte range_start.

Parameters
Expand Down Expand Up @@ -253,21 +262,32 @@ def close(self) -> None:
"""Close the store."""
self._is_open = False

async def _get_many(
self, requests: Iterable[tuple[str, BufferPrototype, ByteRangeRequest | None]]
) -> AsyncGenerator[tuple[str, Buffer | None], None]:
"""
Retrieve a collection of objects from storage. In general this method does not guarantee
that objects will be retrieved in the order in which they were requested, so this method
yields tuple[str, Buffer | None] instead of just Buffer | None
"""
for req in requests:
yield (req[0], await self.get(*req))


@runtime_checkable
class ByteGetter(Protocol):
async def get(
self, prototype: BufferPrototype, byte_range: tuple[int, int | None] | None = None
self, prototype: BufferPrototype, byte_range: ByteRangeRequest | None = None
) -> Buffer | None: ...


@runtime_checkable
class ByteSetter(Protocol):
async def get(
self, prototype: BufferPrototype, byte_range: tuple[int, int | None] | None = None
self, prototype: BufferPrototype, byte_range: ByteRangeRequest | None = None
) -> Buffer | None: ...

async def set(self, value: Buffer, byte_range: tuple[int, int] | None = None) -> None: ...
async def set(self, value: Buffer, byte_range: ByteRangeRequest | None = None) -> None: ...

async def delete(self) -> None: ...

Expand Down
6 changes: 3 additions & 3 deletions src/zarr/codecs/sharding.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Codec,
CodecPipeline,
)
from zarr.abc.store import ByteGetter, ByteSetter
from zarr.abc.store import ByteGetter, ByteRangeRequest, ByteSetter
from zarr.codecs.bytes import BytesCodec
from zarr.codecs.crc32c_ import Crc32cCodec
from zarr.core.array_spec import ArraySpec
Expand Down Expand Up @@ -78,7 +78,7 @@ class _ShardingByteGetter(ByteGetter):
chunk_coords: ChunkCoords

async def get(
self, prototype: BufferPrototype, byte_range: tuple[int, int | None] | None = None
self, prototype: BufferPrototype, byte_range: ByteRangeRequest | None = None
) -> Buffer | None:
assert byte_range is None, "byte_range is not supported within shards"
assert (
Expand All @@ -91,7 +91,7 @@ async def get(
class _ShardingByteSetter(_ShardingByteGetter, ByteSetter):
shard_dict: ShardMutableMapping

async def set(self, value: Buffer, byte_range: tuple[int, int] | None = None) -> None:
async def set(self, value: Buffer, byte_range: ByteRangeRequest | None = None) -> None:
assert byte_range is None, "byte_range is not supported within shards"
self.shard_dict[self.chunk_coords] = value

Expand Down
Loading