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) 2022 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
36: * file.
37: */
38: class FileStorage extends LocalStorage {
39:
40: private final String location;
41:
42: FileStorage(DriverConfiguration configuration) {
43: super(configuration);
44: this.location = configuration.getStorageProperties().getPhysicalURI().getSchemeSpecificPart();
45: initialize();
46: }
47:
48: public void initialize() {
49: try {
50: try {
51: initDataset();
52: } catch (RiotNotFoundException e) {
53: tryCreatingFile();
54: }
55: } catch (RuntimeException e) {
56: throw new OntoDriverInitializationException("Unable to initialize file storage at " + location, e);
57: }
58: }
59:
60: private void initDataset() {
61: final Model model = RDFDataMgr.loadModel(location);
62: this.dataset = DatasetFactory.create(model);
63: }
64:
65: private void tryCreatingFile() {
66: final File file = new File(location);
67: try {
68: final boolean result = file.createNewFile();
69:• assert result;
70: } catch (IOException e) {
71: LOG.error("Unable to create storage file {}.", location);
72: throw new OntoDriverInitializationException("Unable to initialize file storage at " + location, e);
73: }
74: initDataset();
75: }
76:
77: @Override
78: public void writeChanges() throws JenaDriverException {
79: try (final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(location))) {
80: final String language = FileUtils.guessLang(location);
81: RDFDataMgr.write(out, dataset.getDefaultModel(), RDFLanguages.nameToLang(language));
82: } catch (IOException e) {
83: throw new JenaDriverException("Unable to write out dataset changes.", e);
84: }
85: }
86:
87: /**
88: * Reloads data from the underlying file.
89: */
90: @Override
91: public synchronized void reload() {
92:• if (dataset.isInTransaction()) {
93: throw new IllegalStateException("Cannot reload storage which is in transaction.");
94: }
95: dataset.close();
96: initialize();
97: }
98: }