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

# 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/>. 

 

"""Generic datastore code useful for most datastores.""" 

 

__all__ = ("GenericBaseDatastore", ) 

 

import logging 

from typing import MutableMapping 

 

from lsst.daf.butler import Datastore, DatasetTypeNotSupportedError 

 

log = logging.getLogger(__name__) 

 

 

class GenericBaseDatastore(Datastore): 

"""Methods useful for most implementations of a `Datastore`. 

 

Should always be sub-classed since key abstract methods are missing. 

""" 

 

records: MutableMapping 

"""Place to store internal records about datasets.""" 

 

def _info_to_record(self, info): 

"""Convert a `StoredDatastoreItemInfo` to a suitable database record. 

 

Parameters 

---------- 

info : `StoredDatastoreItemInfo` 

Metadata associated with the stored Dataset. 

 

Returns 

------- 

record : `MutableMapping` 

Record to be stored. 

""" 

raise NotImplementedError("Must be implemented by subclass") 

 

def _record_to_info(self, record): 

"""Convert a record associated with this dataset to a 

`StoredDatastoreItemInfo` 

 

Parameters 

---------- 

record : `MutableMapping` 

Object stored in the record table. 

 

Returns 

------- 

info : `StoredDatastoreItemInfo` 

The information associated with this dataset record as a Python 

class. 

""" 

raise NotImplementedError("Must be implemented by subclass") 

 

def addStoredItemInfo(self, ref, info): 

"""Record internal storage information associated with this 

`DatasetRef` 

 

Parameters 

---------- 

ref : `DatasetRef` 

The Dataset that has been stored. 

info : `StoredDatastoreItemInfo` 

Metadata associated with the stored Dataset. 

""" 

86 ↛ 87line 86 didn't jump to line 87, because the condition on line 86 was never true if ref.id in self.records: 

raise KeyError("Attempt to store item info with ID {}" 

" when that ID exists as '{}'".format(ref.id, self.records[ref.id])) 

self.records[ref.id] = self._info_to_record(info) 

 

def getStoredItemInfo(self, ref): 

"""Retrieve information associated with file stored in this 

`Datastore`. 

 

Parameters 

---------- 

ref : `DatasetRef` 

The Dataset that is to be queried. 

 

Returns 

------- 

info : `StoredFilenfo` 

Stored information about this file and its formatter. 

 

Raises 

------ 

KeyError 

Dataset with that id can not be found. 

""" 

record = self.records.get(ref.id, None) 

if record is None: 

raise KeyError("Unable to retrieve formatter associated with Dataset {}".format(ref.id)) 

 

return self._record_to_info(record) 

 

def removeStoredItemInfo(self, ref): 

"""Remove information about the file associated with this dataset. 

 

Parameters 

---------- 

ref : `DatasetRef` 

The Dataset that has been removed. 

""" 

del self.records[ref.id] 

 

def _register_dataset(self, ref, itemInfo): 

"""Update registry to indicate that this dataset has been stored. 

 

Parameters 

---------- 

ref : `DatasetRef` 

Dataset to register. 

itemInfo : `StoredDatastoreItemInfo` 

Internal datastore metadata associated with this dataset. 

""" 

self.registry.addDatasetLocation(ref, self.name) 

 

# TODO: this is only transactional if the DatabaseDict uses 

# self.registry internally. Probably need to add 

# transactions to DatabaseDict to do better than that. 

self.addStoredItemInfo(ref, itemInfo) 

 

# Register all components with same information 

for compRef in ref.components.values(): 

self.registry.addDatasetLocation(compRef, self.name) 

self.addStoredItemInfo(compRef, itemInfo) 

 

def _remove_from_registry(self, ref): 

"""Remove rows from registry. 

 

Parameters 

---------- 

ref : `DatasetRef` 

Dataset to remove from registry. 

""" 

self.removeStoredItemInfo(ref) 

self.registry.removeDatasetLocation(self.name, ref) 

for compRef in ref.components.values(): 

self.registry.removeDatasetLocation(self.name, compRef) 

self.removeStoredItemInfo(compRef) 

 

def _post_process_get(self, inMemoryDataset, readStorageClass, assemblerParams=None): 

"""Given the Python object read from the datastore, manipulate 

it based on the supplied parameters and ensure the Python 

type is correct. 

 

Parameters 

---------- 

inMemoryDataset : `object` 

Dataset to check. 

readStorageClass: `StorageClass` 

The `StorageClass` used to obtain the assembler and to 

check the python type. 

assemblerParams : `dict` 

Parameters to pass to the assembler. Can be `None`. 

""" 

# Process any left over parameters 

if assemblerParams: 

inMemoryDataset = readStorageClass.assembler().handleParameters(inMemoryDataset, assemblerParams) 

 

# Validate the returned data type matches the expected data type 

pytype = readStorageClass.pytype 

183 ↛ 184line 183 didn't jump to line 184, because the condition on line 183 was never true if pytype and not isinstance(inMemoryDataset, pytype): 

raise TypeError("Got Python type {} from datastore but expected {}".format(type(inMemoryDataset), 

pytype)) 

 

return inMemoryDataset 

 

def _validate_put_parameters(self, inMemoryDataset, ref): 

"""Validate the supplied arguments for put. 

 

Parameters 

---------- 

inMemoryDataset : `object` 

The Dataset to store. 

ref : `DatasetRef` 

Reference to the associated Dataset. 

""" 

storageClass = ref.datasetType.storageClass 

 

# Sanity check 

202 ↛ 203line 202 didn't jump to line 203, because the condition on line 202 was never true if not isinstance(inMemoryDataset, storageClass.pytype): 

raise TypeError("Inconsistency between supplied object ({}) " 

"and storage class type ({})".format(type(inMemoryDataset), 

storageClass.pytype)) 

 

# Confirm that we can accept this dataset 

if not self.constraints.isAcceptable(ref): 

# Raise rather than use boolean return value. 

raise DatasetTypeNotSupportedError(f"Dataset {ref} has been rejected by this datastore via" 

" configuration.") 

 

return 

 

def transfer(self, inputDatastore, ref): 

"""Retrieve a Dataset from an input `Datastore`, 

and store the result in this `Datastore`. 

 

Parameters 

---------- 

inputDatastore : `Datastore` 

The external `Datastore` from which to retreive the Dataset. 

ref : `DatasetRef` 

Reference to the required Dataset in the input data store. 

 

""" 

assert inputDatastore is not self # unless we want it for renames? 

inMemoryDataset = inputDatastore.get(ref) 

return self.put(inMemoryDataset, ref)