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 

23from typing import List, Optional 

24 

25import sqlalchemy 

26 

27from ...core import NamedKeyDict 

28from ...core.dimensions import DimensionElement, DimensionUniverse 

29from ..interfaces import ( 

30 Database, 

31 StaticTablesContext, 

32 DimensionRecordStorageManager, 

33 DimensionRecordStorage, 

34 VersionTuple 

35) 

36 

37 

38# This has to be updated on every schema change 

39_VERSION = VersionTuple(0, 1, 0) 

40 

41 

42class StaticDimensionRecordStorageManager(DimensionRecordStorageManager): 

43 """An implementation of `DimensionRecordStorageManager` for single-layer 

44 `Registry` and the base layers of multi-layer `Registry`. 

45 

46 This manager creates `DimensionRecordStorage` instances for all elements 

47 in the `DimensionUniverse` in its own `initialize` method, as part of 

48 static table creation, so it never needs to manage any dynamic registry 

49 tables. 

50 

51 Parameters 

52 ---------- 

53 db : `Database` 

54 Interface to the underlying database engine and namespace. 

55 records : `NamedKeyDict` 

56 Mapping from `DimensionElement` to `DimensionRecordStorage` for that 

57 element. 

58 universe : `DimensionUniverse` 

59 All known dimensions. 

60 """ 

61 def __init__(self, db: Database, records: NamedKeyDict[DimensionElement, DimensionRecordStorage], *, 

62 universe: DimensionUniverse): 

63 super().__init__(universe=universe) 

64 self._db = db 

65 self._records = records 

66 

67 @classmethod 

68 def initialize(cls, db: Database, context: StaticTablesContext, *, 

69 universe: DimensionUniverse) -> DimensionRecordStorageManager: 

70 # Docstring inherited from DimensionRecordStorageManager. 

71 records: NamedKeyDict[DimensionElement, DimensionRecordStorage] = NamedKeyDict() 

72 for element in universe.elements: 

73 ImplementationClass = DimensionRecordStorage.getDefaultImplementation(element) 

74 records[element] = ImplementationClass.initialize(db, element, context=context) 

75 return cls(db=db, records=records, universe=universe) 

76 

77 def refresh(self) -> None: 

78 # Docstring inherited from DimensionRecordStorageManager. 

79 pass 

80 

81 def get(self, element: DimensionElement) -> Optional[DimensionRecordStorage]: 

82 # Docstring inherited from DimensionRecordStorageManager. 

83 return self._records.get(element) 

84 

85 def register(self, element: DimensionElement) -> DimensionRecordStorage: 

86 # Docstring inherited from DimensionRecordStorageManager. 

87 result = self._records.get(element) 

88 assert result, "All records instances should be created in initialize()." 

89 return result 

90 

91 def clearCaches(self) -> None: 

92 # Docstring inherited from DimensionRecordStorageManager. 

93 for storage in self._records.values(): 

94 storage.clearCaches() 

95 

96 @classmethod 

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

98 # Docstring inherited from VersionedExtension. 

99 return _VERSION 

100 

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

102 # Docstring inherited from VersionedExtension. 

103 tables: List[sqlalchemy.schema.Table] = [] 

104 for recStorage in self._records.values(): 

105 tables += recStorage.digestTables() 

106 return self._defaultSchemaDigest(tables, self._db.dialect)