Search in sources :

Example 26 with VisibilityBindingSet

use of org.apache.rya.api.model.VisibilityBindingSet in project incubator-rya by apache.

the class KeyValueJoinStateStore method store.

@Override
public void store(final BinaryResult result) {
    requireNonNull(result);
    // The join key prefix is an ordered list of values from the binding set that match the join variables.
    // This is a prefix for every row that holds values for a specific set of join variable values.
    final Side side = result.getSide();
    final VisibilityBindingSet bs = result.getResult();
    final String joinKeyPrefix = makeCommaDelimitedValues(side, joinVars, bs);
    final List<KeyValue<String, VisibilityBindingSet>> values = new ArrayList<>();
    // For each join variable set, we need a start key for scanning,
    final String startKey = joinKeyPrefix + START_RANGE_SUFFIX;
    values.add(new KeyValue<>(startKey, RANGE_MARKER_VALUE));
    // The actual value that was emitted as a result.
    final String valueKey = makeCommaDelimitedValues(side, allVars, bs);
    values.add(new KeyValue<>(valueKey, bs));
    // And the end key for scanning.
    final String endKey = joinKeyPrefix + END_RANGE_SUFFIX;
    values.add(new KeyValue<>(endKey, RANGE_MARKER_VALUE));
    // Write the pairs to the store.
    log.debug("\nStoring the following values: {}\n", values);
    store.putAll(values);
}
Also used : Side(org.apache.rya.streams.kafka.processors.ProcessorResult.BinaryResult.Side) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) KeyValue(org.apache.kafka.streams.KeyValue) ArrayList(java.util.ArrayList)

Example 27 with VisibilityBindingSet

use of org.apache.rya.api.model.VisibilityBindingSet in project incubator-rya by apache.

the class KeyValueJoinStateStore method getJoinedValues.

@Override
public CloseableIterator<VisibilityBindingSet> getJoinedValues(final BinaryResult result) {
    requireNonNull(result);
    // Get an iterator over the values that start with the join variables for the other side.
    final Side otherSide = result.getSide() == Side.LEFT ? Side.RIGHT : Side.LEFT;
    final VisibilityBindingSet bs = result.getResult();
    final String joinKeyPrefix = makeCommaDelimitedValues(otherSide, joinVars, bs);
    final String startKey = joinKeyPrefix + START_RANGE_SUFFIX;
    final String endKey = joinKeyPrefix + END_RANGE_SUFFIX;
    final KeyValueIterator<String, VisibilityBindingSet> rangeIt = store.range(startKey, endKey);
    // Return a CloseableIterator over the range's value fields, skipping the start and end entry.
    return new CloseableIterator<VisibilityBindingSet>() {

        private Optional<VisibilityBindingSet> next = null;

        @Override
        public boolean hasNext() {
            // If the iterator has not been initialized yet, read a value in.
            if (next == null) {
                next = readNext();
            }
            // Return true if there is a next value, otherwise false.
            return next.isPresent();
        }

        @Override
        public VisibilityBindingSet next() {
            // If the iterator has not been initialized yet, read a value in.
            if (next == null) {
                next = readNext();
            }
            // It's illegal to call next() when there is no next value.
            if (!next.isPresent()) {
                throw new IllegalStateException("May not invoke next() when there is nothing left in the Iterator.");
            }
            // Update and return the next value.
            final VisibilityBindingSet ret = next.get();
            log.debug("\nReturning: {}", ret);
            next = readNext();
            return ret;
        }

        private Optional<VisibilityBindingSet> readNext() {
            // Check to see if there's anything left in the iterator.
            if (!rangeIt.hasNext()) {
                return Optional.empty();
            }
            // Read a candidate key/value pair from the iterator.
            KeyValue<String, VisibilityBindingSet> candidate = rangeIt.next();
            // If we are initializing, then the first thing we must read is a start of range marker.
            if (next == null) {
                if (!candidate.key.endsWith(START_RANGE_SUFFIX)) {
                    throw new IllegalStateException("The first key encountered must be a start of range key.");
                }
                log.debug("Read the start of range markers.\n");
                // Read a new candidate to skip this one.
                if (!rangeIt.hasNext()) {
                    throw new IllegalStateException("There must be another entry after the start of range key.");
                }
                candidate = rangeIt.next();
            } else // If that value is an end of range key, then we are finished. Otherwise, return it.
            if (candidate.key.endsWith(END_RANGE_SUFFIX)) {
                log.debug("Read the end of range marker.\n");
                // If there are more messages, that's a problem.
                if (rangeIt.hasNext()) {
                    throw new IllegalStateException("The end of range marker must be the last key in the iterator.");
                }
                return Optional.empty();
            }
            // Otherwise we found a new value.
            return Optional.of(candidate.value);
        }

        @Override
        public void close() throws Exception {
            rangeIt.close();
        }
    };
}
Also used : Side(org.apache.rya.streams.kafka.processors.ProcessorResult.BinaryResult.Side) CloseableIterator(org.apache.rya.api.utils.CloseableIterator) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) Optional(java.util.Optional)

Example 28 with VisibilityBindingSet

use of org.apache.rya.api.model.VisibilityBindingSet in project incubator-rya by apache.

the class ProjectionEvaluator method project.

/**
 * Applies the projection to a value. If the result has a blank node whose ID is not mapped to a value in
 * {@code blankNodes}, then a random UUID will be used.
 *
 * @param bs - The value the projection will be applied to. (not null)
 * @param blankNodes - A map from node source names to the blank nodes that will be used for those names. (not null)
 * @return A new value that is the result of the projection.
 */
public VisibilityBindingSet project(final VisibilityBindingSet bs, final Map<String, BNode> blankNodes) {
    requireNonNull(bs);
    requireNonNull(blankNodes);
    // Apply the projection elements against the original binding set.
    final MapBindingSet result = new MapBindingSet();
    for (final ProjectionElem elem : projectionElems.getElements()) {
        final String sourceName = elem.getSourceName();
        Value value = null;
        // If the binding set already has the source name, then use the target name.
        if (bs.hasBinding(sourceName)) {
            value = bs.getValue(elem.getSourceName());
        } else // If the source name represents a constant value, then use the constant.
        if (constantSources.containsKey(sourceName)) {
            value = constantSources.get(sourceName);
        } else // If the source name represents an anonymous value, then create a Blank Node.
        if (anonymousSources.contains(sourceName)) {
            if (blankNodes.containsKey(sourceName)) {
                value = blankNodes.get(sourceName);
            } else {
                value = vf.createBNode(UUID.randomUUID().toString());
            }
        }
        // Only add the value if there is one. There may not be one if a binding is optional.
        if (value != null) {
            result.addBinding(elem.getTargetName(), value);
        }
    }
    return new VisibilityBindingSet(result, bs.getVisibility());
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) Value(org.openrdf.model.Value) MapBindingSet(org.openrdf.query.impl.MapBindingSet) ProjectionElem(org.openrdf.query.algebra.ProjectionElem)

Example 29 with VisibilityBindingSet

use of org.apache.rya.api.model.VisibilityBindingSet in project incubator-rya by apache.

the class NaturalJoinTest method newLeftResult_noRightMatches.

@Test
public void newLeftResult_noRightMatches() {
    final IterativeJoin naturalJoin = new NaturalJoin();
    // There is a new left result.
    final MapBindingSet newLeftResult = new MapBindingSet();
    newLeftResult.addBinding("name", vf.createLiteral("Bob"));
    // There are no right results that join with the left result.
    final Iterator<VisibilityBindingSet> rightResults = new ArrayList<VisibilityBindingSet>().iterator();
    // Therefore, the left result is a new join result.
    final Iterator<VisibilityBindingSet> newJoinResultsIt = naturalJoin.newLeftResult(new VisibilityBindingSet(newLeftResult), rightResults);
    assertFalse(newJoinResultsIt.hasNext());
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Example 30 with VisibilityBindingSet

use of org.apache.rya.api.model.VisibilityBindingSet in project incubator-rya by apache.

the class NaturalJoinTest method newLeftResult_joinsWithRightResults.

@Test
public void newLeftResult_joinsWithRightResults() {
    final IterativeJoin naturalJoin = new NaturalJoin();
    // There is a new left result.
    final MapBindingSet newLeftResult = new MapBindingSet();
    newLeftResult.addBinding("name", vf.createLiteral("Bob"));
    newLeftResult.addBinding("height", vf.createLiteral("5'9\""));
    // There are a few right results that join with the left result.
    final MapBindingSet nameAge = new MapBindingSet();
    nameAge.addBinding("name", vf.createLiteral("Bob"));
    nameAge.addBinding("age", vf.createLiteral(56));
    final MapBindingSet nameHair = new MapBindingSet();
    nameHair.addBinding("name", vf.createLiteral("Bob"));
    nameHair.addBinding("hairColor", vf.createLiteral("Brown"));
    final Iterator<VisibilityBindingSet> rightResults = Lists.<VisibilityBindingSet>newArrayList(new VisibilityBindingSet(nameAge), new VisibilityBindingSet(nameHair)).iterator();
    // Therefore, there are a few new join results that mix the two together.
    final Iterator<VisibilityBindingSet> newJoinResultsIt = naturalJoin.newLeftResult(new VisibilityBindingSet(newLeftResult), rightResults);
    final Set<BindingSet> newJoinResults = new HashSet<>();
    while (newJoinResultsIt.hasNext()) {
        newJoinResults.add(newJoinResultsIt.next());
    }
    final Set<BindingSet> expected = Sets.<BindingSet>newHashSet();
    final MapBindingSet nameHeightAge = new MapBindingSet();
    nameHeightAge.addBinding("name", vf.createLiteral("Bob"));
    nameHeightAge.addBinding("height", vf.createLiteral("5'9\""));
    nameHeightAge.addBinding("age", vf.createLiteral(56));
    expected.add(new VisibilityBindingSet(nameHeightAge));
    final MapBindingSet nameHeightHair = new MapBindingSet();
    nameHeightHair.addBinding("name", vf.createLiteral("Bob"));
    nameHeightHair.addBinding("height", vf.createLiteral("5'9\""));
    nameHeightHair.addBinding("hairColor", vf.createLiteral("Brown"));
    expected.add(new VisibilityBindingSet(nameHeightHair));
    assertEquals(expected, newJoinResults);
}
Also used : MapBindingSet(org.openrdf.query.impl.MapBindingSet) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) BindingSet(org.openrdf.query.BindingSet) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) MapBindingSet(org.openrdf.query.impl.MapBindingSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)140 Test (org.junit.Test)105 MapBindingSet (org.openrdf.query.impl.MapBindingSet)93 ValueFactory (org.openrdf.model.ValueFactory)66 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)66 HashSet (java.util.HashSet)52 ArrayList (java.util.ArrayList)31 UUID (java.util.UUID)28 VisibilityStatement (org.apache.rya.api.model.VisibilityStatement)28 TopologyFactory (org.apache.rya.streams.kafka.topology.TopologyFactory)24 BindingSet (org.openrdf.query.BindingSet)24 TopologyBuilder (org.apache.kafka.streams.processor.TopologyBuilder)23 RandomUUIDFactory (org.apache.rya.api.function.projection.RandomUUIDFactory)23 URIImpl (org.openrdf.model.impl.URIImpl)19 QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)19 VariableOrder (org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder)18 Bytes (org.apache.fluo.api.data.Bytes)16 PcjMetadata (org.apache.rya.indexing.pcj.storage.PcjMetadata)15 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)12 Statement (org.openrdf.model.Statement)12