-
Notifications
You must be signed in to change notification settings - Fork 14
/
FileStorage.java
97 lines (87 loc) · 3.44 KB
/
FileStorage.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
/**
* Copyright (C) 2020 Czech Technical University in Prague
*
* 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.
*
* 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.exception.JenaDriverException;
import org.apache.jena.query.DatasetFactory;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.riot.RDFLanguages;
import org.apache.jena.riot.RiotNotFoundException;
import org.apache.jena.util.FileUtils;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* File storage accessor.
* <p>
* Note that currently this accessor does not support working with datasets. Only single graph can be present in the file.
*/
class FileStorage extends LocalStorage {
private final String location;
FileStorage(DriverConfiguration configuration) {
super(configuration);
this.location = configuration.getStorageProperties().getPhysicalURI().toString();
}
@Override
public void initialize() {
try {
try {
initDataset();
} catch (RiotNotFoundException e) {
tryCreatingFile();
}
} catch (RuntimeException e) {
throw new OntoDriverInitializationException("Unable to initialize file storage at " + location, e);
}
}
private void initDataset() {
final Model model = RDFDataMgr.loadModel(location);
this.dataset = DatasetFactory.create(model);
}
private void tryCreatingFile() {
final File file = new File(location);
try {
final boolean result = file.createNewFile();
assert result;
} catch (IOException e) {
LOG.error("Unable to create storage file {}.", location);
throw new OntoDriverInitializationException("Unable to initialize file storage at " + location, e);
}
initDataset();
}
@Override
public void writeChanges() throws JenaDriverException {
try (final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(location))) {
final String language = FileUtils.guessLang(location);
RDFDataMgr.write(out, dataset.getDefaultModel(), RDFLanguages.nameToLang(language));
} catch (IOException e) {
throw new JenaDriverException("Unable to write out dataset changes.", e);
}
}
/**
* Reloads data from the underlying file.
*/
@Override
public synchronized void reload() {
if (dataset.isInTransaction()) {
throw new IllegalStateException("Cannot reload storage which is in transaction.");
}
dataset.close();
initialize();
}
}