s365b.core.catalog ================== .. py:module:: s365b.core.catalog .. autoapi-nested-parse:: Catalog writes: protected objects, item versions and sync state. The catalog is the database record of what was backed up and when. Content itself lives in the repository; versions store a :class:`~s365b.storage.repository.ContentRef` pointing to it. Versioning rules (see :meth:`Catalog.record`): * A new item gets its first version. * A changed item closes its current version (``valid_to = now``) and gets a new one. * An unchanged item (same content, name, path and metadata) gets nothing, so re-reading the same data after a crash or a full resync costs no space. * A deleted item closes its current version and gets a deletion marker. * An item whose current version is in another repository gets a new version in this one, even if unchanged: after a job moves to a new repository, that repository alone holds every current item. Sync state is saved with the repository it was saved for. A backup into another repository starts from what :meth:`~s365b.modules.sdk.BackupModule.state_for_new_repository` keeps. Classes ------- .. autoapisummary:: s365b.core.catalog.VersionStats s365b.core.catalog.Catalog Functions --------- .. autoapisummary:: s365b.core.catalog.refresh_summary Module Contents --------------- .. py:class:: VersionStats Counts of what :meth:`Catalog.record` did. :ivar new: Items seen for the first time. :ivar changed: Items with a new version. :ivar unchanged: Items that matched their current version. :ivar deleted: Items marked deleted. :ivar bytes: Plaintext bytes of content in new versions. .. py:attribute:: new :type: int :value: 0 .. py:attribute:: changed :type: int :value: 0 .. py:attribute:: unchanged :type: int :value: 0 .. py:attribute:: deleted :type: int :value: 0 .. py:attribute:: bytes :type: int :value: 0 .. py:method:: add(other: VersionStats) -> None Add another set of counts to this one. :param other: Counts to add. .. py:method:: as_dict() -> dict[str, int] Return the counts as a dict (for ``job_run.stats``). :returns: The counts. .. py:class:: Catalog(session: sqlalchemy.ext.asyncio.AsyncSession) Catalog operations within one database session. :param session: Open session; the caller commits. .. py:attribute:: s .. py:method:: sync_objects(tenant_fk: int, module_id: str, objects: collections.abc.Iterable[s365b.modules.sdk.ProtectedObject], now: datetime.datetime) -> list[s365b.db.models.ProtectedObjectRow] :async: Upsert discovered objects and mark undiscovered ones as gone. :param tenant_fk: Tenant row ID. :param module_id: Module that discovered the objects. :param objects: Everything the module discovered this time. :param now: Discovery time. :returns: Rows for the discovered objects. .. py:method:: load_state(object_fk: int) -> s365b.modules.sdk.SyncState :async: Return the last saved sync state of an object. :param object_fk: Protected object row ID. :returns: The state; empty if never saved. .. py:method:: state_for(object_fk: int, repository_fk: int, module: s365b.modules.sdk.BackupModule) -> s365b.modules.sdk.SyncState :async: Return the state a backup of an object into a repository starts from. :param object_fk: Protected object row ID. :param repository_fk: Repository the backup writes to. :param module: The object's module. :returns: The saved state; if it was saved for another repository, what the module keeps of it for a backup into a new one. .. py:method:: current_items(object_fk: int, field: str, value: str, repository_fk: int | None = None) -> dict[str, dict[str, Any]] :async: An object's current, not deleted items with ``meta[field] == value``. :param object_fk: Protected object row ID. :param field: Metadata field, e.g. ``"folder_id"``. :param value: Its value. :param repository_fk: If given, items whose current version is in another repository come with empty metadata, so a module comparing change keys reads them again. :returns: Their metadata, by item key. .. py:method:: state_version(object_fk: int) -> str | None :async: Return the version of the module that last saved an object's state. :param object_fk: Protected object row ID. :returns: The module version; ``None`` if never saved. .. py:method:: save_state(object_fk: int, state: s365b.modules.sdk.SyncState, module_version: str, repository_fk: int) -> None :async: Save an object's sync state. :param object_fk: Protected object row ID. :param state: State to save. :param module_version: Version of the module that produced it. :param repository_fk: Repository the backup that produced it writes to. .. py:method:: apply_group(object_fk: int, change: s365b.modules.sdk.GroupChange, *, repository_fk: int, run_fk: int | None, now: datetime.datetime) -> tuple[VersionStats, list[s365b.modules.sdk.ItemVersion]] :async: Re-path or delete every current item of a group. :param object_fk: Protected object row ID. :param change: The group change yielded by the module. :param repository_fk: Repository of the new versions (content is reused, so this is the current version's repository). :param run_fk: Job run that produced it. :param now: Commit time. :returns: What changed, and the new versions (without content) for change tracking. .. py:method:: record(object_fk: int, version: s365b.modules.sdk.ItemVersion, content: s365b.storage.repository.ContentRef | None, *, repository_fk: int, run_fk: int | None, now: datetime.datetime) -> VersionStats :async: Record one item version yielded by a module. :param object_fk: Protected object row ID. :param version: The module's item version. :param content: Where its content was stored, if it has content. :param repository_fk: Repository holding the content. :param run_fk: Job run that produced it. :param now: Commit time; becomes ``valid_from`` / ``valid_to``. :returns: What changed (one counter is incremented). .. py:function:: refresh_summary(s: sqlalchemy.ext.asyncio.AsyncSession, object_fk: int) -> None :async: Store an object's current item count and newest item time on its row. Browse lists thousands of mailboxes; counting their items on every page load would read the whole catalog, so the count is kept after each backup.