-
Notifications
You must be signed in to change notification settings - Fork 14
/
ChangeTrackingStorageConnector.java
187 lines (159 loc) · 6.65 KB
/
ChangeTrackingStorageConnector.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
package cz.cvut.kbss.ontodriver.jena.connector;
import cz.cvut.kbss.ontodriver.jena.config.JenaConfigParam;
import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
import org.apache.jena.query.Dataset;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.*;
import java.util.*;
/**
* This connector tracks transactional changes and writes them on commit to the {@link SharedStorageConnector}.
*/
public class ChangeTrackingStorageConnector extends AbstractStorageConnector {
private final AbstractStorageConnector centralConnector;
private final boolean useDefaultAsUnion;
private LocalModel localModel;
ChangeTrackingStorageConnector(AbstractStorageConnector centralConnector) {
this.centralConnector = centralConnector;
this.useDefaultAsUnion =
configuration != null && configuration.is(JenaConfigParam.TREAT_DEFAULT_GRAPH_AS_UNION);
}
@Override
public void begin() {
transaction.begin();
this.localModel = new LocalModel(useDefaultAsUnion);
}
@Override
public void commit() throws JenaDriverException {
transaction.commit();
try {
centralConnector.begin();
mergeRemovedStatements();
mergeAddedStatements();
centralConnector.commit();
transaction.afterCommit();
} catch (JenaDriverException e) {
transaction.rollback();
centralConnector.rollback();
transaction.afterRollback();
throw e;
} finally {
this.localModel = null;
}
}
private void mergeRemovedStatements() {
final Dataset removed = localModel.getRemoved();
centralConnector.remove(removed.getDefaultModel().listStatements().toList());
removed.listNames().forEachRemaining(context -> {
final Model model = removed.getNamedModel(context);
centralConnector.remove(model.listStatements().toList(), context);
});
}
private void mergeAddedStatements() {
final Dataset added = localModel.getAdded();
centralConnector.add(added.getDefaultModel().listStatements().toList());
added.listNames().forEachRemaining(context -> {
final Model model = added.getNamedModel(context);
centralConnector.add(model.listStatements().toList(), context);
});
}
@Override
public void rollback() {
transaction.rollback();
this.localModel = null;
transaction.afterRollback();
}
@Override
public Collection<Statement> find(Resource subject, Property property, RDFNode value) {
transaction.verifyActive();
final Collection<Statement> existing = centralConnector.find(subject, property, value);
return localModel.enhanceStatements(existing, subject, property, value);
}
@Override
public Collection<Statement> find(Resource subject, Property property, RDFNode value, String context) {
transaction.verifyActive();
final Collection<Statement> existing = centralConnector.find(subject, property, value, context);
return localModel.enhanceStatements(existing, subject, property, value, context);
}
@Override
public boolean contains(Resource subject, Property property, RDFNode value) {
transaction.verifyActive();
final LocalModel.Containment localStatus = localModel.contains(subject, property, value);
return localStatus == LocalModel.Containment.ADDED ||
localStatus == LocalModel.Containment.UNKNOWN && centralConnector.contains(subject, property, value);
}
@Override
public boolean contains(Resource subject, Property property, RDFNode value, String context) {
transaction.verifyActive();
final LocalModel.Containment localStatus = localModel.contains(subject, property, value, context);
return localStatus == LocalModel.Containment.ADDED ||
localStatus == LocalModel.Containment.UNKNOWN &&
centralConnector.contains(subject, property, value, context);
}
@Override
public List<String> getContexts() {
transaction.verifyActive();
final List<String> centralContexts = centralConnector.getContexts();
final Set<String> set = new LinkedHashSet<>(centralContexts);
set.addAll(localModel.getContexts());
return new ArrayList<>(set);
}
@Override
public void add(List<Statement> statements) {
transaction.verifyActive();
localModel.addStatements(statements);
}
@Override
public void add(List<Statement> statements, String context) {
transaction.verifyActive();
localModel.addStatements(statements, context);
}
@Override
public void remove(List<Statement> statements) {
transaction.verifyActive();
localModel.removeStatements(statements);
}
@Override
public void remove(List<Statement> statements, String context) {
transaction.verifyActive();
localModel.removeStatements(statements, context);
}
@Override
public void remove(Resource subject, Property property, RDFNode object) {
transaction.verifyActive();
localModel.removeStatements(new ArrayList<>(find(subject, property, object)));
}
@Override
public void remove(Resource subject, Property property, RDFNode object, String context) {
transaction.verifyActive();
localModel.removeStatements(new ArrayList<>(find(subject, property, object, context)), context);
}
@Override
public ResultSet executeSelectQuery(String query) throws JenaDriverException {
Objects.requireNonNull(query);
// Since query results are not enhanced with transactional changes, do not require an active transaction
return centralConnector.executeSelectQuery(query);
}
@Override
public boolean executeAskQuery(String query) throws JenaDriverException {
Objects.requireNonNull(query);
// Since query results are not enhanced with transactional changes, do not require an active transaction
return centralConnector.executeAskQuery(query);
}
@Override
public void executeUpdate(String query) throws JenaDriverException {
Objects.requireNonNull(query);
// SPARQL Update queries have their own executor in Jena, so let them transcend the transactional boundaries
centralConnector.executeUpdate(query);
}
@Override
public synchronized void close() {
if (transaction.isActive()) {
rollback();
}
super.close();
}
@Override
public <T> T unwrap(Class<T> cls) {
return centralConnector.unwrap(cls);
}
}