Skip to content

Package: PoolingStorageConnector

PoolingStorageConnector

nameinstructionbranchcomplexitylinemethod
PoolingStorageConnector(StorageConnector)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
addStatements(Collection)
M: 4 C: 10
71%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 4
100%
M: 0 C: 1
100%
begin()
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
close()
M: 0 C: 18
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
commit()
M: 0 C: 49
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 16
100%
M: 0 C: 1
100%
containsStatement(Resource, IRI, Value, boolean, Collection)
M: 8 C: 32
80%
M: 0 C: 3
100%
M: 0 C: 3
100%
M: 3 C: 7
70%
M: 0 C: 1
100%
executeBooleanQuery(String)
M: 0 C: 23
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
executeSelectQuery(String)
M: 0 C: 23
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
executeUpdate(String)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
findStatements(Resource, IRI, Value, boolean)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
findStatements(Resource, IRI, Value, boolean, Collection)
M: 0 C: 34
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
getContexts()
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
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%
removeStatements(Collection)
M: 4 C: 10
71%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 4
100%
M: 0 C: 1
100%
rollback()
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%
static {...}
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
unwrap(Class)
M: 0 C: 14
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
wrapConnection()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
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.sesame.connector;
16:
17: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
18: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
19: import org.eclipse.rdf4j.common.iteration.Iterations;
20: import org.eclipse.rdf4j.model.*;
21: import org.eclipse.rdf4j.query.TupleQueryResult;
22: import org.eclipse.rdf4j.repository.RepositoryConnection;
23: import org.eclipse.rdf4j.repository.RepositoryException;
24:
25: import java.util.Collection;
26: import java.util.Collections;
27: import java.util.List;
28: import java.util.concurrent.locks.Lock;
29: import java.util.concurrent.locks.ReentrantReadWriteLock;
30:
31: public class PoolingStorageConnector extends AbstractConnector {
32:
33: private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock();
34: private static final Lock READ = LOCK.readLock();
35: private static final Lock WRITE = LOCK.writeLock();
36:
37: private final StorageConnector centralConnector;
38:
39: private RepositoryConnection connection;
40: private LocalModel localModel;
41:
42: PoolingStorageConnector(StorageConnector centralConnector) {
43: this.centralConnector = centralConnector;
44: this.open = true;
45: }
46:
47: @Override
48: public TupleQueryResult executeSelectQuery(String query) throws SesameDriverException {
49:• if (transaction.isActive()) {
50: return new ConnectionStatementExecutor(wrapConnection()).executeSelectQuery(query);
51: }
52: READ.lock();
53: try {
54: return centralConnector.executeSelectQuery(query);
55: } finally {
56: READ.unlock();
57: }
58: }
59:
60: private RepositoryConnection wrapConnection() {
61: return new TransactionalRepositoryConnection(connection);
62: }
63:
64: @Override
65: public boolean executeBooleanQuery(String query) throws SesameDriverException {
66:• if (transaction.isActive()) {
67: return new ConnectionStatementExecutor(wrapConnection()).executeBooleanQuery(query);
68: }
69: READ.lock();
70: try {
71: return centralConnector.executeBooleanQuery(query);
72: } finally {
73: READ.unlock();
74: }
75: }
76:
77: @Override
78: public void executeUpdate(String query) throws SesameDriverException {
79: WRITE.lock();
80: try {
81: centralConnector.executeUpdate(query);
82: } finally {
83: WRITE.unlock();
84: }
85: }
86:
87: @Override
88: public List<Resource> getContexts() throws SesameDriverException {
89: READ.lock();
90: try {
91: return centralConnector.getContexts();
92: } finally {
93: READ.unlock();
94: }
95: }
96:
97: @Override
98: public ValueFactory getValueFactory() {
99: // We don't need to lock the central connector, as getting the value
100: // factory does not require communication with the repository
101: return centralConnector.getValueFactory();
102: }
103:
104: @Override
105: public void begin() throws SesameDriverException {
106: super.begin();
107: this.localModel = new LocalModel();
108: this.connection = centralConnector.acquireConnection();
109: }
110:
111: @Override
112: public void commit() throws SesameDriverException {
113: transaction.commit();
114: WRITE.lock();
115: try {
116: centralConnector.begin();
117: centralConnector.removeStatements(localModel.getRemovedStatements());
118: centralConnector.addStatements(localModel.getAddedStatements());
119: centralConnector.commit();
120: transaction.afterCommit();
121: } catch (SesameDriverException e) {
122: transaction.rollback();
123: centralConnector.rollback();
124: transaction.afterRollback();
125: throw e;
126: } finally {
127: WRITE.unlock();
128: centralConnector.releaseConnection(connection);
129: this.localModel = null;
130: }
131: }
132:
133: @Override
134: public void rollback() throws SesameDriverException {
135: transaction.rollback();
136: this.localModel = null;
137: centralConnector.releaseConnection(connection);
138: transaction.afterRollback();
139: }
140:
141: @Override
142: public void close() throws OntoDriverException {
143:• if (open && transaction.isActive()) {
144: this.localModel = null;
145: centralConnector.releaseConnection(connection);
146: }
147: super.close();
148: }
149:
150: @Override
151: public void addStatements(Collection<Statement> statements) {
152: verifyTransactionActive();
153:• assert statements != null;
154: localModel.addStatements(statements);
155: }
156:
157: @Override
158: public void removeStatements(Collection<Statement> statements) {
159: verifyTransactionActive();
160:• assert statements != null;
161: localModel.removeStatements(statements);
162: }
163:
164: @Override
165: public Collection<Statement> findStatements(Resource subject, IRI property, Value value, boolean includeInferred)
166: throws SesameDriverException {
167: return findStatements(subject, property, value, includeInferred, Collections.emptySet());
168: }
169:
170: @Override
171: public Collection<Statement> findStatements(Resource subject, IRI property, Value value,
172: boolean includeInferred, Collection<IRI> contexts)
173: throws SesameDriverException {
174: verifyTransactionActive();
175: try {
176: final Collection<Statement> statements;
177: statements = Iterations.asList(connection
178: .getStatements(subject, property, value, includeInferred, contexts.toArray(new IRI[0])));
179: localModel.enhanceStatements(statements, subject, property, value, contexts);
180: return statements;
181: } catch (RepositoryException e) {
182: rollback();
183: throw new SesameDriverException(e);
184: }
185: }
186:
187: @Override
188: public boolean containsStatement(Resource subject, IRI property, Value value, boolean includeInferred,
189: Collection<IRI> contexts)
190: throws SesameDriverException {
191: verifyTransactionActive();
192: try {
193: final LocalModel.Contains containsLocally = localModel.contains(subject, property, value, contexts);
194:• switch (containsLocally) {
195: case TRUE:
196: return true;
197: case FALSE:
198: return false;
199: default:
200: return connection
201: .hasStatement(subject, property, value, includeInferred, contexts.toArray(new IRI[0]));
202: }
203: } catch (RepositoryException e) {
204: rollback();
205: throw new SesameDriverException(e);
206: }
207: }
208:
209: @Override
210: public <T> T unwrap(Class<T> cls) throws OntoDriverException {
211:• if (cls.isAssignableFrom(this.getClass())) {
212: return cls.cast(this);
213: }
214: return centralConnector.unwrap(cls);
215: }
216: }