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

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

# 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__ = ["DimensionRecordStorage", "DimensionRecordStorageManager"] 

 

from abc import ABC, abstractmethod 

from typing import Optional, Type, TYPE_CHECKING 

 

import sqlalchemy 

 

from ...core import SkyPixDimension 

 

32 ↛ 33line 32 didn't jump to line 33, because the condition on line 32 was never trueif TYPE_CHECKING: 

from ...core import ( 

DataId, 

DimensionElement, 

DimensionRecord, 

DimensionUniverse, 

Timespan, 

) 

from ...core.utils import NamedKeyDict 

from ..queries import QueryBuilder 

from ._database import Database, StaticTablesContext 

 

 

class DimensionRecordStorage(ABC): 

"""An abstract base class that represents a way of storing the records 

associated with a single `DimensionElement`. 

 

Concrete `DimensionRecordStorage` instances should generally be constructed 

via a call to `setupDimensionStorage`, which selects the appropriate 

subclass for each element according to its configuration. 

 

All `DimensionRecordStorage` methods are pure abstract, even though in some 

cases a reasonable default implementation might be possible, in order to 

better guarantee all methods are correctly overridden. All of these 

potentially-defaultable implementations are extremely trivial, so asking 

subclasses to provide them is not a significant burden. 

""" 

 

@classmethod 

@abstractmethod 

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

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

"""Construct an instance of this class using a standardized interface. 

 

Parameters 

---------- 

db : `Database` 

Interface to the underlying database engine and namespace. 

element : `DimensionElement` 

Dimension element the new instance will manage records for. 

context : `StaticTablesContext`, optional 

If provided, an object to use to create any new tables. If not 

provided, ``db.ensureTableExists`` should be used instead. 

 

Returns 

------- 

storage : `DimensionRecordStorage` 

A new `DimensionRecordStorage` subclass instance. 

""" 

raise NotImplementedError() 

 

@staticmethod 

def getDefaultImplementation(element: DimensionElement, ignoreCached: bool = False 

) -> Type[DimensionRecordStorage]: 

"""Return the default `DimensionRecordStorage` implementation for the 

given `DimensionElement`. 

 

Parameters 

---------- 

element : `DimensionElement` 

The element whose properties should be examined to determine the 

appropriate default implementation class. 

ignoreCached : `bool`, optional 

If `True`, ignore `DimensionElement.cached` and always return the 

storage implementation that would be used without caching. 

 

Returns 

------- 

cls : `type` 

A concrete subclass of `DimensionRecordStorage`. 

 

Notes 

----- 

At present, these defaults are always used, but we may add support for 

explicitly setting the class to use in configuration in the future. 

""" 

if not ignoreCached and element.cached: 

from ..dimensions.caching import CachingDimensionRecordStorage 

return CachingDimensionRecordStorage 

elif element.hasTable(): 

if element.viewOf is not None: 

if element.spatial: 

raise NotImplementedError("Spatial view dimension storage is not supported.") 

from ..dimensions.query import QueryDimensionRecordStorage 

return QueryDimensionRecordStorage 

elif element.spatial: 

from ..dimensions.spatial import SpatialDimensionRecordStorage 

return SpatialDimensionRecordStorage 

else: 

from ..dimensions.table import TableDimensionRecordStorage 

return TableDimensionRecordStorage 

elif isinstance(element, SkyPixDimension): 

from ..dimensions.skypix import SkyPixDimensionRecordStorage 

return SkyPixDimensionRecordStorage 

raise NotImplementedError(f"No default DimensionRecordStorage class for {element}.") 

 

@property 

@abstractmethod 

def element(self) -> DimensionElement: 

"""The element whose records this instance holds (`DimensionElement`). 

""" 

raise NotImplementedError() 

 

@abstractmethod 

def clearCaches(self): 

"""Clear any in-memory caches held by the storage instance. 

 

This is called by `Registry` when transactions are rolled back, to 

avoid in-memory caches from ever containing records that are not 

present in persistent storage. 

""" 

raise NotImplementedError() 

 

@abstractmethod 

def join( 

self, 

builder: QueryBuilder, *, 

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

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

): 

"""Add the dimension element's logical table to a query under 

construction. 

 

This is a visitor pattern interface that is expected to be called only 

by `QueryBuilder.joinDimensionElement`. 

 

Parameters 

---------- 

builder : `QueryBuilder` 

Builder for the query that should contain this element. 

regions : `NamedKeyDict`, optional 

A mapping from `DimensionElement` to a SQLAlchemy column containing 

the region for that element, which should be updated to include a 

region column for this element if one exists. If `None`, 

``self.element`` is not being included in the query via a spatial 

join. 

timespan : `NamedKeyDict`, optional 

A mapping from `DimensionElement` to a `Timespan` of SQLALchemy 

columns containing the timespan for that element, which should be 

updated to include timespan columns for this element if they exist. 

If `None`, ``self.element`` is not being included in the query via 

a temporal join. 

 

Notes 

----- 

Elements are only included in queries via spatial and/or temporal joins 

when necessary to connect them to other elements in the query, so 

``regions`` and ``timespans`` cannot be assumed to be not `None` just 

because an element has a region or timespan. 

""" 

raise NotImplementedError() 

 

@abstractmethod 

def insert(self, *records: DimensionRecord): 

"""Insert one or more records into storage. 

 

Parameters 

---------- 

records 

One or more instances of the `DimensionRecord` subclass for the 

element this storage is associated with. 

 

Raises 

------ 

TypeError 

Raised if the element does not support record insertion. 

sqlalchemy.exc.IntegrityError 

Raised if one or more records violate database integrity 

constraints. 

 

Notes 

----- 

As `insert` is expected to be called only by a `Registry`, we rely 

on `Registry` to provide transactionality, both by using a SQLALchemy 

connection shared with the `Registry` and by relying on it to call 

`clearCaches` when rolling back transactions. 

""" 

raise NotImplementedError() 

 

@abstractmethod 

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

"""Retrieve a record from storage. 

 

Parameters 

---------- 

dataId : `DataId` 

A data ID that identifies the record to be retrieved. This may 

be an informal data ID dict or a validated `DataCoordinate`. 

 

Returns 

------- 

record : `DimensionRecord` or `None` 

A record retrieved from storage, or `None` if there is no such 

record. 

""" 

raise NotImplementedError() 

 

 

class DimensionRecordStorageManager(ABC): 

"""An interface for managing the dimension records in a `Registry`. 

 

`DimensionRecordStorageManager` primarily serves as a container and factory 

for `DimensionRecordStorage` instances, which each provide access to the 

records for a different `DimensionElement`. 

 

Parameters 

---------- 

universe : `DimensionUniverse` 

Universe of all dimensions and dimension elements known to the 

`Registry`. 

 

Notes 

----- 

In a multi-layer `Registry`, many dimension elements will only have 

records in one layer (often the base layer). The union of the records 

across all layers forms the logical table for the full `Registry`. 

""" 

def __init__(self, *, universe: DimensionUniverse): 

self.universe = universe 

 

@classmethod 

@abstractmethod 

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

universe: DimensionUniverse) -> DimensionRecordStorageManager: 

"""Construct an instance of the manager. 

 

Parameters 

---------- 

db : `Database` 

Interface to the underlying database engine and namespace. 

context : `StaticTablesContext` 

Context object obtained from `Database.declareStaticTables`; used 

to declare any tables that should always be present in a layer 

implemented with this manager. 

universe : `DimensionUniverse` 

Universe graph containing dimensions known to this `Registry`. 

 

Returns 

------- 

manager : `DimensionRecordStorageManager` 

An instance of a concrete `DimensionRecordStorageManager` subclass. 

""" 

raise NotImplementedError() 

 

@abstractmethod 

def refresh(self): 

"""Ensure all other operations on this manager are aware of any 

dataset types that may have been registered by other clients since 

it was initialized or last refreshed. 

""" 

raise NotImplementedError() 

 

def __getitem__(self, element: DimensionElement) -> DimensionRecordStorage: 

"""Interface to `get` that raises `LookupError` instead of returning 

`None` on failure. 

""" 

r = self.get(element) 

if r is None: 

raise LookupError(f"No dimension element '{element.name}' found in this registry layer.") 

return r 

 

@abstractmethod 

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

"""Return an object that provides access to the records associated with 

the given element, if one exists in this layer. 

 

Parameters 

---------- 

element : `DimensionElement` 

Element for which records should be returned. 

 

Returns 

------- 

records : `DimensionRecordStorage` or `None` 

The object representing the records for the given element in this 

layer, or `None` if there are no records for that element in this 

layer. 

 

Note 

---- 

Dimension elements registered by another client of the same layer since 

the last call to `initialize` or `refresh` may not be found. 

""" 

raise NotImplementedError() 

 

@abstractmethod 

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

"""Ensure that this layer can hold records for the given element, 

creating new tables as necessary. 

 

Parameters 

---------- 

element : `DimensionElement` 

Element for which a table should created (as necessary) and 

an associated `DimensionRecordStorage` returned. 

 

Returns 

------- 

records : `DimensionRecordStorage` 

The object representing the records for the given element in this 

layer. 

 

Raises 

------ 

TransactionInterruption 

Raised if this operation is invoked within a `Database.transaction` 

context. 

""" 

raise NotImplementedError() 

 

@abstractmethod 

def clearCaches(self): 

"""Clear any in-memory caches held by nested `DimensionRecordStorage` 

instances. 

 

This is called by `Registry` when transactions are rolled back, to 

avoid in-memory caches from ever containing records that are not 

present in persistent storage. 

""" 

raise NotImplementedError() 

 

universe: DimensionUniverse 

"""Universe of all dimensions and dimension elements known to the 

`Registry` (`DimensionUniverse`). 

"""