-
Notifications
You must be signed in to change notification settings - Fork 8
⚙️ FEATURE-#287: Expand Storage ABC with delete, list, TTL, CAS #297
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
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
caaff3f
⚙️ FEATURE-#287: Expand Storage ABC with delete and CAS primitives
FernandoCelmer 287d0a4
⚙️ FEATURE-#287: Add S3 delete and list_keys helpers
FernandoCelmer 17c1fc4
⚙️ FEATURE-#287: Add GCS delete and list_keys helpers
FernandoCelmer ac5f173
⚙️ FEATURE-#287: Implement new ABC surface in StorageDefault
FernandoCelmer 1fe6bb1
⚙️ FEATURE-#287: Implement new ABC surface in StorageFile
FernandoCelmer e97f31e
⚙️ FEATURE-#287: Implement new ABC surface in StorageS3
FernandoCelmer 174550e
⚙️ FEATURE-#287: Implement new ABC surface in StorageGCS
FernandoCelmer dda6c51
⚙️ FEATURE-#287: Add dotflow.testing public package
FernandoCelmer ac412b5
⚙️ FEATURE-#287: Add StorageContract reusable test suite
FernandoCelmer c745840
❤️ TEST-#287: Run StorageContract against StorageDefault
FernandoCelmer 4a97472
❤️ TEST-#287: Run StorageContract against StorageFile
FernandoCelmer 8f4aeb9
🪲 BUG-#287: Fix S3.delete exception and add conditional helpers
FernandoCelmer d187d08
⚙️ FEATURE-#287: Add GCS conditional write helpers
FernandoCelmer 566537e
⚙️ FEATURE-#287: Pass ttl and fingerprint through atomic_swap
FernandoCelmer 41db3c9
🪲 BUG-#287: Clear stale TTL on StorageDefault.atomic_swap
FernandoCelmer 49acb07
🪲 BUG-#287: Serialize StorageFile public methods through lock
FernandoCelmer 549ef8c
⚙️ FEATURE-#287: Use S3 conditional write for atomic_swap
FernandoCelmer be2f3c6
⚙️ FEATURE-#287: Use GCS generation precondition for atomic_swap
FernandoCelmer 3124bf3
❤️ TEST-#287: Cover ttl propagation through atomic_swap
FernandoCelmer 3660cc9
📝 PEP8-#287: Apply ruff format to S3.write_if_match signature
FernandoCelmer 181e16a
🪲 BUG-#287: Keep new ABC methods optional in 1.x for backward compat
FernandoCelmer 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
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 |
|---|---|---|
| @@ -1,29 +1,103 @@ | ||
| """Storage Default""" | ||
|
|
||
| from collections.abc import Callable | ||
| from __future__ import annotations | ||
|
|
||
| import threading | ||
| import time | ||
| from collections.abc import Callable, Iterable | ||
| from typing import Any | ||
|
|
||
| from dotflow.abc.storage import Storage | ||
| from dotflow.core.context import Context | ||
|
|
||
|
|
||
| class StorageDefault(Storage): | ||
| """In-memory storage using a dictionary.""" | ||
| """In-memory storage.""" | ||
|
|
||
| def __init__(self): | ||
| self._store: dict[str, Context] = {} | ||
| self._fingerprints: dict[str, str] = {} | ||
| self._expirations: dict[str, float] = {} | ||
| self._lock = threading.RLock() | ||
|
|
||
| def post( | ||
| self, | ||
| key: str, | ||
| context: Context, | ||
| ttl: int | None = None, | ||
| fingerprint: str | None = None, | ||
| ) -> None: | ||
| with self._lock: | ||
| self._store[key] = context | ||
|
|
||
| if fingerprint is not None: | ||
| self._fingerprints[key] = fingerprint | ||
|
|
||
| def post(self, key: str, context: Context) -> None: | ||
| self._store[key] = context | ||
| if ttl is not None: | ||
| self._expirations[key] = time.monotonic() + ttl | ||
| else: | ||
| self._expirations.pop(key, None) | ||
|
|
||
| def get(self, key: str) -> Context: | ||
| return self._store.get(key, Context()) | ||
| with self._lock: | ||
| self._evict_if_expired(key) | ||
|
|
||
| return self._store.get(key, Context()) | ||
|
|
||
| def delete(self, key: str) -> bool: | ||
| with self._lock: | ||
| existed = key in self._store | ||
| self._store.pop(key, None) | ||
| self._fingerprints.pop(key, None) | ||
| self._expirations.pop(key, None) | ||
|
|
||
| return existed | ||
|
|
||
| def delete_prefix(self, prefix: str) -> int: | ||
| with self._lock: | ||
| stale = [k for k in self._store if k.startswith(prefix)] | ||
|
|
||
| for key in stale: | ||
| self._store.pop(key, None) | ||
| self._fingerprints.pop(key, None) | ||
| self._expirations.pop(key, None) | ||
|
|
||
| return len(stale) | ||
|
|
||
| def list_keys(self, prefix: str) -> Iterable[str]: | ||
| with self._lock: | ||
| for key in list(self._store): | ||
| self._evict_if_expired(key) | ||
|
|
||
| return [k for k in self._store if k.startswith(prefix)] | ||
|
|
||
| def atomic_swap(self, key: str, expected: Any, new: Any) -> bool: | ||
| with self._lock: | ||
| current = self._store.get(key) | ||
| current_value = ( | ||
| current.storage if isinstance(current, Context) else current | ||
| ) | ||
|
|
||
| if current_value != expected: | ||
| return False | ||
|
|
||
| payload = new if isinstance(new, Context) else Context(storage=new) | ||
| self._store[key] = payload | ||
|
|
||
|
FernandoCelmer marked this conversation as resolved.
|
||
| return True | ||
|
|
||
| def key(self, task: Callable) -> str: | ||
| return f"{task.workflow_id}-{task.task_id}" | ||
|
|
||
| def clear(self, workflow_id: str) -> None: | ||
| prefix = f"{workflow_id}-" | ||
| stale = [k for k in self._store if k.startswith(prefix)] | ||
| def _evict_if_expired(self, key: str) -> None: | ||
| expiry = self._expirations.get(key) | ||
|
|
||
| if expiry is None: | ||
| return | ||
|
|
||
| if time.monotonic() < expiry: | ||
| return | ||
|
|
||
| for key in stale: | ||
| del self._store[key] | ||
| self._store.pop(key, None) | ||
| self._fingerprints.pop(key, None) | ||
| self._expirations.pop(key, None) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.