Skip to content

Method: lambda$removePropertyValues$3(SubjectPredicateContext)

1: /*
2: * JOPA
3: * Copyright (C) 2023 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.rdf4j.connector;
19:
20: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
21: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
22: import cz.cvut.kbss.ontodriver.rdf4j.query.QuerySpecification;
23: import cz.cvut.kbss.ontodriver.rdf4j.util.ThrowingFunction;
24: import org.eclipse.rdf4j.common.transaction.IsolationLevel;
25: import org.eclipse.rdf4j.model.IRI;
26: import org.eclipse.rdf4j.model.Resource;
27: import org.eclipse.rdf4j.model.Statement;
28: import org.eclipse.rdf4j.model.Value;
29: import org.eclipse.rdf4j.model.ValueFactory;
30: import org.eclipse.rdf4j.query.TupleQueryResult;
31: import org.eclipse.rdf4j.repository.RepositoryConnection;
32: import org.eclipse.rdf4j.repository.RepositoryException;
33: import org.slf4j.Logger;
34: import org.slf4j.LoggerFactory;
35:
36: import java.util.Collection;
37: import java.util.Collections;
38: import java.util.List;
39: import java.util.Set;
40: import java.util.stream.Collectors;
41:
42: public class StorageConnection implements RepoConnection {
43:
44: private static final Logger LOG = LoggerFactory.getLogger(StorageConnection.class);
45:
46: private boolean open;
47:
48: final StorageConnector storageConnector;
49: private final IsolationLevel isolationLevel;
50: private RepositoryConnection connection;
51:
52: public StorageConnection(StorageConnector storageConnector, IsolationLevel isolationLevel) {
53: this.storageConnector = storageConnector;
54: this.isolationLevel = isolationLevel;
55: this.open = true;
56: }
57:
58: @Override
59: public boolean isOpen() {
60: return open;
61: }
62:
63: @Override
64: public void close() throws Rdf4jDriverException {
65: if (!open) {
66: return;
67: }
68: LOG.debug("Closing repository connector.");
69: try {
70: if (connection != null) {
71: if (connection.isActive()) {
72: connection.rollback();
73: }
74: connection.close();
75: }
76: } catch (RepositoryException e) {
77: throw new Rdf4jDriverException("Exception caught when closing RDF4J repository connection.", e);
78: } finally {
79: this.open = false;
80: }
81: }
82:
83: @Override
84: public TupleQueryResult executeSelectQuery(QuerySpecification query) throws Rdf4jDriverException {
85: // Always create a separate connection, it is released by the result set once it is closed
86: return new ConnectionStatementExecutor(storageConnector.acquireConnection()).executeSelectQuery(query);
87: }
88:
89: @Override
90: public boolean executeBooleanQuery(QuerySpecification query) throws Rdf4jDriverException {
91: return withConnection((conn) -> new ConnectionStatementExecutor(conn).executeBooleanQuery(query));
92: }
93:
94: private <R> R withConnection(ThrowingFunction<RepositoryConnection, R> call) throws Rdf4jDriverException {
95: if (connection != null) {
96: return call.apply(connection);
97: } else {
98: try (final RepositoryConnection conn = storageConnector.acquireConnection()) {
99: return call.apply(conn);
100: }
101: }
102: }
103:
104: @Override
105: public void executeUpdate(QuerySpecification query) throws Rdf4jDriverException {
106: if (connection != null) {
107: new ConnectionStatementExecutor(connection).executeUpdate(query);
108: } else {
109: try (final RepositoryConnection conn = storageConnector.acquireConnection()) {
110: new ConnectionStatementExecutor(conn).executeUpdate(query);
111: }
112: }
113: }
114:
115: @Override
116: public List<Resource> getContexts() throws Rdf4jDriverException {
117: return withConnection(conn -> {
118: try {
119: return connection.getContextIDs().stream().collect(Collectors.toList());
120: } catch (RepositoryException e) {
121: throw new Rdf4jDriverException(e);
122: }
123: });
124: }
125:
126: @Override
127: public ValueFactory getValueFactory() {
128: return storageConnector.getValueFactory();
129: }
130:
131: @Override
132: public void begin() throws Rdf4jDriverException {
133: this.connection = storageConnector.acquireConnection();
134: try {
135: connection.begin(isolationLevel);
136: } catch (RepositoryException e) {
137: throw new Rdf4jDriverException(e);
138: }
139: }
140:
141: @Override
142: public void commit() throws Rdf4jDriverException {
143: assert connection != null;
144:
145: try {
146: connection.commit();
147: connection.close();
148: this.connection = null;
149: } catch (RepositoryException e) {
150: throw new Rdf4jDriverException(e);
151: }
152: }
153:
154: @Override
155: public void rollback() throws Rdf4jDriverException {
156: assert connection != null;
157: try {
158: connection.rollback();
159: connection.close();
160: this.connection = null;
161: } catch (RepositoryException e) {
162: throw new Rdf4jDriverException(e);
163: }
164: }
165:
166: @Override
167: public void addStatements(Collection<Statement> statements) throws Rdf4jDriverException {
168: verifyTransactionActive();
169: assert connection != null;
170:
171: try {
172: connection.add(statements);
173: } catch (RepositoryException e) {
174: throw new Rdf4jDriverException(e);
175: }
176: }
177:
178: protected void verifyTransactionActive() {
179: if (connection == null || !connection.isActive()) {
180: throw new IllegalStateException();
181: }
182: }
183:
184: @Override
185: public void removeStatements(Collection<Statement> statements) throws Rdf4jDriverException {
186: verifyTransactionActive();
187: assert connection != null;
188:
189: try {
190: connection.remove(statements);
191: } catch (RepositoryException e) {
192: throw new Rdf4jDriverException(e);
193: }
194: }
195:
196: @Override
197: public void removePropertyValues(Collection<SubjectPredicateContext> spc) throws Rdf4jDriverException {
198: verifyTransactionActive();
199: assert connection != null;
200:
201: try {
202: spc.forEach(spcItem -> connection.remove(spcItem.getSubject(), spcItem.getPredicate(), null, spcItem.getContexts()
203: .toArray(Resource[]::new)));
204: } catch (RepositoryException e) {
205: throw new Rdf4jDriverException(e);
206: }
207: }
208:
209: @Override
210: public Collection<Statement> findStatements(Resource subject, IRI property, Value value, boolean includeInferred)
211: throws Rdf4jDriverException {
212: return findStatements(subject, property, value, includeInferred, Collections.emptySet());
213: }
214:
215: @Override
216: public Collection<Statement> findStatements(Resource subject, org.eclipse.rdf4j.model.IRI property,
217: Value value, boolean includeInferred, Set<IRI> context)
218: throws Rdf4jDriverException {
219: return withConnection(conn -> {
220: try {
221: return conn.getStatements(subject, property, null, includeInferred, context.toArray(new IRI[0]))
222: .stream()
223: .collect(Collectors.toList());
224: } catch (RepositoryException e) {
225: throw new Rdf4jDriverException(e);
226: }
227: });
228: }
229:
230: @Override
231: public boolean containsStatement(Resource subject, IRI property, Value value, boolean includeInferred,
232: Set<IRI> contexts) throws Rdf4jDriverException {
233: assert contexts != null;
234: return withConnection(conn -> {
235: try {
236: return conn.hasStatement(subject, property, value, includeInferred, contexts.toArray(new IRI[0]));
237: } catch (RepositoryException e) {
238: throw new Rdf4jDriverException(e);
239: }
240: });
241: }
242:
243: @Override
244: public boolean isInferred(Statement statement, Set<IRI> contexts) throws Rdf4jDriverException {
245: assert contexts != null;
246: return withConnection(conn -> {
247: try {
248: final IRI[] ctxArr = contexts.toArray(new IRI[0]);
249: return conn.hasStatement(statement, true, ctxArr) && !connection.hasStatement(statement, false, ctxArr);
250: } catch (RepositoryException e) {
251: throw new Rdf4jDriverException(e);
252: }
253: });
254: }
255:
256: @Override
257: public <T> T unwrap(Class<T> cls) throws OntoDriverException {
258: if (cls.isAssignableFrom(getClass())) {
259: return cls.cast(this);
260: }
261: if (cls.isAssignableFrom(RepositoryConnection.class)) {
262: return cls.cast(connection);
263: }
264: return storageConnector.unwrap(cls);
265: }
266: }