-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared_store.py
More file actions
115 lines (90 loc) · 3.26 KB
/
Copy pathshared_store.py
File metadata and controls
115 lines (90 loc) · 3.26 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
"""
Bullet-proof shared key-value store.
- Atomic writes (tmp -> fsync -> rename)
- File locking (fcntl), lock omvat de volledige read-modify-write cyclus
- Lock is per bestand (afgeleid van path), geen onnodige contentie tussen files
- No race conditions
- Never produces corrupt JSON
"""
import json
import os
import fcntl
from pathlib import Path
from typing import Any, Dict, Optional
import sys
DEFAULT_PATH = Path("/home/pi/share/shared_data.json")
class SharedStoreError(Exception):
pass
def _lock_path(path: Path) -> Path:
return path.with_suffix(".lock")
def _acquire_lock(lockfile):
"""Exclusive lock. Blocks until available."""
fcntl.flock(lockfile, fcntl.LOCK_EX)
def _release_lock(lockfile):
fcntl.flock(lockfile, fcntl.LOCK_UN)
def _read_unlocked(path: Path) -> Dict[str, Any]:
if not path.exists():
return {}
try:
with path.open("r", encoding="utf-8") as f:
return json.load(f)
except json.JSONDecodeError:
return {}
def _write_unlocked(data: Dict[str, Any], path: Path) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
tmp_path = path.with_suffix(".json.tmp")
with tmp_path.open("w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
f.flush()
os.fsync(f.fileno())
os.replace(tmp_path, path)
def _read_raw(path: Path = DEFAULT_PATH) -> Dict[str, Any]:
lock_path = _lock_path(path)
lock_path.parent.mkdir(parents=True, exist_ok=True)
with open(lock_path, "a") as lock:
_acquire_lock(lock)
try:
return _read_unlocked(path)
finally:
_release_lock(lock)
def _write_raw(data: Dict[str, Any], path: Path = DEFAULT_PATH) -> None:
lock_path = _lock_path(path)
lock_path.parent.mkdir(parents=True, exist_ok=True)
with open(lock_path, "a") as lock:
_acquire_lock(lock)
try:
_write_unlocked(data, path)
finally:
_release_lock(lock)
def set_key(key: str, value: Any, path: Path = DEFAULT_PATH) -> None:
lock_path = _lock_path(path)
lock_path.parent.mkdir(parents=True, exist_ok=True)
with open(lock_path, "a") as lock:
_acquire_lock(lock)
try:
data = _read_unlocked(path)
# Als zowel bestaande waarde als nieuwe waarde dicts zijn -> merge
if isinstance(value, dict) and isinstance(data.get(key), dict):
for k, v in value.items():
data[key][k] = v
else:
# Anders gewoon vervangen
data[key] = value
_write_unlocked(data, path)
finally:
_release_lock(lock)
def get_key(key: str, default: Optional[Any] = None, path: Path = DEFAULT_PATH) -> Any:
return _read_raw(path).get(key, default)
def update_many(values: Dict[str, Any], path: Path = DEFAULT_PATH) -> None:
lock_path = _lock_path(path)
lock_path.parent.mkdir(parents=True, exist_ok=True)
with open(lock_path, "a") as lock:
_acquire_lock(lock)
try:
data = _read_unlocked(path)
data.update(values)
_write_unlocked(data, path)
finally:
_release_lock(lock)
def read_all(path: Path = DEFAULT_PATH) -> Dict[str, Any]:
return _read_raw(path)