Coverage for python/lsst/daf/butler/registry/opaque.py: 32%

68 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-08-12 09:20 +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/>. 

21 

22"""The default concrete implementations of the classes that manage 

23opaque tables for `Registry`. 

24""" 

25 

26from __future__ import annotations 

27 

28__all__ = ["ByNameOpaqueTableStorage", "ByNameOpaqueTableStorageManager"] 

29 

30import itertools 

31from collections.abc import Iterable, Iterator 

32from typing import TYPE_CHECKING, Any, ClassVar 

33 

34import sqlalchemy 

35 

36from ..core.ddl import FieldSpec, TableSpec 

37from .interfaces import ( 

38 Database, 

39 OpaqueTableStorage, 

40 OpaqueTableStorageManager, 

41 StaticTablesContext, 

42 VersionTuple, 

43) 

44 

45if TYPE_CHECKING: 

46 from ..core.datastore import DatastoreTransaction 

47 

48# This has to be updated on every schema change 

49_VERSION = VersionTuple(0, 2, 0) 

50 

51 

52class ByNameOpaqueTableStorage(OpaqueTableStorage): 

53 """An implementation of `OpaqueTableStorage` that simply creates a true 

54 table for each different named opaque logical table. 

55 

56 A `ByNameOpaqueTableStorageManager` instance should always be used to 

57 construct and manage instances of this class. 

58 

59 Parameters 

60 ---------- 

61 db : `Database` 

62 Database engine interface for the namespace in which this table lives. 

63 name : `str` 

64 Name of the logical table (also used as the name of the actual table). 

65 table : `sqlalchemy.schema.Table` 

66 SQLAlchemy representation of the table, which must have already been 

67 created in the namespace managed by ``db`` (this is the responsibility 

68 of `ByNameOpaqueTableStorageManager`). 

69 """ 

70 

71 def __init__(self, *, db: Database, name: str, table: sqlalchemy.schema.Table): 

72 super().__init__(name=name) 

73 self._db = db 

74 self._table = table 

75 

76 def insert(self, *data: dict, transaction: DatastoreTransaction | None = None) -> None: 

77 # Docstring inherited from OpaqueTableStorage. 

78 # The provided transaction object can be ignored since we rely on 

79 # the database itself providing any rollback functionality. 

80 self._db.insert(self._table, *data) 

81 

82 def fetch(self, **where: Any) -> Iterator[sqlalchemy.RowMapping]: 

83 # Docstring inherited from OpaqueTableStorage. 

84 

85 def _batch_in_clause( 

86 column: sqlalchemy.schema.Column, values: Iterable[Any] 

87 ) -> Iterator[sqlalchemy.sql.expression.ClauseElement]: 

88 """Split one long IN clause into a series of shorter ones.""" 

89 in_limit = 1000 

90 # We have to remove possible duplicates from values; and in many 

91 # cases it should be helpful to order the items in the clause. 

92 values = sorted(set(values)) 

93 for iposn in range(0, len(values), in_limit): 

94 in_clause = column.in_(values[iposn : iposn + in_limit]) 

95 yield in_clause 

96 

97 def _batch_in_clauses(**where: Any) -> Iterator[sqlalchemy.sql.expression.ColumnElement]: 

98 """Generate a sequence of WHERE clauses with a limited number of 

99 items in IN clauses. 

100 """ 

101 batches: list[Iterable[Any]] = [] 

102 for k, v in where.items(): 

103 column = self._table.columns[k] 

104 if isinstance(v, list | tuple | set): 

105 batches.append(_batch_in_clause(column, v)) 

106 else: 

107 # single "batch" for a regular eq operator 

108 batches.append([column == v]) 

109 

110 for clauses in itertools.product(*batches): 

111 yield sqlalchemy.sql.and_(*clauses) 

112 

113 sql = self._table.select() 

114 if where: 

115 # Split long IN clauses into shorter batches 

116 batched_sql = [sql.where(clause) for clause in _batch_in_clauses(**where)] 

117 else: 

118 batched_sql = [sql] 

119 for sql_batch in batched_sql: 

120 with self._db.query(sql_batch) as sql_result: 

121 sql_mappings = sql_result.mappings().fetchall() 

122 yield from sql_mappings 

123 

124 def delete(self, columns: Iterable[str], *rows: dict) -> None: 

125 # Docstring inherited from OpaqueTableStorage. 

126 self._db.delete(self._table, columns, *rows) 

127 

128 

129class ByNameOpaqueTableStorageManager(OpaqueTableStorageManager): 

130 """An implementation of `OpaqueTableStorageManager` that simply creates a 

131 true table for each different named opaque logical table. 

132 

133 Instances of this class should generally be constructed via the 

134 `initialize` class method instead of invoking ``__init__`` directly. 

135 

136 Parameters 

137 ---------- 

138 db : `Database` 

139 Database engine interface for the namespace in which this table lives. 

140 metaTable : `sqlalchemy.schema.Table` 

141 SQLAlchemy representation of the table that records which opaque 

142 logical tables exist. 

143 """ 

144 

145 def __init__( 

146 self, 

147 db: Database, 

148 metaTable: sqlalchemy.schema.Table, 

149 registry_schema_version: VersionTuple | None = None, 

150 ): 

151 super().__init__(registry_schema_version=registry_schema_version) 

152 self._db = db 

153 self._metaTable = metaTable 

154 self._storage: dict[str, OpaqueTableStorage] = {} 

155 

156 _META_TABLE_NAME: ClassVar[str] = "opaque_meta" 

157 

158 _META_TABLE_SPEC: ClassVar[TableSpec] = TableSpec( 

159 fields=[ 

160 FieldSpec("table_name", dtype=sqlalchemy.String, length=128, primaryKey=True), 

161 ], 

162 ) 

163 

164 @classmethod 

165 def initialize( 

166 cls, db: Database, context: StaticTablesContext, registry_schema_version: VersionTuple | None = None 

167 ) -> OpaqueTableStorageManager: 

168 # Docstring inherited from OpaqueTableStorageManager. 

169 metaTable = context.addTable(cls._META_TABLE_NAME, cls._META_TABLE_SPEC) 

170 return cls(db=db, metaTable=metaTable, registry_schema_version=registry_schema_version) 

171 

172 def get(self, name: str) -> OpaqueTableStorage | None: 

173 # Docstring inherited from OpaqueTableStorageManager. 

174 return self._storage.get(name) 

175 

176 def register(self, name: str, spec: TableSpec) -> OpaqueTableStorage: 

177 # Docstring inherited from OpaqueTableStorageManager. 

178 result = self._storage.get(name) 

179 if result is None: 

180 # Create the table itself. If it already exists but wasn't in 

181 # the dict because it was added by another client since this one 

182 # was initialized, that's fine. 

183 table = self._db.ensureTableExists(name, spec) 

184 # Add a row to the meta table so we can find this table in the 

185 # future. Also okay if that already exists, so we use sync. 

186 self._db.sync(self._metaTable, keys={"table_name": name}) 

187 result = ByNameOpaqueTableStorage(name=name, table=table, db=self._db) 

188 self._storage[name] = result 

189 return result 

190 

191 @classmethod 

192 def currentVersions(cls) -> list[VersionTuple]: 

193 # Docstring inherited from VersionedExtension. 

194 return [_VERSION]