Skip to content

Package: StorageConnector

StorageConnector

nameinstructionbranchcomplexitylinemethod
StorageConnector(Configuration)
M: 4 C: 14
78%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 6
100%
M: 0 C: 1
100%
acquireConnection()
M: 8 C: 11
58%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 3 C: 3
50%
M: 0 C: 1
100%
addStatements(Collection)
M: 10 C: 14
58%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 5
71%
M: 0 C: 1
100%
begin()
M: 9 C: 11
55%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 3 C: 5
63%
M: 0 C: 1
100%
close()
M: 8 C: 23
74%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 3 C: 7
70%
M: 0 C: 1
100%
commit()
M: 13 C: 21
62%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 3 C: 8
73%
M: 0 C: 1
100%
createInMemoryRepository(Configuration)
M: 0 C: 24
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
createLocalNativeRepositoryConfig(String, Configuration)
M: 5 C: 19
79%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 4
80%
M: 0 C: 1
100%
createLocalRepository(Configuration)
M: 0 C: 20
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
createNativeRepository(Configuration, URI)
M: 22 C: 62
74%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 3 C: 10
77%
M: 0 C: 1
100%
executeBooleanQuery(String)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
executeSelectQuery(String)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
executeUpdate(String)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
findStatements(Resource, URI, Value, boolean, URI[])
M: 27 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
getContexts()
M: 22 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getValueFactory()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
initialize()
M: 39 C: 28
42%
M: 4 C: 2
33%
M: 3 C: 1
25%
M: 6 C: 9
60%
M: 0 C: 1
100%
isFileUri(URI)
M: 0 C: 12
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 1
100%
M: 0 C: 1
100%
isRemoteRepository(URI)
M: 2 C: 25
93%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 4
80%
M: 0 C: 1
100%
releaseConnection(RepositoryConnection)
M: 6 C: 9
60%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 5
71%
M: 0 C: 1
100%
removeStatements(Collection)
M: 10 C: 14
58%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 5
71%
M: 0 C: 1
100%
rollback()
M: 30 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
unwrap(Class)
M: 0 C: 34
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2016 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.sesame.connector;
16:
17: import cz.cvut.kbss.ontodriver.config.Configuration;
18: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
19: import cz.cvut.kbss.ontodriver.sesame.config.SesameConfigParam;
20: import cz.cvut.kbss.ontodriver.sesame.exceptions.RepositoryCreationException;
21: import cz.cvut.kbss.ontodriver.sesame.exceptions.RepositoryNotFoundException;
22: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
23: import info.aduna.iteration.Iterations;
24: import org.openrdf.model.Resource;
25: import org.openrdf.model.Statement;
26: import org.openrdf.model.Value;
27: import org.openrdf.model.ValueFactory;
28: import org.openrdf.query.TupleQueryResult;
29: import org.openrdf.repository.Repository;
30: import org.openrdf.repository.RepositoryConnection;
31: import org.openrdf.repository.RepositoryException;
32: import org.openrdf.repository.RepositoryResult;
33: import org.openrdf.repository.config.RepositoryConfig;
34: import org.openrdf.repository.config.RepositoryConfigException;
35: import org.openrdf.repository.manager.RepositoryManager;
36: import org.openrdf.repository.manager.RepositoryProvider;
37: import org.openrdf.repository.sail.SailRepository;
38: import org.openrdf.repository.sail.config.SailRepositoryConfig;
39: import org.openrdf.sail.config.SailImplConfig;
40: import org.openrdf.sail.inferencer.fc.ForwardChainingRDFSInferencer;
41: import org.openrdf.sail.inferencer.fc.config.ForwardChainingRDFSInferencerConfig;
42: import org.openrdf.sail.memory.MemoryStore;
43: import org.openrdf.sail.nativerdf.config.NativeStoreConfig;
44: import org.slf4j.Logger;
45: import org.slf4j.LoggerFactory;
46:
47: import java.net.URI;
48: import java.util.Collection;
49: import java.util.List;
50:
51: class StorageConnector extends AbstractConnector {
52:
53: private static final String[] KNOWN_REMOTE_SCHEMES = {"http", "https", "ftp"};
54: private static final String LOCAL_NATIVE_REPO = "repositories/";
55: private static final String FILE_SCHEME = "file";
56:
57: private static final Logger LOG = LoggerFactory.getLogger(StorageConnector.class);
58:
59: private final Configuration configuration;
60:
61: private Repository repository;
62: private RepositoryManager manager;
63: private RepositoryConnection connection;
64:
65: StorageConnector(Configuration configuration) throws SesameDriverException {
66:• assert configuration != null;
67:
68: this.configuration = configuration;
69: initialize();
70: this.open = true;
71: }
72:
73: private void initialize() throws SesameDriverException {
74: final URI serverUri = configuration.getStorageProperties().getPhysicalURI();
75: LOG.debug("Initializing connector to repository at {}", serverUri);
76: try {
77: final boolean isRemote = isRemoteRepository(serverUri);
78:• if (isRemote) {
79: this.repository = RepositoryProvider.getRepository(serverUri.toString());
80: } else {
81: this.repository = createLocalRepository(configuration);
82: }
83:• if (repository == null) {
84:• if (isRemote) {
85: throw new RepositoryNotFoundException("Unable to reach repository at "
86: + serverUri);
87: } else {
88: throw new RepositoryCreationException("Unable to create local repository at "
89: + serverUri);
90: }
91: }
92: repository.initialize();
93: } catch (RepositoryException | RepositoryConfigException e) {
94: throw new SesameDriverException("Failed to acquire sesame repository connection.", e);
95: }
96: }
97:
98: private Repository createLocalRepository(Configuration configuration) {
99: final URI localUri = configuration.getStorageProperties().getPhysicalURI();
100:• if (!isFileUri(localUri) && configuration.is(SesameConfigParam.USE_VOLATILE_STORAGE)) {
101: return createInMemoryRepository(configuration);
102: } else {
103: return createNativeRepository(configuration, localUri);
104: }
105: }
106:
107: /**
108: * Creates a local in-memory Sesame repository which is disposed when the VM shuts down.
109: */
110: private Repository createInMemoryRepository(Configuration configuration) {
111: LOG.trace("Creating local in-memory repository.");
112: final MemoryStore ms = new MemoryStore();
113:• if (configuration.is(SesameConfigParam.USE_INFERENCE)) {
114: return new SailRepository(new ForwardChainingRDFSInferencer(ms));
115: } else {
116: return new SailRepository(ms);
117: }
118: }
119:
120: /**
121: * Creates native repository. </p>
122: * <p>
123: * This kind of repository stores data in files and is persistent after the VM shuts down.
124: */
125: private Repository createNativeRepository(Configuration configuration, final URI localUri) {
126: LOG.trace("Creating local native repository at " + localUri);
127: final String[] tmp = localUri.toString().split(LOCAL_NATIVE_REPO);
128:• if (tmp.length != 2) {
129: throw new RepositoryCreationException(
130: "Unsupported local Sesame repository path. Expected file://path/repositories/id but got "
131: + localUri);
132: }
133: String repoId = tmp[1];
134:• if (repoId.charAt(repoId.length() - 1) == '/') {
135: repoId = repoId.substring(0, repoId.length() - 1);
136: }
137: try {
138: this.manager = RepositoryProvider.getRepositoryManagerOfRepository(localUri.toASCIIString());
139: final RepositoryConfig cfg = createLocalNativeRepositoryConfig(repoId, configuration);
140: manager.addRepositoryConfig(cfg);
141: return manager.getRepository(repoId);
142: } catch (RepositoryConfigException | RepositoryException e) {
143: throw new RepositoryCreationException("Unable to create local repository at "
144: + localUri, e);
145: }
146: }
147:
148: private RepositoryConfig createLocalNativeRepositoryConfig(String repoId, Configuration configuration) {
149: SailImplConfig backend = new NativeStoreConfig();
150:• if (configuration.is(SesameConfigParam.USE_INFERENCE)) {
151: backend = new ForwardChainingRDFSInferencerConfig(backend);
152: }
153: final SailRepositoryConfig repoType = new SailRepositoryConfig(backend);
154: return new RepositoryConfig(repoId, repoType);
155: }
156:
157: @Override
158: public void close() throws SesameDriverException {
159:• if (!open) {
160: return;
161: }
162: LOG.debug("Closing connector to repository {}.", configuration.getStorageProperties().getPhysicalURI());
163: try {
164: repository.shutDown();
165:• if (manager != null) {
166: manager.shutDown();
167: }
168: } catch (RepositoryException e) {
169: throw new SesameDriverException(
170: "Exception caught when closing Sesame repository connection.", e);
171: } finally {
172: this.open = false;
173: }
174: }
175:
176: private static boolean isFileUri(URI uri) {
177:• return (uri.getScheme() != null && uri.getScheme().equals(FILE_SCHEME));
178: }
179:
180: private static boolean isRemoteRepository(URI uri) {
181: final String scheme = uri.getScheme();
182:• for (String s : KNOWN_REMOTE_SCHEMES) {
183:• if (s.equals(scheme)) {
184: return true;
185: }
186: }
187: return false;
188: }
189:
190: @Override
191: public TupleQueryResult executeSelectQuery(String query) throws SesameDriverException {
192: RepositoryConnection connection = acquireConnection();
193: return new ConnectionStatementExecutor(connection).executeSelectQuery(query);
194: // The connection is released by the result set once it is closed
195: }
196:
197: RepositoryConnection acquireConnection() throws SesameDriverException {
198: // Workaround for local native storage being reset when multiple drivers access it
199:• if (!repository.isInitialized()) {
200: initialize();
201: }
202: try {
203: LOG.trace("Acquiring repository connection.");
204: return repository.getConnection();
205: } catch (RepositoryException e) {
206: throw new SesameDriverException(e);
207: }
208: }
209:
210: void releaseConnection(RepositoryConnection connection) throws SesameDriverException {
211: try {
212:• if (connection != null) {
213: LOG.trace("Releasing repository connection.");
214: connection.close();
215: }
216: } catch (RepositoryException e) {
217: throw new SesameDriverException(e);
218: }
219: }
220:
221: @Override
222: public boolean executeBooleanQuery(String query) throws SesameDriverException {
223: RepositoryConnection connection = null;
224: try {
225: connection = acquireConnection();
226: return new ConnectionStatementExecutor(connection).executeBooleanQuery(query);
227: } finally {
228: releaseConnection(connection);
229: }
230: }
231:
232: @Override
233: public void executeUpdate(String query) throws SesameDriverException {
234: RepositoryConnection connection = null;
235: try {
236: connection = acquireConnection();
237: new ConnectionStatementExecutor(connection).executeUpdate(query);
238: } finally {
239: releaseConnection(connection);
240: }
241: }
242:
243: @Override
244: public List<Resource> getContexts() throws SesameDriverException {
245: RepositoryConnection connection = null;
246: try {
247: connection = acquireConnection();
248: final RepositoryResult<Resource> res = connection.getContextIDs();
249: return Iterations.asList(res);
250: } catch (RepositoryException e) {
251: throw new SesameDriverException(e);
252: } finally {
253: releaseConnection(connection);
254: }
255: }
256:
257: @Override
258: public ValueFactory getValueFactory() {
259: return repository.getValueFactory();
260: }
261:
262: @Override
263: public void begin() throws SesameDriverException {
264: super.begin();
265: this.connection = acquireConnection();
266: try {
267: connection.begin();
268: } catch (RepositoryException e) {
269: transaction.rollback();
270: throw new SesameDriverException(e);
271: }
272: }
273:
274: @Override
275: public void commit() throws SesameDriverException {
276:• assert connection != null;
277:
278: transaction.commit();
279: try {
280: connection.commit();
281: connection.close();
282: this.connection = null;
283: transaction.afterCommit();
284: } catch (RepositoryException e) {
285: transaction.rollback();
286: throw new SesameDriverException(e);
287: }
288: }
289:
290: @Override
291: public void rollback() throws SesameDriverException {
292:• assert connection != null;
293: transaction.rollback();
294: try {
295: connection.rollback();
296: connection.close();
297: this.connection = null;
298: } catch (RepositoryException e) {
299: throw new SesameDriverException(e);
300: } finally {
301: transaction.afterRollback();
302: }
303: }
304:
305: @Override
306: public void addStatements(Collection<Statement> statements) throws SesameDriverException {
307: verifyTransactionActive();
308:• assert connection != null;
309:
310: try {
311: connection.add(statements);
312: } catch (RepositoryException e) {
313: throw new SesameDriverException(e);
314: }
315: }
316:
317: @Override
318: public void removeStatements(Collection<Statement> statements) throws SesameDriverException {
319: verifyTransactionActive();
320:• assert connection != null;
321:
322: try {
323: connection.remove(statements);
324: } catch (RepositoryException e) {
325: throw new SesameDriverException(e);
326: }
327: }
328:
329: @Override
330: public Collection<Statement> findStatements(Resource subject, org.openrdf.model.URI property,
331: Value value, boolean includeInferred, org.openrdf.model.URI... contexts)
332: throws SesameDriverException {
333: RepositoryConnection connection = null;
334: try {
335: connection = acquireConnection();
336: final RepositoryResult<Statement> m = connection
337: .getStatements(subject, property, null, includeInferred, contexts);
338: return Iterations.asList(m);
339: } catch (RepositoryException e) {
340: throw new SesameDriverException(e);
341: } finally {
342: releaseConnection(connection);
343: }
344: }
345:
346: @Override
347: public <T> T unwrap(Class<T> cls) throws OntoDriverException {
348:• if (cls.isAssignableFrom(getClass())) {
349: return cls.cast(this);
350: }
351:• if (cls.isAssignableFrom(repository.getClass())) {
352: return cls.cast(repository);
353: }
354: throw new SesameDriverException("No instance of class " + cls + " found.");
355: }
356: }