Coverage for python / lsst / daf / butler / registry / bridge / ephemeral.py: 29%
49 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-22 08:55 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-22 08:55 +0000
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
27from __future__ import annotations
29__all__ = ("EphemeralDatastoreRegistryBridge",)
31from collections.abc import Collection, Iterable, Iterator
32from contextlib import contextmanager
33from typing import TYPE_CHECKING
35from ..._dataset_ref import DatasetId
36from ..interfaces import DatasetIdRef, DatastoreRegistryBridge, FakeDatasetRef, OpaqueTableStorage
38if TYPE_CHECKING:
39 from ...datastore import DatastoreTransaction
40 from ...datastore.stored_file_info import StoredDatastoreItemInfo
43class EphemeralDatastoreRegistryBridge(DatastoreRegistryBridge):
44 """An implementation of `DatastoreRegistryBridge` for ephemeral datastores
45 - those whose artifacts never outlive the current process.
47 Parameters
48 ----------
49 datastoreName : `str`
50 Name of the `Datastore` as it should appear in `Registry` tables
51 referencing it.
53 Notes
54 -----
55 The current implementation just uses a Python set to remember the dataset
56 IDs associated with the datastore. This will probably need to be converted
57 to use in-database temporary tables instead in the future to support
58 "in-datastore" constraints in `Registry.queryDatasets`.
59 """
61 def __init__(self, datastoreName: str):
62 super().__init__(datastoreName)
63 self._datasetIds: set[DatasetId] = set()
64 self._trashedIds: set[DatasetId] = set()
66 def insert(self, refs: Iterable[DatasetIdRef]) -> None:
67 # Docstring inherited from DatastoreRegistryBridge
68 self._datasetIds.update(ref.id for ref in refs)
70 def ensure(self, refs: Iterable[DatasetIdRef]) -> None:
71 # Docstring inherited from DatastoreRegistryBridge
72 self._datasetIds.update(ref.id for ref in refs)
74 def forget(self, refs: Iterable[DatasetIdRef]) -> None:
75 self._datasetIds.difference_update(ref.id for ref in refs)
77 def _rollbackMoveToTrash(self, refs: Iterable[DatasetIdRef]) -> None:
78 """Rollback a moveToTrash call."""
79 for ref in refs:
80 self._trashedIds.remove(ref.id)
82 def moveToTrash(self, refs: Iterable[DatasetIdRef], transaction: DatastoreTransaction | None) -> None:
83 # Docstring inherited from DatastoreRegistryBridge
84 if transaction is None:
85 raise RuntimeError("Must be called with a defined transaction.")
86 ref_list = list(refs)
87 with transaction.undoWith(f"Trash {len(ref_list)} datasets", self._rollbackMoveToTrash, ref_list):
88 self._trashedIds.update(ref.id for ref in ref_list)
90 def check(self, refs: Iterable[DatasetIdRef]) -> Iterable[DatasetIdRef]:
91 # Docstring inherited from DatastoreRegistryBridge
92 yield from (ref for ref in refs if ref in self)
94 def __contains__(self, ref: DatasetIdRef) -> bool:
95 return ref.id in self._datasetIds and ref.id not in self._trashedIds
97 @contextmanager
98 def emptyTrash(
99 self,
100 records_table: OpaqueTableStorage | None = None,
101 record_class: type[StoredDatastoreItemInfo] | None = None,
102 record_column: str | None = None,
103 selected_ids: Collection[DatasetId] | None = None,
104 dry_run: bool = False,
105 ) -> Iterator[tuple[Iterable[tuple[DatasetIdRef, StoredDatastoreItemInfo | None]], set[str] | None]]:
106 # Docstring inherited from DatastoreRegistryBridge
107 matches: Iterable[tuple[FakeDatasetRef, StoredDatastoreItemInfo | None]] = ()
108 trashed_ids = self._trashedIds
110 if selected_ids is not None:
111 trashed_ids = {tid for tid in trashed_ids if tid in selected_ids}
113 if isinstance(records_table, OpaqueTableStorage):
114 if record_class is None:
115 raise ValueError("Record class must be provided if records table is given.")
116 matches = (
117 (FakeDatasetRef(id), record_class.from_record(record))
118 for id in trashed_ids
119 for record in records_table.fetch(dataset_id=id)
120 )
121 else:
122 matches = ((FakeDatasetRef(id), None) for id in trashed_ids)
124 # Indicate to caller that we do not know about artifacts that
125 # should be retained.
126 yield ((matches, None))
128 if dry_run:
129 return
131 if isinstance(records_table, OpaqueTableStorage):
132 # Remove the records entries
133 records_table.delete(["dataset_id"], *[{"dataset_id": id} for id in trashed_ids])
135 # Empty the trash table
136 self._datasetIds.difference_update(trashed_ids)
137 self._trashedIds = self._trashedIds - trashed_ids