|
| 1 | +# Copyright (c) Jupyter Development Team. |
| 2 | +# Distributed under the terms of the Modified BSD License. |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +from abc import ABC, abstractmethod |
| 7 | +from inspect import isawaitable |
| 8 | +from typing import AsyncIterator, Awaitable, Callable, cast |
| 9 | + |
| 10 | +import y_py as Y |
| 11 | +from anyio import Event |
| 12 | + |
| 13 | + |
| 14 | +class BaseYStore(ABC): |
| 15 | + """ |
| 16 | + Base class for the stores. |
| 17 | + """ |
| 18 | + |
| 19 | + version = 3 |
| 20 | + metadata_callback: Callable[[], Awaitable[bytes] | bytes] | None = None |
| 21 | + |
| 22 | + _store_path: str |
| 23 | + _initialized: Event | None = None |
| 24 | + |
| 25 | + @abstractmethod |
| 26 | + def __init__( |
| 27 | + self, path: str, metadata_callback: Callable[[], Awaitable[bytes] | bytes] | None = None |
| 28 | + ): |
| 29 | + """ |
| 30 | + Initialize the object. |
| 31 | +
|
| 32 | + Arguments: |
| 33 | + path: The path where the store will be located. |
| 34 | + metadata_callback: An optional callback to call to get the metadata. |
| 35 | + log: An optional logger. |
| 36 | + """ |
| 37 | + ... |
| 38 | + |
| 39 | + @abstractmethod |
| 40 | + async def initialize(self) -> None: |
| 41 | + """ |
| 42 | + Initializes the store. |
| 43 | + """ |
| 44 | + ... |
| 45 | + |
| 46 | + @abstractmethod |
| 47 | + async def exists(self, path: str) -> bool: |
| 48 | + """ |
| 49 | + Returns True if the document exists, else returns False. |
| 50 | +
|
| 51 | + Arguments: |
| 52 | + path: The document name/path. |
| 53 | + """ |
| 54 | + ... |
| 55 | + |
| 56 | + @abstractmethod |
| 57 | + async def list(self) -> AsyncIterator[str]: |
| 58 | + """ |
| 59 | + Returns a list with the name/path of the documents stored. |
| 60 | + """ |
| 61 | + ... |
| 62 | + |
| 63 | + @abstractmethod |
| 64 | + async def get(self, path: str, updates: bool = False) -> dict | None: |
| 65 | + """ |
| 66 | + Returns the document's metadata or None if the document does't exist. |
| 67 | +
|
| 68 | + Arguments: |
| 69 | + path: The document name/path. |
| 70 | + updates: Whether to return document's content or only the metadata. |
| 71 | + """ |
| 72 | + ... |
| 73 | + |
| 74 | + @abstractmethod |
| 75 | + async def create(self, path: str, version: int) -> None: |
| 76 | + """ |
| 77 | + Creates a new document. |
| 78 | +
|
| 79 | + Arguments: |
| 80 | + path: The document name/path. |
| 81 | + version: Document version. |
| 82 | + """ |
| 83 | + ... |
| 84 | + |
| 85 | + @abstractmethod |
| 86 | + async def remove(self, path: str) -> dict | None: |
| 87 | + """ |
| 88 | + Removes a document. |
| 89 | +
|
| 90 | + Arguments: |
| 91 | + path: The document name/path. |
| 92 | + """ |
| 93 | + ... |
| 94 | + |
| 95 | + @abstractmethod |
| 96 | + async def write(self, path: str, data: bytes) -> None: |
| 97 | + """ |
| 98 | + Store a document update. |
| 99 | +
|
| 100 | + Arguments: |
| 101 | + path: The document name/path. |
| 102 | + data: The update to store. |
| 103 | + """ |
| 104 | + ... |
| 105 | + |
| 106 | + @abstractmethod |
| 107 | + async def read(self, path: str) -> AsyncIterator[tuple[bytes, bytes]]: |
| 108 | + """ |
| 109 | + Async iterator for reading document's updates. |
| 110 | +
|
| 111 | + Arguments: |
| 112 | + path: The document name/path. |
| 113 | +
|
| 114 | + Returns: |
| 115 | + A tuple of (update, metadata, timestamp) for each update. |
| 116 | + """ |
| 117 | + ... |
| 118 | + |
| 119 | + @property |
| 120 | + def initialized(self) -> bool: |
| 121 | + if self._initialized is not None: |
| 122 | + return self._initialized.is_set() |
| 123 | + return False |
| 124 | + |
| 125 | + async def get_metadata(self) -> bytes: |
| 126 | + """ |
| 127 | + Returns: |
| 128 | + The metadata. |
| 129 | + """ |
| 130 | + if self.metadata_callback is None: |
| 131 | + return b"" |
| 132 | + |
| 133 | + metadata = self.metadata_callback() |
| 134 | + if isawaitable(metadata): |
| 135 | + metadata = await metadata |
| 136 | + metadata = cast(bytes, metadata) |
| 137 | + return metadata |
| 138 | + |
| 139 | + async def encode_state_as_update(self, path: str, ydoc: Y.YDoc) -> None: |
| 140 | + """Store a YDoc state. |
| 141 | +
|
| 142 | + Arguments: |
| 143 | + path: The document name/path. |
| 144 | + ydoc: The YDoc from which to store the state. |
| 145 | + """ |
| 146 | + update = Y.encode_state_as_update(ydoc) # type: ignore |
| 147 | + await self.write(path, update) |
| 148 | + |
| 149 | + async def apply_updates(self, path: str, ydoc: Y.YDoc) -> None: |
| 150 | + """Apply all stored updates to the YDoc. |
| 151 | +
|
| 152 | + Arguments: |
| 153 | + path: The document name/path. |
| 154 | + ydoc: The YDoc on which to apply the updates. |
| 155 | + """ |
| 156 | + async for update, *rest in self.read(path): # type: ignore |
| 157 | + Y.apply_update(ydoc, update) # type: ignore |
0 commit comments