use of org.apache.rya.api.utils.CloseableIterator 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.utils.CloseableIterator in project incubator-rya by apache.
the class MongoPcjDocuments method queryForBindings.
private CloseableIterator<BindingSet> queryForBindings(final Document query) {
final FindIterable<Document> rez = pcjCollection.find(query);
final Iterator<Document> resultsIter = rez.iterator();
return new CloseableIterator<BindingSet>() {
@Override
public boolean hasNext() {
return resultsIter.hasNext();
}
@Override
public BindingSet next() {
final Document bs = resultsIter.next();
final MapBindingSet binding = new MapBindingSet();
for (final String key : bs.keySet()) {
if (key.equals(VISIBILITIES_FIELD)) {
// has auths, is a visibility binding set.
} else if (!key.equals("_id") && !key.equals(PCJ_ID)) {
// is the binding value.
final Document typeDoc = (Document) bs.get(key);
final URI dataType = new URIImpl(typeDoc.getString(BINDING_TYPE));
final RyaType type = new RyaType(dataType, typeDoc.getString(BINDING_VALUE));
final Value value = RyaToRdfConversions.convertValue(type);
binding.addBinding(key, value);
}
}
return binding;
}
@Override
public void close() throws Exception {
}
};
}
use of org.apache.rya.api.utils.CloseableIterator in project incubator-rya by apache.
the class AccumuloPcjStorageIT method listResults.
@Test
public void listResults() throws Exception {
// Setup the PCJ storage that will be tested against.
final Connector connector = super.getClusterInstance().getConnector();
final String ryaInstanceName = super.getRyaInstanceName();
try (final PrecomputedJoinStorage pcjStorage = new AccumuloPcjStorage(connector, ryaInstanceName)) {
// Create a PCJ.
final String sparql = "SELECT * WHERE { ?a <http://isA> ?b }";
final String pcjId = pcjStorage.createPcj(sparql);
// Add some binding sets to it.
final Set<VisibilityBindingSet> storedResults = new HashSet<>();
final MapBindingSet aliceBS = new MapBindingSet();
aliceBS.addBinding("a", new URIImpl("http://Alice"));
aliceBS.addBinding("b", new URIImpl("http://Person"));
storedResults.add(new VisibilityBindingSet(aliceBS, ""));
final MapBindingSet charlieBS = new MapBindingSet();
charlieBS.addBinding("a", new URIImpl("http://Charlie"));
charlieBS.addBinding("b", new URIImpl("http://Comedian"));
storedResults.add(new VisibilityBindingSet(charlieBS, ""));
pcjStorage.addResults(pcjId, storedResults);
// List the results that were stored.
final Set<BindingSet> results = new HashSet<>();
try (CloseableIterator<BindingSet> resultsIt = pcjStorage.listResults(pcjId)) {
while (resultsIt.hasNext()) {
results.add(resultsIt.next());
}
}
// The stored results are returned as normal binding sets, so unwrap them.
final Set<BindingSet> expectedResults = storedResults.stream().map(visBs -> visBs.getBindingSet()).collect(Collectors.toSet());
assertEquals(expectedResults, results);
}
}
Aggregations