s365b.storage.backends

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 StorageBackend.

Exceptions

BackendError

Raised for storage errors, such as a missing object.

ObjectNotFoundError

Raised when a key does not exist.

Classes

StorageBackend

Interface every storage backend implements.

FilesystemBackend

Stores objects as files under a root directory.

Functions

validate_key(key) → str

Check that a key is a safe relative path.

Module Contents

exception s365b.storage.backends.BackendError[source]

Bases: Exception

Raised for storage errors, such as a missing object.

exception s365b.storage.backends.ObjectNotFoundError[source]

Bases: BackendError

Raised when a key does not exist.

s365b.storage.backends.validate_key(key: str) → str[source]

Check that a key is a safe relative path.

Parameters:

key – Object key such as index/abc.

Returns:

The key, unchanged.

Raises:

ValueError – If the key is empty, absolute, or contains .. or unexpected characters.

class s365b.storage.backends.StorageBackend[source]

Bases: 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.

abstractmethod put(key: str, data: bytes, *, overwrite: bool = False) → None[source]
Async:

Store an object atomically.

Parameters:
  • key – Object key.

  • data – Object content.

  • 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.

abstractmethod get(key: str) → bytes[source]
Async:

Read a whole object.

Parameters:

key – Object key.

Returns:

The object content.

Raises:

ObjectNotFoundError – If the key does not exist.

abstractmethod get_range(key: str, offset: int, length: int) → bytes[source]
Async:

Read part of an object.

Parameters:
  • key – Object key.

  • offset – First byte to read.

  • length – Number of bytes to read.

Returns:

Exactly length bytes.

Raises:
abstractmethod exists(key: str) → bool[source]
Async:

Return whether an object exists.

Parameters:

key – Object key.

Returns:

True if the object exists.

abstractmethod list(prefix: str) → collections.abc.AsyncIterator[str][source]

Yield keys under a prefix (a directory such as index).

Parameters:

prefix – Key prefix without trailing slash.

Returns:

An async iterator of full keys, in no particular order.

abstractmethod delete(key: str) → None[source]
Async:

Delete an object. Deleting a missing object is not an error.

Parameters:

key – Object key.

class s365b.storage.backends.FilesystemBackend(root: pathlib.Path)[source]

Bases: 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.

Parameters:

root – Directory holding the repository. Created if missing.

root[source]
async put(key: str, data: bytes, *, overwrite: bool = False) → None[source]

Store an object atomically (see StorageBackend.put()).

async get(key: str) → bytes[source]

Read a whole object (see StorageBackend.get()).

async get_range(key: str, offset: int, length: int) → bytes[source]

Read part of an object (see StorageBackend.get_range()).

async exists(key: str) → bool[source]

Return whether an object exists (see StorageBackend.exists()).

async list(prefix: str) → collections.abc.AsyncIterator[str][source]

Yield keys under a prefix (see StorageBackend.list()).

async delete(key: str) → None[source]

Delete an object (see StorageBackend.delete()).