Skip to content

Package: FileStorage

FileStorage

nameinstructionbranchcomplexitylinemethod
FileStorage(DriverConfiguration)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
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: *
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.exception.JenaDriverException;
20: import org.apache.jena.query.DatasetFactory;
21: import org.apache.jena.rdf.model.Model;
22: import org.apache.jena.riot.RDFDataMgr;
23: import org.apache.jena.riot.RDFLanguages;
24: import org.apache.jena.riot.RiotNotFoundException;
25: import org.apache.jena.util.FileUtils;
26:
27: import java.io.BufferedOutputStream;
28: import java.io.File;
29: import java.io.FileOutputStream;
30: import java.io.IOException;
31:
32: /**
33: * File storage accessor.
34: * <p>
35: * Note that currently this accessor does not support working with datasets. Only single graph can be present in the file.
36: */
37: class FileStorage extends Storage {
38:
39: private final String location;
40:
41: FileStorage(DriverConfiguration configuration) {
42: super(configuration);
43: this.location = configuration.getStorageProperties().getPhysicalURI().toString();
44: }
45:
46: @Override
47: public void initialize() {
48: try {
49: try {
50: initDataset();
51: } catch (RiotNotFoundException e) {
52: tryCreatingFile();
53: }
54: } catch (RuntimeException e) {
55: throw new OntoDriverInitializationException("Unable to initialize file storage at " + location, e);
56: }
57: }
58:
59: private void initDataset() {
60: final Model model = RDFDataMgr.loadModel(location);
61: this.dataset = DatasetFactory.create(model);
62: }
63:
64: private void tryCreatingFile() {
65: final File file = new File(location);
66: try {
67: final boolean result = file.createNewFile();
68:• assert result;
69: } catch (IOException e) {
70: LOG.error("Unable to create storage file {}.", location);
71: throw new OntoDriverInitializationException("Unable to initialize file storage at " + location, e);
72: }
73: initDataset();
74: }
75:
76: @Override
77: void writeChanges() throws JenaDriverException {
78: try (final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(location))) {
79: final String language = FileUtils.guessLang(location);
80: RDFDataMgr.write(out, dataset.getDefaultModel(), RDFLanguages.nameToLang(language));
81: } catch (IOException e) {
82: throw new JenaDriverException("Unable to write out dataset changes.", e);
83: }
84: }
85:
86: /**
87: * Reloads data from the underlying file.
88: */
89: @Override
90: synchronized void reload() {
91:• if (dataset.isInTransaction()) {
92: throw new IllegalStateException("Cannot reload storage which is in transaction.");
93: }
94: dataset.close();
95: initialize();
96: }
97: }