Coverage for python / lsst / daf / butler / registry / bridge / monolithic.py: 25%
120 statements
« prev ^ index » next coverage.py v7.12.0, created at 2025-12-12 01:12 -0800
« prev ^ index » next coverage.py v7.12.0, created at 2025-12-12 01:12 -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 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
29from ... import ddl
31__all__ = ("MonolithicDatastoreRegistryBridge", "MonolithicDatastoreRegistryBridgeManager")
33from collections import namedtuple
34from collections.abc import Collection, Iterable, Iterator
35from contextlib import contextmanager
36from typing import TYPE_CHECKING, cast
38import sqlalchemy
40from lsst.utils.iteration import chunk_iterable
42from ..._dataset_ref import DatasetId
43from ...datastore.stored_file_info import StoredDatastoreItemInfo
44from ..interfaces import (
45 DatasetIdRef,
46 DatastoreRegistryBridge,
47 DatastoreRegistryBridgeManager,
48 FakeDatasetRef,
49 OpaqueTableStorage,
50 VersionTuple,
51)
52from ..opaque import ByNameOpaqueTableStorage
53from .ephemeral import EphemeralDatastoreRegistryBridge
55if TYPE_CHECKING:
56 from ...datastore import DatastoreTransaction
57 from ...dimensions import DimensionUniverse
58 from ..interfaces import (
59 Database,
60 DatasetRecordStorageManager,
61 OpaqueTableStorageManager,
62 StaticTablesContext,
63 )
65_TablesTuple = namedtuple(
66 "_TablesTuple",
67 [
68 "dataset_location",
69 "dataset_location_trash",
70 ],
71)
73# This has to be updated on every schema change
74_VERSION = VersionTuple(0, 2, 1)
77def _makeTableSpecs(datasets: type[DatasetRecordStorageManager]) -> _TablesTuple:
78 """Construct specifications for tables used by the monolithic datastore
79 bridge classes.
81 Parameters
82 ----------
83 datasets : subclass of `DatasetRecordStorageManager`
84 Manager class for datasets; used only to create foreign key fields.
86 Returns
87 -------
88 specs : `_TablesTuple`
89 A named tuple containing `ddl.TableSpec` instances.
90 """
91 # We want the dataset_location and dataset_location_trash tables
92 # to have the same definition, aside from the behavior of their link
93 # to the dataset table: the trash table has no foreign key constraint.
94 # The order of columns in dataset_location_trash is reversed, it is more
95 # optimal for query planner.
97 datastore_field = ddl.FieldSpec(
98 name="datastore_name",
99 dtype=sqlalchemy.String,
100 length=256,
101 primaryKey=True,
102 nullable=False,
103 doc="Name of the Datastore this entry corresponds to.",
104 )
106 dataset_location = ddl.TableSpec(
107 doc=(
108 "A table that provides information on whether a dataset is stored in "
109 "one or more Datastores. The presence or absence of a record in this "
110 "table itself indicates whether the dataset is present in that "
111 "Datastore. "
112 ),
113 fields=[datastore_field],
114 )
115 datasets.addDatasetForeignKey(dataset_location, primaryKey=True)
117 dataset_location_trash = ddl.TableSpec(
118 doc="A table that keeps iinformation about datasets that are removed from Datastores.",
119 fields=[],
120 )
121 datasets.addDatasetForeignKey(dataset_location_trash, primaryKey=True, constraint=False)
122 dataset_location_trash.fields.add(datastore_field)
124 return _TablesTuple(
125 dataset_location=dataset_location,
126 dataset_location_trash=dataset_location_trash,
127 )
130class MonolithicDatastoreRegistryBridge(DatastoreRegistryBridge):
131 """An implementation of `DatastoreRegistryBridge` that uses the same two
132 tables for all non-ephemeral datastores.
134 Parameters
135 ----------
136 datastoreName : `str`
137 Name of the `Datastore` as it should appear in `Registry` tables
138 referencing it.
139 db : `Database`
140 Object providing a database connection and generic distractions.
141 tables : `_TablesTuple`
142 Named tuple containing `sqlalchemy.schema.Table` instances.
143 """
145 def __init__(self, datastoreName: str, *, db: Database, tables: _TablesTuple):
146 super().__init__(datastoreName)
147 self._db = db
148 self._tables = tables
150 def _refsToRows(self, refs: Iterable[DatasetIdRef]) -> list[dict]:
151 """Transform an iterable of `DatasetRef` or `FakeDatasetRef` objects to
152 a list of dictionaries that match the schema of the tables used by this
153 class.
155 Parameters
156 ----------
157 refs : `~collections.abc.Iterable` [ `DatasetRef` or `FakeDatasetRef` ]
158 Datasets to transform.
160 Returns
161 -------
162 rows : `list` [ `dict` ]
163 List of dictionaries, with "datastoreName" and "dataset_id" keys.
164 """
165 return [{"datastore_name": self.datastoreName, "dataset_id": ref.id} for ref in refs]
167 def ensure(self, refs: Iterable[DatasetIdRef]) -> None:
168 # Docstring inherited from DatastoreRegistryBridge
169 self._db.ensure(self._tables.dataset_location, *self._refsToRows(refs))
171 def insert(self, refs: Iterable[DatasetIdRef]) -> None:
172 # Docstring inherited from DatastoreRegistryBridge
173 self._db.insert(self._tables.dataset_location, *self._refsToRows(refs))
175 def forget(self, refs: Iterable[DatasetIdRef]) -> None:
176 # Docstring inherited from DatastoreRegistryBridge
177 with self._db.transaction():
178 # The list of IDs can be very large, split it into reasonable size
179 # chunks to avoid hitting limits.
180 for refs_chunk in chunk_iterable(refs, 50_000):
181 dataset_ids = [ref.id for ref in refs_chunk]
182 where = sqlalchemy.sql.and_(
183 self._tables.dataset_location.columns.datastore_name == self.datastoreName,
184 self._tables.dataset_location.columns.dataset_id.in_(dataset_ids),
185 )
186 self._db.deleteWhere(self._tables.dataset_location, where)
188 def moveToTrash(self, refs: Iterable[DatasetIdRef], transaction: DatastoreTransaction | None) -> None:
189 # Docstring inherited from DatastoreRegistryBridge
190 location = self._tables.dataset_location
191 location_trash = self._tables.dataset_location_trash
192 with self._db.transaction():
193 for refs_chunk in chunk_iterable(refs, 50_000):
194 # We only want to move IDs that actually exist in the
195 # dataset_location table. Instead of querying for existing IDs,
196 # which would need an extra query, we use INSERT ... SELECT
197 # and DELETE using WHERE clause that limits operations to
198 # existing IDs.
199 dataset_ids = [ref.id for ref in refs_chunk]
201 where = sqlalchemy.sql.and_(
202 location.columns.datastore_name == self.datastoreName,
203 location.columns.dataset_id.in_(dataset_ids),
204 )
206 select = (
207 sqlalchemy.sql.select(location.columns.datastore_name, location.columns.dataset_id)
208 .where(where)
209 .with_for_update()
210 )
211 self._db.insert(location_trash, select=select)
213 self._db.deleteWhere(location, where)
215 def check(self, refs: Iterable[DatasetIdRef]) -> Iterable[DatasetIdRef]:
216 # Docstring inherited from DatastoreRegistryBridge
217 byId = {ref.id: ref for ref in refs}
218 sql = (
219 sqlalchemy.sql.select(self._tables.dataset_location.columns.dataset_id)
220 .select_from(self._tables.dataset_location)
221 .where(
222 sqlalchemy.sql.and_(
223 self._tables.dataset_location.columns.datastore_name == self.datastoreName,
224 self._tables.dataset_location.columns.dataset_id.in_(byId.keys()),
225 )
226 )
227 )
228 with self._db.query(sql) as sql_result:
229 sql_rows = sql_result.fetchall()
230 for row in sql_rows:
231 yield byId[row.dataset_id]
233 @contextmanager
234 def emptyTrash(
235 self,
236 records_table: OpaqueTableStorage | None = None,
237 record_class: type[StoredDatastoreItemInfo] | None = None,
238 record_column: str | None = None,
239 selected_ids: Collection[DatasetId] | None = None,
240 dry_run: bool = False,
241 ) -> Iterator[tuple[Iterable[tuple[DatasetIdRef, StoredDatastoreItemInfo | None]], set[str] | None]]:
242 # Docstring inherited from DatastoreRegistryBridge
244 if records_table is None:
245 raise ValueError("This implementation requires a records table.")
247 assert isinstance(records_table, ByNameOpaqueTableStorage), (
248 f"Records table must support hidden attributes. Got {type(records_table)}."
249 )
251 if record_class is None:
252 raise ValueError("Record class must be provided if records table is given.")
254 # Helper closure to generate the common join+where clause.
255 def join_records(
256 select: sqlalchemy.sql.Select, location_table: sqlalchemy.schema.Table
257 ) -> sqlalchemy.sql.Select:
258 # mypy needs to be sure
259 assert isinstance(records_table, ByNameOpaqueTableStorage)
260 return select.select_from(
261 records_table._table.join(
262 location_table,
263 onclause=records_table._table.columns.dataset_id == location_table.columns.dataset_id,
264 )
265 ).where(location_table.columns.datastore_name == self.datastoreName)
267 # SELECT records.dataset_id, records.path FROM records
268 # JOIN records on dataset_location.dataset_id == records.dataset_id
269 # WHERE dataset_location.datastore_name = datastoreName
271 # It's possible that we may end up with a ref listed in the trash
272 # table that is not listed in the records table. Such an
273 # inconsistency would be missed by this query.
274 info_in_trash = join_records(records_table._table.select(), self._tables.dataset_location_trash)
275 if selected_ids:
276 info_in_trash = info_in_trash.where(
277 self._tables.dataset_location_trash.columns["dataset_id"].in_(selected_ids)
278 )
279 info_in_trash = info_in_trash.with_for_update(skip_locked=True)
281 # Run query, transform results into a list of dicts that we can later
282 # use to delete.
283 with self._db.query(info_in_trash) as sql_result:
284 rows = [dict(row, datastore_name=self.datastoreName) for row in sql_result.mappings()]
286 # It is possible for trashed refs to be linked to artifacts that
287 # are still associated with refs that are not to be trashed. We
288 # need to be careful to consider those and indicate to the caller
289 # that those artifacts should be retained. Can only do this check
290 # if the caller provides a column name that can map to multiple
291 # refs.
292 preserved: set[str] | None = None
293 if record_column is not None:
294 # Some helper subqueries
295 items_not_in_trash = join_records(
296 sqlalchemy.sql.select(records_table._table.columns[record_column]),
297 self._tables.dataset_location,
298 ).alias("items_not_in_trash")
299 items_in_trash = join_records(
300 sqlalchemy.sql.select(records_table._table.columns[record_column]),
301 self._tables.dataset_location_trash,
302 )
303 if selected_ids:
304 items_in_trash = items_in_trash.where(
305 self._tables.dataset_location_trash.columns["dataset_id"].in_(selected_ids)
306 )
307 items_in_trash_alias = items_in_trash.alias("items_in_trash")
309 # A query for paths that are referenced by datasets in the trash
310 # and datasets not in the trash.
311 items_to_preserve = sqlalchemy.sql.select(
312 items_in_trash_alias.columns[record_column]
313 ).select_from(
314 items_not_in_trash.join(
315 items_in_trash_alias,
316 onclause=items_in_trash_alias.columns[record_column]
317 == items_not_in_trash.columns[record_column],
318 )
319 )
320 with self._db.query(items_to_preserve) as sql_result:
321 preserved = {row[record_column] for row in sql_result.mappings()}
323 # Convert results to a tuple of id+info and a record of the artifacts
324 # that should not be deleted from datastore. The id+info tuple is
325 # solely to allow logging to report the relevant ID.
326 id_info = ((FakeDatasetRef(row["dataset_id"]), record_class.from_record(row)) for row in rows)
328 # Start contextmanager, return results
329 yield ((id_info, preserved))
331 # No exception raised in context manager block.
332 if not rows or dry_run:
333 return
335 # Delete the rows from the records table
336 records_table.delete(["dataset_id"], *[{"dataset_id": row["dataset_id"]} for row in rows])
338 # Delete those rows from the trash table.
339 self._db.delete(
340 self._tables.dataset_location_trash,
341 ["dataset_id", "datastore_name"],
342 *[{"dataset_id": row["dataset_id"], "datastore_name": row["datastore_name"]} for row in rows],
343 )
346class MonolithicDatastoreRegistryBridgeManager(DatastoreRegistryBridgeManager):
347 """An implementation of `DatastoreRegistryBridgeManager` that uses the same
348 two tables for all non-ephemeral datastores.
350 Parameters
351 ----------
352 db : `Database`
353 Object providing a database connection and generic distractions.
354 tables : `_TablesTuple`
355 Named tuple containing `sqlalchemy.schema.Table` instances.
356 opaque : `OpaqueTableStorageManager`
357 Manager object for opaque table storage in the `Registry`.
358 universe : `DimensionUniverse`
359 All dimensions know to the `Registry`.
360 registry_schema_version : `VersionTuple` or `None`, optional
361 The version of the registry schema.
362 """
364 def __init__(
365 self,
366 *,
367 db: Database,
368 tables: _TablesTuple,
369 opaque: OpaqueTableStorageManager,
370 universe: DimensionUniverse,
371 registry_schema_version: VersionTuple | None = None,
372 ):
373 super().__init__(
374 opaque=opaque,
375 universe=universe,
376 registry_schema_version=registry_schema_version,
377 )
378 self._db = db
379 self._tables = tables
380 self._ephemeral: dict[str, EphemeralDatastoreRegistryBridge] = {}
382 def clone(self, *, db: Database, opaque: OpaqueTableStorageManager) -> DatastoreRegistryBridgeManager:
383 return MonolithicDatastoreRegistryBridgeManager(
384 db=db,
385 tables=self._tables,
386 opaque=opaque,
387 universe=self.universe,
388 registry_schema_version=self._registry_schema_version,
389 )
391 @classmethod
392 def initialize(
393 cls,
394 db: Database,
395 context: StaticTablesContext,
396 *,
397 opaque: OpaqueTableStorageManager,
398 datasets: type[DatasetRecordStorageManager],
399 universe: DimensionUniverse,
400 registry_schema_version: VersionTuple | None = None,
401 ) -> DatastoreRegistryBridgeManager:
402 # Docstring inherited from DatastoreRegistryBridge
403 tables = context.addTableTuple(_makeTableSpecs(datasets))
404 return cls(
405 db=db,
406 tables=cast(_TablesTuple, tables),
407 opaque=opaque,
408 universe=universe,
409 registry_schema_version=registry_schema_version,
410 )
412 def refresh(self) -> None:
413 # Docstring inherited from DatastoreRegistryBridge
414 # This implementation has no in-Python state that depends on which
415 # datastores exist, so there's nothing to do.
416 pass
418 def register(self, name: str, *, ephemeral: bool = False) -> DatastoreRegistryBridge:
419 # Docstring inherited from DatastoreRegistryBridge
420 if ephemeral:
421 return self._ephemeral.setdefault(name, EphemeralDatastoreRegistryBridge(name))
422 return MonolithicDatastoreRegistryBridge(name, db=self._db, tables=self._tables)
424 def findDatastores(self, ref: DatasetIdRef) -> Iterable[str]:
425 # Docstring inherited from DatastoreRegistryBridge
426 sql = (
427 sqlalchemy.sql.select(self._tables.dataset_location.columns.datastore_name)
428 .select_from(self._tables.dataset_location)
429 .where(self._tables.dataset_location.columns.dataset_id == ref.id)
430 )
431 with self._db.query(sql) as sql_result:
432 sql_rows = sql_result.mappings().fetchall()
433 for row in sql_rows:
434 yield row[self._tables.dataset_location.columns.datastore_name]
435 for name, bridge in self._ephemeral.items():
436 if ref in bridge:
437 yield name
439 @classmethod
440 def currentVersions(cls) -> list[VersionTuple]:
441 # Docstring inherited from VersionedExtension.
442 return [_VERSION]