Skip to content

fix(store): speed up Store.open by avoiding empty/clear calls unless needed #2314

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

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 4 additions & 7 deletions src/zarr/abc/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,10 @@ def __exit__(
async def _open(self) -> None:
if self._is_open:
raise ValueError("store is already open")
if not await self.empty():
if self.mode.update or self.mode.readonly:
pass
elif self.mode.overwrite:
await self.clear()
else:
raise FileExistsError("Store already exists")
if self.mode.str == "w":
await self.clear()
elif self.mode.str == "w-" and not await self.empty():
raise FileExistsError("Store already exists")
self._is_open = True

async def _ensure_open(self) -> None:
Expand Down
9 changes: 9 additions & 0 deletions src/zarr/testing/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ def test_store_mode(self, store: S, store_kwargs: dict[str, Any]) -> None:
with pytest.raises(AttributeError):
store.mode = AccessMode.from_literal("w") # type: ignore[misc]

@pytest.mark.parametrize("mode", ["r", "r+", "a", "w", "w-"])
async def test_store_open_mode(
self, store_kwargs: dict[str, Any], mode: AccessModeLiteral
) -> None:
store_kwargs["mode"] = mode
store = await self.store_cls.open(**store_kwargs)
assert store._is_open
assert store.mode == AccessMode.from_literal(mode)

async def test_not_writable_store_raises(self, store_kwargs: dict[str, Any]) -> None:
kwargs = {**store_kwargs, "mode": "r"}
store = await self.store_cls.open(**kwargs)
Expand Down
4 changes: 4 additions & 0 deletions tests/v3/test_store/test_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,7 @@ def test_api_integration(self, store: ZipStore) -> None:
async def test_with_mode(self, store: ZipStore) -> None:
with pytest.raises(NotImplementedError, match="new mode"):
await super().test_with_mode(store)

@pytest.mark.parametrize("mode", ["a", "w"])
async def test_store_open_mode(self, store_kwargs: dict[str, Any], mode: str) -> None:
super().test_store_open_mode(store_kwargs, mode)