-
Notifications
You must be signed in to change notification settings - Fork 14
/
SharedStorageConnector.java
186 lines (165 loc) · 6.18 KB
/
SharedStorageConnector.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
package cz.cvut.kbss.ontodriver.jena.connector;
import cz.cvut.kbss.ontodriver.Statement.StatementOntology;
import cz.cvut.kbss.ontodriver.config.DriverConfiguration;
import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
import cz.cvut.kbss.ontodriver.jena.query.AbstractResultSet;
import cz.cvut.kbss.ontodriver.jena.query.AskResultSet;
import cz.cvut.kbss.ontodriver.jena.query.SelectResultSet;
import org.apache.jena.query.*;
import org.apache.jena.rdf.model.*;
import org.apache.jena.system.Txn;
import org.apache.jena.update.UpdateAction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/**
* Main storage connector using the {@link cz.cvut.kbss.ontodriver.jena.config.JenaOntoDriverProperties#READ_COMMITTED}
* connector strategy.
* <p>
* Adding statements to it actually adds them to the repository.
* <p>
* Note on transactions:
* <p>
* Starting a transaction on this connector also starts a write transaction on the underlying dataset. Commit then
* commits the transaction. Therefore, these transactions should be short. Reading can happen in parallel (as per Jena
* documentation).
*/
public class SharedStorageConnector extends AbstractStorageConnector {
SharedStorageConnector(DriverConfiguration configuration) {
super(configuration);
}
@Override
void initialize() {
this.storage = Storage.create(configuration);
}
@Override
public synchronized void begin() {
ensureOpen();
transaction.begin();
storage.begin(ReadWrite.WRITE);
}
@Override
public synchronized void commit() throws JenaDriverException {
ensureTransactionalState();
transaction.commit();
storage.writeChanges();
storage.commit();
transaction.afterCommit();
}
void ensureTransactionalState() {
ensureOpen();
transaction.verifyActive();
}
@Override
public void rollback() {
ensureOpen();
transaction.rollback();
storage.rollback();
transaction.afterRollback();
}
@Override
public Collection<Statement> find(Resource subject, Property property, RDFNode value, String context) {
ensureOpen();
return Txn.calculateRead(storage.getDataset(), () -> {
final Model target = context != null ? storage.getNamedGraph(context) : storage.getDefaultGraph();
final StmtIterator it = target.listStatements(subject, property, value);
return it.toList();
});
}
@Override
public boolean contains(Resource subject, Property property, RDFNode value, String context) {
ensureOpen();
return Txn.calculateRead(storage.getDataset(), () -> {
final Model target = context != null ? storage.getNamedGraph(context) : storage.getDefaultGraph();
return target.contains(subject, property, value);
});
}
@Override
public List<String> getContexts() {
ensureOpen();
final Iterator<String> it = Txn.calculateRead(storage.getDataset(), () -> storage.getDataset().listNames());
final List<String> contexts = new ArrayList<>();
it.forEachRemaining(contexts::add);
return contexts;
}
@Override
public void add(List<Statement> statements, String context) {
ensureTransactionalState();
storage.add(statements, context);
}
@Override
public void remove(List<Statement> statements, String context) {
ensureTransactionalState();
storage.remove(statements, context);
}
@Override
public void remove(Resource subject, Property property, RDFNode object, String context) {
ensureTransactionalState();
if (context != null) {
storage.remove(storage.getNamedGraph(context).listStatements(subject, property, object), context);
} else {
storage.remove(storage.getDefaultGraph().listStatements(subject, property, object), null);
}
}
@Override
public AbstractResultSet executeSelectQuery(Query query, StatementOntology target) throws JenaDriverException {
ensureOpen();
try {
QueryExecution exec = QueryExecutionFactory.create(query, storage.getDataset());
final org.apache.jena.query.ResultSet rs = exec.execSelect();
// The QueryExecution is closed by the SelectResultSet (so that it has access to the results)
return new SelectResultSet(exec, rs);
} catch (RuntimeException e) {
throw new JenaDriverException("Execution of query " + query + " failed.", e);
}
}
@Override
public AbstractResultSet executeAskQuery(Query query, StatementOntology target) throws JenaDriverException {
ensureOpen();
try (final QueryExecution exec = QueryExecutionFactory.create(query, storage.getDataset())) {
return new AskResultSet(exec.execAsk());
} catch (RuntimeException e) {
throw new JenaDriverException("Execution of query " + query + " failed.", e);
}
}
@Override
public void executeUpdate(String query, StatementOntology target) throws JenaDriverException {
ensureOpen();
try {
UpdateAction.parseExecute(query, storage.getDataset());
} catch (RuntimeException e) {
throw new JenaDriverException("Execution of update " + query + " failed.", e);
}
}
@Override
public synchronized void close() {
if (!isOpen()) {
return;
}
if (storage != null) {
storage.close();
}
super.close();
}
/**
* Reloads data from the underlying storage (if possible).
* <p>
* Note that this applies only to RDF file-based storage access, other storage do not support reloading.
*/
public synchronized void reloadStorage() {
ensureOpen();
storage.reload();
}
/**
* Sets new dataset on the underlying storage.
* <p>
* Note that this is supported only for in-memory storage.
*
* @param dataset The dataset to use
*/
public synchronized void setDataset(Dataset dataset) {
ensureOpen();
storage.setDataset(dataset);
}
}