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