Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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 

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

23opaque tables for `Registry`. 

24""" 

25 

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

27 

28from typing import ( 

29 Any, 

30 ClassVar, 

31 Dict, 

32 Iterable, 

33 Iterator, 

34 Optional, 

35) 

36 

37import sqlalchemy 

38 

39from ..core.ddl import TableSpec, FieldSpec 

40from .interfaces import ( 

41 Database, 

42 OpaqueTableStorageManager, 

43 OpaqueTableStorage, 

44 StaticTablesContext, 

45 VersionTuple 

46) 

47 

48 

49# This has to be updated on every schema change 

50_VERSION = VersionTuple(0, 2, 0) 

51 

52 

53class ByNameOpaqueTableStorage(OpaqueTableStorage): 

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

55 table for each different named opaque logical table. 

56 

57 A `ByNameOpaqueTableStorageManager` instance should always be used to 

58 construct and manage instances of this class. 

59 

60 Parameters 

61 ---------- 

62 db : `Database` 

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

64 name : `str` 

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

66 table : `sqlalchemy.schema.Table` 

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

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

69 of `ByNameOpaqueTableStorageManager`). 

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) -> None: 

77 # Docstring inherited from OpaqueTableStorage. 

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

79 

80 def fetch(self, **where: Any) -> Iterator[dict]: 

81 # Docstring inherited from OpaqueTableStorage. 

82 sql = self._table.select() 

83 if where: 

84 sql = sql.where( 

85 sqlalchemy.sql.and_(*[self._table.columns[k] == v for k, v in where.items()]) 

86 ) 

87 for row in self._db.query(sql): 

88 yield dict(row) 

89 

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

91 # Docstring inherited from OpaqueTableStorage. 

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

93 

94 

95class ByNameOpaqueTableStorageManager(OpaqueTableStorageManager): 

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

97 true table for each different named opaque logical table. 

98 

99 Instances of this class should generally be constructed via the 

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

101 

102 Parameters 

103 ---------- 

104 db : `Database` 

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

106 metaTable : `sqlalchemy.schema.Table` 

107 SQLAlchemy representation of the table that records which opaque 

108 logical tables exist. 

109 """ 

110 def __init__(self, db: Database, metaTable: sqlalchemy.schema.Table): 

111 self._db = db 

112 self._metaTable = metaTable 

113 self._storage: Dict[str, OpaqueTableStorage] = {} 

114 

115 _META_TABLE_NAME: ClassVar[str] = "opaque_meta" 

116 

117 _META_TABLE_SPEC: ClassVar[TableSpec] = TableSpec( 

118 fields=[ 

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

120 ], 

121 ) 

122 

123 @classmethod 

124 def initialize(cls, db: Database, context: StaticTablesContext) -> OpaqueTableStorageManager: 

125 # Docstring inherited from OpaqueTableStorageManager. 

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

127 return cls(db=db, metaTable=metaTable) 

128 

129 def get(self, name: str) -> Optional[OpaqueTableStorage]: 

130 # Docstring inherited from OpaqueTableStorageManager. 

131 return self._storage.get(name) 

132 

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

134 # Docstring inherited from OpaqueTableStorageManager. 

135 result = self._storage.get(name) 

136 if result is None: 136 ↛ 146line 136 didn't jump to line 146, because the condition on line 136 was never false

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

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

139 # was initialized, that's fine. 

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

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

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

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

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

145 self._storage[name] = result 

146 return result 

147 

148 @classmethod 

149 def currentVersion(cls) -> Optional[VersionTuple]: 

150 # Docstring inherited from VersionedExtension. 

151 return _VERSION 

152 

153 def schemaDigest(self) -> Optional[str]: 

154 # Docstring inherited from VersionedExtension. 

155 return self._defaultSchemaDigest([self._metaTable], self._db.dialect)