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

Add TDB Support - WIP #12

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
46 changes: 0 additions & 46 deletions .classpath

This file was deleted.

8 changes: 0 additions & 8 deletions .settings/org.eclipse.core.resources.prefs

This file was deleted.

13 changes: 0 additions & 13 deletions .settings/org.eclipse.jdt.core.prefs

This file was deleted.

4 changes: 0 additions & 4 deletions .settings/org.eclipse.m2e.core.prefs

This file was deleted.

5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@
<artifactId>jena-base</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-tdb</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>
<build>
<resources>
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/org/spdx/spdxRdfStore/RdfStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.apache.jena.graph.Node;
import org.apache.jena.graph.Triple;
import org.apache.jena.query.ARQ;
import org.apache.jena.query.Dataset;
import org.apache.jena.query.DatasetFactory;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.util.FileManager;
Expand Down Expand Up @@ -71,11 +73,21 @@ public class RdfStore implements IModelStore, ISerializableModelStore {
Map<String, RdfSpdxDocumentModelManager> documentUriModelMap = new ConcurrentHashMap<>();

private OutputFormat outputFormat = OutputFormat.XML_ABBREV;

private Dataset dataset;

static {
ARQ.init(); // Insure ARQ is initialized
}

public RdfStore() {
dataset = DatasetFactory.create();
}

public RdfStore(Dataset dataset) {
this.dataset = dataset;
}


/**
* @return the outputFormat
Expand Down Expand Up @@ -143,7 +155,7 @@ public void create(String documentUri, String id, String type) throws InvalidSPD
Objects.requireNonNull(type, "Missing required type");
RdfSpdxDocumentModelManager modelManager = documentUriModelMap.get(documentUri);
if (Objects.isNull(modelManager)) {
Model model = ModelFactory.createDefaultModel();
Model model = dataset.getNamedModel(documentUri);
model.getGraph().getPrefixMapping().setNsPrefix("spdx", SpdxConstants.SPDX_NAMESPACE);
model.getGraph().getPrefixMapping().setNsPrefix("doap", SpdxConstants.DOAP_NAMESPACE);
model.getGraph().getPrefixMapping().setNsPrefix("ptr", SpdxConstants.RDF_POINTER_NAMESPACE);
Expand Down Expand Up @@ -218,7 +230,7 @@ public String getNextId(IdType idType, String documentUri) throws InvalidSPDXAna
Objects.requireNonNull(idType, "Missing required ID type");
RdfSpdxDocumentModelManager modelManager = documentUriModelMap.get(documentUri);
if (Objects.isNull(modelManager)) {
Model model = ModelFactory.createDefaultModel();
Model model = dataset.getNamedModel(documentUri);
model.getGraph().getPrefixMapping().setNsPrefix("spdx", SpdxConstants.SPDX_NAMESPACE);
model.getGraph().getPrefixMapping().setNsPrefix("doap", SpdxConstants.DOAP_NAMESPACE);
modelManager = new RdfSpdxDocumentModelManager(documentUri, model);
Expand Down Expand Up @@ -518,8 +530,10 @@ public String deSerialize(InputStream stream, boolean overwrite) throws InvalidS
Model model = ModelFactory.createDefaultModel();
model.read(stream, null);
String documentNamespace = getDocumentNamespace(model);
RdfSpdxDocumentModelManager modelManager = new RdfSpdxDocumentModelManager(documentNamespace, model);
CompatibilityUpgrader.upgrade(model);
dataset.addNamedModel(documentNamespace, model);
RdfSpdxDocumentModelManager modelManager = new RdfSpdxDocumentModelManager(documentNamespace,
dataset.getNamedModel(documentNamespace));
RdfSpdxDocumentModelManager previousModel = documentUriModelMap.putIfAbsent(documentNamespace, modelManager);
if (!Objects.isNull(previousModel)) {
if (overwrite) {
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/org/spdx/spdxRdfStore/RdfStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.io.FileUtils;
import org.apache.jena.query.Dataset;
import org.apache.jena.tdb.TDBFactory;
import org.spdx.library.InvalidSPDXAnalysisException;
import org.spdx.library.SpdxConstants;
import org.spdx.library.model.ExternalRef;
Expand Down Expand Up @@ -174,5 +179,33 @@ public void testLoadModelFromFile() throws InvalidSPDXAnalysisException, IOExcep
documentDescribes = doc.getDocumentDescribes();
assertEquals(2, documentDescribes.size());
}

public void testTdb() throws InvalidSPDXAnalysisException, IOException {
File tdbDir = Files.createTempDirectory("TDB").toFile();
Dataset dataset = null;
try {
dataset = TDBFactory.createDataset(tdbDir.getAbsolutePath());
RdfStore rdfStore = new RdfStore(dataset);
SpdxModelFactory.createModelObject(rdfStore, DOCUMENT_URI1, SpdxConstants.SPDX_DOCUMENT_ID, SpdxConstants.CLASS_SPDX_DOCUMENT, null);
SpdxModelFactory.createModelObject(rdfStore, DOCUMENT_URI1, ID_2, SpdxConstants.CLASS_SPDX_FILE, null);
SpdxModelFactory.createModelObject(rdfStore, DOCUMENT_URI1, ID_3, SpdxConstants.CLASS_SPDX_FILE, null);
SpdxModelFactory.createModelObject(rdfStore, DOCUMENT_URI1, ID_4, SpdxConstants.CLASS_SPDX_FILE, null);
Stream<TypedValue> result = rdfStore.getAllItems(DOCUMENT_URI1, SpdxConstants.CLASS_SPDX_FILE);
final ArrayList<TypedValue> resultList = new ArrayList<>();
resultList.add(new TypedValue(ID_2, SpdxConstants.CLASS_SPDX_FILE));
resultList.add(new TypedValue(ID_3, SpdxConstants.CLASS_SPDX_FILE));
resultList.add(new TypedValue(ID_4, SpdxConstants.CLASS_SPDX_FILE));
for (TypedValue tv:result.collect(Collectors.toList())) {
assertTrue(resultList.contains(tv));
resultList.remove(tv);
}
assertEquals(0, resultList.size());
} finally {
if (dataset != null) {
dataset.end();
}
FileUtils.deleteDirectory(tdbDir);
}
}

}