Skip to content

Commit

Permalink
Code cleanup and factoring managed-RDF policy into its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Oct 29, 2013
1 parent c0d3582 commit 3d2be9e
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 159 deletions.
Expand Up @@ -18,7 +18,6 @@

import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.ImmutableSet.builder;
import static com.google.common.collect.Iterators.transform;
import static com.hp.hpl.jena.graph.NodeFactory.createAnon;
import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
import static com.hp.hpl.jena.graph.NodeFactory.createURI;
Expand Down Expand Up @@ -68,7 +67,7 @@ public FixityRdfContext(final Node node, final GraphSubjects graphSubjects,
final Iterable<FixityResult> blobs) throws RepositoryException {
super(node, graphSubjects, lowLevelStorageService);

concat(Iterators.concat(transform(blobs.iterator(),
concat(Iterators.concat(Iterators.transform(blobs.iterator(),
new Function<FixityResult, Iterator<Triple>>() {

@Override
Expand Down
Expand Up @@ -18,8 +18,6 @@

import static com.google.common.base.Predicates.not;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Iterators.filter;
import static com.google.common.collect.Iterators.transform;
import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
import static com.hp.hpl.jena.graph.Triple.create;
import static com.hp.hpl.jena.vocabulary.RDF.type;
Expand Down Expand Up @@ -130,10 +128,10 @@ private Iterator<Triple> parentContext() throws RepositoryException {
private Iterator<Triple> childrenContext(final Node pageContext) throws RepositoryException {

final Iterator<javax.jcr.Node> niceChildren =
filter(new NodeIterator(node().getNodes()), not(nastyChildren));
Iterators.filter(new NodeIterator(node().getNodes()), not(nastyChildren));

return Iterators.concat(
transform(niceChildren, child2triples(pageContext)));
Iterators.transform(niceChildren, child2triples(pageContext)));
}

private Function<javax.jcr.Node, Iterator<Triple>> child2triples(
Expand Down
Expand Up @@ -18,7 +18,6 @@

import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Iterators.forArray;
import static com.google.common.collect.Iterators.transform;
import static com.hp.hpl.jena.graph.NodeFactory.createURI;
import static com.hp.hpl.jena.graph.Triple.create;
import static com.hp.hpl.jena.vocabulary.RDF.type;
Expand All @@ -37,6 +36,7 @@
import org.slf4j.Logger;

import com.google.common.base.Function;
import com.google.common.collect.Iterators;
import com.hp.hpl.jena.graph.Triple;

/**
Expand Down Expand Up @@ -89,7 +89,7 @@ public NodeRdfContext(final Node node, final GraphSubjects graphSubjects,

// add JCR mixins as rdf:type triples
final Iterator<NodeType> nodeTypes = forArray(node.getMixinNodeTypes());
concat(transform(nodeTypes, nodetype2triple()));
concat(Iterators.transform(nodeTypes, nodetype2triple()));
}

/**
Expand Down
Expand Up @@ -17,8 +17,6 @@
package org.fcrepo.kernel.rdf.impl;

import static com.google.common.base.Predicates.not;
import static com.google.common.collect.Iterators.filter;
import static com.google.common.collect.Iterators.transform;
import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
import static com.hp.hpl.jena.graph.Triple.create;
import static org.fcrepo.jcr.FedoraJcrTypes.ROOT;
Expand Down Expand Up @@ -103,7 +101,7 @@ private void putPropertiesIntoContext() throws RepositoryException {
// add triples describing storage of content child
lowLevelStorageService().setRepository(
node().getSession().getRepository());
concat(transform(lowLevelStorageService().getLowLevelCacheEntries(
concat(Iterators.transform(lowLevelStorageService().getLowLevelCacheEntries(
contentNode).iterator(),
new Function<LowLevelCacheEntry, Triple>() {

Expand All @@ -127,17 +125,17 @@ private Iterator<Triple> triplesFromProperties(final javax.jcr.Node n)
throws RepositoryException {
LOGGER.debug("Creating triples for node: {}", n);
final UnmodifiableIterator<Property> nonBinaryProperties =
filter(new PropertyIterator(n.getProperties()),
Iterators.filter(new PropertyIterator(n.getProperties()),
not(isBinaryProperty));

final UnmodifiableIterator<Property> nonBinaryPropertiesCopy =
filter(new PropertyIterator(n.getProperties()),
Iterators.filter(new PropertyIterator(n.getProperties()),
not(isBinaryProperty));

return Iterators.concat(new ZippingIterator<>(
transform(
Iterators.transform(
nonBinaryProperties, property2values),
transform(
Iterators.transform(
nonBinaryPropertiesCopy, property2triple)));

}
Expand Down
Expand Up @@ -16,7 +16,6 @@
package org.fcrepo.kernel.rdf.impl;

import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Iterators.transform;
import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
import static com.hp.hpl.jena.graph.Triple.create;
import static org.fcrepo.kernel.RdfLexicon.HAS_VERSION;
Expand Down Expand Up @@ -71,7 +70,7 @@ public VersionsRdfContext(final Node node, final GraphSubjects graphSubjects,
}

private Iterator<Triple> versionTriples() throws RepositoryException {
return Iterators.concat(transform(new VersionIterator(versionHistory
return Iterators.concat(Iterators.transform(new VersionIterator(versionHistory
.getAllVersions()), version2triples));
}

Expand Down
Expand Up @@ -30,14 +30,14 @@
*
* @author ajs6f
* @date Oct 10, 2013
* @param <F>
* @param <T>
* @param <FromType>
* @param <ToType>
*/
public class ZippingIterator<F, T> extends AbstractIterator<T> {
public class ZippingIterator<FromType, ToType> extends AbstractIterator<ToType> {

private Iterator<F> from;
private Iterator<FromType> from;

private Iterator<Function<F, T>> through;
private Iterator<Function<FromType, ToType>> through;

private static Logger LOGGER = getLogger(ZippingIterator.class);

Expand All @@ -47,19 +47,19 @@ public class ZippingIterator<F, T> extends AbstractIterator<T> {
* @param from
* @param through
*/
public ZippingIterator(final Iterator<F> from,
final Iterator<Function<F, T>> through) {
public ZippingIterator(final Iterator<FromType> from,
final Iterator<Function<FromType, ToType>> through) {
this.from = from;
this.through = through;
}

@Override
protected T computeNext() {
protected ToType computeNext() {
final boolean hasNext = (from.hasNext() && through.hasNext());
if (hasNext) {
LOGGER.debug("Found next element.");
final F f = from.next();
final Function<F, T> t = through.next();
final FromType f = from.next();
final Function<FromType, ToType> t = through.next();
LOGGER.debug("Supplying from next element {} through function {}",
f, t);
return t.apply(f);
Expand Down
Expand Up @@ -16,26 +16,22 @@

package org.fcrepo.kernel.utils.iterators;

import static com.google.common.base.Predicates.not;
import static com.google.common.collect.Iterators.filter;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;

import static org.fcrepo.kernel.RdfLexicon.isManagedNamespace;
import static org.fcrepo.kernel.RdfLexicon.isManagedPredicate;
import java.util.Iterator;

import com.google.common.base.Predicate;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;

/**
* This wraps an {@link Iterator} of {@link Triple}s and produces only those
* safe for persistence into the repo as properties. It does this by filtering
* all triples with managed predicates, as defined in {@link RdfLexicon}.
* {@link Predicate}s for determining when RDF is managed by the repository.
*
* @author ajs6f
* @date Oct 23, 2013
*/
public class UnmanagedRdfStream extends RdfStream {
public class ManagedRdf {

private static final Model model = createDefaultModel();

Expand All @@ -50,18 +46,13 @@ public boolean apply(final Triple t) {

};

/**
* Ordinary constructor.
*
* @param triples
*/
public UnmanagedRdfStream(final Iterator<Triple> triples) {
super(triples);
}
public static final Predicate<Resource> isManagedMixin =
new Predicate<Resource>() {

@Override
protected Iterator<Triple> delegate() {
return filter(triples, not(isManagedTriple));
}
@Override
public boolean apply(final Resource m) {
return isManagedNamespace.apply(m.getNameSpace());
}

}
};
}
Expand Up @@ -19,12 +19,11 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.and;
import static com.google.common.base.Predicates.not;
import static com.google.common.collect.Iterators.filter;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static com.hp.hpl.jena.vocabulary.RDF.type;
import static org.fcrepo.kernel.RdfLexicon.isManagedNamespace;
import static org.fcrepo.kernel.utils.JcrRdfTools.getJcrNamespaceForRDFNamespace;
import static org.fcrepo.kernel.utils.iterators.UnmanagedRdfStream.isManagedTriple;
import static org.fcrepo.kernel.utils.iterators.ManagedRdf.isManagedMixin;
import static org.fcrepo.kernel.utils.iterators.ManagedRdf.isManagedTriple;
import static org.slf4j.LoggerFactory.getLogger;

import java.util.Map;
Expand Down Expand Up @@ -103,10 +102,9 @@ public boolean apply(final Triple t) {

};
// we knock out managed RDF and non-Fedora RDF

this.stream =
new RdfStream(filter(stream, and(not(isManagedTriple),
isFedoraSubjectTriple))).addNamespaces(stream.namespaces());
stream.withThisContext(stream.filter(and(not(isManagedTriple),
isFedoraSubjectTriple)).iterator());
this.session = session;
}

Expand All @@ -129,7 +127,7 @@ protected void operateOnTriple(final Statement t)
// mixins. If it isn't, treat it as a "data" property.
if (t.getPredicate().equals(type) && t.getObject().isResource()) {
final Resource mixinResource = t.getObject().asResource();
if (!isManagedNamespace.apply(mixinResource.getNameSpace())) {
if (!isManagedMixin.apply(mixinResource)) {
LOGGER.debug("Operating on node: {} with mixin: {}.",
subjectNode, mixinResource);
operateOnMixin(mixinResource, subjectNode);
Expand Down
Expand Up @@ -18,13 +18,13 @@

import static com.google.common.collect.ImmutableSet.copyOf;
import static com.google.common.collect.Iterators.singletonIterator;
import static com.google.common.collect.Iterators.transform;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ForwardingIterator;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
Expand Down Expand Up @@ -81,10 +81,20 @@ public <Tr extends Triple, T extends Collection<Tr>> RdfStream(
*
* @param triples
*/
public <T extends Triple> RdfStream(final T[] triples) {
public <T extends Triple> RdfStream(final T... triples) {
this(Iterators.forArray(triples));
}

/**
* Constructor that begins the stream with proffered statements.
*
* @param statements
*/
public <T extends Statement> RdfStream(final T... statements) {
this(Iterators.transform(Iterators.forArray(statements),
statement2triple));
}

/**
* Constructor that begins the stream with proffered triple.
*
Expand Down Expand Up @@ -143,7 +153,7 @@ public <T extends Triple> RdfStream concat(final T newTriple) {
* @param newTriples Triples to add.
* @return This object for continued use.
*/
public <T extends Triple> RdfStream concat(final T[] newTriples) {
public <T extends Triple> RdfStream concat(final T... newTriples) {
triples = Iterators.concat(Iterators.forArray(newTriples), triples);
return this;
}
Expand Down Expand Up @@ -183,6 +193,26 @@ public RdfStream skip(final Integer skipNum) {
return withThisContext(Iterables.skip(this, skipNum));
}

/**
* As {@link Iterables#filter(Iterable, Predicate)} while maintaining context.
*
* @param predicate
* @return
*/
public RdfStream filter(final Predicate<? super Triple> predicate) {
return withThisContext(Iterables.filter(this, predicate));
}

/**
* As {@link Iterators#transform(Iterator, Function)}.
*
* @param f
* @return
*/
public <ToType> Iterator<ToType> transform(final Function<? super Triple, ToType> f) {
return Iterators.transform(this, f);
}

/**
* @param prefix
* @param uri
Expand Down Expand Up @@ -225,7 +255,7 @@ public Model asModel() {
* @return
*/
public static RdfStream fromModel(final Model model) {
final Iterator<Triple> triples = transform(model.listStatements(), statement2triple);
final Iterator<Triple> triples = Iterators.transform(model.listStatements(), statement2triple);
return new RdfStream(triples).addNamespaces(model.getNsPrefixMap());
}

Expand Down Expand Up @@ -287,4 +317,9 @@ public boolean equals(final Object o) {
}
}

@Override
public int hashCode() {
return namespaces().hashCode() + 2 * triples.hashCode();
}

}
Expand Up @@ -23,7 +23,7 @@
import com.google.common.collect.ForwardingIterator;

/**
* Type-aware iterator to wrap the JCR NodeIterator
* Type-aware iterator to wrap the JCR NodeIterator.
*
* @author ajs6f
* @date Apr 20, 2013
Expand All @@ -34,7 +34,7 @@ public class VersionIterator extends ForwardingIterator<Version> implements
private javax.jcr.version.VersionIterator i;

/**
* Wrap the NodeIterator with our generic version
* Wrap the NodeIterator with our generic version.
*
* @param i
*/
Expand Down

0 comments on commit 3d2be9e

Please sign in to comment.