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 Iterator, 

32 Optional, 

33) 

34 

35import sqlalchemy 

36 

37from ..core.ddl import TableSpec, FieldSpec 

38from .interfaces import Database, OpaqueTableStorageManager, OpaqueTableStorage, StaticTablesContext 

39 

40 

41class ByNameOpaqueTableStorage(OpaqueTableStorage): 

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

43 table for each different named opaque logical table. 

44 

45 A `ByNameOpaqueTableStorageManager` instance should always be used to 

46 construct and manage instances of this class. 

47 

48 Parameters 

49 ---------- 

50 db : `Database` 

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

52 name : `str` 

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

54 table : `sqlalchemy.schema.Table` 

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

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

57 of `ByNameOpaqueTableStorageManager`). 

58 """ 

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

60 super().__init__(name=name) 

61 self._db = db 

62 self._table = table 

63 

64 def insert(self, *data: dict): 

65 # Docstring inherited from OpaqueTableStorage. 

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

67 

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

69 # Docstring inherited from OpaqueTableStorage. 

70 sql = self._table.select().where( 

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

72 ) 

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

74 yield dict(row) 

75 

76 def delete(self, **where: Any): 

77 # Docstring inherited from OpaqueTableStorage. 

78 self._db.delete(self._table, where.keys(), where) 

79 

80 

81class ByNameOpaqueTableStorageManager(OpaqueTableStorageManager): 

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

83 true table for each different named opaque logical table. 

84 

85 Instances of this class should generally be constructed via the 

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

87 

88 Parameters 

89 ---------- 

90 db : `Database` 

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

92 metaTable : `sqlalchemy.schema.Table` 

93 SQLAlchemy representation of the table that records which opaque 

94 logical tables exist. 

95 """ 

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

97 self._db = db 

98 self._metaTable = metaTable 

99 self._storage = {} 

100 

101 _META_TABLE_NAME: ClassVar[str] = "opaque_meta" 

102 

103 _META_TABLE_SPEC: ClassVar[TableSpec] = TableSpec( 

104 fields=[ 

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

106 ], 

107 ) 

108 

109 @classmethod 

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

111 # Docstring inherited from OpaqueTableStorageManager. 

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

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

114 

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

116 # Docstring inherited from OpaqueTableStorageManager. 

117 return self._storage.get(name) 

118 

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

120 # Docstring inherited from OpaqueTableStorageManager. 

121 result = self._storage.get(name) 

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

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

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

125 # was initialized, that's fine. 

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

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

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

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

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

131 self._storage[name] = result 

132 return result