-
Notifications
You must be signed in to change notification settings - Fork 14
/
ChangeTrackingStorageConnector.java
158 lines (134 loc) · 5.23 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
package cz.cvut.kbss.ontodriver.jena.connector;
import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
import org.apache.jena.query.Dataset;
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 LocalModel localModel;
ChangeTrackingStorageConnector(AbstractStorageConnector centralConnector) {
this.centralConnector = centralConnector;
}
@Override
public void begin() {
transaction.begin();
this.localModel = new LocalModel();
}
@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) {
// TODO
}
@Override
public void remove(Resource subject, Property property, RDFNode object, String context) {
// TODO
}
@Override
public synchronized void close() {
if (transaction.isActive()) {
rollback();
}
super.close();
}
@Override
public <T> T unwrap(Class<T> cls) {
return centralConnector.unwrap(cls);
}
}