Coverage for python/lsst/daf/butler/registry/bridge/monolithic.py: 26%
98 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-17 02:01 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-17 02:01 -0800
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 program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
21from __future__ import annotations
23__all__ = ("MonolithicDatastoreRegistryBridgeManager", "MonolithicDatastoreRegistryBridge")
25import copy
26from collections import namedtuple
27from contextlib import contextmanager
28from typing import TYPE_CHECKING, Dict, Iterable, Iterator, List, Optional, Set, Tuple, Type, cast
30import sqlalchemy
32from ...core import NamedValueSet, StoredDatastoreItemInfo, ddl
33from ..interfaces import (
34 DatasetIdRef,
35 DatastoreRegistryBridge,
36 DatastoreRegistryBridgeManager,
37 FakeDatasetRef,
38 OpaqueTableStorage,
39 VersionTuple,
40)
41from ..opaque import ByNameOpaqueTableStorage
42from .ephemeral import EphemeralDatastoreRegistryBridge
44if TYPE_CHECKING: 44 ↛ 45line 44 didn't jump to line 45, because the condition on line 44 was never true
45 from ...core import DimensionUniverse
46 from ...core.datastore import DatastoreTransaction
47 from ..interfaces import (
48 Database,
49 DatasetRecordStorageManager,
50 OpaqueTableStorageManager,
51 StaticTablesContext,
52 )
54_TablesTuple = namedtuple(
55 "_TablesTuple",
56 [
57 "dataset_location",
58 "dataset_location_trash",
59 ],
60)
62# This has to be updated on every schema change
63_VERSION = VersionTuple(0, 2, 0)
66def _makeTableSpecs(datasets: Type[DatasetRecordStorageManager]) -> _TablesTuple:
67 """Construct specifications for tables used by the monolithic datastore
68 bridge classes.
70 Parameters
71 ----------
72 universe : `DimensionUniverse`
73 All dimensions known to the `Registry`.
74 datasets : subclass of `DatasetRecordStorageManager`
75 Manager class for datasets; used only to create foreign key fields.
77 Returns
78 -------
79 specs : `_TablesTuple`
80 A named tuple containing `ddl.TableSpec` instances.
81 """
82 # We want the dataset_location and dataset_location_trash tables
83 # to have the same definition, aside from the behavior of their link
84 # to the dataset table: the trash table has no foreign key constraint.
85 dataset_location_spec = ddl.TableSpec(
86 doc=(
87 "A table that provides information on whether a dataset is stored in "
88 "one or more Datastores. The presence or absence of a record in this "
89 "table itself indicates whether the dataset is present in that "
90 "Datastore. "
91 ),
92 fields=NamedValueSet(
93 [
94 ddl.FieldSpec(
95 name="datastore_name",
96 dtype=sqlalchemy.String,
97 length=256,
98 primaryKey=True,
99 nullable=False,
100 doc="Name of the Datastore this entry corresponds to.",
101 ),
102 ]
103 ),
104 )
105 dataset_location = copy.deepcopy(dataset_location_spec)
106 datasets.addDatasetForeignKey(dataset_location, primaryKey=True)
107 dataset_location_trash = copy.deepcopy(dataset_location_spec)
108 datasets.addDatasetForeignKey(dataset_location_trash, primaryKey=True, constraint=False)
109 return _TablesTuple(
110 dataset_location=dataset_location,
111 dataset_location_trash=dataset_location_trash,
112 )
115class MonolithicDatastoreRegistryBridge(DatastoreRegistryBridge):
116 """An implementation of `DatastoreRegistryBridge` that uses the same two
117 tables for all non-ephemeral datastores.
119 Parameters
120 ----------
121 datastoreName : `str`
122 Name of the `Datastore` as it should appear in `Registry` tables
123 referencing it.
124 db : `Database`
125 Object providing a database connection and generic distractions.
126 tables : `_TablesTuple`
127 Named tuple containing `sqlalchemy.schema.Table` instances.
128 """
130 def __init__(self, datastoreName: str, *, db: Database, tables: _TablesTuple):
131 super().__init__(datastoreName)
132 self._db = db
133 self._tables = tables
135 def _refsToRows(self, refs: Iterable[DatasetIdRef]) -> List[dict]:
136 """Transform an iterable of `DatasetRef` or `FakeDatasetRef` objects to
137 a list of dictionaries that match the schema of the tables used by this
138 class.
140 Parameters
141 ----------
142 refs : `Iterable` [ `DatasetRef` or `FakeDatasetRef` ]
143 Datasets to transform.
145 Returns
146 -------
147 rows : `list` [ `dict` ]
148 List of dictionaries, with "datastoreName" and "dataset_id" keys.
149 """
150 return [{"datastore_name": self.datastoreName, "dataset_id": ref.getCheckedId()} for ref in refs]
152 def insert(self, refs: Iterable[DatasetIdRef]) -> None:
153 # Docstring inherited from DatastoreRegistryBridge
154 self._db.insert(self._tables.dataset_location, *self._refsToRows(refs))
156 def forget(self, refs: Iterable[DatasetIdRef]) -> None:
157 # Docstring inherited from DatastoreRegistryBridge
158 rows = self._refsToRows(self.check(refs))
159 self._db.delete(self._tables.dataset_location, ["datastore_name", "dataset_id"], *rows)
161 def moveToTrash(self, refs: Iterable[DatasetIdRef], transaction: Optional[DatastoreTransaction]) -> None:
162 # Docstring inherited from DatastoreRegistryBridge
163 # TODO: avoid self.check() call via queries like
164 # INSERT INTO dataset_location_trash
165 # SELECT datastore_name, dataset_id FROM dataset_location
166 # WHERE datastore_name=? AND dataset_id IN (?);
167 # DELETE FROM dataset_location
168 # WHERE datastore_name=? AND dataset_id IN (?);
169 # ...but the Database interface doesn't support those kinds of queries
170 # right now.
171 rows = self._refsToRows(self.check(refs))
172 with self._db.transaction():
173 self._db.delete(self._tables.dataset_location, ["datastore_name", "dataset_id"], *rows)
174 self._db.insert(self._tables.dataset_location_trash, *rows)
176 def check(self, refs: Iterable[DatasetIdRef]) -> Iterable[DatasetIdRef]:
177 # Docstring inherited from DatastoreRegistryBridge
178 byId = {ref.getCheckedId(): ref for ref in refs}
179 sql = (
180 sqlalchemy.sql.select(self._tables.dataset_location.columns.dataset_id)
181 .select_from(self._tables.dataset_location)
182 .where(
183 sqlalchemy.sql.and_(
184 self._tables.dataset_location.columns.datastore_name == self.datastoreName,
185 self._tables.dataset_location.columns.dataset_id.in_(byId.keys()),
186 )
187 )
188 )
189 for row in self._db.query(sql).fetchall():
190 yield byId[row.dataset_id]
192 @contextmanager
193 def emptyTrash(
194 self,
195 records_table: Optional[OpaqueTableStorage] = None,
196 record_class: Optional[Type[StoredDatastoreItemInfo]] = None,
197 record_column: Optional[str] = None,
198 ) -> Iterator[
199 Tuple[Iterable[Tuple[DatasetIdRef, Optional[StoredDatastoreItemInfo]]], Optional[Set[str]]]
200 ]:
201 # Docstring inherited from DatastoreRegistryBridge
203 if records_table is None:
204 raise ValueError("This implementation requires a records table.")
206 assert isinstance(
207 records_table, ByNameOpaqueTableStorage
208 ), f"Records table must support hidden attributes. Got {type(records_table)}."
210 if record_class is None:
211 raise ValueError("Record class must be provided if records table is given.")
213 # Helper closure to generate the common join+where clause.
214 def join_records(
215 select: sqlalchemy.sql.Select, location_table: sqlalchemy.schema.Table
216 ) -> sqlalchemy.sql.FromClause:
217 # mypy needs to be sure
218 assert isinstance(records_table, ByNameOpaqueTableStorage)
219 return select.select_from(
220 records_table._table.join(
221 location_table,
222 onclause=records_table._table.columns.dataset_id == location_table.columns.dataset_id,
223 )
224 ).where(location_table.columns.datastore_name == self.datastoreName)
226 # SELECT records.dataset_id, records.path FROM records
227 # JOIN records on dataset_location.dataset_id == records.dataset_id
228 # WHERE dataset_location.datastore_name = datastoreName
230 # It's possible that we may end up with a ref listed in the trash
231 # table that is not listed in the records table. Such an
232 # inconsistency would be missed by this query.
233 info_in_trash = join_records(records_table._table.select(), self._tables.dataset_location_trash)
235 # Run query, transform results into a list of dicts that we can later
236 # use to delete.
237 rows = [
238 dict(**row, datastore_name=self.datastoreName) for row in self._db.query(info_in_trash).mappings()
239 ]
241 # It is possible for trashed refs to be linked to artifacts that
242 # are still associated with refs that are not to be trashed. We
243 # need to be careful to consider those and indicate to the caller
244 # that those artifacts should be retained. Can only do this check
245 # if the caller provides a column name that can map to multiple
246 # refs.
247 preserved: Optional[Set[str]] = None
248 if record_column is not None:
249 # Some helper subqueries
250 items_not_in_trash = join_records(
251 sqlalchemy.sql.select(records_table._table.columns[record_column]),
252 self._tables.dataset_location,
253 ).alias("items_not_in_trash")
254 items_in_trash = join_records(
255 sqlalchemy.sql.select(records_table._table.columns[record_column]),
256 self._tables.dataset_location_trash,
257 ).alias("items_in_trash")
259 # A query for paths that are referenced by datasets in the trash
260 # and datasets not in the trash.
261 items_to_preserve = sqlalchemy.sql.select(items_in_trash.columns[record_column]).select_from(
262 items_not_in_trash.join(
263 items_in_trash,
264 onclause=items_in_trash.columns[record_column]
265 == items_not_in_trash.columns[record_column],
266 )
267 )
268 preserved = {row[record_column] for row in self._db.query(items_to_preserve).mappings()}
270 # Convert results to a tuple of id+info and a record of the artifacts
271 # that should not be deleted from datastore. The id+info tuple is
272 # solely to allow logging to report the relevant ID.
273 id_info = ((FakeDatasetRef(row["dataset_id"]), record_class.from_record(row)) for row in rows)
275 # Start contextmanager, return results
276 yield ((id_info, preserved))
278 # No exception raised in context manager block.
279 if not rows:
280 return
282 # Delete the rows from the records table
283 records_table.delete(["dataset_id"], *[{"dataset_id": row["dataset_id"]} for row in rows])
285 # Delete those rows from the trash table.
286 self._db.delete(
287 self._tables.dataset_location_trash,
288 ["dataset_id", "datastore_name"],
289 *[{"dataset_id": row["dataset_id"], "datastore_name": row["datastore_name"]} for row in rows],
290 )
293class MonolithicDatastoreRegistryBridgeManager(DatastoreRegistryBridgeManager):
294 """An implementation of `DatastoreRegistryBridgeManager` that uses the same
295 two tables for all non-ephemeral datastores.
297 Parameters
298 ----------
299 db : `Database`
300 Object providing a database connection and generic distractions.
301 tables : `_TablesTuple`
302 Named tuple containing `sqlalchemy.schema.Table` instances.
303 opaque : `OpaqueTableStorageManager`
304 Manager object for opaque table storage in the `Registry`.
305 universe : `DimensionUniverse`
306 All dimensions know to the `Registry`.
307 datasetIdColumnType : `type`
308 Type for dataset ID column.
309 """
311 def __init__(
312 self,
313 *,
314 db: Database,
315 tables: _TablesTuple,
316 opaque: OpaqueTableStorageManager,
317 universe: DimensionUniverse,
318 datasetIdColumnType: type,
319 ):
320 super().__init__(opaque=opaque, universe=universe, datasetIdColumnType=datasetIdColumnType)
321 self._db = db
322 self._tables = tables
323 self._ephemeral: Dict[str, EphemeralDatastoreRegistryBridge] = {}
325 @classmethod
326 def initialize(
327 cls,
328 db: Database,
329 context: StaticTablesContext,
330 *,
331 opaque: OpaqueTableStorageManager,
332 datasets: Type[DatasetRecordStorageManager],
333 universe: DimensionUniverse,
334 ) -> DatastoreRegistryBridgeManager:
335 # Docstring inherited from DatastoreRegistryBridge
336 tables = context.addTableTuple(_makeTableSpecs(datasets))
337 return cls(
338 db=db,
339 tables=cast(_TablesTuple, tables),
340 opaque=opaque,
341 universe=universe,
342 datasetIdColumnType=datasets.getIdColumnType(),
343 )
345 def refresh(self) -> None:
346 # Docstring inherited from DatastoreRegistryBridge
347 # This implementation has no in-Python state that depends on which
348 # datastores exist, so there's nothing to do.
349 pass
351 def register(self, name: str, *, ephemeral: bool = False) -> DatastoreRegistryBridge:
352 # Docstring inherited from DatastoreRegistryBridge
353 if ephemeral:
354 return self._ephemeral.setdefault(name, EphemeralDatastoreRegistryBridge(name))
355 return MonolithicDatastoreRegistryBridge(name, db=self._db, tables=self._tables)
357 def findDatastores(self, ref: DatasetIdRef) -> Iterable[str]:
358 # Docstring inherited from DatastoreRegistryBridge
359 sql = (
360 sqlalchemy.sql.select(self._tables.dataset_location.columns.datastore_name)
361 .select_from(self._tables.dataset_location)
362 .where(self._tables.dataset_location.columns.dataset_id == ref.getCheckedId())
363 )
364 for row in self._db.query(sql).mappings():
365 yield row[self._tables.dataset_location.columns.datastore_name]
366 for name, bridge in self._ephemeral.items():
367 if ref in bridge:
368 yield name
370 @classmethod
371 def currentVersion(cls) -> Optional[VersionTuple]:
372 # Docstring inherited from VersionedExtension.
373 return _VERSION
375 def schemaDigest(self) -> Optional[str]:
376 # Docstring inherited from VersionedExtension.
377 return self._defaultSchemaDigest(self._tables, self._db.dialect)