Skip to content

Commit

Permalink
fix(store): speed up Store.open by avoiding empty/clear calls unless …
Browse files Browse the repository at this point in the history
…needed (#2314)
  • Loading branch information
jhamman authored Oct 9, 2024
1 parent 8f4ef26 commit aa46b45
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
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)

0 comments on commit aa46b45

Please sign in to comment.