Search in sources :

Example 1 with Side

use of org.apache.rya.streams.kafka.processors.ProcessorResult.BinaryResult.Side 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 2 with Side

use of org.apache.rya.streams.kafka.processors.ProcessorResult.BinaryResult.Side 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)

Aggregations

VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)2 Side (org.apache.rya.streams.kafka.processors.ProcessorResult.BinaryResult.Side)2 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1 KeyValue (org.apache.kafka.streams.KeyValue)1 CloseableIterator (org.apache.rya.api.utils.CloseableIterator)1