Coverage for python/lsst/daf/butler/registry/bridge/ephemeral.py: 24%

41 statements  

« prev     ^ index     » next       coverage.py v7.2.4, created at 2023-04-29 02:58 -0700

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__ = ("EphemeralDatastoreRegistryBridge",) 

24 

25from contextlib import contextmanager 

26from typing import TYPE_CHECKING, Iterable, Iterator, Optional, Set, Tuple, Type 

27 

28from ...core import DatasetId 

29from ..interfaces import DatasetIdRef, DatastoreRegistryBridge, FakeDatasetRef, OpaqueTableStorage 

30 

31if TYPE_CHECKING: 

32 from ...core import StoredDatastoreItemInfo 

33 from ...core.datastore import DatastoreTransaction 

34 

35 

36class EphemeralDatastoreRegistryBridge(DatastoreRegistryBridge): 

37 """An implementation of `DatastoreRegistryBridge` for ephemeral datastores 

38 - those whose artifacts never outlive the current process. 

39 

40 Parameters 

41 ---------- 

42 datastoreName : `str` 

43 Name of the `Datastore` as it should appear in `Registry` tables 

44 referencing it. 

45 

46 Notes 

47 ----- 

48 The current implementation just uses a Python set to remember the dataset 

49 IDs associated with the datastore. This will probably need to be converted 

50 to use in-database temporary tables instead in the future to support 

51 "in-datastore" constraints in `Registry.queryDatasets`. 

52 """ 

53 

54 def __init__(self, datastoreName: str): 

55 super().__init__(datastoreName) 

56 self._datasetIds: Set[DatasetId] = set() 

57 self._trashedIds: Set[DatasetId] = set() 

58 

59 def insert(self, refs: Iterable[DatasetIdRef]) -> None: 

60 # Docstring inherited from DatastoreRegistryBridge 

61 self._datasetIds.update(ref.getCheckedId() for ref in refs) 

62 

63 def forget(self, refs: Iterable[DatasetIdRef]) -> None: 

64 self._datasetIds.difference_update(ref.id for ref in refs) 

65 

66 def _rollbackMoveToTrash(self, refs: Iterable[DatasetIdRef]) -> None: 

67 """Rollback a moveToTrash call.""" 

68 for ref in refs: 

69 self._trashedIds.remove(ref.getCheckedId()) 

70 

71 def moveToTrash(self, refs: Iterable[DatasetIdRef], transaction: Optional[DatastoreTransaction]) -> None: 

72 # Docstring inherited from DatastoreRegistryBridge 

73 if transaction is None: 

74 raise RuntimeError("Must be called with a defined transaction.") 

75 ref_list = list(refs) 

76 with transaction.undoWith(f"Trash {len(ref_list)} datasets", self._rollbackMoveToTrash, ref_list): 

77 self._trashedIds.update(ref.getCheckedId() for ref in ref_list) 

78 

79 def check(self, refs: Iterable[DatasetIdRef]) -> Iterable[DatasetIdRef]: 

80 # Docstring inherited from DatastoreRegistryBridge 

81 yield from (ref for ref in refs if ref in self) 

82 

83 def __contains__(self, ref: DatasetIdRef) -> bool: 

84 return ref.getCheckedId() in self._datasetIds and ref.getCheckedId() not in self._trashedIds 

85 

86 @contextmanager 

87 def emptyTrash( 

88 self, 

89 records_table: Optional[OpaqueTableStorage] = None, 

90 record_class: Optional[Type[StoredDatastoreItemInfo]] = None, 

91 record_column: Optional[str] = None, 

92 ) -> Iterator[ 

93 Tuple[Iterable[Tuple[DatasetIdRef, Optional[StoredDatastoreItemInfo]]], Optional[Set[str]]] 

94 ]: 

95 # Docstring inherited from DatastoreRegistryBridge 

96 matches: Iterable[Tuple[FakeDatasetRef, Optional[StoredDatastoreItemInfo]]] = () 

97 if isinstance(records_table, OpaqueTableStorage): 

98 if record_class is None: 

99 raise ValueError("Record class must be provided if records table is given.") 

100 matches = ( 

101 (FakeDatasetRef(id), record_class.from_record(record)) 

102 for id in self._trashedIds 

103 for record in records_table.fetch(dataset_id=id) 

104 ) 

105 else: 

106 matches = ((FakeDatasetRef(id), None) for id in self._trashedIds) 

107 

108 # Indicate to caller that we do not know about artifacts that 

109 # should be retained. 

110 yield ((matches, None)) 

111 

112 if isinstance(records_table, OpaqueTableStorage): 

113 # Remove the records entries 

114 records_table.delete(["dataset_id"], *[{"dataset_id": id} for id in self._trashedIds]) 

115 

116 # Empty the trash table 

117 self._datasetIds.difference_update(self._trashedIds) 

118 self._trashedIds = set()