Coverage for python/lsst/daf/butler/registry/opaque.py: 29%
68 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-10-07 09:47 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2022-10-07 09:47 +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 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"""The default concrete implementations of the classes that manage
24opaque tables for `Registry`.
25"""
27__all__ = ["ByNameOpaqueTableStorage", "ByNameOpaqueTableStorageManager"]
29import itertools
30from typing import Any, ClassVar, Dict, Iterable, Iterator, List, Optional
32import sqlalchemy
34from ..core.ddl import FieldSpec, TableSpec
35from .interfaces import (
36 Database,
37 OpaqueTableStorage,
38 OpaqueTableStorageManager,
39 StaticTablesContext,
40 VersionTuple,
41)
43# This has to be updated on every schema change
44_VERSION = VersionTuple(0, 2, 0)
47class ByNameOpaqueTableStorage(OpaqueTableStorage):
48 """An implementation of `OpaqueTableStorage` that simply creates a true
49 table for each different named opaque logical table.
51 A `ByNameOpaqueTableStorageManager` instance should always be used to
52 construct and manage instances of this class.
54 Parameters
55 ----------
56 db : `Database`
57 Database engine interface for the namespace in which this table lives.
58 name : `str`
59 Name of the logical table (also used as the name of the actual table).
60 table : `sqlalchemy.schema.Table`
61 SQLAlchemy representation of the table, which must have already been
62 created in the namespace managed by ``db`` (this is the responsibility
63 of `ByNameOpaqueTableStorageManager`).
64 """
66 def __init__(self, *, db: Database, name: str, table: sqlalchemy.schema.Table):
67 super().__init__(name=name)
68 self._db = db
69 self._table = table
71 def insert(self, *data: dict) -> None:
72 # Docstring inherited from OpaqueTableStorage.
73 self._db.insert(self._table, *data)
75 def fetch(self, **where: Any) -> Iterator[dict]:
76 # Docstring inherited from OpaqueTableStorage.
78 def _batch_in_clause(
79 column: sqlalchemy.schema.Column, values: Iterable[Any]
80 ) -> Iterator[sqlalchemy.sql.expression.ClauseElement]:
81 """Split one long IN clause into a series of shorter ones."""
82 in_limit = 1000
83 # We have to remove possible duplicates from values; and in many
84 # cases it should be helpful to order the items in the clause.
85 values = sorted(set(values))
86 for iposn in range(0, len(values), in_limit):
87 in_clause = column.in_(values[iposn : iposn + in_limit])
88 yield in_clause
90 def _batch_in_clauses(**where: Any) -> Iterator[sqlalchemy.sql.expression.ClauseElement]:
91 """Generate a sequence of WHERE clauses with a limited number of
92 items in IN clauses.
93 """
94 batches: List[Iterable[Any]] = []
95 for k, v in where.items():
96 column = self._table.columns[k]
97 if isinstance(v, (list, tuple, set)):
98 batches.append(_batch_in_clause(column, v))
99 else:
100 # single "batch" for a regular eq operator
101 batches.append([column == v])
103 for clauses in itertools.product(*batches):
104 yield sqlalchemy.sql.and_(*clauses)
106 sql = self._table.select()
107 if where:
108 # Split long IN clauses into shorter batches
109 for clause in _batch_in_clauses(**where):
110 sql_where = sql.where(clause)
111 for row in self._db.query(sql_where):
112 yield row._asdict()
113 else:
114 for row in self._db.query(sql):
115 yield row._asdict()
117 def delete(self, columns: Iterable[str], *rows: dict) -> None:
118 # Docstring inherited from OpaqueTableStorage.
119 self._db.delete(self._table, columns, *rows)
122class ByNameOpaqueTableStorageManager(OpaqueTableStorageManager):
123 """An implementation of `OpaqueTableStorageManager` that simply creates a
124 true table for each different named opaque logical table.
126 Instances of this class should generally be constructed via the
127 `initialize` class method instead of invoking ``__init__`` directly.
129 Parameters
130 ----------
131 db : `Database`
132 Database engine interface for the namespace in which this table lives.
133 metaTable : `sqlalchemy.schema.Table`
134 SQLAlchemy representation of the table that records which opaque
135 logical tables exist.
136 """
138 def __init__(self, db: Database, metaTable: sqlalchemy.schema.Table):
139 self._db = db
140 self._metaTable = metaTable
141 self._storage: Dict[str, OpaqueTableStorage] = {}
143 _META_TABLE_NAME: ClassVar[str] = "opaque_meta"
145 _META_TABLE_SPEC: ClassVar[TableSpec] = TableSpec(
146 fields=[
147 FieldSpec("table_name", dtype=sqlalchemy.String, length=128, primaryKey=True),
148 ],
149 )
151 @classmethod
152 def initialize(cls, db: Database, context: StaticTablesContext) -> OpaqueTableStorageManager:
153 # Docstring inherited from OpaqueTableStorageManager.
154 metaTable = context.addTable(cls._META_TABLE_NAME, cls._META_TABLE_SPEC)
155 return cls(db=db, metaTable=metaTable)
157 def get(self, name: str) -> Optional[OpaqueTableStorage]:
158 # Docstring inherited from OpaqueTableStorageManager.
159 return self._storage.get(name)
161 def register(self, name: str, spec: TableSpec) -> OpaqueTableStorage:
162 # Docstring inherited from OpaqueTableStorageManager.
163 result = self._storage.get(name)
164 if result is None:
165 # Create the table itself. If it already exists but wasn't in
166 # the dict because it was added by another client since this one
167 # was initialized, that's fine.
168 table = self._db.ensureTableExists(name, spec)
169 # Add a row to the meta table so we can find this table in the
170 # future. Also okay if that already exists, so we use sync.
171 self._db.sync(self._metaTable, keys={"table_name": name})
172 result = ByNameOpaqueTableStorage(name=name, table=table, db=self._db)
173 self._storage[name] = result
174 return result
176 @classmethod
177 def currentVersion(cls) -> Optional[VersionTuple]:
178 # Docstring inherited from VersionedExtension.
179 return _VERSION
181 def schemaDigest(self) -> Optional[str]:
182 # Docstring inherited from VersionedExtension.
183 return self._defaultSchemaDigest([self._metaTable], self._db.dialect)