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 

23__all__ = ["CachingDimensionRecordStorage"] 

24 

25from typing import Optional 

26 

27import sqlalchemy 

28 

29from ...core import DataCoordinate, DimensionElement, DimensionRecord, Timespan 

30from ...core.utils import NamedKeyDict 

31from ..interfaces import Database, DimensionRecordStorage, StaticTablesContext 

32from ..queries import QueryBuilder 

33 

34 

35class CachingDimensionRecordStorage(DimensionRecordStorage): 

36 """A record storage implementation that adds caching to some other nested 

37 storage implementation. 

38 

39 Parameters 

40 ---------- 

41 nested : `DimensionRecordStorage` 

42 The other storage to cache fetches from and to delegate all other 

43 operations to. 

44 """ 

45 def __init__(self, nested: DimensionRecordStorage): 

46 self._nested = nested 

47 self._cache = {} 

48 

49 @classmethod 

50 def initialize(cls, db: Database, element: DimensionElement, *, 

51 context: Optional[StaticTablesContext] = None) -> DimensionRecordStorage: 

52 # Docstring inherited from DimensionRecordStorage. 

53 NestedClass = DimensionRecordStorage.getDefaultImplementation(element, ignoreCached=True) 

54 nested = NestedClass.initialize(db, element, context=context) 

55 return cls(nested) 

56 

57 @property 

58 def element(self) -> DimensionElement: 

59 # Docstring inherited from DimensionRecordStorage.element. 

60 return self._nested.element 

61 

62 def clearCaches(self): 

63 # Docstring inherited from DimensionRecordStorage.clearCaches. 

64 self._cache.clear() 

65 self._nested.clearCaches() 

66 

67 def join( 

68 self, 

69 builder: QueryBuilder, *, 

70 regions: Optional[NamedKeyDict[DimensionElement, sqlalchemy.sql.ColumnElement]] = None, 

71 timespans: Optional[NamedKeyDict[DimensionElement, Timespan[sqlalchemy.sql.ColumnElement]]] = None, 

72 ): 

73 # Docstring inherited from DimensionRecordStorage. 

74 return self._nested.join(builder, regions=regions, timespans=timespans) 

75 

76 def insert(self, *records: DimensionRecord): 

77 # Docstring inherited from DimensionRecordStorage.insert. 

78 self._nested.insert(*records) 

79 for record in records: 

80 self._cache[record.dataId] = record 

81 

82 def sync(self, record: DimensionRecord): 

83 # Docstring inherited from DimensionRecordStorage.sync. 

84 inserted = self._nested.sync(record) 

85 if inserted: 

86 self._cache[record.dataId] = record 

87 return inserted 

88 

89 def fetch(self, dataId: DataCoordinate) -> Optional[DimensionRecord]: 

90 # Docstring inherited from DimensionRecordStorage.fetch. 

91 dataId = DataCoordinate.standardize(dataId, graph=self.element.graph) 

92 record = self._cache.get(dataId) 

93 if record is None: 

94 record = self._nested.fetch(dataId) 

95 self._cache[dataId] = record 

96 return record