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

21 

22__all__ = ("DummyRegistry", ) 

23 

24 

25from contextlib import contextmanager 

26from typing import Any, Iterator 

27 

28from lsst.daf.butler import DimensionUniverse, ddl 

29 

30 

31class DummyRegistry: 

32 """Dummy Registry, for Datastore test purposes. 

33 """ 

34 def __init__(self): 

35 self._counter = 0 

36 self._entries = {} 

37 self._externalTableRows = {} 

38 self._externalTableSpecs = {} 

39 self.dimensions = DimensionUniverse() 

40 

41 def registerOpaqueTable(self, name: str, spec: ddl.TableSpec): 

42 self._externalTableSpecs[name] = spec 

43 self._externalTableRows[name] = [] 

44 

45 def insertOpaqueData(self, name: str, *data: dict): 

46 spec = self._externalTableSpecs[name] 

47 uniqueConstraints = list(spec.unique) 

48 uniqueConstraints.append(tuple(field.name for field in spec.fields if field.primaryKey)) 

49 for d in data: 

50 for constraint in uniqueConstraints: 

51 matching = list(self.fetchOpaqueData(name, **{k: d[k] for k in constraint})) 

52 if len(matching) != 0: 

53 raise RuntimeError(f"Unique constraint {constraint} violation in external table {name}.") 

54 self._externalTableRows[name].append(d) 

55 

56 def fetchOpaqueData(self, name: str, **where: Any) -> Iterator[dict]: 

57 for d in self._externalTableRows[name]: 

58 if all(d[k] == v for k, v in where.items()): 

59 yield d 

60 

61 def deleteOpaqueData(self, name: str, **where: Any): 

62 kept = [] 

63 for d in self._externalTableRows[name]: 

64 if not all(d[k] == v for k, v in where.items()): 

65 kept.append(d) 

66 self._externalTableRows[name] = kept 

67 

68 def insertDatasetLocations(self, datastoreName, refs): 

69 # Only set ID if ID is 0 or None 

70 for ref in refs: 

71 incrementCounter = True 

72 if ref.id is None or ref.id == 0: 

73 ref._id = self._counter 

74 incrementCounter = False 

75 if ref.id not in self._entries: 

76 self._entries[ref.id] = set() 

77 self._entries[ref.id].add(datastoreName) 

78 if incrementCounter: 

79 self._counter += 1 

80 

81 def getDatasetLocations(self, ref): 

82 return self._entries[ref.id].copy() 

83 

84 def removeDatasetLocation(self, datastoreName, ref): 

85 self._entries[ref.id].remove(datastoreName) 

86 

87 def makeDatabaseDict(self, table, key, value): 

88 return dict() 

89 

90 @contextmanager 

91 def transaction(self): 

92 yield