Source code

Download source archive ↓

src/rdflib/plugins/stores/auditable.py

1"""
2
3This wrapper intercepts calls through the store interface and implements
4thread-safe logging of destructive operations (adds / removes) in reverse.
5This is persisted on the store instance and the reverse operations are
6executed In order to return the store to the state it was when the transaction
7began Since the reverse operations are persisted on the store, the store
8itself acts as a transaction.
9
10Calls to commit or rollback, flush the list of reverse operations This
11provides thread-safe atomicity and isolation (assuming concurrent operations
12occur with different store instances), but no durability (transactions are
13persisted in memory and won't be available to reverse operations after the
14system fails): A and I out of ACID.
15
16"""
17
18from __future__ import annotations
19
20import threading
21from typing import TYPE_CHECKING, Any, Generator, Iterator, List, Optional, Tuple, Union
22
23from rdflib.graph import ConjunctiveGraph, Graph
24from rdflib.store import Store
25
26if TYPE_CHECKING:
27    from rdflib.graph import (
28        _ContextIdentifierType,
29        _ContextType,
30        _ObjectType,
31        _PredicateType,
32        _SubjectType,
33        _TriplePatternType,
34        _TripleType,
35    )
36    from rdflib.query import Result
37    from rdflib.term import URIRef
38
39
40destructiveOpLocks = {  # noqa: N816
41    "add": None,
42    "remove": None,
43}
44
45
46class AuditableStore(Store):
47    """A store that logs destructive operations (add/remove) in reverse order."""
48
49    def __init__(self, store: Store):
50        self.store = store
51        self.context_aware = store.context_aware
52        # NOTE: this store can't be formula_aware as it doesn't have enough
53        # info to reverse the removal of a quoted statement
54        self.formula_aware = False  # store.formula_aware
55        self.transaction_aware = True  # This is only half true
56        self.reverseOps: List[
57            Tuple[
58                Optional[_SubjectType],
59                Optional[_PredicateType],
60                Optional[_ObjectType],
61                Optional[_ContextIdentifierType],
62                str,
63            ]
64        ] = []
65        self.rollbackLock = threading.RLock()
66
67    def open(
68        self, configuration: Union[str, tuple[str, str]], create: bool = True
69    ) -> Optional[int]:
70        return self.store.open(configuration, create)
71
72    def close(self, commit_pending_transaction: bool = False) -> None:
73        self.store.close()
74
75    def destroy(self, configuration: str) -> None:
76        self.store.destroy(configuration)
77
78    def query(self, *args: Any, **kw: Any) -> Result:
79        return self.store.query(*args, **kw)
80
81    def add(
82        self, triple: _TripleType, context: _ContextType, quoted: bool = False
83    ) -> None:
84        (s, p, o) = triple
85        lock = destructiveOpLocks["add"]
86        lock = lock if lock else threading.RLock()
87        with lock:
88            context = (
89                context.__class__(self.store, context.identifier)
90                if context is not None
91                else None
92            )
93            ctxId = context.identifier if context is not None else None  # noqa: N806
94            if list(self.store.triples(triple, context)):
95                return  # triple already in store, do nothing
96            self.reverseOps.append((s, p, o, ctxId, "remove"))
97            try:
98                self.reverseOps.remove((s, p, o, ctxId, "add"))
99            except ValueError:
100                pass
101            self.store.add((s, p, o), context, quoted)
102
103    def remove(
104        self, spo: _TriplePatternType, context: Optional[_ContextType] = None
105    ) -> None:
106        subject, predicate, object_ = spo
107        lock = destructiveOpLocks["remove"]
108        lock = lock if lock else threading.RLock()
109        with lock:
110            # Need to determine which quads will be removed if any term is a
111            # wildcard
112            context = (
113                context.__class__(self.store, context.identifier)
114                if context is not None
115                else None
116            )
117            ctxId = context.identifier if context is not None else None  # noqa: N806
118            if None in [subject, predicate, object_, context]:
119                if ctxId:
120                    # type error: Item "None" of "Optional[Graph]" has no attribute "triples"
121                    for s, p, o in context.triples((subject, predicate, object_)):  # type: ignore[union-attr]
122                        try:
123                            self.reverseOps.remove((s, p, o, ctxId, "remove"))
124                        except ValueError:
125                            self.reverseOps.append((s, p, o, ctxId, "add"))
126                else:
127                    for s, p, o, ctx in ConjunctiveGraph(self.store).quads(
128                        (subject, predicate, object_)
129                    ):
130                        try:
131                            # type error: Item "None" of "Optional[Graph]" has no attribute "identifier"
132                            self.reverseOps.remove((s, p, o, ctx.identifier, "remove"))  # type: ignore[union-attr]
133                        except ValueError:
134                            # type error: Item "None" of "Optional[Graph]" has no attribute "identifier"
135                            self.reverseOps.append((s, p, o, ctx.identifier, "add"))  # type: ignore[union-attr]
136            else:
137                if not list(self.triples((subject, predicate, object_), context)):
138                    return  # triple not present in store, do nothing
139                try:
140                    self.reverseOps.remove(
141                        (subject, predicate, object_, ctxId, "remove")
142                    )
143                except ValueError:
144                    self.reverseOps.append((subject, predicate, object_, ctxId, "add"))
145            self.store.remove((subject, predicate, object_), context)
146
147    def triples(
148        self, triple: _TriplePatternType, context: Optional[_ContextType] = None
149    ) -> Iterator[Tuple[_TripleType, Iterator[Optional[_ContextType]]]]:
150        (su, pr, ob) = triple
151        context = (
152            context.__class__(self.store, context.identifier)
153            if context is not None
154            else None
155        )
156        for (s, p, o), cg in self.store.triples((su, pr, ob), context):
157            yield (s, p, o), cg
158
159    def __len__(self, context: Optional[_ContextType] = None):
160        context = (
161            context.__class__(self.store, context.identifier)
162            if context is not None
163            else None
164        )
165        return self.store.__len__(context)
166
167    def contexts(
168        self, triple: Optional[_TripleType] = None
169    ) -> Generator[_ContextType, None, None]:
170        for ctx in self.store.contexts(triple):
171            yield ctx
172
173    def bind(self, prefix: str, namespace: URIRef, override: bool = True) -> None:
174        self.store.bind(prefix, namespace, override=override)
175
176    def prefix(self, namespace: URIRef) -> Optional[str]:
177        return self.store.prefix(namespace)
178
179    def namespace(self, prefix: str) -> Optional[URIRef]:
180        return self.store.namespace(prefix)
181
182    def namespaces(self) -> Iterator[Tuple[str, URIRef]]:
183        return self.store.namespaces()
184
185    def commit(self) -> None:
186        self.reverseOps = []
187
188    def rollback(self) -> None:
189        # Acquire Rollback lock and apply reverse operations in the forward
190        # order
191        with self.rollbackLock:
192            for subject, predicate, obj, context, op in self.reverseOps:
193                if op == "add":
194                    # type error: Argument 2 to "Graph" has incompatible type "Optional[Node]"; expected "Union[IdentifiedNode, str, None]"
195                    self.store.add(
196                        (subject, predicate, obj), Graph(self.store, context)  # type: ignore[arg-type]
197                    )
198                else:
199                    self.store.remove(
200                        (subject, predicate, obj), Graph(self.store, context)
201                    )
202
203            self.reverseOps = []