-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathystore.py
446 lines (379 loc) · 15.2 KB
/
ystore.py
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
from __future__ import annotations
import struct
import tempfile
import time
from abc import ABC, abstractmethod
from contextlib import AsyncExitStack
from inspect import isawaitable
from logging import Logger, getLogger
from pathlib import Path
from typing import AsyncIterator, Awaitable, Callable, cast
import aiosqlite
import anyio
import y_py as Y
from anyio import TASK_STATUS_IGNORED, Event, Lock, create_task_group
from anyio.abc import TaskGroup, TaskStatus
from .yutils import Decoder, get_new_path, write_var_uint
class YDocNotFound(Exception):
pass
class BaseYStore(ABC):
metadata_callback: Callable[[], Awaitable[bytes] | bytes] | None = None
version = 2
_started: Event | None = None
_starting: bool = False
_task_group: TaskGroup | None = None
@abstractmethod
def __init__(
self, path: str, metadata_callback: Callable[[], Awaitable[bytes] | bytes] | None = None
):
...
@abstractmethod
async def write(self, data: bytes) -> None:
...
@abstractmethod
async def read(self) -> AsyncIterator[tuple[bytes, bytes]]:
...
@property
def started(self) -> Event:
if self._started is None:
self._started = Event()
return self._started
async def __aenter__(self) -> BaseYStore:
if self._task_group is not None:
raise RuntimeError("YStore already running")
async with AsyncExitStack() as exit_stack:
tg = create_task_group()
self._task_group = await exit_stack.enter_async_context(tg)
self._exit_stack = exit_stack.pop_all()
tg.start_soon(self.start)
return self
async def __aexit__(self, exc_type, exc_value, exc_tb):
if self._task_group is None:
raise RuntimeError("YStore not running")
self._task_group.cancel_scope.cancel()
self._task_group = None
return await self._exit_stack.__aexit__(exc_type, exc_value, exc_tb)
async def start(self, *, task_status: TaskStatus[None] = TASK_STATUS_IGNORED):
"""Start the store.
Arguments:
task_status: The status to set when the task has started.
"""
if self._starting:
return
else:
self._staring = True
if self._task_group is not None:
raise RuntimeError("YStore already running")
self.started.set()
self._starting = False
task_status.started()
def stop(self) -> None:
"""Stop the store."""
if self._task_group is None:
raise RuntimeError("YStore not running")
self._task_group.cancel_scope.cancel()
self._task_group = None
async def get_metadata(self) -> bytes:
"""
Returns:
The metadata.
"""
if self.metadata_callback is None:
return b""
metadata = self.metadata_callback()
if isawaitable(metadata):
metadata = await metadata
metadata = cast(bytes, metadata)
return metadata
async def encode_state_as_update(self, ydoc: Y.YDoc) -> None:
"""Store a YDoc state.
Arguments:
ydoc: The YDoc from which to store the state.
"""
update = Y.encode_state_as_update(ydoc) # type: ignore
await self.write(update)
async def apply_updates(self, ydoc: Y.YDoc) -> None:
"""Apply all stored updates to the YDoc.
Arguments:
ydoc: The YDoc on which to apply the updates.
"""
async for update, *rest in self.read(): # type: ignore
Y.apply_update(ydoc, update) # type: ignore
class FileYStore(BaseYStore):
"""A YStore which uses one file per document."""
path: str
metadata_callback: Callable[[], Awaitable[bytes] | bytes] | None
lock: Lock
def __init__(
self,
path: str,
metadata_callback: Callable[[], Awaitable[bytes] | bytes] | None = None,
log: Logger | None = None,
) -> None:
"""Initialize the object.
Arguments:
path: The file path used to store the updates.
metadata_callback: An optional callback to call to get the metadata.
log: An optional logger.
"""
self.path = path
self.metadata_callback = metadata_callback
self.log = log or getLogger(__name__)
self.lock = Lock()
async def check_version(self) -> int:
"""Check the version of the store format.
Returns:
The offset where the data is located in the file.
"""
if not await anyio.Path(self.path).exists():
version_mismatch = True
else:
version_mismatch = False
move_file = False
async with await anyio.open_file(self.path, "rb") as f:
header = await f.read(8)
if header == b"VERSION:":
version = int(await f.readline())
if version == self.version:
offset = await f.tell()
else:
version_mismatch = True
else:
version_mismatch = True
if version_mismatch:
move_file = True
if move_file:
new_path = await get_new_path(self.path)
self.log.warning(f"YStore version mismatch, moving {self.path} to {new_path}")
await anyio.Path(self.path).rename(new_path)
if version_mismatch:
async with await anyio.open_file(self.path, "wb") as f:
version_bytes = f"VERSION:{self.version}\n".encode()
await f.write(version_bytes)
offset = len(version_bytes)
return offset
async def read(self) -> AsyncIterator[tuple[bytes, bytes, float]]: # type: ignore
"""Async iterator for reading the store content.
Returns:
A tuple of (update, metadata, timestamp) for each update.
"""
async with self.lock:
if not await anyio.Path(self.path).exists():
raise YDocNotFound
offset = await self.check_version()
async with await anyio.open_file(self.path, "rb") as f:
await f.seek(offset)
data = await f.read()
if not data:
raise YDocNotFound
i = 0
for d in Decoder(data).read_messages():
if i == 0:
update = d
elif i == 1:
metadata = d
else:
timestamp = struct.unpack("<d", d)[0]
yield update, metadata, timestamp
i = (i + 1) % 3
async def write(self, data: bytes) -> None:
"""Store an update.
Arguments:
data: The update to store.
"""
parent = Path(self.path).parent
async with self.lock:
await anyio.Path(parent).mkdir(parents=True, exist_ok=True)
await self.check_version()
async with await anyio.open_file(self.path, "ab") as f:
data_len = write_var_uint(len(data))
await f.write(data_len + data)
metadata = await self.get_metadata()
metadata_len = write_var_uint(len(metadata))
await f.write(metadata_len + metadata)
timestamp = struct.pack("<d", time.time())
timestamp_len = write_var_uint(len(timestamp))
await f.write(timestamp_len + timestamp)
class TempFileYStore(FileYStore):
"""A YStore which uses the system's temporary directory.
Files are writen under a common directory.
To prefix the directory name (e.g. /tmp/my_prefix_b4whmm7y/):
```py
class PrefixTempFileYStore(TempFileYStore):
prefix_dir = "my_prefix_"
```
"""
prefix_dir: str | None = None
base_dir: str | None = None
def __init__(
self,
path: str,
metadata_callback: Callable[[], Awaitable[bytes] | bytes] | None = None,
log: Logger | None = None,
):
"""Initialize the object.
Arguments:
path: The file path used to store the updates.
metadata_callback: An optional callback to call to get the metadata.
log: An optional logger.
"""
full_path = str(Path(self.get_base_dir()) / path)
super().__init__(full_path, metadata_callback=metadata_callback, log=log)
def get_base_dir(self) -> str:
"""Get the base directory where the update file is written.
Returns:
The base directory path.
"""
if self.base_dir is None:
self.make_directory()
assert self.base_dir is not None
return self.base_dir
def make_directory(self):
"""Create the base directory where the update file is written."""
type(self).base_dir = tempfile.mkdtemp(prefix=self.prefix_dir)
class SQLiteYStore(BaseYStore):
"""A YStore which uses an SQLite database.
Unlike file-based YStores, the Y updates of all documents are stored in the same database.
Subclass to point to your database file:
```py
class MySQLiteYStore(SQLiteYStore):
db_path = "path/to/my_ystore.db"
```
"""
db_path: str = "ystore.db"
# Determines the "time to live" for all documents, i.e. how recent the
# latest update of a document must be before purging document history.
# Defaults to never purging document history (None).
document_ttl: int | None = None
path: str
lock: Lock
db_initialized: Event
def __init__(
self,
path: str,
metadata_callback: Callable[[], Awaitable[bytes] | bytes] | None = None,
log: Logger | None = None,
) -> None:
"""Initialize the object.
Arguments:
path: The file path used to store the updates.
metadata_callback: An optional callback to call to get the metadata.
log: An optional logger.
"""
self.path = path
self.metadata_callback = metadata_callback
self.log = log or getLogger(__name__)
self.lock = Lock()
self.db_initialized = Event()
async def start(self, *, task_status: TaskStatus[None] = TASK_STATUS_IGNORED):
"""Start the SQLiteYStore.
Arguments:
task_status: The status to set when the task has started.
"""
if self._starting:
return
else:
self._starting = True
if self._task_group is not None:
raise RuntimeError("YStore already running")
async with create_task_group() as self._task_group:
self._task_group.start_soon(self._init_db)
self.started.set()
self._starting = False
task_status.started()
async def _init_db(self):
create_db = False
move_db = False
if not await anyio.Path(self.db_path).exists():
create_db = True
else:
async with self.lock:
async with aiosqlite.connect(self.db_path) as db:
cursor = await db.execute(
"SELECT count(name) FROM sqlite_master WHERE type='table' and name='yupdates'"
)
table_exists = (await cursor.fetchone())[0]
if table_exists:
cursor = await db.execute("pragma user_version")
version = (await cursor.fetchone())[0]
if version != self.version:
move_db = True
create_db = True
else:
create_db = True
if move_db:
new_path = await get_new_path(self.db_path)
self.log.warning(f"YStore version mismatch, moving {self.db_path} to {new_path}")
await anyio.Path(self.db_path).rename(new_path)
if create_db:
async with self.lock:
async with aiosqlite.connect(self.db_path) as db:
await db.execute(
"CREATE TABLE yupdates (path TEXT NOT NULL, yupdate BLOB, metadata BLOB, timestamp REAL NOT NULL)"
)
await db.execute(
"CREATE INDEX idx_yupdates_path_timestamp ON yupdates (path, timestamp)"
)
await db.execute(f"PRAGMA user_version = {self.version}")
await db.commit()
self.db_initialized.set()
async def read(self) -> AsyncIterator[tuple[bytes, bytes, float]]: # type: ignore
"""Async iterator for reading the store content.
Returns:
A tuple of (update, metadata, timestamp) for each update.
"""
await self.db_initialized.wait()
try:
async with self.lock:
async with aiosqlite.connect(self.db_path) as db:
async with db.execute(
"SELECT yupdate, metadata, timestamp FROM yupdates WHERE path = ?",
(self.path,),
) as cursor:
found = False
async for update, metadata, timestamp in cursor:
found = True
yield update, metadata, timestamp
if not found:
raise YDocNotFound
except Exception:
raise YDocNotFound
async def write(self, data: bytes) -> None:
"""Store an update.
Arguments:
data: The update to store.
"""
await self.db_initialized.wait()
async with self.lock:
async with aiosqlite.connect(self.db_path) as db:
# first, determine time elapsed since last update
cursor = await db.execute(
"SELECT timestamp FROM yupdates WHERE path = ? ORDER BY timestamp DESC LIMIT 1",
(self.path,),
)
row = await cursor.fetchone()
diff = (time.time() - row[0]) if row else 0
if self.document_ttl is not None and diff > self.document_ttl:
# squash updates
ydoc = Y.YDoc()
async with db.execute(
"SELECT yupdate FROM yupdates WHERE path = ?", (self.path,)
) as cursor:
async for update, in cursor:
Y.apply_update(ydoc, update)
# delete history
await db.execute("DELETE FROM yupdates WHERE path = ?", (self.path,))
# insert squashed updates
squashed_update = Y.encode_state_as_update(ydoc)
metadata = await self.get_metadata()
await db.execute(
"INSERT INTO yupdates VALUES (?, ?, ?, ?)",
(self.path, squashed_update, metadata, time.time()),
)
# finally, write this update to the DB
metadata = await self.get_metadata()
await db.execute(
"INSERT INTO yupdates VALUES (?, ?, ?, ?)",
(self.path, data, metadata, time.time()),
)
await db.commit()