s365b.core.generation ===================== .. py:module:: s365b.core.generation .. autoapi-nested-parse:: Catalog generation: never reclaim space the catalog does not know about. Retention deletes repository data that no catalog entry refers to. That is only safe if the catalog database knows everything in the repository. It does not after a database loss (a new server connected to an existing repository) or after restoring an older database dump: data written since looks unreferenced, and reclaiming it would delete those backups. So each repository has a generation, a counter kept twice: in the database (``repository.generation``) and in the repository (``generation/``). Every catalog commit that may point at newly written content raises the database's number in the same transaction, then writes it to the repository. A dump taken before that commit therefore holds a lower number than the repository: restored, it is recognised as behind, and retention keeps every pack (it still prunes catalog entries, which only touches the database). Content written but never committed (a crash between flushing and committing) is referenced by no database at all, so reclaiming it is safe. The repository keeps one file per number written, and reading takes the highest: two writers finishing out of order cannot lower it. One gap remains: if the process dies between a commit and writing the repository's number, and a dump from just before that commit is restored, the difference goes unnoticed. What it exposes is only the content that one lost commit stored (possibly several items): the restored database refers to none of it, so it can be reclaimed. The next backup reads those items again from the state saved before, but only as they are at the source then: a version since changed or deleted there is not recovered. Writing the number before the commit would close the gap but open a worse one: a commit that then fails would leave the repository ahead, and retention stopped until a newer dump is restored, which does not exist. Backups do not raise the number while the repository is ahead: that would hide the mismatch. Attributes ---------- .. autoapisummary:: s365b.core.generation.log Functions --------- .. autoapisummary:: s365b.core.generation.bump s365b.core.generation.catalog_behind s365b.core.generation.mark_connected Module Contents --------------- .. py:data:: log .. py:function:: bump(s: sqlalchemy.ext.asyncio.AsyncSession, repository_fk: int, on_disk: int) -> int | None :async: Raise the database's generation within a catalog commit. Call it last in the transaction (it holds the repository's row until the commit), then write the result with :meth:`Repository.write_generation` once the transaction has committed. :param s: The session of the catalog commit. :param repository_fk: Repository row ID. :param on_disk: The repository's generation, read before the transaction. :returns: The new generation, or ``None`` if the repository is ahead of this database and was left alone. .. py:function:: catalog_behind(db: s365b.db.session.Database, repository_fk: int, repo: s365b.storage.Repository) -> int | None :async: Whether the repository holds data newer than this database knows. :param db: Database. :param repository_fk: Repository row ID. :param repo: The open repository. :returns: The repository's generation if it is ahead of the database, else ``None``. .. py:function:: mark_connected(repo: s365b.storage.Repository) -> None :async: Mark a repository connected to a new database as holding unknown data. A repository from before generations has none; if it holds data, it is given generation 1, so that this database (generation 0) is behind. :param repo: The repository just connected.