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