Skip to content

Package: FileStorage

FileStorage

nameinstructionbranchcomplexitylinemethod
FileStorage(DriverConfiguration)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
initDataset()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
initialize()
M: 15 C: 8
35%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 6
75%
M: 0 C: 1
100%
reload()
M: 0 C: 15
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
tryCreatingFile()
M: 24 C: 16
40%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 3 C: 6
67%
M: 0 C: 1
100%
writeChanges()
M: 7 C: 24
77%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 5
71%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.ontodriver.jena.connector;
14:
15: import cz.cvut.kbss.ontodriver.config.DriverConfiguration;
16: import cz.cvut.kbss.ontodriver.exception.OntoDriverInitializationException;
17: import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
18: import org.apache.jena.query.DatasetFactory;
19: import org.apache.jena.rdf.model.Model;
20: import org.apache.jena.riot.RDFDataMgr;
21: import org.apache.jena.riot.RDFLanguages;
22: import org.apache.jena.riot.RiotNotFoundException;
23: import org.apache.jena.util.FileUtils;
24:
25: import java.io.BufferedOutputStream;
26: import java.io.File;
27: import java.io.FileOutputStream;
28: import java.io.IOException;
29:
30: /**
31: * File storage accessor.
32: * <p>
33: * Note that currently this accessor does not support working with datasets. Only single graph can be present in the file.
34: */
35: class FileStorage extends LocalStorage {
36:
37: private final String location;
38:
39: FileStorage(DriverConfiguration configuration) {
40: super(configuration);
41: this.location = configuration.getStorageProperties().getPhysicalURI().toString();
42: initialize();
43: }
44:
45: public void initialize() {
46: try {
47: try {
48: initDataset();
49: } catch (RiotNotFoundException e) {
50: tryCreatingFile();
51: }
52: } catch (RuntimeException e) {
53: throw new OntoDriverInitializationException("Unable to initialize file storage at " + location, e);
54: }
55: }
56:
57: private void initDataset() {
58: final Model model = RDFDataMgr.loadModel(location);
59: this.dataset = DatasetFactory.create(model);
60: }
61:
62: private void tryCreatingFile() {
63: final File file = new File(location);
64: try {
65: final boolean result = file.createNewFile();
66:• assert result;
67: } catch (IOException e) {
68: LOG.error("Unable to create storage file {}.", location);
69: throw new OntoDriverInitializationException("Unable to initialize file storage at " + location, e);
70: }
71: initDataset();
72: }
73:
74: @Override
75: public void writeChanges() throws JenaDriverException {
76: try (final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(location))) {
77: final String language = FileUtils.guessLang(location);
78: RDFDataMgr.write(out, dataset.getDefaultModel(), RDFLanguages.nameToLang(language));
79: } catch (IOException e) {
80: throw new JenaDriverException("Unable to write out dataset changes.", e);
81: }
82: }
83:
84: /**
85: * Reloads data from the underlying file.
86: */
87: @Override
88: public synchronized void reload() {
89:• if (dataset.isInTransaction()) {
90: throw new IllegalStateException("Cannot reload storage which is in transaction.");
91: }
92: dataset.close();
93: initialize();
94: }
95: }