Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactor RDF production to leverage iterators
Resolves https://www.pivotaltracker.com/story/show/57304426

Migrated RDF generated from namespaces to new iterative modality
Switching to use iterated RDF production for triples derived from node properties, except for root node.
RDF triples from version info now returned from iterating components
Now RDF triples returned from Iterators of Nodes also returned from iteration
Fixity result -sourced RDF triples now coming from iterator machinery
RDF based in JCR hierarchy now being constructed in iterator mode
Factoring our type-safe iterators through Guavas ForwardingIterator
Lazy-ifacting Version-based RDF generation
  • Loading branch information
ajs6f authored and Andrew Woods committed Oct 22, 2013
1 parent 4efdca8 commit b225254
Show file tree
Hide file tree
Showing 47 changed files with 3,750 additions and 1,515 deletions.
Expand Up @@ -75,19 +75,19 @@ public void testGetWorkspaces() throws Exception {
when(mockWorkspace.getNamespaceRegistry()).thenReturn(
mockNamespaceRegistry);
when(mockNamespaceRegistry.getPrefixes()).thenReturn(new String[]{"yyy"});

when(mockNamespaceRegistry.getURI("yyy")).thenReturn("http://example.com");
when(mockUriInfo.getBaseUriBuilder()).thenReturn(mockUriBuilder);
when(mockUriBuilder.path(any(String.class))).thenReturn(mockUriBuilder);

URI uri = new URI("http://base/workspaces");
final URI uri = new URI("http://base/workspaces");
when(mockUriBuilder.build()).thenReturn(uri);

// Do the test.
Dataset dataset = workspaces.getWorkspaces();
final Dataset dataset = workspaces.getWorkspaces();

Resource resource = dataset.getDefaultModel()
final Resource resource = dataset.getDefaultModel()
.getResource(uri.toString());
String resourceName = resource.toString();
final String resourceName = resource.toString();

org.junit.Assert.assertNotNull(resourceName);
org.junit.Assert.assertEquals(uri.toString(), resourceName);
Expand Down
Expand Up @@ -16,18 +16,23 @@

package org.fcrepo.integration.http.api;

import static com.hp.hpl.jena.graph.Node.ANY;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createPlainLiteral;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static org.fcrepo.http.commons.test.util.TestHelpers.parseTriples;
import static org.fcrepo.kernel.RdfLexicon.HAS_COMPUTED_CHECKSUM;
import static org.fcrepo.kernel.RdfLexicon.HAS_COMPUTED_SIZE;
import static org.fcrepo.kernel.RdfLexicon.HAS_FIXITY_STATE;
import static org.fcrepo.kernel.RdfLexicon.IS_FIXITY_RESULT_OF;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.fcrepo.http.commons.test.util.TestHelpers;
import org.fcrepo.kernel.RdfLexicon;
import org.junit.Test;

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.update.GraphStore;

Expand All @@ -47,28 +52,20 @@ public void testCheckDatastreamFixity() throws Exception {
final HttpResponse response = execute(method2);
assertEquals(200, response.getStatusLine().getStatusCode());
final HttpEntity entity = response.getEntity();
final GraphStore graphStore =
TestHelpers.parseTriples(entity.getContent());
final GraphStore graphStore = parseTriples(entity.getContent());

logger.info("Got triples {}", graphStore);

assertTrue(graphStore.contains(Node.ANY, Node.ANY,
RdfLexicon.IS_FIXITY_RESULT_OF.asNode(), ResourceFactory
.createResource(
serverAddress +
"FedoraDatastreamsTest11/zxc")
assertTrue(graphStore.contains(ANY, ANY, IS_FIXITY_RESULT_OF.asNode(),
createResource(serverAddress + "FedoraDatastreamsTest11/zxc")
.asNode()));
assertTrue(graphStore.contains(Node.ANY, Node.ANY,
RdfLexicon.HAS_FIXITY_STATE.asNode(), ResourceFactory
.createPlainLiteral("SUCCESS").asNode()));
assertTrue(graphStore.contains(ANY, ANY, HAS_FIXITY_STATE.asNode(),
createPlainLiteral("SUCCESS").asNode()));

assertTrue(graphStore.contains(Node.ANY, Node.ANY,
RdfLexicon.HAS_COMPUTED_CHECKSUM.asNode(),
ResourceFactory.createResource(
"urn:sha1:0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
.asNode()));
assertTrue(graphStore.contains(Node.ANY, Node.ANY,
RdfLexicon.HAS_COMPUTED_SIZE.asNode(), ResourceFactory
assertTrue(graphStore.contains(ANY, ANY, HAS_COMPUTED_CHECKSUM.asNode(),
createResource("urn:sha1:0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33").asNode()));
assertTrue(graphStore.contains(ANY, ANY,
HAS_COMPUTED_SIZE.asNode(), ResourceFactory
.createTypedLiteral(3).asNode()));
}
}
Expand Up @@ -19,6 +19,10 @@
import static com.hp.hpl.jena.graph.Node.ANY;
import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
import static com.hp.hpl.jena.graph.NodeFactory.createURI;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createPlainLiteral;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createProperty;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static java.util.UUID.randomUUID;
import static java.util.regex.Pattern.DOTALL;
import static java.util.regex.Pattern.compile;
Expand Down Expand Up @@ -46,6 +50,8 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.UUID;

Expand Down Expand Up @@ -74,6 +80,8 @@
import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.sparql.core.Quad;
import com.hp.hpl.jena.update.GraphStore;
Expand Down Expand Up @@ -294,45 +302,36 @@ public void testGetObjectGraphHtml() throws Exception {

@Test
public void testGetObjectGraph() throws Exception {
logger.debug("Entering testGetObjectGraph()...");
client.execute(postObjMethod("FedoraDescribeTestGraph"));
final HttpGet getObjMethod =
new HttpGet(serverAddress + "FedoraDescribeTestGraph");
getObjMethod.addHeader("Accept", "application/n-triples");
getObjMethod.addHeader("Accept", "application/rdf+xml");
final HttpResponse response = client.execute(getObjMethod);
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
final String content = EntityUtils.toString(response.getEntity());

logger.debug("Retrieved object graph:\n" + content);

final Model model = createDefaultModel();
model.read(response.getEntity().getContent(), null);
try (final Writer w = new StringWriter()) {
model.write(w);
logger.debug(
"Retrieved object graph for testGetObjectGraph():\n {}", w);
}

assertEquals("application/sparql-update", response.getFirstHeader("Accept-Patch").getValue());
assertEquals("http://www.w3.org/ns/ldp/Resource;rel=\"type\"", response.getFirstHeader("Link").getValue());
final Resource nodeUri = createResource(serverAddress + "FedoraDescribeTestGraph");
assertTrue("Didn't find inlined resources!", model.contains(nodeUri,
createProperty("http://www.w3.org/ns/ldp#inlinedResource")));

assertTrue(
"Expect inlined resources",
compile(
"<" +
serverAddress +
"FedoraDescribeTestGraph> <http://www.w3.org/ns/ldp#inlinedResource>",
DOTALL).matcher(content).find());

assertTrue(
"Didn't find an expected ntriple",
compile(
"<" +
serverAddress +
"FedoraDescribeTestGraph> <" + REPOSITORY_NAMESPACE + "mixinTypes> \"fedora:object\" \\.",
DOTALL).matcher(content).find());
assertTrue("Didn't find an expected triple!", model.contains(nodeUri,
createProperty(REPOSITORY_NAMESPACE + "mixinTypes"),
createPlainLiteral("fedora:object"))
);

assertTrue(
"Didn't find an expected ntriple",
compile(
"<" +
serverAddress +
"FedoraDescribeTestGraph> <" + REPOSITORY_NAMESPACE + "mixinTypes> \"fedora:object\" \\.",
DOTALL).matcher(content).find());

logger.debug("Leaving testGetObjectGraph()...");
}

@Test
Expand All @@ -342,16 +341,33 @@ public void testGetObjectGraphWithChildren() throws Exception {
client.execute(postObjMethod("FedoraDescribeWithChildrenTestGraph/b"));
client.execute(postObjMethod("FedoraDescribeWithChildrenTestGraph/c"));
final HttpGet getObjMethod =
new HttpGet(serverAddress + "FedoraDescribeWithChildrenTestGraph?limit=1&offset=1");
getObjMethod.addHeader("Accept", "application/n-triples");
new HttpGet(serverAddress + "FedoraDescribeWithChildrenTestGraph");
getObjMethod.addHeader("Accept", "application/rdf+xml");
final HttpResponse response = client.execute(getObjMethod);
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
final String content = EntityUtils.toString(response.getEntity());

logger.debug("Retrieved object graph:\n" + content);

assertEquals(serverAddress + "FedoraDescribeWithChildrenTestGraph?limit=1&offset=0;rel=\"first\"", response.getFirstHeader("Link").getValue());
final Model model = createDefaultModel();
model.read(response.getEntity().getContent(), null);
try (final Writer w = new StringWriter()) {
model.write(w);
logger.debug(
"Retrieved object graph for testGetObjectGraphWithChildren():\n {}",
w);
}
final Resource subjectUri =
createResource(serverAddress
+ "FedoraDescribeWithChildrenTestGraph");
assertTrue(
"Didn't find child node!",
model.contains(
subjectUri,
createProperty(REPOSITORY_NAMESPACE + "hasChild"),
createResource(serverAddress
+ "FedoraDescribeWithChildrenTestGraph/c")));
logger.debug("Found Link header: {}", response.getFirstHeader("Link")
.getValue());
assertEquals("http://www.w3.org/ns/ldp/Resource;rel=\"type\"", response
.getFirstHeader("Link").getValue());
}

@Test
Expand Down Expand Up @@ -506,26 +522,30 @@ public void testReplaceGraph() throws Exception {
replaceMethod.addHeader("Content-Type", "application/n3");
final BasicHttpEntity e = new BasicHttpEntity();
e.setContent(new ByteArrayInputStream(
("<" + subjectURI + "> <info:rubydora#label> \"asdfg\"")
.getBytes()));
("<" + subjectURI + "> <info:rubydora#label> \"asdfg\"")
.getBytes()));
replaceMethod.setEntity(e);
final HttpResponse response = client.execute(replaceMethod);
assertEquals(204, response.getStatusLine().getStatusCode());


final HttpGet getObjMethod = new HttpGet(subjectURI);

getObjMethod.addHeader("Accept", "application/n-triples");
getObjMethod.addHeader("Accept", "application/rdf+xml");
final HttpResponse getResponse = client.execute(getObjMethod);
assertEquals(OK.getStatusCode(), getResponse.getStatusLine()
.getStatusCode());
final String content = EntityUtils.toString(getResponse.getEntity());
logger.debug("Retrieved object graph:\n" + content);

assertTrue("Didn't find a triple we tried to create.", compile(
"<" + subjectURI + "> <info:rubydora#label> \"asdfg\" \\.",
DOTALL).matcher(content).find());

final Model model = createDefaultModel();
model.read(getResponse.getEntity().getContent(), null);
try (final Writer w = new StringWriter()) {
model.write(w);
logger.debug(
"Retrieved object graph for testReplaceGraph():\n {}", w);
}
assertTrue("Didn't find a triple we tried to create!", model.contains(
createResource(subjectURI),
createProperty("info:rubydora#label"),
createPlainLiteral("asdfg")));

}

Expand Down Expand Up @@ -569,8 +589,7 @@ public void testDescribeSize() throws Exception {
graphStore.getDefaultGraph().find(ANY, HAS_OBJECT_SIZE.asNode(),
ANY);

final Integer oldSize =
(Integer) iterator.next().getObject().getLiteralValue();
final String oldSize = (String) iterator.next().getObject().getLiteralValue();

assertEquals(CREATED.getStatusCode(),
getStatus(postObjMethod(sizeNode)));
Expand All @@ -588,13 +607,12 @@ public void testDescribeSize() throws Exception {
graphStore.getDefaultGraph().find(ANY,
HAS_OBJECT_SIZE.asNode(), ANY);

final Integer newSize =
(Integer) iterator.next().getObject().getLiteralValue();
final String newSize = (String) iterator.next().getObject().getLiteralValue();

logger.debug("Old size was: " + oldSize + " and new size was: " +
newSize);
assertTrue("No increment in size occurred when we expected one!",
oldSize < newSize);
Integer.parseInt(oldSize) < Integer.parseInt(newSize));
}

@Test
Expand All @@ -609,11 +627,10 @@ public void testDescribeCount() throws Exception {
graphStore.toString());

Iterator<Triple> iterator =
graphStore.getDefaultGraph().find(Node.ANY,
HAS_OBJECT_COUNT.asNode(), Node.ANY);
graphStore.getDefaultGraph().find(ANY, HAS_OBJECT_COUNT.asNode(),
ANY);

final Integer oldSize =
(Integer) iterator.next().getObject().getLiteralValue();
final String oldSize = (String) iterator.next().getObject().getLiteralValue();

assertEquals(CREATED.getStatusCode(),
getStatus(postObjMethod("countNode")));
Expand All @@ -628,16 +645,16 @@ public void testDescribeCount() throws Exception {
graphStore.toString());

iterator =
graphStore.getDefaultGraph().find(Node.ANY,
HAS_OBJECT_COUNT.asNode(), Node.ANY);
graphStore.getDefaultGraph().find(ANY, HAS_OBJECT_COUNT.asNode(),
ANY);

final Integer newSize =
(Integer) iterator.next().getObject().getLiteralValue();
final String newSize =
(String) iterator.next().getObject().getLiteralValue();

logger.debug("Old size was: " + oldSize + " and new size was: " +
newSize);
assertTrue("No increment in count occurred when we expected one!",
oldSize < newSize);
Integer.parseInt(oldSize) < Integer.parseInt(newSize));
}

/**
Expand Down
10 changes: 5 additions & 5 deletions fcrepo-kernel/src/main/java/org/fcrepo/kernel/FedoraObject.java
Expand Up @@ -16,6 +16,8 @@
package org.fcrepo.kernel;

import static org.fcrepo.kernel.utils.FedoraTypesUtils.isFedoraObject;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.modeshape.jcr.api.JcrConstants.NT_FOLDER;
import static org.slf4j.LoggerFactory.getLogger;

import javax.jcr.Node;
Expand Down Expand Up @@ -72,14 +74,12 @@ private void mixinTypeSpecificCrap() {
try {
if (node.isNew() || !hasMixin(node)) {
logger.debug("Setting {} properties on a {} node {}...",
FEDORA_OBJECT,
JcrConstants.NT_FOLDER,
node.getPath());
FEDORA_OBJECT, NT_FOLDER, node.getPath());
node.addMixin(FEDORA_OBJECT);
}
} catch (RepositoryException e) {
} catch (final RepositoryException e) {
logger.warn("Could not decorate {} with {} properties: {} ",
JcrConstants.JCR_CONTENT, FEDORA_OBJECT, e);
JCR_CONTENT, FEDORA_OBJECT, e);
}
}

Expand Down
Expand Up @@ -236,7 +236,7 @@ public Dataset updatePropertiesDataset(final GraphSubjects subjects,
* @throws RepositoryException
*/
public Dataset getPropertiesDataset(final GraphSubjects subjects,
final long offset, final int limit)
final long offset, final int limit)
throws RepositoryException {

if (this.properties != null) {
Expand Down Expand Up @@ -267,8 +267,7 @@ public Dataset getVersionDataset(final GraphSubjects subjects)
throws RepositoryException {
final Model model =
JcrRdfTools.withContext(subjects, node.getSession())
.getJcrPropertiesModel(getVersionHistory(node),
subjects.getGraphSubject(node));
.getJcrVersionPropertiesModel(node);

final Dataset dataset = DatasetFactory.create(model);

Expand Down
16 changes: 16 additions & 0 deletions fcrepo-kernel/src/main/java/org/fcrepo/kernel/RdfLexicon.java
Expand Up @@ -101,6 +101,22 @@ public final class RdfLexicon {
public static final Property NEXT_PAGE =
createProperty("http://www.w3.org/ns/ldp#nextPage");

public static final Property MEMBERS_INLINED =
createProperty("http://www.w3.org/ns/ldp#membersInlined");
public static final Property CONTAINER =
createProperty("http://www.w3.org/ns/ldp#Container");
public static final Property MEMBERSHIP_SUBJECT =
createProperty("http://www.w3.org/ns/ldp#membershipSubject");
public static final Property MEMBERSHIP_PREDICATE =
createProperty("http://www.w3.org/ns/ldp#membershipPredicate");
public static final Property MEMBERSHIP_OBJECT =
createProperty("http://www.w3.org/ns/ldp#membershipObject");
public static final Property MEMBER_SUBJECT =
createProperty("http://www.w3.org/ns/ldp#MemberSubject");
public static final Property INLINED_RESOURCE =
createProperty("http://www.w3.org/ns/ldp#inlinedResource");


// REPOSITORY INFORMATION
public static final Property HAS_OBJECT_COUNT =
createProperty(REPOSITORY_NAMESPACE + "objectCount");
Expand Down

0 comments on commit b225254

Please sign in to comment.