Repository navigation
Expand file tree
/
Copy pathpersistence_assembly.py
More file actions
348 lines (280 loc) · 16.1 KB
/
Copy pathpersistence_assembly.py
File metadata and controls
348 lines (280 loc) · 16.1 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
"""The one door from application code to persistence.
``profile_scope`` resolves the profile a caller works on; ``open_persistence(scope)`` returns a handle on that profile's
process-shared store; ``open_persistence_reader`` returns an owned read-only handle over one target that never creates,
migrates or repairs a store. A handle's repositories act on its profile only and no verb takes a scope. Opening a
handle reads no configuration and touches no file: the provider is read and the backend assembled on the handle's first
use. Every handle releases its reference on ``close``; the last release of a profile's shared backend releases its
store. ``storage_health``, ``store_exists`` and ``store_location`` answer for a profile without opening its store; ``live_stores`` lends a handle on
every store the process holds open. ``open_store_admin``
and ``open_profile_repair`` open the physical admin capability, or answer ``AdminUnsupported`` on a backend without it.
Only this module names a backend package: callers branch on ``capabilities``, never on a provider name.
"""
from __future__ import annotations
import inspect
import logging
import re
import threading
from contextlib import contextmanager
from pathlib import Path
from typing import Any, Callable, Iterator, Mapping, Optional
from persistence import ProfileScope, StorageHealth
from persistence.core.admin import PHYSICAL_ADMIN, AdminUnsupported, ProfileRepairAdmin, StoreAdmin
from persistence.core.backend import PersistenceStore
__all__ = [
"Persistence",
"close_all_stores",
"close_stores_under",
"live_stores",
"open_persistence",
"open_persistence_reader",
"open_profile_repair",
"open_store_admin",
"profile_scope",
"storage_health",
"store_exists",
"store_location",
"store_owner_name",
]
_KNOWN_PROVIDERS = ("sqlite",)
# The names a handle forwards to its bound store: the storage interface's attributes and the eager open.
_FORWARDED = frozenset(inspect.get_annotations(PersistenceStore)) | {"ready"}
_cache_lock = threading.Lock()
# hermes_home_key -> [shared backend (None until a handle's first use), open handles]
_shared: dict[str, list[Any]] = {}
class Persistence:
"""A caller's handle on one profile's store. ``scope`` and ``read_only`` (True for a reader's handle) are known at
open; ``capabilities``, ``aio``, ``ready`` and every repository attribute are the bound store's, bound on the
first access to any of them. ``close`` releases this handle's reference once; any of them read after it raises.
A name ``PersistenceStore`` does not declare, other than ``ready``, raises ``AttributeError``, open or closed.
A repository, ``aio`` view or bound method taken before ``close`` belongs to the backend, not to this handle, and
must not outlive it: the check runs on attribute lookup only. Once the backend's last handle closes, such a view
refuses too."""
def __init__(self, scope: ProfileScope, *, read_only: bool, bind: Callable[[], Any], release: Callable[[], None]) -> None:
self.scope = scope
self.read_only = read_only
self._bind = bind
self._release: Optional[Callable[[], None]] = release
self._store: Any = None
self._lock = threading.Lock()
def __getattr__(self, name: str) -> Any:
if name not in _FORWARDED:
raise AttributeError(name)
return getattr(self._bound_store(), name)
def _bound_store(self) -> Any:
"""The bound store, bound now if this is the first use; raises after ``close``."""
with self._lock:
if self._release is None:
raise RuntimeError("persistence handle used after close")
if self._store is None:
self._store = self._bind()
return self._store
def serves_same_store(self, other: Persistence) -> bool:
"""``PersistenceStore.serves_same_store`` between the stores the two handles are bound to."""
return self._bound_store().serves_same_store(other._bound_store())
def close(self) -> None:
with self._lock:
release, self._release = self._release, None
if release is not None:
release()
def __enter__(self) -> Persistence:
return self
def __exit__(self, *exc: object) -> None:
self.close()
def profile_scope(profile: Optional[str] = None, *, home: Optional[Path] = None) -> ProfileScope:
"""The scope a target names: an explicit ``home`` first, then a named profile under the default root, then the
current home. Only a named profile's own home carries its name; any other target is its own root, labelled
``default``."""
from hermes_constants import get_default_hermes_root, get_hermes_home, named_profile_home, profile_name_for_home
if home is not None:
target = Path(home)
elif profile is not None and profile != "default":
target = get_default_hermes_root() / "profiles" / profile
elif profile == "default":
target = get_default_hermes_root()
else:
target = get_hermes_home()
canonical = target.expanduser().resolve(strict=False)
name = profile_name_for_home(canonical)
if name is not None and name != "default" and (named_profile_home(canonical) or canonical) == canonical:
return ProfileScope(profile_dir=canonical, name=name)
return ProfileScope(profile_dir=canonical, name="default")
def store_owner_name(profile_dir: Path) -> Optional[str]:
"""The profile owning the store in ``profile_dir``: ``default`` for the default root, the profile's name for a
folder directly under ``<root>/profiles`` with a valid profile name, else None. Never raises: a failure logs at
debug and answers None."""
try:
from hermes_constants import get_default_hermes_root
root = get_default_hermes_root().resolve()
folder = Path(profile_dir).resolve()
if folder == root:
return "default"
if folder.parent == root / "profiles" and re.fullmatch(r"[a-z0-9][a-z0-9_-]{0,63}", folder.name):
return folder.name
except Exception: # health: allow BLE001 -- an underivable owner is None, never a fabricated one
logging.getLogger("hermes_state").debug("own-profile derivation failed", exc_info=True)
return None
def _provider_for_home(home: Path) -> str:
"""The configured ``persistence.provider`` for ``home``: the process config for the current home, the target's own
``config.yaml`` for another one. An absent section or file means ``sqlite``."""
from hermes_constants import get_hermes_home, hermes_home_key
section: Any = None
if hermes_home_key(home) == hermes_home_key(get_hermes_home()):
from hermes_cli.config import load_config_readonly
section = load_config_readonly().get("persistence")
else:
config_path = home / "config.yaml"
if config_path.exists():
from utils import fast_safe_load
try:
with open(config_path, encoding="utf-8") as fh:
loaded = fast_safe_load(fh)
except Exception: # health: allow BLE001 -- an unreadable foreign config means the default provider
loaded = None
if isinstance(loaded, dict):
section = loaded.get("persistence")
provider = "sqlite"
if isinstance(section, dict) and section.get("provider"):
provider = str(section["provider"])
if provider not in _KNOWN_PROVIDERS:
raise ValueError(
f"unknown persistence provider {provider!r} for {home} (known: {', '.join(_KNOWN_PROVIDERS)})"
)
return provider
def _assemble(provider: str, stores: Mapping[str, Path], *, read_only: bool = False,
read_patience_s: Optional[float] = None, check_drift: bool = False) -> Any:
"""The one provider branch."""
if provider == "sqlite":
from persistence.sqlite import SQLiteBackend
return SQLiteBackend(stores, read_only=read_only, read_patience_s=read_patience_s, check_drift=check_drift)
raise ValueError(f"unknown persistence provider {provider!r}")
def _state_db(scope: ProfileScope) -> Path:
return Path(scope.hermes_home_key) / "state.db"
def _bind_shared(scope: ProfileScope, entry: list[Any]) -> Any:
with _cache_lock:
if entry[0] is None:
entry[0] = _assemble(_provider_for_home(scope.profile_dir), {scope.hermes_home_key: _state_db(scope)})
backend = entry[0]
return backend.open(scope)
def _release_shared(key: str, entry: list[Any]) -> None:
with _cache_lock:
entry[1] -= 1
if entry[1] > 0 or _shared.get(key) is not entry:
return
del _shared[key]
if entry[0] is not None:
entry[0].close()
def open_persistence(scope: ProfileScope, *, eager: bool = False) -> Persistence:
"""A handle on ``scope``'s process-shared store.
Every handle on one profile shares one backend, whose store is the registry's single writer for that file. The
first use of a handle reads the configured provider and assembles the profile's backend when no handle did yet,
raising ``ValueError`` for an unknown provider. With ``eager`` that use and the store's open happen here, so an
unopenable store raises its typed error at acquisition."""
key = scope.hermes_home_key
with _cache_lock:
entry = _shared.setdefault(key, [None, 0])
entry[1] += 1
persistence = Persistence(scope, read_only=False, bind=lambda: _bind_shared(scope, entry),
release=lambda: _release_shared(key, entry))
if eager:
try:
persistence.ready()
except BaseException:
persistence.close()
raise
return persistence
@contextmanager
def open_persistence_reader(target: str | Path, *, patience_s: Optional[float] = None,
check_drift: bool = False) -> Iterator[Persistence]:
"""An owned, read-only handle over one target, closed on exit or by its ``close``.
``target`` is a profile name, a Hermes home directory, or a database file (a ``state.db`` path or any existing
file). The store must exist: a reader never creates, migrates or repairs it. The handle's first use reads the
configured provider and assembles its backend; closing the handle closes that backend. ``patience_s`` is how
long the handle's open and its reads wait for a store another connection holds before raising
``StoreContention`` (a verb that sets its own wait reads with that one); None keeps the backend's read wait.
With ``check_drift`` the handle's connection, when it opens (``ready()`` or the first repository read; fetching a
repository such as ``reader.sessions`` opens nothing), also runs ``StoreAdmin.schema_drift_reason``'s probe and
raises ``DeclarativeDrift`` with that reason, so a browse needs no second connection for it. Such a handle also
reports every open failure to its caller only and leaves the process's last session-store init error alone."""
path = target if isinstance(target, Path) and (target.name == "state.db" or target.is_file()) else None
if isinstance(target, Path):
scope = profile_scope(home=path.parent if path is not None else target)
else:
scope = profile_scope(target)
store = path.expanduser().resolve(strict=False) if path is not None else _state_db(scope)
backends: list[Any] = []
def bind() -> Any:
backend = _assemble(_provider_for_home(scope.profile_dir), {scope.hermes_home_key: store}, read_only=True,
read_patience_s=patience_s, check_drift=check_drift)
backends.append(backend)
return backend.open(scope)
def release() -> None:
for backend in backends:
backend.close()
with Persistence(scope, read_only=True, bind=bind, release=release) as handle:
yield handle
def _admin_door(provider: str, stores: Mapping[str, Path], read_only: bool, open_admin: Callable[[Any], Any]) -> Any:
"""``provider``'s admin handle through ``open_admin``, or ``AdminUnsupported`` without the capability."""
backend = _assemble(provider, stores, read_only=read_only)
if not backend.capabilities.physical_admin:
backend.close()
return AdminUnsupported(PHYSICAL_ADMIN, provider)
return open_admin(backend)
def open_store_admin(target: ProfileScope | Path, *, read_only: bool = True,
provider: Optional[str] = None) -> StoreAdmin | AdminUnsupported:
"""An admin handle bound to ``target``, a profile's store or one database file with no live store behind it;
``AdminUnsupported`` when the backend has no physical admin capability. A profile's store uses that profile's
configured provider. A file target reads no configuration of the folder it lies in: it uses ``provider`` when the
caller passes one, else the provider configured for the current home; an unknown provider raises ``ValueError``."""
if isinstance(target, ProfileScope):
scope, path = target, _state_db(target)
provider = _provider_for_home(scope.profile_dir)
else:
from hermes_constants import get_hermes_home
path = Path(target).expanduser().resolve(strict=False)
scope = profile_scope(home=path.parent)
if provider is None:
provider = _provider_for_home(get_hermes_home())
return _admin_door(provider, {scope.hermes_home_key: path}, read_only,
lambda backend: backend.open_store_admin(target, read_only=read_only))
def open_profile_repair(scope: ProfileScope, *, read_only: bool) -> ProfileRepairAdmin | AdminUnsupported:
"""The crossed-profile repair handle bound to ``scope``'s store; ``AdminUnsupported`` when the configured backend
has no physical admin capability."""
return _admin_door(_provider_for_home(scope.profile_dir), {scope.hermes_home_key: _state_db(scope)}, read_only,
lambda backend: backend.open_profile_repair(scope, read_only=read_only))
def store_location(scope: ProfileScope) -> Optional[Path]:
"""The file that holds ``scope``'s store under the configured provider, or ``None`` for a provider without one."""
return _state_db(scope)
def storage_health(scope: ProfileScope) -> StorageHealth:
"""The health latch and readiness probe of ``scope``'s store, answered without opening it."""
from persistence.sqlite.services import SQLiteStorageHealth
return SQLiteStorageHealth(_state_db(scope))
def store_exists(scope: ProfileScope) -> bool:
"""Whether ``scope`` has a store yet, answered by its readiness probe without opening, creating or migrating it."""
return storage_health(scope).probe().detail != "not initialized"
@contextmanager
def live_stores() -> Iterator[list[Persistence]]:
"""A handle on every store this process holds open, for in-process maintenance such as the deferred search-index
retry. Each store stays open for the block even when its last owner releases it meanwhile; a handle acts on the
store as it is held and never opens it again. The handles close when the block exits."""
from persistence.sqlite import SQLiteBackend, _registry
with _registry.borrow_live_shared_session_dbs() as holders:
handles = []
for holder in holders:
scope = profile_scope(home=Path(holder.db_path).parent)
backend = SQLiteBackend.lending(scope.hermes_home_key, holder)
handles.append(Persistence(scope, read_only=False, bind=lambda backend=backend, scope=scope: backend.open(scope),
release=backend.close))
try:
yield handles
finally:
for handle in handles:
handle.close()
def close_stores_under(directory: str | Path) -> int:
"""Force-close every store this process holds under ``directory`` (profile delete); returns how many closed. A
backend that held one acquires it again on its next touch."""
from persistence.sqlite import _registry
return _registry.close_all_under(directory)
def close_all_stores() -> int:
"""Close every store this process holds, regardless of references (gateway shutdown); returns how many closed."""
from persistence.sqlite import _registry
return _registry.close_all()