Skip to content

Commit

Permalink
VIVO 3828: Remove SDB in preparation to moving to new Jena.
Browse files Browse the repository at this point in the history
The Jena version being switched to (Jena 4) has removed support for SDB.

The current forms of `DatasetWrapperFactory` and `StaticDatasetFactory` should no longer be needed.

The SDB related code has been stripped out.
Many of the classes with "SDB" in the name that actually provide more than just SDB have been re-created.
These recreations are generally child classes of a similarly named Jena class.
These recreates have "DB" in their name rather than "SDB".

The `DatasetFactory.createMem()` is deprecated and may not be available in Jena 4.
Attempts to replace this with `createTxnMem()` have revealed problems that are potentially transaction related.
Replacing with `createGeneral()` might be possible but this is not done to avoid introducing more potential problems.

Notable points in regards to replacing `DatasetFactory.createMem()`:
1) The method is deprecated.
2) The `DatasetFactory.createGeneral()` is the compatible equivalent.
3) The `DatasetFactory.createGeneral()` better supports TDB (which is the main reason for including in this commit set).
4) The `DatasetFactory.createTxnMem()` is the more recommended alternative and is transactional.
5) This better prepares the code for the upcoming Jena upgrade changes.

There are some serious existing problems with closing dataset connections (and related).
The (now removed) `DatasetWrapperFactory` probably should be rewritten to provide a new dataset each time rather than copying an existing one.
The `close()` functionality appears to not be being called for TDB due to the SDB conditions.
With this SDB condition the connections are now being closed.
This has exposed several problems that both tests and runtime expose.
This problem is considered out of scope for the changes and the close code is commented out with a FIXME statement.

The documentation for `example.runtime.properties` refers to `VitroConnection.DataSource.*` only being used by SDB but this is incorrect.
The OpenSocial code appears to talk directly to an SQL database using these properties.
Update the documentation in this regard and replace the references to SDB with OpenSocial.

Remove no longer necessary SDB helper code that is added to TDB as well because it "shouldn't hurt".

Remove much of the documentation and installation functionality using or referencing SDB.

The `IndividualDaoSDB.java` file is implemting `getAllIndividualUris()` and `getUpdatedSinceIterator()` via the now removed `IndivdiualSDB` class.
This functionality is used by `RebuildIndexTask` and `UpdateUrisTask` classes.

The new *DB classes now have java docs.
These java docs have a bare-bones level of detail.
  • Loading branch information
kaladay committed Mar 6, 2023
1 parent 4d60cd9 commit d43a2fe
Show file tree
Hide file tree
Showing 55 changed files with 2,765 additions and 4,110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement;
import edu.cornell.mannlib.vitro.webapp.dao.jena.exception.IndividualNotFoundException;

/**
* Created by IntelliJ IDEA.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public int compare (DataProperty dp1, DataProperty dp2) {
}

public DataPropertyDaoJena(RDFService rdfService,
DatasetWrapperFactory dwf,
DatasetWrapper dw,
WebappDaoFactoryJena wadf) {
super(rdfService, dwf, wadf);
super(rdfService, dw, wadf);
}

public void deleteDataProperty(DataProperty dtp) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* $This file is distributed under the terms of the license in LICENSE$ */

package edu.cornell.mannlib.vitro.webapp.dao.jena;

import java.util.ArrayList;
import java.util.List;

import org.apache.jena.ontology.OntModel;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.query.Dataset;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.StmtIterator;
import org.apache.jena.shared.Lock;
import org.apache.jena.vocabulary.RDF;

import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatement;
import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatementImpl;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.dao.DataPropertyStatementDao;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;

/**
* An extension of {@link DataPropertyStatementDaoJena} for databases, such as TDB.
*/
public class DataPropertyStatementDaoDB extends DataPropertyStatementDaoJena implements DataPropertyStatementDao {

/**
* Initialize the data property statement DAO.
*
* @param dw The data wrapper.
* @param wadf The web application DAO factory.
*/
public DataPropertyStatementDaoDB(DatasetWrapper dw, WebappDaoFactoryDB wadf) {
super (dw, wadf);
}

/**
* Fill existing data property statements for an individual.
*
* @param entity The individual.
*
* @return A filled out individual.
*/
public Individual fillExistingDataPropertyStatementsForIndividual(Individual entity) {
if (entity.getURI() == null) {
return entity;
}

String query =
"CONSTRUCT { \n" +
" <" + entity.getURI() + "> ?p ?o . \n" +
"} WHERE { \n" +
" <" + entity.getURI() + "> ?p ?o . \n" +
" FILTER(isLiteral(?o)) \n" +
"}" ;
Model results = null;
Dataset dataset = getDataWrapper().getDataset();
QueryExecution qexec = null;

dataset.getLock().enterCriticalSection(Lock.READ);
try {
qexec = QueryExecutionFactory.create(QueryFactory.create(query), dataset);
results = qexec.execConstruct();
} finally {
if (qexec != null) qexec.close();
dataset.getLock().leaveCriticalSection();
getDataWrapper().close();
}

OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, results);
ontModel.enterCriticalSection(Lock.READ);
try {
Resource ind = ontModel.getResource(entity.getURI());
List<DataPropertyStatement> edList = new ArrayList<>();
StmtIterator stmtIt = ind.listProperties();

while (stmtIt.hasNext()) {
Statement st = stmtIt.next();
boolean addToList = st.getObject().isLiteral() &&
(
(
RDF.value.equals(st.getPredicate())
|| VitroVocabulary.value.equals(st.getPredicate().getURI()))
|| MONIKER.equals(st.getPredicate())
|| !(NONUSER_NAMESPACES.contains(st.getPredicate().getNameSpace())
)
);

if (addToList) {
DataPropertyStatement ed = new DataPropertyStatementImpl();
Literal lit = (Literal)st.getObject();
fillDataPropertyStatementWithJenaLiteral(ed,lit);
ed.setDatapropURI(st.getPredicate().getURI());
ed.setIndividualURI(ind.getURI());
ed.setIndividual(entity);
edList.add(ed);
}
}

entity.setDataPropertyStatements(edList);
return entity;
} finally {
ontModel.leaveCriticalSection();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,22 @@ public class DataPropertyStatementDaoJena extends JenaBaseDao implements DataPro
{
private static final Log log = LogFactory.getLog(DataPropertyStatementDaoJena.class);

private DatasetWrapper dw;

private DatasetWrapperFactory dwf;

public DataPropertyStatementDaoJena(DatasetWrapperFactory dwf,
public DataPropertyStatementDaoJena(DatasetWrapper dw,
WebappDaoFactoryJena wadf) {
super(wadf);
this.dwf = dwf;
this.dw = dw;
}

/**
* Get the data wrapper.
*
* @return The data wrapper.
*/
public DatasetWrapper getDataWrapper() {
return dw;
}

public void deleteDataPropertyStatement( DataPropertyStatement dataPropertyStatement )
{
Expand Down Expand Up @@ -366,13 +373,11 @@ public List<Literal> getDataPropertyValuesForIndividualByProperty(String subject

// Run the SPARQL query to get the properties
List<Literal> values = new ArrayList<Literal>();
DatasetWrapper w = dwf.getDatasetWrapper();
Dataset dataset = w.getDataset();
Dataset dataset = dw.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ);
QueryExecution qexec = null;
try {
qexec = QueryExecutionFactory.create(
queryString, dataset);
qexec = QueryExecutionFactory.create(queryString, dataset);
ResultSet results = qexec.execSelect();

while (results.hasNext()) {
Expand All @@ -388,7 +393,7 @@ public List<Literal> getDataPropertyValuesForIndividualByProperty(String subject

} finally {
dataset.getLock().leaveCriticalSection();
w.close();
dw.close();
if (qexec != null) {
qexec.close();
}
Expand Down Expand Up @@ -430,8 +435,7 @@ public List<Literal> getDataPropertyValuesForIndividualByProperty(

// Run the SPARQL query to get the properties
List<Literal> values = new ArrayList<Literal>();
DatasetWrapper w = dwf.getDatasetWrapper();
Dataset dataset = w.getDataset();
Dataset dataset = dw.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ);
QueryExecution qexec = null;
try {
Expand Down Expand Up @@ -459,7 +463,7 @@ public List<Literal> getDataPropertyValuesForIndividualByProperty(
return Collections.emptyList();
} finally {
dataset.getLock().leaveCriticalSection();
w.close();
dw.close();
if (qexec != null) {
qexec.close();
}
Expand Down Expand Up @@ -497,8 +501,7 @@ private Model constructModelForSelectQueries(String subjectUri,
initialBindings.add(
"property", ResourceFactory.createResource(propertyUri));

DatasetWrapper w = dwf.getDatasetWrapper();
Dataset dataset = w.getDataset();
Dataset dataset = dw.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ);
QueryExecution qe = null;
try {
Expand All @@ -512,7 +515,7 @@ private Model constructModelForSelectQueries(String subjectUri,
qe.close();
}
dataset.getLock().leaveCriticalSection();
w.close();
dw.close();
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package edu.cornell.mannlib.vitro.webapp.dao.jena;

/**
* Modes for optimizing database queries.
*/
public enum DatasetMode {

/**
* Only perform assertions.
*/
ASSERTIONS_ONLY,

/**
* Only perform inferences.
*/
INFERENCES_ONLY,

/**
* Perform both assertions and inferences.
*/
ASSERTIONS_AND_INFERENCES,
}
Loading

0 comments on commit d43a2fe

Please sign in to comment.