-
Notifications
You must be signed in to change notification settings - Fork 14
/
LocalStorage.java
157 lines (134 loc) · 5.24 KB
/
LocalStorage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Copyright (C) 2020 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.ontodriver.jena.connector;
import cz.cvut.kbss.ontodriver.config.DriverConfiguration;
import cz.cvut.kbss.ontodriver.exception.OntoDriverInitializationException;
import cz.cvut.kbss.ontodriver.jena.config.JenaConfigParam;
import cz.cvut.kbss.ontodriver.jena.config.JenaOntoDriverProperties;
import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
import org.apache.jena.query.*;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.StmtIterator;
import org.apache.jena.sparql.core.Transactional;
import org.apache.jena.update.UpdateAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
* Represents a local Jena storage, e.g., a TDB dataset.
*/
abstract class LocalStorage implements Storage {
static final Logger LOG = LoggerFactory.getLogger(LocalStorage.class);
private final boolean defaultAsUnion;
Dataset dataset;
LocalStorage(DriverConfiguration configuration) {
this.defaultAsUnion = configuration.is(JenaConfigParam.TREAT_DEFAULT_GRAPH_AS_UNION);
}
public void writeChanges() throws JenaDriverException {
// Do nothing by default
}
abstract void initialize();
public Dataset getDataset() {
return dataset;
}
public Model getDefaultGraph() {
return defaultAsUnion ? ModelFactory.createUnion(dataset.getUnionModel(), dataset.getDefaultModel()) :
dataset.getDefaultModel();
}
public Model getNamedGraph(String ctx) {
return dataset.getNamedModel(ctx);
}
@Override
public Transactional getTransactional() {
return dataset;
}
public void begin(ReadWrite readWrite) {
dataset.begin(readWrite);
}
public void commit() {
dataset.commit();
}
public void rollback() {
dataset.abort();
}
public void close() {
dataset.close();
}
public void add(List<Statement> statements, String context) {
if (context != null) {
dataset.getNamedModel(context).add(statements);
} else {
dataset.getDefaultModel().add(statements);
}
}
public void remove(List<Statement> statements, String context) {
if (context != null) {
dataset.getNamedModel(context).remove(statements);
} else {
dataset.getDefaultModel().remove(statements);
if (defaultAsUnion) {
dataset.listNames().forEachRemaining(n -> dataset.getNamedModel(n).remove(statements));
}
}
}
public void remove(StmtIterator iterator, String context) {
if (context != null) {
dataset.getNamedModel(context).remove(iterator);
} else {
iterator.forEachRemaining(statement -> {
dataset.getDefaultModel().remove(statement);
if (defaultAsUnion) {
dataset.listNames().forEachRemaining(n -> dataset.getNamedModel(n).remove(statement));
}
});
}
}
@Override
public QueryExecution prepareQuery(Query query) {
return QueryExecutionFactory.create(query, dataset);
}
@Override
public void executeUpdate(String update) {
UpdateAction.parseExecute(update, dataset);
}
/**
* Creates a storage accessor according to the specified configuration.
*
* @param configuration Access configuration
* @return Storage accessor instance
* @throws OntoDriverInitializationException When storage type is not supported
*/
static LocalStorage create(DriverConfiguration configuration) {
final String type = configuration.getProperty(JenaConfigParam.STORAGE_TYPE, JenaOntoDriverProperties.IN_MEMORY);
final LocalStorage storage;
switch (type) {
case JenaOntoDriverProperties.IN_MEMORY:
storage = new MemoryStorage(configuration);
break;
case JenaOntoDriverProperties.FILE:
storage = new FileStorage(configuration);
break;
case JenaOntoDriverProperties.TDB:
storage = new TDBStorage(configuration);
break;
case JenaOntoDriverProperties.SDB:
throw new UnsupportedOperationException("Not implemented, yet.");
default:
throw new OntoDriverInitializationException("Unsupported storage type '" + type + "'.");
}
storage.initialize();
return storage;
}
}