-
-
Notifications
You must be signed in to change notification settings - Fork 423
Add Store.get_many bulk fetch API #4112
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
Draft
TomNicholas
wants to merge
1
commit into
zarr-developers:main
Choose a base branch
from
TomNicholas:feat/store-get-many
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+130
−2
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| Add `zarr.abc.store.Store.get_many`, a bulk counterpart to `Store.get` that | ||
| retrieves many values — each a whole key or a `(key, byte_range)` pair — in a | ||
| single call. It generalizes `Store.get_ranges` (many ranges of one key) to many | ||
| keys, yielding `(request_index, Buffer | None)` batches in completion order so a | ||
| store can coalesce reads that land in the same underlying object. The method is | ||
| defined on the `Store` ABC with a default implementation that fetches the | ||
| requests concurrently with `Store.get`, so every store inherits a working | ||
| version; stores whose backend can retrieve many objects together should override | ||
| it (`FsspecStore` does, coalescing via `fsspec`'s `cat_ranges`). Coalescing | ||
| tuning is left to each store rather than exposed on the interface. This restores | ||
| and generalizes the batched-fetch capability of the v2 `getitems` Store API. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -262,7 +262,7 @@ async def test_get_raises(self, store: S) -> None: | |
| with pytest.raises((ValueError, TypeError), match=r"Unexpected byte_range, got.*"): | ||
| await store.get("c/0", prototype=default_buffer_prototype(), byte_range=(0, 2)) # type: ignore[arg-type] | ||
|
|
||
| async def test_get_many(self, store: S) -> None: | ||
| async def test_get_many_streaming(self, store: S) -> None: | ||
| """ | ||
| Ensure that multiple keys can be retrieved at once with the _get_many method. | ||
| """ | ||
|
|
@@ -407,6 +407,41 @@ async def test_get_partial_values( | |
| obs.to_bytes() == exp.to_bytes() for obs, exp in zip(observed, expected, strict=True) | ||
| ) | ||
|
|
||
| async def test_get_many(self, store: S) -> None: | ||
| # put a handful of whole values | ||
| for key, data in {"c/0/0": b"aaaaa", "c/0/1": b"bb", "c/0/2": b"cccc"}.items(): | ||
| await self.set(store, key, self.buffer_cls.from_bytes(data)) | ||
|
|
||
| # mix bare keys, an explicit (key, None) tuple, a partial range, and a | ||
| # missing key. Each request must be reported exactly once, by index. | ||
| requests: list[tuple[str, ByteRequest | None] | str] = [ | ||
| "c/0/0", | ||
| ("c/0/1", None), | ||
| ("c/0/0", RangeByteRequest(1, 3)), | ||
| "c/0/2", | ||
| "c/0/missing", | ||
| ] | ||
| collected: dict[int, Buffer | None] = {} | ||
| async for batch in store.get_many(requests, prototype=default_buffer_prototype()): | ||
| for index, value in batch: | ||
| assert index not in collected # reported exactly once | ||
| collected[index] = value | ||
|
|
||
| assert set(collected) == set(range(len(requests))) | ||
| assert collected[4] is None # missing key -> None (not omitted) | ||
| expected = {0: b"aaaaa", 1: b"bb", 2: b"aa", 3: b"cccc"} # index 2 is "aaaaa"[1:3] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we define |
||
| for index, want in expected.items(): | ||
| buffer = collected[index] | ||
| assert buffer is not None | ||
| assert buffer.to_bytes() == want | ||
|
|
||
| async def test_get_many_empty(self, store: S) -> None: | ||
| # an empty request is valid and yields no results | ||
| batches = [ | ||
| batch async for batch in store.get_many([], prototype=default_buffer_prototype()) | ||
| ] | ||
| assert [pair for batch in batches for pair in batch] == [] | ||
|
|
||
| async def test_exists(self, store: S) -> None: | ||
| assert not await store.exists("foo") | ||
| await store.set("foo/zarr.json", self.buffer_cls.from_bytes(b"bar")) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rst -> mkdocs