Skip to content

Package: SnapshotStorageConnector

SnapshotStorageConnector

nameinstructionbranchcomplexitylinemethod
SnapshotStorageConnector(AbstractStorageConnector)
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%
add(List, String)
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%
applyAdditions()
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
applyRemovals()
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
applyTransactionUpdateQueries()
M: 0 C: 18
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
begin()
M: 0 C: 31
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
cleanup()
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%
commit()
M: 0 C: 23
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
contains(Resource, Property, RDFNode, String)
M: 0 C: 21
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
executeAskQuery(Query, Statement.StatementOntology)
M: 0 C: 19
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
executeSelectQuery(Query, Statement.StatementOntology)
M: 0 C: 19
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
executeUpdate(String, Statement.StatementOntology)
M: 16 C: 25
61%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 2 C: 8
80%
M: 0 C: 1
100%
find(Resource, Property, RDFNode, String)
M: 0 C: 23
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getContexts()
M: 0 C: 20
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
lambda$applyAdditions$1(Dataset, 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$applyRemovals$0(Dataset, 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%
remove(List, String)
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%
remove(Resource, Property, RDFNode, String)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
rollback()
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
snapshotCentralDataset()
M: 0 C: 18
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2020 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.jena.config.JenaConfigParam;
19: import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
20: import cz.cvut.kbss.ontodriver.jena.query.AbstractResultSet;
21: import org.apache.jena.query.Dataset;
22: import org.apache.jena.query.Query;
23: import org.apache.jena.rdf.model.Property;
24: import org.apache.jena.rdf.model.RDFNode;
25: import org.apache.jena.rdf.model.Resource;
26: import org.apache.jena.rdf.model.Statement;
27: import org.apache.jena.update.UpdateAction;
28:
29: import java.util.ArrayList;
30: import java.util.Iterator;
31: import java.util.List;
32:
33: /**
34: * This connector implements the {@link cz.cvut.kbss.ontodriver.jena.config.JenaOntoDriverProperties#SNAPSHOT}-based transactional strategy.
35: * <p>
36: * It is also used when inference is required from the driver.
37: */
38: public class SnapshotStorageConnector extends SharedStorageConnector {
39:
40: final AbstractStorageConnector centralConnector;
41:
42: private LocalModel transactionalChanges;
43: private List<String> transactionalUpdates;
44:
45: SnapshotStorageConnector(AbstractStorageConnector centralConnector) {
46: super(centralConnector.configuration);
47: this.centralConnector = centralConnector;
48: }
49:
50: @Override
51: public void begin() {
52: ensureOpen();
53:• if (transaction.isActive()) {
54: throw new IllegalStateException("Transaction is already active.");
55: }
56: transaction.begin();
57: snapshotCentralDataset();
58: this.transactionalUpdates = new ArrayList<>();
59: this.transactionalChanges = new LocalModel(configuration.is(JenaConfigParam.TREAT_DEFAULT_GRAPH_AS_UNION));
60: }
61:
62: void snapshotCentralDataset() {
63: final SnapshotStorage s = new SnapshotStorage(configuration);
64: s.initialize();
65: s.addCentralData(centralConnector.getStorage().getDataset());
66: this.storage = s;
67: }
68:
69: @Override
70: public void commit() throws JenaDriverException {
71: ensureTransactionalState();
72: transaction.commit();
73: try {
74: centralConnector.begin();
75: applyRemovals();
76: applyAdditions();
77: applyTransactionUpdateQueries();
78: centralConnector.commit();
79: } finally {
80: cleanup();
81: transaction.afterCommit();
82: }
83:
84: }
85:
86: private void applyRemovals() {
87: final Dataset removed = transactionalChanges.getRemoved();
88: centralConnector.remove(removed.getDefaultModel().listStatements().toList(), null);
89: removed.listNames()
90: .forEachRemaining(n -> centralConnector.remove(removed.getNamedModel(n).listStatements().toList(), n));
91: }
92:
93: private void applyAdditions() {
94: final Dataset added = transactionalChanges.getAdded();
95: centralConnector.add(added.getDefaultModel().listStatements().toList(), null);
96: added.listNames()
97: .forEachRemaining(n -> centralConnector.add(added.getNamedModel(n).listStatements().toList(), n));
98: }
99:
100: private void applyTransactionUpdateQueries() throws JenaDriverException {
101:• for (String query : transactionalUpdates) {
102: centralConnector.executeUpdate(query, StatementOntology.CENTRAL);
103: }
104: }
105:
106: @Override
107: public void rollback() {
108: ensureTransactionalState();
109: transaction.rollback();
110: cleanup();
111: transaction.afterRollback();
112: }
113:
114: private void cleanup() {
115: this.storage = null;
116: this.transactionalChanges = null;
117: this.transactionalUpdates = null;
118: }
119:
120: @Override
121: public List<Statement> find(Resource subject, Property property, RDFNode value, String context) {
122: ensureTransactionalState();
123:• if (context != null) {
124: return storage.getNamedGraph(context).listStatements(subject, property, value).toList();
125: } else {
126: return storage.getDefaultGraph().listStatements(subject, property, value).toList();
127: }
128: }
129:
130: @Override
131: public boolean contains(Resource subject, Property property, RDFNode value, String context) {
132: ensureTransactionalState();
133:• if (context != null) {
134: return storage.getNamedGraph(context).contains(subject, property, value);
135: } else {
136: return storage.getDefaultGraph().contains(subject, property, value);
137: }
138: }
139:
140: @Override
141: public List<String> getContexts() {
142: ensureTransactionalState();
143: final Iterator<String> contexts = storage.getDataset().listNames();
144: final List<String> result = new ArrayList<>();
145: contexts.forEachRemaining(result::add);
146: return result;
147: }
148:
149: @Override
150: public void add(List<Statement> statements, String context) {
151: ensureTransactionalState();
152: storage.add(statements, context);
153: transactionalChanges.addStatements(statements, context);
154: }
155:
156: @Override
157: public void remove(List<Statement> statements, String context) {
158: ensureTransactionalState();
159: storage.remove(statements, context);
160: transactionalChanges.removeStatements(statements, context);
161: }
162:
163: @Override
164: public void remove(Resource subject, Property property, RDFNode object, String context) {
165: ensureTransactionalState();
166: final List<Statement> toRemove = find(subject, property, object, context);
167: remove(toRemove, context);
168: }
169:
170: @Override
171: public AbstractResultSet executeSelectQuery(Query query, StatementOntology target) throws JenaDriverException {
172: ensureOpen();
173:• if (target == StatementOntology.TRANSACTIONAL) {
174: transaction.verifyActive();
175: return super.executeSelectQuery(query, target);
176: } else {
177: return centralConnector.executeSelectQuery(query, target);
178: }
179: }
180:
181: @Override
182: public AbstractResultSet executeAskQuery(Query query, StatementOntology target) throws JenaDriverException {
183: ensureOpen();
184:• if (target == StatementOntology.TRANSACTIONAL) {
185: transaction.verifyActive();
186: return super.executeAskQuery(query, target);
187: } else {
188: return centralConnector.executeAskQuery(query, target);
189: }
190: }
191:
192: @Override
193: public void executeUpdate(String query, StatementOntology target) throws JenaDriverException {
194: ensureOpen();
195:• if (target == StatementOntology.TRANSACTIONAL) {
196: transaction.verifyActive();
197: try {
198: UpdateAction.parseExecute(query, storage.getDataset());
199: transactionalUpdates.add(query);
200: } catch (RuntimeException e) {
201: throw new JenaDriverException("Execution of update " + query + " failed.", e);
202: }
203: } else {
204: centralConnector.executeUpdate(query, target);
205: }
206: }
207: }