-
Notifications
You must be signed in to change notification settings - Fork 14
/
PendingReferenceRegistry.java
194 lines (174 loc) · 7.43 KB
/
PendingReferenceRegistry.java
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
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* 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/>.
*/
package cz.cvut.kbss.jopa.oom;
import cz.cvut.kbss.ontodriver.descriptor.ListValueDescriptor;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URI;
import java.util.*;
import java.util.function.Predicate;
/**
* Used to track references to unpersisted instances during transaction.
* <p>
* The general rule is that on commit, this registry must be empty.
*/
class PendingReferenceRegistry {
private static final Logger LOG = LoggerFactory.getLogger(PendingReferenceRegistry.class);
private final Map<Object, Set<PendingAssertion>> pendingAssertions = new HashMap<>();
private Map<Object, Set<PendingListReference>> pendingLists;
/**
* Counts number of pending items per each list
*/
private Map<ListValueDescriptor, Integer> pendingListItems;
/**
* Registers a new pending assertion.
*
* @param owner Subject of the assertion
* @param assertion The assertion representation
* @param object The value of the assertion. Always an individual identifier
* @param context Context into which the assertion will be added
*/
void addPendingAssertion(NamedResource owner, Assertion assertion, Object object, URI context) {
assert owner != null;
assert assertion != null;
assert object != null;
final PendingAssertion pa = new PendingAssertion(owner, assertion, context);
if (!pendingAssertions.containsKey(object)) {
pendingAssertions.put(object, new HashSet<>());
}
pendingAssertions.get(object).add(pa);
}
/**
* Registers a pending reference to a sequence (simple or referenced).
*
* @param item The pending (unpersisted) item
* @param valueDescriptor Descriptor containing info about list owner, linking property etc.
* @param values Values of the sequence
*/
void addPendingListReference(Object item, ListValueDescriptor valueDescriptor, List<?> values) {
if (pendingLists == null) {
this.pendingLists = new HashMap<>();
this.pendingListItems = new HashMap<>();
}
pendingLists.putIfAbsent(item, new HashSet<>());
pendingLists.get(item).add(new PendingListReference(valueDescriptor, values));
pendingListItems.compute(valueDescriptor, (k, v) -> v == null ? 1 : v + 1);
}
/**
* Removes any pending persists with the specified object (value of the assertion).
*
* @param object Object which is no longer considered pending, so all assertions referencing it should be removed
* from this registry
*/
Set<PendingAssertion> removeAndGetPendingAssertionsWith(Object object) {
assert object != null;
final Set<PendingAssertion> pending = pendingAssertions.remove(object);
return pending != null ? pending : Collections.emptySet();
}
Set<PendingListReference> removeAndGetPendingListReferencesWith(Object object) {
assert object != null;
final Set<PendingListReference> refs = pendingLists == null ? null : pendingLists.remove(object);
if (refs == null) {
return Collections.emptySet();
}
final Iterator<PendingListReference> it = refs.iterator();
while (it.hasNext()) {
final PendingListReference ref = it.next();
pendingListItems.compute(ref.descriptor, (k, v) -> v - 1);
if (pendingListItems.get(ref.descriptor) == 0) {
pendingListItems.remove(ref.descriptor);
} else {
it.remove();
}
}
return refs;
}
Set<Object> getPendingResources() {
final Set<Object> pending = new HashSet<>(pendingAssertions.keySet());
if (pendingLists != null) {
pending.addAll(pendingLists.keySet());
}
return pending;
}
boolean hasPendingResources() {
return !pendingAssertions.isEmpty() || pendingLists != null && !pendingLists.isEmpty();
}
/**
* Removes all pending assertions which have the same subject (owner).
*
* @param subject The subject of assertions to remove
*/
void removePendingReferences(NamedResource subject) {
if (LOG.isTraceEnabled()) {
LOG.trace("Removing pending assertions for subject {}.", subject);
}
for (Set<PendingAssertion> pending : pendingAssertions.values()) {
pending.removeIf(item -> item.getOwner().equals(subject));
}
pendingAssertions.entrySet().removeIf(e -> e.getValue().isEmpty());
removePendingListReferences(desc -> desc.getListOwner().equals(subject));
}
private void removePendingListReferences(Predicate<ListValueDescriptor> condition) {
if (pendingLists == null) {
return;
}
final Set<ListValueDescriptor> removed = new HashSet<>();
for (Set<PendingListReference> pending : pendingLists.values()) {
final Iterator<PendingListReference> it = pending.iterator();
while (it.hasNext()) {
final ListValueDescriptor desc = it.next().descriptor;
if (condition.test(desc)) {
it.remove();
removed.add(desc);
}
}
}
pendingLists.entrySet().removeIf(e -> e.getValue().isEmpty());
removed.forEach(pendingListItems::remove);
}
/**
* Removes pending references representing the specified assertion about the specified subject.
*
* @param subject Assertion subject
* @param assertion The assertion
*/
void removePendingReferences(NamedResource subject, Assertion assertion) {
if (LOG.isTraceEnabled()) {
LOG.trace("Removing pending assertions {} for subject {}.", assertion, subject);
}
for (Set<PendingAssertion> pending : pendingAssertions.values()) {
pending.removeIf(item -> item.getOwner().equals(subject) && item.getAssertion().equals(assertion));
}
pendingAssertions.entrySet().removeIf(e -> e.getValue().isEmpty());
removePendingListReferences(
desc -> desc.getListOwner().equals(subject) && desc.getListProperty().equals(assertion));
}
static class PendingListReference {
private final ListValueDescriptor descriptor;
private final List<?> values;
private PendingListReference(ListValueDescriptor descriptor, List<?> values) {
this.descriptor = descriptor;
this.values = values;
}
public ListValueDescriptor getDescriptor() {
return descriptor;
}
public List<?> getValues() {
return values;
}
}
}