"""Durable filesystem JSON store. Each entity is stored as its own JSON file under a per-type directory. Writes are atomic (temp file + os.replace + fsync). Thread-safe via a per-path lock. """ from __future__ import annotations import json import os import threading import uuid from pathlib import Path from typing import Any, Callable from ..config import Config class StoreError(Exception): pass class _Locks: def __init__(self) -> None: self._locks: dict[str, threading.RLock] = {} self._guard = threading.Lock() def get(self, key: str) -> threading.RLock: with self._guard: if key not in self._locks: self._locks[key] = threading.RLock() return self._locks[key] _locks = _Locks() def new_id(prefix: str) -> str: return f"{prefix}-{uuid.uuid4().hex[:12]}" def _atomic_write(path: Path, value: Any) -> None: path.parent.mkdir(parents=True, exist_ok=True) tmp = path.with_name(f".{path.name}.{uuid.uuid4().hex}.tmp") try: with tmp.open("w", encoding="utf-8") as fh: json.dump(value, fh, ensure_ascii=False, indent=2) fh.flush() os.fsync(fh.fileno()) os.replace(tmp, path) finally: if tmp.exists(): try: tmp.unlink() except OSError: pass def _read_json(path: Path) -> Any: with path.open("r", encoding="utf-8") as fh: return json.load(fh) class JsonStore: """Simple JSON-file collection with CRUD + locking.""" def __init__(self, root: Path, *, key_attr: str = "id") -> None: self.root = root self.key_attr = key_attr self.root.mkdir(parents=True, exist_ok=True) def _path(self, key: str) -> Path: if not key or "/" in key or ".." in key: raise StoreError("invalid id") return self.root / f"{key}.json" def create(self, value: dict[str, Any], *, key: str | None = None) -> dict[str, Any]: key = key or value.get(self.key_attr) or new_id(self.key_attr) if self.key_attr not in value: value = dict(value) value[self.key_attr] = key path = self._path(key) lock = _locks.get(str(path)) with lock: if path.exists(): raise StoreError(f"already exists: {key}") _atomic_write(path, value) return value def get(self, key: str) -> dict[str, Any]: path = self._path(key) lock = _locks.get(str(path)) with lock: if not path.exists(): raise StoreError(f"not found: {key}") return _read_json(path) def get_or_none(self, key: str) -> dict[str, Any] | None: try: return self.get(key) except StoreError: return None def update(self, key: str, **fields: Any) -> dict[str, Any]: path = self._path(key) lock = _locks.get(str(path)) with lock: if not path.exists(): raise StoreError(f"not found: {key}") cur = _read_json(path) cur.update(fields) _atomic_write(path, cur) return cur def replace(self, key: str, value: dict[str, Any]) -> dict[str, Any]: path = self._path(key) lock = _locks.get(str(path)) with lock: _atomic_write(path, value) return value def delete(self, key: str) -> None: path = self._path(key) lock = _locks.get(str(path)) with lock: if path.exists(): path.unlink() def all(self) -> list[dict[str, Any]]: out: list[dict[str, Any]] = [] for path in sorted(self.root.glob("*.json")): try: out.append(_read_json(path)) except (OSError, ValueError): continue return out def where(self, pred: Callable[[dict[str, Any]], bool]) -> list[dict[str, Any]]: return [row for row in self.all() if pred(row)]