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