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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

# This file is part of daf_butler. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (http://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <http://www.gnu.org/licenses/>. 

from __future__ import annotations 

 

__all__ = ["SpatialDimensionRecordStorage"] 

 

from typing import Optional 

 

import sqlalchemy 

 

from ...core import DimensionElement, DimensionRecord, Timespan, ddl 

from ...core.utils import NamedKeyDict, NamedValueSet 

from ...core.dimensions.schema import makeElementTableSpec, REGION_FIELD_SPEC, addDimensionForeignKey 

from ..interfaces import Database, DimensionRecordStorage, StaticTablesContext 

from ..queries import QueryBuilder 

from .table import TableDimensionRecordStorage 

 

 

_OVERLAP_TABLE_NAME_PATTERN = "{0}_{1}_overlap" 

 

 

def _makeOverlapTableSpec(a: DimensionElement, b: DimensionElement) -> ddl.TableSpec: 

"""Create a specification for a table that represents a many-to-many 

relationship between two `DimensionElement` tables. 

 

Parameters 

---------- 

a : `DimensionElement` 

First element in the relationship. 

b : `DimensionElement` 

Second element in the relationship. 

 

Returns 

------- 

spec : `TableSpec` 

Database-agnostic specification for a table. 

""" 

tableSpec = ddl.TableSpec( 

fields=NamedValueSet(), 

unique=set(), 

foreignKeys=[], 

) 

for dimension in a.graph.required: 

addDimensionForeignKey(tableSpec, dimension, primaryKey=True) 

for dimension in b.graph.required: 

addDimensionForeignKey(tableSpec, dimension, primaryKey=True) 

return tableSpec 

 

 

class SpatialDimensionRecordStorage(TableDimensionRecordStorage): 

"""A record storage implementation for spatial dimension elements that uses 

a regular database table. 

 

Parameters 

---------- 

db : `Database` 

Interface to the database engine and namespace that will hold these 

dimension records. 

element : `DimensionElement` 

The element whose records this storage will manage. 

table : `sqlalchemy.schema.Table` 

The logical table for the element. 

commonSkyPixOverlapTable : `sqlalchemy.schema.Table`, optional 

The logical table for the overlap table with the dimension universe's 

common skypix dimension. 

""" 

def __init__(self, db: Database, element: DimensionElement, *, table: sqlalchemy.schema.Table, 

commonSkyPixOverlapTable: sqlalchemy.schema.Table): 

super().__init__(db, element, table=table) 

self._commonSkyPixOverlapTable = commonSkyPixOverlapTable 

assert element.spatial 

 

@classmethod 

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

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

# Docstring inherited from DimensionRecordStorage. 

95 ↛ 98line 95 didn't jump to line 98, because the condition on line 95 was never false if context is not None: 

method = context.addTable 

else: 

method = db.ensureTableExists 

return cls( 

db, 

element, 

table=method(element.name, makeElementTableSpec(element)), 

commonSkyPixOverlapTable=method( 

_OVERLAP_TABLE_NAME_PATTERN.format(element.name, element.universe.commonSkyPix.name), 

_makeOverlapTableSpec(element, element.universe.commonSkyPix) 

) 

) 

 

def join( 

self, 

builder: QueryBuilder, *, 

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

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

): 

# Docstring inherited from DimensionRecordStorage. 

if regions is not None: 

dimensions = NamedValueSet(self.element.graph.required) 

dimensions.add(self.element.universe.universe.commonSkyPix) 

builder.joinTable(self._commonSkyPixOverlapTable, dimensions) 

regions[self.element] = self._table.columns[REGION_FIELD_SPEC.name] 

super().join(builder, regions=None, timespans=timespans) 

 

def insert(self, *records: DimensionRecord): 

# Docstring inherited from DimensionRecordStorage.insert. 

commonSkyPixRows = [] 

commonSkyPix = self.element.universe.commonSkyPix 

for record in records: 

if record.region is None: 

# TODO: should we warn about this case? 

continue 

base = record.dataId.byName() 

for begin, end in commonSkyPix.pixelization.envelope(record.region): 

for skypix in range(begin, end): 

row = base.copy() 

row[commonSkyPix.name] = skypix 

commonSkyPixRows.append(row) 

with self._db.transaction(): 

super().insert(*records) 

if commonSkyPixRows: 

self._db.insert(self._commonSkyPixOverlapTable, *commonSkyPixRows)