DatabaseDimensionRecordStorage

class lsst.daf.butler.registry.interfaces.DatabaseDimensionRecordStorage

Bases: lsst.daf.butler.registry.interfaces.DimensionRecordStorage

Intermediate interface for DimensionRecordStorage objects that provide storage for DatabaseDimensionElement instances.

Attributes Summary

element The element whose records this instance holds (DimensionElement).

Methods Summary

clearCaches() Clear any in-memory caches held by the storage instance.
connect(overlaps) Inform this record storage object of the object that will manage the overlaps between this element and another element.
digestTables() Return tables used for schema digest.
fetch(dataIds) Retrieve records from storage.
initialize(db, element, *, context, config, …) Construct an instance of this class using a standardized interface.
insert(*records, replace) Insert one or more records into storage.
join(builder, *, regions, …) Add the dimension element’s logical table to a query under construction.
sync(record, update) Synchronize a record with the database, inserting it only if it does not exist and comparing values if it does.

Attributes Documentation

element

The element whose records this instance holds (DimensionElement).

Methods Documentation

clearCaches() → None

Clear any in-memory caches held by the storage instance.

This is called by Registry when transactions are rolled back, to avoid in-memory caches from ever containing records that are not present in persistent storage.

connect(overlaps: lsst.daf.butler.registry.interfaces._dimensions.DatabaseDimensionOverlapStorage) → None

Inform this record storage object of the object that will manage the overlaps between this element and another element.

This will only be called if self.element.spatial is not None, and will be called immediately after construction (before any other methods). In the future, implementations will be required to call a method on any connected overlap storage objects any time new records for the element are inserted.

Parameters:
overlaps : DatabaseDimensionRecordStorage

Object managing overlaps between this element and another database-backed element.

digestTables() → Iterable[sqlalchemy.sql.schema.Table]

Return tables used for schema digest.

Returns:
tables : Iterable [ sqlalchemy.schema.Table ]

Possibly empty set of tables for schema digest calculations.

fetch(dataIds: DataCoordinateIterable) → Iterable[DimensionRecord]

Retrieve records from storage.

Parameters:
dataIds : DataCoordinateIterable

Data IDs that identify the records to be retrieved.

Returns:
records : Iterable [ DimensionRecord ]

Record retrieved from storage. Not all data IDs may have corresponding records (if there are no records that match a data ID), and even if they are, the order of inputs is not preserved.

classmethod initialize(db: Database, element: DatabaseDimensionElement, *, context: Optional[StaticTablesContext] = None, config: Mapping[str, Any], governors: NamedKeyMapping[GovernorDimension, GovernorDimensionRecordStorage]) → DatabaseDimensionRecordStorage

Construct an instance of this class using a standardized interface.

Parameters:
db : Database

Interface to the underlying database engine and namespace.

element : DatabaseDimensionElement

Dimension element the new instance will manage records for.

context : StaticTablesContext, optional

If provided, an object to use to create any new tables. If not provided, db.ensureTableExists should be used instead.

config : Mapping

Extra configuration options specific to the implementation.

governors : NamedKeyMapping

Mapping containing all governor dimension storage implementations.

Returns:
storage : DatabaseDimensionRecordStorage

A new DatabaseDimensionRecordStorage subclass instance.

insert(*records, replace: bool = False) → None

Insert one or more records into storage.

Parameters:
records

One or more instances of the DimensionRecord subclass for the element this storage is associated with.

replace: `bool`, optional

If True (False is default), replace existing records in the database if there is a conflict.

Raises:
TypeError

Raised if the element does not support record insertion.

sqlalchemy.exc.IntegrityError

Raised if one or more records violate database integrity constraints.

Notes

As insert is expected to be called only by a Registry, we rely on Registry to provide transactionality, both by using a SQLALchemy connection shared with the Registry and by relying on it to call clearCaches when rolling back transactions.

join(builder: QueryBuilder, *, regions: Optional[NamedKeyDict[DimensionElement, sqlalchemy.sql.ColumnElement]] = None, timespans: Optional[NamedKeyDict[DimensionElement, TimespanDatabaseRepresentation]] = None) → sqlalchemy.sql.FromClause

Add the dimension element’s logical table to a query under construction.

This is a visitor pattern interface that is expected to be called only by QueryBuilder.joinDimensionElement.

Parameters:
builder : QueryBuilder

Builder for the query that should contain this element.

regions : NamedKeyDict, optional

A mapping from DimensionElement to a SQLAlchemy column containing the region for that element, which should be updated to include a region column for this element if one exists. If None, self.element is not being included in the query via a spatial join.

timespan : NamedKeyDict, optional

A mapping from DimensionElement to a Timespan of SQLALchemy columns containing the timespan for that element, which should be updated to include timespan columns for this element if they exist. If None, self.element is not being included in the query via a temporal join.

Returns:
fromClause : sqlalchemy.sql.FromClause

Table or clause for the element which is joined.

Notes

Elements are only included in queries via spatial and/or temporal joins when necessary to connect them to other elements in the query, so regions and timespans cannot be assumed to be not None just because an element has a region or timespan.

sync(record: DimensionRecord, update: bool = False) → Union[bool, Dict[str, Any]]

Synchronize a record with the database, inserting it only if it does not exist and comparing values if it does.

Parameters:
record : DimensionRecord.

An instance of the DimensionRecord subclass for the element this storage is associated with.

update: `bool`, optional

If True (False is default), update the existing record in the database if there is a conflict.

Returns:
inserted_or_updated : bool or dict

True if a new row was inserted, False if no changes were needed, or a dict mapping updated column names to their old values if an update was performed (only possible if update=True).

Raises:
DatabaseConflictError

Raised if the record exists in the database (according to primary key lookup) but is inconsistent with the given one.

TypeError

Raised if the element does not support record synchronization.

sqlalchemy.exc.IntegrityError

Raised if one or more records violate database integrity constraints.