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

44 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-25 15:14 +0000

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 collections.abc import Iterable, Iterator 

26from contextlib import contextmanager 

27from typing import TYPE_CHECKING 

28 

29from ...core import DatasetId 

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

31 

32if TYPE_CHECKING: 

33 from ...core import StoredDatastoreItemInfo 

34 from ...core.datastore import DatastoreTransaction 

35 

36 

37class EphemeralDatastoreRegistryBridge(DatastoreRegistryBridge): 

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

39 - those whose artifacts never outlive the current process. 

40 

41 Parameters 

42 ---------- 

43 datastoreName : `str` 

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

45 referencing it. 

46 

47 Notes 

48 ----- 

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

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

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

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

53 """ 

54 

55 def __init__(self, datastoreName: str): 

56 super().__init__(datastoreName) 

57 self._datasetIds: set[DatasetId] = set() 

58 self._trashedIds: set[DatasetId] = set() 

59 

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

61 # Docstring inherited from DatastoreRegistryBridge 

62 self._datasetIds.update(ref.id for ref in refs) 

63 

64 def ensure(self, refs: Iterable[DatasetIdRef]) -> None: 

65 # Docstring inherited from DatastoreRegistryBridge 

66 self._datasetIds.update(ref.id for ref in refs) 

67 

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

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

70 

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

72 """Rollback a moveToTrash call.""" 

73 for ref in refs: 

74 self._trashedIds.remove(ref.id) 

75 

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

77 # Docstring inherited from DatastoreRegistryBridge 

78 if transaction is None: 

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

80 ref_list = list(refs) 

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

82 self._trashedIds.update(ref.id for ref in ref_list) 

83 

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

85 # Docstring inherited from DatastoreRegistryBridge 

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

87 

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

89 return ref.id in self._datasetIds and ref.id not in self._trashedIds 

90 

91 @contextmanager 

92 def emptyTrash( 

93 self, 

94 records_table: OpaqueTableStorage | None = None, 

95 record_class: type[StoredDatastoreItemInfo] | None = None, 

96 record_column: str | None = None, 

97 ) -> Iterator[tuple[Iterable[tuple[DatasetIdRef, StoredDatastoreItemInfo | None]], set[str] | None]]: 

98 # Docstring inherited from DatastoreRegistryBridge 

99 matches: Iterable[tuple[FakeDatasetRef, StoredDatastoreItemInfo | None]] = () 

100 if isinstance(records_table, OpaqueTableStorage): 

101 if record_class is None: 

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

103 matches = ( 

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

105 for id in self._trashedIds 

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

107 ) 

108 else: 

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

110 

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

112 # should be retained. 

113 yield ((matches, None)) 

114 

115 if isinstance(records_table, OpaqueTableStorage): 

116 # Remove the records entries 

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

118 

119 # Empty the trash table 

120 self._datasetIds.difference_update(self._trashedIds) 

121 self._trashedIds = set()