-
Notifications
You must be signed in to change notification settings - Fork 14
/
PendingReferenceRegistry.java
151 lines (132 loc) · 5.54 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
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.*;
/**
* 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 final Map<Object, Set<PendingListReference>> pendingLists = new HashMap<>();
/**
* Counts number of pending items per each list
*/
private final Map<ListValueDescriptor, Integer> pendingListItems = new HashMap<>();
/**
* 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) {
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.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());
pending.addAll(pendingLists.keySet());
return pending;
}
boolean hasPendingResources() {
return !pendingAssertions.isEmpty() || !pendingLists.isEmpty();
}
/**
* Removes all pending assertions which have the same subject (owner).
*
* @param subject The subject of assertions to remove
*/
void removePendingAssertions(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());
}
/**
* Removes pending assertions representing the specified assertion about the specified subject.
*
* @param subject Assertion subject
* @param assertion The assertion
*/
void removePendingAssertions(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());
}
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;
}
}
}