-
Notifications
You must be signed in to change notification settings - Fork 14
/
PendingAssertionRegistry.java
140 lines (118 loc) · 4.72 KB
/
PendingAssertionRegistry.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
package cz.cvut.kbss.jopa.oom;
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 should be empty.
*/
class PendingAssertionRegistry {
private static final Logger LOG = LoggerFactory.getLogger(PendingAssertionRegistry.class);
private final Map<Object, Set<PendingAssertion>> pendingAssertions = 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);
}
/**
* 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<Object> getPendingResources() {
return Collections.unmodifiableSet(pendingAssertions.keySet());
}
/**
* 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.owner.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.owner.equals(subject) && item.assertion.equals(assertion));
}
pendingAssertions.entrySet().removeIf(e -> e.getValue().isEmpty());
}
public static class PendingAssertion {
private final NamedResource owner;
private final Assertion assertion;
private final URI context;
private PendingAssertion(NamedResource owner, Assertion assertion, URI context) {
this.owner = owner;
this.assertion = assertion;
this.context = context;
}
public NamedResource getOwner() {
return owner;
}
public Assertion getAssertion() {
return assertion;
}
public URI getContext() {
return context;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PendingAssertion that = (PendingAssertion) o;
return owner.equals(that.owner) && assertion.equals(that.assertion) && (context != null ?
context.equals(that.context) : that.context == null);
}
@Override
public int hashCode() {
int result = owner.hashCode();
result = 31 * result + assertion.hashCode();
result = 31 * result + (context != null ? context.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "PendingAssertion{" +
owner + " -> " + assertion +
", context=" + context +
'}';
}
}
}