Skip to content

Commit

Permalink
[ issue #99 ] Replace Jena raw with Solrdf client API (UPDATE ops are
Browse files Browse the repository at this point in the history
still missing)
  • Loading branch information
agazzarini committed Sep 29, 2015
1 parent eb04ce4 commit e377432
Show file tree
Hide file tree
Showing 8 changed files with 292 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.sparql.engine.binding.Binding;

/**
Expand Down Expand Up @@ -221,6 +222,80 @@ public static Builder newBuilder() {
this.solr = solr;
this.sparqlEndpoint = sparqlEndpointAddress;
}

/**
* Clears the default model on SolRDF.
*
* @throws UnableToDeleteException in case the deletion is not possible.
*/
public void deleteDefaultGraph() throws UnableToDeleteException {
try {
remoteDataset.deleteDefault();
} catch(final Exception exception) {
throw new UnableToDeleteException(exception);
}
}

/**
* Clears the whole data on SolRDF.
*
* @throws UnableToDeleteException in case the deletion is not possible.
*/
public void clear() throws UnableToDeleteException {
try {
solr.deleteByQuery("*:*");
} catch(final Exception exception) {
throw new UnableToDeleteException(exception);
}
}

/**
* Clears the default model on SolRDF.
*
* @throws UnableToDeleteException in case the deletion is not possible.
*/
public void deleteNamedGraph(final String ... graphURIs) throws UnableToDeleteException {
if (graphURIs == null) {
return;
}

try {
for (final String graphURI : graphURIs) {
remoteDataset.deleteModel(graphURI);
}
} catch(final Exception exception) {
throw new UnableToDeleteException(exception);
}
}

/**
* Adds a given set of statements to the unnamed graph.
*
* @param iterator the statements iterator.
* @throws UnableToAddException in case of add (local or remote) failure.
*/
public void add(final StmtIterator iterator) throws UnableToAddException {
try {
remoteDataset.add(model().add(iterator));
} catch (final Exception exception) {
throw new UnableToAddException(exception);
}
}

/**
* Adds a given set of statements to a named graph.
*
* @param uri the graph URI.
* @param iterator the statements iterator.
* @throws UnableToAddException in case of add (local or remote) failure.
*/
public void add(final String uri, final StmtIterator iterator) throws UnableToAddException {
try {
remoteDataset.add(uri, model(uri).add(iterator));
} catch (final Exception exception) {
throw new UnableToAddException(exception);
}
}

/**
* Adds a given set of statements to the unnamed graph.
Expand Down Expand Up @@ -430,7 +505,25 @@ public void add(final String uri, final Reader stream, final String lang) throws
throw new UnableToAddException(exception);
}
}


/**
* Executes a SPARQL ASK.
*
* @param query the ASK SELECT Query.
* @return the {@link ResultSet} that includes matching bindings.
* @throws UnableToExecuteQueryException in case of failure before, during or after the query execution.
*/
public boolean ask(final String askQuery) throws UnableToExecuteQueryException {
QueryExecution execution = null;
try {
return (execution = execution(askQuery)).execAsk();
} catch (final Exception exception) {
throw new UnableToExecuteQueryException(exception);
} finally {
execution.close();
}
}

/**
* Executes a SPARQL SELECT.
*
Expand All @@ -444,7 +537,6 @@ public CloseableResultSet select(final String selectQuery) throws UnableToExecut
execution = execution(selectQuery);
return new CloseableResultSet(execution.execSelect(), execution);
} catch (final Exception exception) {
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
if (execution != null) {
execution.close();
}
Expand Down Expand Up @@ -488,6 +580,25 @@ public Model describe(final String describeQuery) throws UnableToExecuteQueryExc
}
}

/**
* Returns the {@link Model} representation associated with the graph associated with the given URI.
*
* @param graphUri the graph URI.
* @return the {@link Model} representation associated with the graph associated with the given URI.
*/
public Model getNamedModel(final String graphUri) {
return remoteDataset.getModel(graphUri);
}

/**
* Returns the {@link Model} representation associated with the default graph.
*
* @return the {@link Model} representation associated with the default graph.
*/
public Model getDefaultModel() {
return remoteDataset.getModel();
}

/**
* Commits pending changes.
*
Expand Down Expand Up @@ -517,6 +628,13 @@ public void commit(final boolean waitFlush, final boolean waitSearcher) throws U
}
}

/**
* The caller does no longer need this client.
*/
public void done() {
solr.shutdown();
}

/**
* Commits pending changes.
*
Expand Down Expand Up @@ -558,7 +676,7 @@ Model model(final String uri) {
model = ModelFactory.createDefaultModel();
localDataset.addNamedModel(uri, model);
}

model.removeAll();
return model;
}

Expand All @@ -568,6 +686,6 @@ Model model(final String uri) {
* @return the a local default model.
*/
Model model() {
return localDataset.getDefaultModel();
return localDataset.getDefaultModel().removeAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.gazzax.labs.solrdf.client;

/**
* Thrown in case a deletion cannot be executed (or it raises some exception).
*
* @author Andrea Gazzarini
* @since 1.0
*/
public class UnableToDeleteException extends Exception {
private static final long serialVersionUID = 1L;

/**
* Builds a new exception with the given cause.
*
* @param throwable the exception cause.
*/
public UnableToDeleteException(final Throwable throwable) {
super(throwable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import static org.gazzax.labs.solrdf.client.TestUtility.sampleStatements;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.when;

import java.io.FileInputStream;
import java.io.FileReader;
Expand Down
7 changes: 6 additions & 1 deletion solrdf/solrdf-integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@
<dependency>
<groupId>org.gazzax.labs</groupId>
<artifactId>solrdf-core</artifactId>
<version>1.0</version>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr</artifactId>
<version>${solr.version}</version>
<type>war</type>
</dependency>
<dependency>
<groupId>org.gazzax.labs</groupId>
<artifactId>solrdf-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
Expand Down
Loading

0 comments on commit e377432

Please sign in to comment.