Skip to content

Package: SharedStorageConnector

SharedStorageConnector

nameinstructionbranchcomplexitylinemethod
SharedStorageConnector(DriverConfiguration)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
add(List, String)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
begin()
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
close()
M: 1 C: 12
92%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 5
83%
M: 0 C: 1
100%
commit()
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
contains(Resource, Property, RDFNode, Collection)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
ensureTransactionalState()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
executeAskQuery(Query, Statement.StatementOntology)
M: 0 C: 22
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
executeSelectQuery(Query, Statement.StatementOntology)
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
executeUpdate(String, Statement.StatementOntology)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
find(Resource, Property, RDFNode, Collection)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getContexts()
M: 0 C: 23
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
initialize()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$contains$3(Collection, Resource, Property, RDFNode)
M: 0 C: 22
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
lambda$find$1(Collection, Resource, Property, RDFNode)
M: 0 C: 29
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
lambda$getContexts$4()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$null$0(Resource, Property, RDFNode, String)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$null$2(Resource, Property, RDFNode, String)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
queryFailed(Object, RuntimeException)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
reloadStorage()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
remove(List, String)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
remove(Resource, Property, RDFNode, String)
M: 0 C: 29
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
rollback()
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
setDataset(Dataset)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.jena.connector;
16:
17: import cz.cvut.kbss.ontodriver.Statement.StatementOntology;
18: import cz.cvut.kbss.ontodriver.config.DriverConfiguration;
19: import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
20: import cz.cvut.kbss.ontodriver.jena.query.AbstractResultSet;
21: import cz.cvut.kbss.ontodriver.jena.query.AskResultSet;
22: import cz.cvut.kbss.ontodriver.jena.query.SelectResultSet;
23: import org.apache.jena.query.Dataset;
24: import org.apache.jena.query.Query;
25: import org.apache.jena.query.QueryExecution;
26: import org.apache.jena.query.ReadWrite;
27: import org.apache.jena.rdf.model.Property;
28: import org.apache.jena.rdf.model.RDFNode;
29: import org.apache.jena.rdf.model.Resource;
30: import org.apache.jena.rdf.model.Statement;
31: import org.apache.jena.system.Txn;
32: import org.apache.jena.update.UpdateAction;
33:
34: import java.util.ArrayList;
35: import java.util.Collection;
36: import java.util.Iterator;
37: import java.util.List;
38: import java.util.stream.Collectors;
39:
40: /**
41: * Main storage connector using the {@link cz.cvut.kbss.ontodriver.jena.config.JenaOntoDriverProperties#READ_COMMITTED}
42: * connector strategy.
43: * <p>
44: * Adding statements to it actually adds them to the repository.
45: * <p>
46: * Note on transactions:
47: * <p>
48: * Starting a transaction on this connector also starts a write transaction on the underlying dataset. Commit then
49: * commits the transaction. Therefore, these transactions should be short. Reading can happen in parallel (as per Jena
50: * documentation).
51: */
52: public class SharedStorageConnector extends AbstractStorageConnector {
53:
54: SharedStorageConnector(DriverConfiguration configuration) {
55: super(configuration);
56: }
57:
58: @Override
59: void initialize() {
60: this.storage = Storage.create(configuration);
61: }
62:
63: @Override
64: public synchronized void begin() {
65: ensureOpen();
66: transaction.begin();
67: storage.begin(ReadWrite.WRITE);
68: }
69:
70: @Override
71: public synchronized void commit() throws JenaDriverException {
72: ensureTransactionalState();
73: transaction.commit();
74: storage.writeChanges();
75: storage.commit();
76: transaction.afterCommit();
77: }
78:
79: void ensureTransactionalState() {
80: ensureOpen();
81: transaction.verifyActive();
82: }
83:
84: @Override
85: public void rollback() {
86: ensureOpen();
87: transaction.rollback();
88: storage.rollback();
89: transaction.afterRollback();
90: }
91:
92: @Override
93: public Collection<Statement> find(Resource subject, Property property, RDFNode value, Collection<String> contexts) {
94: ensureOpen();
95: return Txn.calculateRead(storage.getTransactional(), () -> {
96: final List<Statement> result;
97:• if (contexts.isEmpty()) {
98: result = storage.getDefaultGraph().listStatements(subject, property, value).toList();
99: } else {
100: result = contexts.stream()
101: .map(c -> storage.getNamedGraph(c).listStatements(subject, property, value).toList())
102: .flatMap(Collection::stream).collect(Collectors.toList());
103: }
104: return result;
105: });
106: }
107:
108: @Override
109: public boolean contains(Resource subject, Property property, RDFNode value, Collection<String> contexts) {
110: ensureOpen();
111: return Txn.calculateRead(storage.getTransactional(), () -> {
112:• if (contexts.isEmpty()) {
113: return storage.getDefaultGraph().contains(subject, property, value);
114: } else {
115: return contexts.stream().anyMatch(c -> storage.getNamedGraph(c).contains(subject, property, value));
116: }
117: });
118: }
119:
120: @Override
121: public List<String> getContexts() {
122: ensureOpen();
123: final Iterator<String> it = Txn
124: .calculateRead(storage.getTransactional(), () -> storage.getDataset().listNames());
125: final List<String> contexts = new ArrayList<>();
126: it.forEachRemaining(contexts::add);
127: return contexts;
128: }
129:
130: @Override
131: public void add(List<Statement> statements, String context) {
132: ensureTransactionalState();
133: storage.add(statements, context);
134: }
135:
136: @Override
137: public void remove(List<Statement> statements, String context) {
138: ensureTransactionalState();
139: storage.remove(statements, context);
140: }
141:
142: @Override
143: public void remove(Resource subject, Property property, RDFNode object, String context) {
144: ensureTransactionalState();
145:• if (context != null) {
146: storage.remove(storage.getNamedGraph(context).listStatements(subject, property, object), context);
147: } else {
148: storage.remove(storage.getDefaultGraph().listStatements(subject, property, object), null);
149: }
150: }
151:
152: @Override
153: public AbstractResultSet executeSelectQuery(Query query, StatementOntology target) throws JenaDriverException {
154: ensureOpen();
155: try {
156: QueryExecution exec = storage.prepareQuery(query);
157: final org.apache.jena.query.ResultSet rs = exec.execSelect();
158: // The QueryExecution is closed by the SelectResultSet (so that it has access to the results)
159: return new SelectResultSet(exec, rs);
160: } catch (RuntimeException e) {
161: throw queryFailed(query, e);
162: }
163: }
164:
165: private static JenaDriverException queryFailed(Object query, RuntimeException e) {
166: return new JenaDriverException("Execution of query " + query + " failed.", e);
167: }
168:
169: @Override
170: public AbstractResultSet executeAskQuery(Query query, StatementOntology target) throws JenaDriverException {
171: ensureOpen();
172: try (final QueryExecution exec = storage.prepareQuery(query)) {
173: return new AskResultSet(exec.execAsk());
174: } catch (RuntimeException e) {
175: throw queryFailed(query, e);
176: }
177: }
178:
179: @Override
180: public void executeUpdate(String query, StatementOntology target) throws JenaDriverException {
181: ensureOpen();
182: try {
183: UpdateAction.parseExecute(query, storage.getDataset());
184: } catch (RuntimeException e) {
185: throw queryFailed(query, e);
186: }
187: }
188:
189: @Override
190: public synchronized void close() {
191:• if (!isOpen()) {
192: return;
193: }
194:• if (storage != null) {
195: storage.close();
196: }
197: super.close();
198: }
199:
200: /**
201: * Reloads data from the underlying storage (if possible).
202: * <p>
203: * Note that this applies only to RDF file-based storage access, other storage do not support reloading.
204: */
205: public synchronized void reloadStorage() {
206: ensureOpen();
207: storage.reload();
208: }
209:
210: /**
211: * Sets new dataset on the underlying storage.
212: * <p>
213: * Note that this is supported only for in-memory storage.
214: *
215: * @param dataset The dataset to use
216: */
217: public synchronized void setDataset(Dataset dataset) {
218: ensureOpen();
219: storage.setDataset(dataset);
220: }
221: }