Skip to content

Package: ChangeTrackingStorageConnector

ChangeTrackingStorageConnector

nameinstructionbranchcomplexitylinemethod
ChangeTrackingStorageConnector(AbstractStorageConnector)
M: 0 C: 21
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 5
100%
M: 0 C: 1
100%
add(List, String)
M: 0 C: 9
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: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
close()
M: 0 C: 9
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
commit()
M: 0 C: 32
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
contains(Resource, Property, RDFNode, Collection)
M: 0 C: 29
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
executeAskQuery(Query, Statement.StatementOntology)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
executeSelectQuery(Query, Statement.StatementOntology)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
executeUpdate(String, Statement.StatementOntology)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
find(Resource, Property, RDFNode, Collection)
M: 0 C: 20
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
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: 5
100%
M: 0 C: 1
100%
lambda$mergeAddedStatements$1(Dataset, String)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
lambda$mergeRemovedStatements$0(Dataset, String)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
mergeAddedStatements()
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
mergeRemovedStatements()
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
remove(List, String)
M: 0 C: 9
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: 22
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
rollback()
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%
unwrap(Class)
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%

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.*;
24:
25: import java.util.*;
26:
27: /**
28: * This connector tracks transactional changes and writes them on commit to the {@link SharedStorageConnector}.
29: */
30: public class ChangeTrackingStorageConnector extends AbstractStorageConnector {
31:
32: private final AbstractStorageConnector centralConnector;
33:
34: private final boolean useDefaultAsUnion;
35:
36: private LocalModel localModel;
37:
38: ChangeTrackingStorageConnector(AbstractStorageConnector centralConnector) {
39: super(centralConnector.configuration);
40: this.centralConnector = centralConnector;
41:• this.useDefaultAsUnion =
42:• configuration != null && configuration.is(JenaConfigParam.TREAT_DEFAULT_GRAPH_AS_UNION);
43: }
44:
45: @Override
46: public void begin() {
47: transaction.begin();
48: this.localModel = new LocalModel(useDefaultAsUnion);
49: }
50:
51: @Override
52: public void commit() throws JenaDriverException {
53: transaction.commit();
54: try {
55: centralConnector.begin();
56: mergeRemovedStatements();
57: mergeAddedStatements();
58: centralConnector.commit();
59: transaction.afterCommit();
60: } catch (JenaDriverException e) {
61: transaction.rollback();
62: centralConnector.rollback();
63: transaction.afterRollback();
64: throw e;
65: } finally {
66: this.localModel = null;
67: }
68: }
69:
70: private void mergeRemovedStatements() {
71: final Dataset removed = localModel.getRemoved();
72: centralConnector.remove(removed.getDefaultModel().listStatements().toList(), null);
73: removed.listNames().forEachRemaining(context -> {
74: final Model model = removed.getNamedModel(context);
75: centralConnector.remove(model.listStatements().toList(), context);
76: });
77: }
78:
79: private void mergeAddedStatements() {
80: final Dataset added = localModel.getAdded();
81: centralConnector.add(added.getDefaultModel().listStatements().toList(), null);
82: added.listNames().forEachRemaining(context -> {
83: final Model model = added.getNamedModel(context);
84: centralConnector.add(model.listStatements().toList(), context);
85: });
86: }
87:
88: @Override
89: public void rollback() {
90: transaction.rollback();
91: this.localModel = null;
92: transaction.afterRollback();
93: }
94:
95: @Override
96: public Collection<Statement> find(Resource subject, Property property, RDFNode value, Collection<String> contexts) {
97: transaction.verifyActive();
98: final Collection<Statement> existing = centralConnector.find(subject, property, value, contexts);
99: return localModel.enhanceStatements(existing, subject, property, value, contexts);
100: }
101:
102: @Override
103: public boolean contains(Resource subject, Property property, RDFNode value, Collection<String> contexts) {
104: transaction.verifyActive();
105: final LocalModel.Containment localStatus = localModel.contains(subject, property, value, contexts);
106:• return localStatus == LocalModel.Containment.ADDED ||
107: localStatus == LocalModel.Containment.UNKNOWN &&
108:• centralConnector.contains(subject, property, value, contexts);
109: }
110:
111: @Override
112: public List<String> getContexts() {
113: transaction.verifyActive();
114: final List<String> centralContexts = centralConnector.getContexts();
115: final Set<String> set = new LinkedHashSet<>(centralContexts);
116: set.addAll(localModel.getContexts());
117: return new ArrayList<>(set);
118: }
119:
120: @Override
121: public void add(List<Statement> statements, String context) {
122: transaction.verifyActive();
123: localModel.addStatements(statements, context);
124: }
125:
126: @Override
127: public void remove(List<Statement> statements, String context) {
128: transaction.verifyActive();
129: localModel.removeStatements(statements, context);
130: }
131:
132: @Override
133: public void remove(Resource subject, Property property, RDFNode object, String context) {
134: transaction.verifyActive();
135:• localModel.removeStatements(new ArrayList<>(find(subject, property, object, context != null ? Collections.singleton(context) : Collections.emptySet())), context);
136: }
137:
138: @Override
139: public AbstractResultSet executeSelectQuery(Query query, StatementOntology target) throws JenaDriverException {
140: Objects.requireNonNull(query);
141: // Since query results are not enhanced with transactional changes, do not require an active transaction
142: return centralConnector.executeSelectQuery(query, target);
143: }
144:
145: @Override
146: public AbstractResultSet executeAskQuery(Query query, StatementOntology target) throws JenaDriverException {
147: Objects.requireNonNull(query);
148: // Since query results are not enhanced with transactional changes, do not require an active transaction
149: return centralConnector.executeAskQuery(query, target);
150: }
151:
152: @Override
153: public void executeUpdate(String query, StatementOntology target) throws JenaDriverException {
154: Objects.requireNonNull(query);
155: // SPARQL Update queries have their own executor in Jena, so let them transcend the transactional boundaries
156: centralConnector.executeUpdate(query, target);
157: }
158:
159: @Override
160: public synchronized void close() {
161:• if (transaction.isActive()) {
162: rollback();
163: }
164: super.close();
165: }
166:
167: @Override
168: public <T> T unwrap(Class<T> cls) {
169: return centralConnector.unwrap(cls);
170: }
171: }