s365b.storage.backends ====================== .. py:module:: s365b.storage.backends .. autoapi-nested-parse:: Storage backends: where repository files physically live. A backend stores opaque, immutable objects under ``/``-separated keys such as ``packs/ab/ab12…``. It knows nothing about encryption or deduplication; that is the repository's job. New backends (S3, Azure Blob) implement :class:`StorageBackend`. Exceptions ---------- .. autoapisummary:: s365b.storage.backends.BackendError s365b.storage.backends.ObjectNotFoundError Classes ------- .. autoapisummary:: s365b.storage.backends.StorageBackend s365b.storage.backends.FilesystemBackend Functions --------- .. autoapisummary:: s365b.storage.backends.validate_key Module Contents --------------- .. py:exception:: BackendError Bases: :py:obj:`Exception` Raised for storage errors, such as a missing object. .. py:exception:: ObjectNotFoundError Bases: :py:obj:`BackendError` Raised when a key does not exist. .. py:function:: validate_key(key: str) -> str Check that a key is a safe relative path. :param key: Object key such as ``index/abc``. :returns: The key, unchanged. :raises ValueError: If the key is empty, absolute, or contains ``..`` or unexpected characters. .. py:class:: StorageBackend Bases: :py:obj:`abc.ABC` Interface every storage backend implements. Objects are write-once: the repository never modifies an object in place, which is what makes object-lock immutability possible on S3. .. py:method:: put(key: str, data: bytes, *, overwrite: bool = False) -> None :abstractmethod: :async: Store an object atomically. :param key: Object key. :param data: Object content. :param overwrite: Allow replacing an existing object. Only used for the repository config during key changes. :raises BackendError: If the object exists and ``overwrite`` is false. .. py:method:: get(key: str) -> bytes :abstractmethod: :async: Read a whole object. :param key: Object key. :returns: The object content. :raises ObjectNotFoundError: If the key does not exist. .. py:method:: get_range(key: str, offset: int, length: int) -> bytes :abstractmethod: :async: Read part of an object. :param key: Object key. :param offset: First byte to read. :param length: Number of bytes to read. :returns: Exactly ``length`` bytes. :raises ObjectNotFoundError: If the key does not exist. :raises BackendError: If the object is shorter than requested. .. py:method:: exists(key: str) -> bool :abstractmethod: :async: Return whether an object exists. :param key: Object key. :returns: True if the object exists. .. py:method:: list(prefix: str) -> collections.abc.AsyncIterator[str] :abstractmethod: Yield keys under a prefix (a directory such as ``index``). :param prefix: Key prefix without trailing slash. :returns: An async iterator of full keys, in no particular order. .. py:method:: delete(key: str) -> None :abstractmethod: :async: Delete an object. Deleting a missing object is not an error. :param key: Object key. .. py:class:: FilesystemBackend(root: pathlib.Path) Bases: :py:obj:`StorageBackend` Stores objects as files under a root directory. Works for local disks and for NFS/SMB shares mounted into the container. Writes go to a temporary file that is fsynced and then renamed into place, so a crash never leaves a half-written object under its final name. :param root: Directory holding the repository. Created if missing. .. py:attribute:: root .. py:method:: put(key: str, data: bytes, *, overwrite: bool = False) -> None :async: Store an object atomically (see :meth:`StorageBackend.put`). .. py:method:: get(key: str) -> bytes :async: Read a whole object (see :meth:`StorageBackend.get`). .. py:method:: get_range(key: str, offset: int, length: int) -> bytes :async: Read part of an object (see :meth:`StorageBackend.get_range`). .. py:method:: exists(key: str) -> bool :async: Return whether an object exists (see :meth:`StorageBackend.exists`). .. py:method:: list(prefix: str) -> collections.abc.AsyncIterator[str] :async: Yield keys under a prefix (see :meth:`StorageBackend.list`). .. py:method:: delete(key: str) -> None :async: Delete an object (see :meth:`StorageBackend.delete`).