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

Refactor: store mode and initialization #2442

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

jhamman
Copy link
Member

@jhamman jhamman commented Oct 28, 2024

Previously, opening a store in w mode, had a side effect (Store.clear). That was leading to unexpected behavior when a path argument was also supplied. This refactor takes the following approach:

  • keeps Store.mode but allows this to be only StoreAccessMode = Literal["r", "w"].
  • removes any side effect when opening a store in w mode.
  • adds StorePath._init(mode: AccessModeLiteral) where side effects can be applied at the path level

builds on #2430
closes #2359

TODO:

  • Add unit tests and/or doctests in docstrings
  • Add docstrings and API docs for any new/modified user-facing classes and functions
  • New/modified features documented in docs/tutorial.rst
  • Changes documented in docs/release.rst
  • GitHub Actions have all passed
  • Test coverage is 100% (Codecov passes)

Copy link
Member Author

@jhamman jhamman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few notes to guide review

Comment on lines +32 to +33
assert mode in ("r", "w")
self._mode = mode
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we may consider a different setting here (e.g. readonly)


@abstractmethod
def with_mode(self, mode: AccessModeLiteral) -> Self:
def with_mode(self, mode: StoreAccessMode) -> Self:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To limit the scope here, I decided to keep this. But I'm not convinced we need it anymore.

else:
mode = "r+"
store_mode = _handle_store_mode(mode)
print(f"store_mode: {store_mode}")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print(f"store_mode: {store_mode}")

Comment on lines +81 to +85
def _handle_store_mode(mode: AccessModeLiteral | None) -> StoreAccessMode:
if mode is None:
return "r"
else:
return persistence_to_store_modes[mode]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do we have to change higher up in the codebase if we narrow the function signature to mode: AccessModeLiteral? My gut feeling is that we should not need to handle None here, and the None -> r normalization can happen earlier.

except (KeyError, FileNotFoundError):
pass
if mode in {"a", "w", "w-"}:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we bind this set to a variable, and similarly for {"r", "r+", "a"}? that would improve the literacy a bit.

match mode:
case "w-":
if not await self.empty():
raise FileExistsError(f"{self} must be empty. Mode is 'w-'")
Copy link
Contributor

@d-v-b d-v-b Oct 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise FileExistsError(f"{self} must be empty. Mode is 'w-'")
msg = (
f"{self} is not empty, but `mode` is set to 'w-'."
"Either remove the existing objects in storage,"
"or set `mode` to a value that handles pre-existing objects"
"in storage, like `a` or `w`."
)
raise FileExistsError(msg)

FileExistsError
If the mode is 'w-' and the store path already exists.
"""
match mode:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this our first match statement in the codebase?

Comment on lines +141 to +144
# try:
# del root[array_path]
# except KeyError:
# pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# try:
# del root[array_path]
# except KeyError:
# pass

self._is_open = False
self._mode = AccessMode.from_literal(mode)
assert mode in ("r", "w")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's this assertion for?

"""
Check if the store is empty.
Check if the directory is empty.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we going with object storage vocab or file system vocab? If the former, then maybe we should say something like "check if the store contains any keys with the given prefix"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[v3] Re-opening an existing RemoteStore V3 group causes the first child to be deleted
2 participants