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);
}
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();
}
};
}
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());
}
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());
}
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);
}
Aggregations