Skip to content

Method: rollback()

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.config.DriverConfiguration;
18: import cz.cvut.kbss.ontodriver.exception.OntoDriverInitializationException;
19: import cz.cvut.kbss.ontodriver.jena.config.JenaConfigParam;
20: import cz.cvut.kbss.ontodriver.jena.config.JenaOntoDriverProperties;
21: import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
22: import org.apache.jena.query.Dataset;
23: import org.apache.jena.query.ReadWrite;
24: import org.apache.jena.rdf.model.Model;
25: import org.apache.jena.rdf.model.ModelFactory;
26: import org.apache.jena.rdf.model.Statement;
27: import org.apache.jena.rdf.model.StmtIterator;
28: import org.slf4j.Logger;
29: import org.slf4j.LoggerFactory;
30:
31: import java.util.List;
32:
33: abstract class Storage {
34:
35: static final Logger LOG = LoggerFactory.getLogger(Storage.class);
36:
37: private final boolean defaultAsUnion;
38:
39: Dataset dataset;
40:
41: Storage(DriverConfiguration configuration) {
42: this.defaultAsUnion = configuration.is(JenaConfigParam.TREAT_DEFAULT_GRAPH_AS_UNION);
43: }
44:
45: void writeChanges() throws JenaDriverException {
46: // Do nothing by default
47: }
48:
49: abstract void initialize();
50:
51: Dataset getDataset() {
52: return dataset;
53: }
54:
55: Model getDefaultGraph() {
56: return defaultAsUnion ? ModelFactory.createUnion(dataset.getUnionModel(), dataset.getDefaultModel()) :
57: dataset.getDefaultModel();
58: }
59:
60: Model getNamedGraph(String ctx) {
61: return dataset.getNamedModel(ctx);
62: }
63:
64: void begin(ReadWrite readWrite) {
65: dataset.begin(readWrite);
66: }
67:
68: void commit() {
69: dataset.commit();
70: }
71:
72: void rollback() {
73: dataset.abort();
74: }
75:
76: void close() {
77: dataset.close();
78: }
79:
80: void add(List<Statement> statements, String context) {
81: if (context != null) {
82: dataset.getNamedModel(context).add(statements);
83: } else {
84: dataset.getDefaultModel().add(statements);
85: }
86: }
87:
88: void remove(List<Statement> statements, String context) {
89: if (context != null) {
90: dataset.getNamedModel(context).remove(statements);
91: } else {
92: dataset.getDefaultModel().remove(statements);
93: if (defaultAsUnion) {
94: dataset.listNames().forEachRemaining(n -> dataset.getNamedModel(n).remove(statements));
95: }
96: }
97: }
98:
99: void remove(StmtIterator iterator, String context) {
100: if (context != null) {
101: dataset.getNamedModel(context).remove(iterator);
102: } else {
103: iterator.forEachRemaining(statement -> {
104: dataset.getDefaultModel().remove(statement);
105: if (defaultAsUnion) {
106: dataset.listNames().forEachRemaining(n -> dataset.getNamedModel(n).remove(statement));
107: }
108: });
109: }
110: }
111:
112: void reload() {
113: // Do nothing by default
114: }
115:
116: /**
117: * Sets the dataset on this storage.
118: *
119: * Note that by default this method throws {@link UnsupportedOperationException}, because such an operation is supported
120: * only by the in-memory storage.
121: * @param dataset The new dataset
122: */
123: void setDataset(Dataset dataset) {
124: throw new UnsupportedOperationException("Cannot set dataset on storage of type " + getClass().getSimpleName());
125: }
126:
127: /**
128: * Creates a storage accessor according to the specified configuration.
129: *
130: * @param configuration Access configuration
131: * @return Storage accessor instance
132: * @throws OntoDriverInitializationException When storage type is not supported
133: */
134: static Storage create(DriverConfiguration configuration) {
135: final String type = configuration.getProperty(JenaConfigParam.STORAGE_TYPE, JenaOntoDriverProperties.IN_MEMORY);
136: final Storage storage;
137: switch (type) {
138: case JenaOntoDriverProperties.IN_MEMORY:
139: storage = new MemoryStorage(configuration);
140: break;
141: case JenaOntoDriverProperties.FILE:
142: storage = new FileStorage(configuration);
143: break;
144: case JenaOntoDriverProperties.TDB:
145: storage = new TDBStorage(configuration);
146: break;
147: case JenaOntoDriverProperties.SDB:
148: throw new UnsupportedOperationException("Not implemented, yet.");
149: default:
150: throw new OntoDriverInitializationException("Unsupported storage type \'" + type + "\'.");
151: }
152: storage.initialize();
153: return storage;
154: }
155: }