Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement #261] Add support for Jena TDB2. #272

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ public class JenaOntoDriverProperties {
*/
public static final String TDB = "tdb";

/**
* Jena TDB2 storage.
*
* @see #JENA_STORAGE_TYPE
*/
public static final String TDB2 = "tdb2";

/**
* Jena Fuseki server.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ abstract class LocalStorage implements Storage {

static final Logger LOG = LoggerFactory.getLogger(LocalStorage.class);

static final String FILE_PREFIX = "file:";

private final boolean defaultAsUnion;

Dataset dataset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,13 @@ default void setDataset(Dataset dataset) {
*/
static Storage create(DriverConfiguration configuration) {
final String type = configuration.getProperty(JenaConfigParam.STORAGE_TYPE, JenaOntoDriverProperties.IN_MEMORY);
switch (type) {
case JenaOntoDriverProperties.IN_MEMORY:
return new MemoryStorage(configuration);
case JenaOntoDriverProperties.FILE:
return new FileStorage(configuration);
case JenaOntoDriverProperties.TDB:
return new TDBStorage(configuration);
case JenaOntoDriverProperties.FUSEKI:
return new FusekiStorage(configuration);
default:
throw new OntoDriverInitializationException("Unsupported storage type '" + type + "'.");
}
return switch (type) {
case JenaOntoDriverProperties.IN_MEMORY -> new MemoryStorage(configuration);
case JenaOntoDriverProperties.FILE -> new FileStorage(configuration);
case JenaOntoDriverProperties.TDB -> new TDBStorage(configuration);
case JenaOntoDriverProperties.TDB2 -> new TDB2Storage(configuration);
case JenaOntoDriverProperties.FUSEKI -> new FusekiStorage(configuration);
default -> throw new OntoDriverInitializationException("Unsupported storage type '" + type + "'.");
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cz.cvut.kbss.ontodriver.jena.connector;

import cz.cvut.kbss.ontodriver.config.DriverConfiguration;
import org.apache.jena.dboe.base.file.Location;
import org.apache.jena.tdb2.TDB2Factory;

class TDB2Storage extends LocalStorage {

TDB2Storage(DriverConfiguration configuration) {
super(configuration);
String targetDir = configuration.getStorageProperties().getPhysicalURI().toString();
if (targetDir.startsWith(FILE_PREFIX)) {
targetDir = configuration.getStorageProperties().getPhysicalURI().getSchemeSpecificPart();
}
Location location = Location.create(targetDir);
this.dataset = TDB2Factory.connectDataset(location);
}

// Changes are written automatically on commit by TDB2
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

class TDBStorage extends LocalStorage {

private static final String FILE_PREFIX = "file:";

TDBStorage(DriverConfiguration configuration) {
super(configuration);
String location = configuration.getStorageProperties().getPhysicalURI().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import static cz.cvut.kbss.ontodriver.jena.connector.StorageTestUtil.*;
import static cz.cvut.kbss.ontodriver.jena.connector.StorageTestUtil.SUBJECT;
import static cz.cvut.kbss.ontodriver.jena.connector.StorageTestUtil.createConfiguration;
import static cz.cvut.kbss.ontodriver.jena.connector.StorageTestUtil.generateTestData;
import static org.apache.jena.rdf.model.ResourceFactory.createResource;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -38,7 +41,7 @@ public class SnapshotStorageTest {
private File storageDir;

@AfterEach
public void tearDown() {
public void tearDown() throws IOException {
if (storageDir != null) {
StorageTestUtil.deleteStorageDir(storageDir);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import org.apache.jena.vocabulary.RDFS;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Comparator;

import static org.apache.jena.rdf.model.ResourceFactory.createProperty;
import static org.apache.jena.rdf.model.ResourceFactory.createResource;
Expand Down Expand Up @@ -59,14 +62,17 @@ static void generateTestData(Dataset dataset) {
dataset.addNamedModel(NAMED_GRAPH, namedGraph);
}

static void deleteStorageDir(File directory) {
static void deleteStorageDir(File directory) throws IOException {
if (directory.exists()) {
if (directory.listFiles() != null) {
for (File f : directory.listFiles()) {
f.delete();
}
}
directory.delete();
Files.walk(directory.toPath())
.sorted(Comparator.reverseOrder())
.forEach(path -> {
try {
Files.delete(path);
} catch (IOException e) {
throw new RuntimeException("Unable to delete file " + path, e);
}
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package cz.cvut.kbss.ontodriver.jena.connector;

import org.apache.jena.query.Dataset;
import org.apache.jena.query.ReadWrite;
import org.apache.jena.tdb2.TDB2Factory;
import org.apache.jena.vocabulary.RDF;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import static cz.cvut.kbss.ontodriver.jena.connector.StorageTestUtil.RESOURCE;
import static cz.cvut.kbss.ontodriver.jena.connector.StorageTestUtil.TYPE_ONE;
import static cz.cvut.kbss.ontodriver.jena.connector.StorageTestUtil.createConfiguration;
import static cz.cvut.kbss.ontodriver.jena.connector.StorageTestUtil.generateTestData;
import static org.apache.jena.rdf.model.ResourceFactory.createResource;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class TDB2StorageTest {

private File storageDir;

private TDB2Storage sut;

@AfterEach
public void tearDown() throws IOException {
if (storageDir != null) {
StorageTestUtil.deleteStorageDir(storageDir);
}
}

@Test
public void initializeCreatesNewTDB2Storage() throws Exception {
final File storageDir = initStorage();
assertTrue(storageDir.exists());
final File[] files = storageDir.listFiles();
assertNotNull(files);
assertTrue(files.length > 0);
}

private File initStorage() throws IOException {
this.storageDir = Files.createTempDirectory("tdb2-test").toFile();
storageDir.deleteOnExit();
this.sut = new TDB2Storage(createConfiguration(storageDir.getAbsolutePath()));
return storageDir;
}

@Test
public void initializeLoadsExistingTDB2Dataset() throws Exception {
this.storageDir = Files.createTempDirectory("tdb2-test").toFile();
storageDir.deleteOnExit();
final Dataset dataset = TDB2Factory.connectDataset(storageDir.getAbsolutePath());
dataset.begin(ReadWrite.WRITE);
generateTestData(dataset);
dataset.commit();
dataset.close();

this.sut = new TDB2Storage(createConfiguration(storageDir.getAbsolutePath()));
sut.begin(ReadWrite.READ);
assertTrue(sut.getDataset().getDefaultModel().contains(RESOURCE, RDF.type, createResource(TYPE_ONE)));
sut.commit();
}

@Test
public void initializeCreatesNewTDBStorageWhenTargetDirectoryDoesNotExist() throws Exception {
this.storageDir = Files.createTempDirectory("tdb2-test").toFile();
storageDir.deleteOnExit();
storageDir.delete();
assertFalse(storageDir.exists());
this.sut = new TDB2Storage(createConfiguration(storageDir.getAbsolutePath()));
assertTrue(storageDir.exists());
}

@Test
public void writeChangesSynchronizesDatasetToStorage() throws Exception {
final File storageDir = initStorage();
sut.begin(ReadWrite.WRITE);
generateTestData(sut.getDataset());
sut.commit();
sut.writeChanges();
sut.close();

final Dataset result = TDB2Factory.connectDataset(storageDir.getAbsolutePath());
result.begin(ReadWrite.READ);
assertTrue(result.getDefaultModel().contains(RESOURCE, RDF.type, createResource(TYPE_ONE)));
result.abort();
result.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class TDBStorageTest {
private File storageDir;

@AfterEach
public void tearDown() {
public void tearDown() throws Exception {
if (storageDir != null) {
StorageTestUtil.deleteStorageDir(storageDir);
}
Expand Down Expand Up @@ -96,6 +96,7 @@ public void writeChangesSynchronizesDatasetToStorage() throws Exception {
generateTestData(storage.getDataset());
storage.commit();
storage.writeChanges();
storage.close();

final Dataset result = TDBFactory.createDataset(storageDir.getAbsolutePath());
result.begin(ReadWrite.READ);
Expand Down
Loading