File size: 1,491 Bytes
ddebc3e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | """Revision semantics for storage backends.
## Contract
- A **revision** is a monotonically increasing integer scoped to a single
storage domain (e.g. accounts, proxies).
- Every mutation that changes persisted state **must** increment the revision
by exactly 1.
- Readers compare their last-seen revision against the current value to detect
whether new data is available (``scan_changes(since_revision=...)``).
- Revisions are **not** globally ordered across domains — only within one.
## RevisionTracker mixin
Backends that manage their own revision counter (e.g. SQLite, in-memory)
can inherit from ``RevisionTracker`` to get a thread-safe bump + read helper.
"""
import threading
class RevisionTracker:
"""Thread-safe monotonic revision counter.
Subclass or mix into a storage backend to provide ``bump()`` and
``current`` without touching the database.
"""
def __init__(self, initial: int = 0) -> None:
self._revision = initial
self._lock = threading.Lock()
@property
def current(self) -> int:
with self._lock:
return self._revision
def bump(self) -> int:
"""Increment and return the new revision."""
with self._lock:
self._revision += 1
return self._revision
def set(self, value: int) -> None:
"""Force-set the revision (e.g. after loading from DB)."""
with self._lock:
self._revision = value
__all__ = ["RevisionTracker"]
|