-
-
Notifications
You must be signed in to change notification settings - Fork 304
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
DOC: Add memory-mapping example to storage guide #2737
Open
bendichter
wants to merge
3
commits into
zarr-developers:main
Choose a base branch
from
bendichter:doc/add-mmap-example
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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,83 @@ | ||
from __future__ import annotations | ||
|
||
import mmap | ||
from typing import TYPE_CHECKING | ||
|
||
import pytest | ||
|
||
import zarr | ||
from zarr.core.buffer import Buffer, cpu | ||
from zarr.storage import LocalStore | ||
from zarr.testing.store import StoreTests | ||
|
||
if TYPE_CHECKING: | ||
import pathlib | ||
|
||
|
||
class MemoryMappedDirectoryStore(LocalStore): | ||
def _fromfile(self, fn: str) -> memoryview: | ||
with open(fn, "rb") as fh: | ||
return memoryview(mmap.mmap(fh.fileno(), 0, prot=mmap.PROT_READ)) | ||
|
||
|
||
class TestMemoryMappedDirectoryStore(StoreTests[MemoryMappedDirectoryStore, cpu.Buffer]): | ||
store_cls = MemoryMappedDirectoryStore | ||
buffer_cls = cpu.Buffer | ||
|
||
async def get(self, store: MemoryMappedDirectoryStore, key: str) -> Buffer: | ||
return self.buffer_cls.from_bytes((store.root / key).read_bytes()) | ||
|
||
async def set(self, store: MemoryMappedDirectoryStore, key: str, value: Buffer) -> None: | ||
parent = (store.root / key).parent | ||
if not parent.exists(): | ||
parent.mkdir(parents=True) | ||
(store.root / key).write_bytes(value.to_bytes()) | ||
|
||
@pytest.fixture | ||
def store_kwargs(self, tmpdir) -> dict[str, str]: | ||
return {"root": str(tmpdir)} | ||
|
||
def test_store_repr(self, store: MemoryMappedDirectoryStore) -> None: | ||
assert str(store) == f"file://{store.root.as_posix()}" | ||
|
||
def test_store_supports_writes(self, store: MemoryMappedDirectoryStore) -> None: | ||
assert store.supports_writes | ||
|
||
def test_store_supports_partial_writes(self, store: MemoryMappedDirectoryStore) -> None: | ||
assert store.supports_partial_writes | ||
|
||
def test_store_supports_listing(self, store: MemoryMappedDirectoryStore) -> None: | ||
assert store.supports_listing | ||
|
||
async def test_empty_with_empty_subdir(self, store: MemoryMappedDirectoryStore) -> None: | ||
assert await store.is_empty("") | ||
(store.root / "foo/bar").mkdir(parents=True) | ||
assert await store.is_empty("") | ||
|
||
def test_creates_new_directory(self, tmp_path: pathlib.Path): | ||
target = tmp_path.joinpath("a", "b", "c") | ||
assert not target.exists() | ||
|
||
store = self.store_cls(root=target) | ||
zarr.group(store=store) | ||
|
||
async def test_mmap_slice_reads(self, store: MemoryMappedDirectoryStore) -> None: | ||
"""Test reading slices with memory mapping""" | ||
# Create array with large chunks | ||
z = zarr.create_array(store=store, shape=(2000, 2000), chunks=(1000, 1000), dtype="float64") | ||
# Write test data | ||
data = zarr.full(shape=(2000, 2000), chunks=(1000, 1000), fill_value=42.0, dtype="float64") | ||
z[:] = data[:] | ||
|
||
# Test reading various slices | ||
slices = [ | ||
# Within single chunk | ||
(slice(100, 200), slice(100, 200)), | ||
# Across chunk boundaries | ||
(slice(900, 1100), slice(900, 1100)), | ||
# Full chunk | ||
(slice(0, 1000), slice(0, 1000)), | ||
] | ||
|
||
for test_slice in slices: | ||
assert (z[test_slice] == data[test_slice]).all() |
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.
I don't think
_fromfile
will ever be invoked, since it's not part of theLocalStore
API