Skip to contentMethod: create(DriverConfiguration)
1: /**
2: * Copyright (C) 2023 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.Query;
24: import org.apache.jena.query.QueryExecution;
25: import org.apache.jena.query.ReadWrite;
26: import org.apache.jena.rdf.model.Model;
27: import org.apache.jena.rdf.model.Statement;
28: import org.apache.jena.rdf.model.StmtIterator;
29: import org.apache.jena.sparql.core.Transactional;
30:
31: import java.util.List;
32:
33: /**
34: * Represents underlying Jena storage.
35: */
36: interface Storage {
37:
38: /**
39: * Synchronize changes with the storage (e.g., disk).
40: * <p>
41: * Does nothing by default.
42: *
43: * @throws JenaDriverException Indicates that changes could not be written out
44: */
45: default void writeChanges() throws JenaDriverException {
46: }
47:
48: /**
49: * Gets a transactional representation of the underlying storage.
50: * <p>
51: * The results can be used by the {@link org.apache.jena.system.Txn} utility class.
52: *
53: * @return Jena {@code Transactional} instance
54: */
55: Transactional getTransactional();
56:
57: /**
58: * Gets the dataset to which this instance is connected.
59: *
60: * @return Jena {@code Dataset}
61: */
62: Dataset getDataset();
63:
64: /**
65: * Gets the default graph from this storage's dataset.
66: *
67: * @return Default graph
68: */
69: Model getDefaultGraph();
70:
71: /**
72: * Gets a named graph with the specified identifier.
73: *
74: * @param ctx Context identifier
75: * @return Named graph
76: */
77: Model getNamedGraph(String ctx);
78:
79: /**
80: * Begins a transaction.
81: *
82: * @param readWrite Transaction read/write mode
83: */
84: void begin(ReadWrite readWrite);
85:
86: /**
87: * Commits the current transaction.
88: */
89: void commit();
90:
91: /**
92: * Rolls back the current transaction.
93: */
94: void rollback();
95:
96: /**
97: * Closes this storage.
98: */
99: void close();
100:
101: /**
102: * Adds the specified statements to the specified context (can be {@code null}).
103: *
104: * @param statements Statements to add
105: * @param context Context identifier, possibly {@code null} indicating default context
106: */
107: void add(List<Statement> statements, String context);
108:
109: /**
110: * Removes the specified statements from the specified context (can be {@code null}).
111: *
112: * @param statements Statements to remove
113: * @param context Context identifier, possibly {@code null} indicating default context
114: */
115: void remove(List<Statement> statements, String context);
116:
117: /**
118: * Removes the specified statements from the specified context (can be {@code null}).
119: *
120: * @param iterator Statement iterator
121: * @param context Context identifier, possibly {@code null} indicating default context
122: */
123: void remove(StmtIterator iterator, String context);
124:
125: /**
126: * Creates a query execution which can be run.
127: *
128: * @param query Query to prepare execution for
129: * @return {@code QueryExecution}
130: */
131: QueryExecution prepareQuery(Query query);
132:
133: /**
134: * Executes the specified SPARQL update.
135: *
136: * @param update SPARQL update to execute
137: */
138: void executeUpdate(String update);
139:
140: /**
141: * Reloads data from the underlying storage (if applicable).
142: * <p>
143: * Default implementation does nothing.
144: */
145: default void reload() {
146: }
147:
148: /**
149: * Sets the dataset on this storage.
150: * <p>
151: * Note that by default this method throws {@link UnsupportedOperationException}, because such an operation is
152: * supported only by the in-memory storage.
153: *
154: * @param dataset The new dataset
155: */
156: default void setDataset(Dataset dataset) {
157: throw new UnsupportedOperationException("Cannot set dataset on storage of type " + getClass().getSimpleName());
158: }
159:
160: /**
161: * Creates a storage accessor according to the specified configuration.
162: *
163: * @param configuration Access configuration
164: * @return Storage accessor instance
165: * @throws OntoDriverInitializationException When storage type is not supported
166: */
167: static Storage create(DriverConfiguration configuration) {
168: final String type = configuration.getProperty(JenaConfigParam.STORAGE_TYPE, JenaOntoDriverProperties.IN_MEMORY);
169:• switch (type) {
170: case JenaOntoDriverProperties.IN_MEMORY:
171: return new MemoryStorage(configuration);
172: case JenaOntoDriverProperties.FILE:
173: return new FileStorage(configuration);
174: case JenaOntoDriverProperties.TDB:
175: return new TDBStorage(configuration);
176: case JenaOntoDriverProperties.FUSEKI:
177: return new FusekiStorage(configuration);
178: default:
179: throw new OntoDriverInitializationException("Unsupported storage type '" + type + "'.");
180: }
181: }
182: }