Skip to content
This repository has been archived by the owner on Jan 3, 2019. It is now read-only.

Commit

Permalink
Ensure child resources are removed from index on deletes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Woods committed Feb 5, 2014
1 parent a9269e1 commit 294c4fe
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 8 deletions.
Expand Up @@ -104,7 +104,7 @@ public Callable<Void> removeSynch(final String subject) {
LOGGER.debug("Received remove for: {}", subject);
// find triples/quads to delete
final String describeQuery = "DESCRIBE <" + subject + ">";
final QueryEngineHTTP qexec = new QueryEngineHTTP( queryBase, describeQuery );
final QueryEngineHTTP qexec = buildQueryEngineHTTP(describeQuery);
final Iterator<Triple> results = qexec.execDescribeTriples();

// build list of triples to delete
Expand All @@ -131,7 +131,7 @@ public Callable<Void> removeSynch(final String subject) {
qexec.close();

// build update commands
final UpdateRequest del = new UpdateRequest();
final UpdateRequest del = buildUpdateRequest();
for (final String uri : uris) {
final String cmd = "DELETE WHERE { <" + uri + "> ?p ?o }";
LOGGER.debug("Executing: {}", cmd);
Expand All @@ -148,8 +148,8 @@ public Callable<Void> removeSynch(final String subject) {
* suffix.
**/
private boolean matches( final String uri1, final String uri2 ) {
return uri1.equals(uri2) || uri1.startsWith(uri2 + "/")
|| uri1.startsWith(uri2 + "#");
return uri1.equals(uri2) || uri2.startsWith(uri1 + "/")
|| uri2.startsWith(uri1 + "#");
}

private Callable<Void> exec(final UpdateRequest update) {
Expand Down Expand Up @@ -226,7 +226,7 @@ public void run() {
public int countTriples(final String uri) {
// perform describe query
final String describeQuery = "DESCRIBE <" + uri + ">";
final QueryEngineHTTP qexec = new QueryEngineHTTP( queryBase, describeQuery );
final QueryEngineHTTP qexec = buildQueryEngineHTTP(describeQuery);
final Iterator<Triple> results = qexec.execDescribeTriples();

// count triples
Expand Down Expand Up @@ -271,5 +271,18 @@ public ListeningExecutorService executorService() {
return executorService;
}

/**
* Note: Protected for Unit Tests to overwrite.
*/
protected QueryEngineHTTP buildQueryEngineHTTP(String describeQuery) {
return new QueryEngineHTTP( queryBase, describeQuery );
}

/**
* Note: Protected for Unit Tests to overwrite.
*/
protected UpdateRequest buildUpdateRequest() {
return new UpdateRequest();
}

}
Expand Up @@ -15,24 +15,104 @@
*/
package org.fcrepo.indexer.sparql;

import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
import static com.hp.hpl.jena.graph.NodeFactory.createURI;
import static org.fcrepo.indexer.Indexer.IndexerType.RDF;
import static org.junit.Assert.assertEquals;
import static org.slf4j.LoggerFactory.getLogger;

import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP;
import com.hp.hpl.jena.update.UpdateRequest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;

import java.util.HashSet;
import java.util.Set;


/**
* @author Andrew Woods
* @date Feb 04 2014
*/
public class SparqlIndexerTest {

private static final Logger LOGGER = getLogger(SparqlIndexerTest.class);

private SparqlIndexer testIndexer = new SparqlIndexer();
private SparqlIndexer testIndexer = new MockSparqlIndexer();

@Before
public void before() {
MockitoAnnotations.initMocks(this);
}

@Test
public void testGetIndexerType() {
assertEquals("Got wrong indexer type!", RDF, testIndexer
.getIndexerType());
assertEquals("Got wrong indexer type!", RDF, testIndexer.getIndexerType());
LOGGER.debug("Received correct indexer type.");
}

@Test
public void testRemoveSynch() {
testIndexer.removeSynch("info://obj-0");

String cmd0 = "DELETE WHERE { <" + createURI("info://obj-0") + "> ?p ?o }";
String cmd1 = "DELETE WHERE { <" + createURI("info://obj-0/fcr:content") + "> ?p ?o }";
String cmd2 = "DELETE WHERE { <" + createURI("info://obj-0/child") + "> ?p ?o }";
Mockito.verify(updateRequest).add(cmd0);
Mockito.verify(updateRequest).add(cmd1);
Mockito.verify(updateRequest).add(cmd2);
}

@Test
public void testCountTriples() {
int count = testIndexer.countTriples("info://obj-0");
Assert.assertEquals(4, count);
}

@Test
public void testUpdateSynch() {
Model model = ModelFactory.createDefaultModel();
testIndexer.updateSynch("", model);
}

@Mock
private QueryEngineHTTP queryEngineHTTP;

@Mock
private UpdateRequest updateRequest;

/**
* Test extension of SparqlIndexer to eliminate HTTP interactions.
*/
private class MockSparqlIndexer extends SparqlIndexer {

protected QueryEngineHTTP buildQueryEngineHTTP(String describeQuery) {
Triple t0 = new Triple(createURI("info://sub"), createLiteral("p"), createURI("info://obj-0"));
Triple t2 = new Triple(createURI("info://sub"), createLiteral("p"), createURI("info://obj-0/fcr:content"));
Triple t1 = new Triple(createURI("info://sub"), createLiteral("p"), createURI("info://obj-1"));
Triple t3 = new Triple(createURI("info://obj-0/child"), createLiteral("p"), createURI("info://obj-1"));

Set<Triple> triples = new HashSet<>();
triples.add(t0);
triples.add(t1);
triples.add(t2);
triples.add(t3);

Mockito.when(queryEngineHTTP.execDescribeTriples()).thenReturn(triples.iterator());
return queryEngineHTTP;
}

protected UpdateRequest buildUpdateRequest() {
return updateRequest;
}
}

}

0 comments on commit 294c4fe

Please sign in to comment.