use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class AccumuloDocIdIndexer method queryDocIndex.
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> queryDocIndex(final StarQuery query, final Collection<BindingSet> constraints) throws TableNotFoundException, QueryEvaluationException {
final StarQuery starQ = query;
final Iterator<BindingSet> bs = constraints.iterator();
final Iterator<BindingSet> bs2 = constraints.iterator();
final Set<String> unCommonVarNames;
final Set<String> commonVarNames;
if (bs2.hasNext()) {
final BindingSet currBs = bs2.next();
commonVarNames = StarQuery.getCommonVars(query, currBs);
unCommonVarNames = Sets.difference(currBs.getBindingNames(), commonVarNames);
} else {
commonVarNames = Sets.newHashSet();
unCommonVarNames = Sets.newHashSet();
}
if (commonVarNames.size() == 1 && !query.commonVarConstant() && commonVarNames.contains(query.getCommonVarName())) {
final HashMultimap<String, BindingSet> map = HashMultimap.create();
final String commonVar = starQ.getCommonVarName();
final Iterator<Entry<Key, Value>> intersections;
final BatchScanner scan;
final Set<Range> ranges = Sets.newHashSet();
while (bs.hasNext()) {
final BindingSet currentBs = bs.next();
if (currentBs.getBinding(commonVar) == null) {
continue;
}
final String row = currentBs.getBinding(commonVar).getValue().stringValue();
ranges.add(new Range(row));
map.put(row, currentBs);
}
scan = runQuery(starQ, ranges);
intersections = scan.iterator();
return new CloseableIteration<BindingSet, QueryEvaluationException>() {
private QueryBindingSet currentSolutionBs = null;
private boolean hasNextCalled = false;
private boolean isEmpty = false;
private Iterator<BindingSet> inputSet = new ArrayList<BindingSet>().iterator();
private BindingSet currentBs;
private Key key;
@Override
public boolean hasNext() throws QueryEvaluationException {
if (!hasNextCalled && !isEmpty) {
while (inputSet.hasNext() || intersections.hasNext()) {
if (!inputSet.hasNext()) {
key = intersections.next().getKey();
inputSet = map.get(key.getRow().toString()).iterator();
}
currentBs = inputSet.next();
currentSolutionBs = deserializeKey(key, starQ, currentBs, unCommonVarNames);
if (currentSolutionBs.size() == unCommonVarNames.size() + starQ.getUnCommonVars().size() + 1) {
hasNextCalled = true;
return true;
}
}
isEmpty = true;
return false;
} else if (isEmpty) {
return false;
} else {
return true;
}
}
@Override
public BindingSet next() throws QueryEvaluationException {
if (hasNextCalled) {
hasNextCalled = false;
} else if (isEmpty) {
throw new NoSuchElementException();
} else {
if (this.hasNext()) {
hasNextCalled = false;
} else {
throw new NoSuchElementException();
}
}
return currentSolutionBs;
}
@Override
public void remove() throws QueryEvaluationException {
throw new UnsupportedOperationException();
}
@Override
public void close() throws QueryEvaluationException {
scan.close();
}
};
} else {
return new CloseableIteration<BindingSet, QueryEvaluationException>() {
@Override
public void remove() throws QueryEvaluationException {
throw new UnsupportedOperationException();
}
private Iterator<Entry<Key, Value>> intersections = null;
private QueryBindingSet currentSolutionBs = null;
private boolean hasNextCalled = false;
private boolean isEmpty = false;
private boolean init = false;
private BindingSet currentBs;
private StarQuery sq = new StarQuery(starQ);
private final Set<Range> emptyRangeSet = Sets.newHashSet();
private BatchScanner scan;
@Override
public BindingSet next() throws QueryEvaluationException {
if (hasNextCalled) {
hasNextCalled = false;
} else if (isEmpty) {
throw new NoSuchElementException();
} else {
if (this.hasNext()) {
hasNextCalled = false;
} else {
throw new NoSuchElementException();
}
}
return currentSolutionBs;
}
@Override
public boolean hasNext() throws QueryEvaluationException {
if (!init) {
if (intersections == null && bs.hasNext()) {
currentBs = bs.next();
sq = StarQuery.getConstrainedStarQuery(sq, currentBs);
scan = runQuery(sq, emptyRangeSet);
intersections = scan.iterator();
// binding set empty
} else if (intersections == null && !bs.hasNext()) {
currentBs = new QueryBindingSet();
scan = runQuery(starQ, emptyRangeSet);
intersections = scan.iterator();
}
init = true;
}
if (!hasNextCalled && !isEmpty) {
while (intersections.hasNext() || bs.hasNext()) {
if (!intersections.hasNext()) {
scan.close();
currentBs = bs.next();
sq = StarQuery.getConstrainedStarQuery(sq, currentBs);
scan = runQuery(sq, emptyRangeSet);
intersections = scan.iterator();
}
if (intersections.hasNext()) {
currentSolutionBs = deserializeKey(intersections.next().getKey(), sq, currentBs, unCommonVarNames);
} else {
continue;
}
if (sq.commonVarConstant() && currentSolutionBs.size() == unCommonVarNames.size() + sq.getUnCommonVars().size()) {
hasNextCalled = true;
return true;
} else if (currentSolutionBs.size() == unCommonVarNames.size() + sq.getUnCommonVars().size() + 1) {
hasNextCalled = true;
return true;
}
}
isEmpty = true;
return false;
} else if (isEmpty) {
return false;
} else {
return true;
}
}
@Override
public void close() throws QueryEvaluationException {
scan.close();
}
};
}
}
use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class BatchIT method simpleJoinAdd.
@Test
public void simpleJoinAdd() throws Exception {
final String sparql = "SELECT ?subject ?object1 ?object2 WHERE { ?subject <urn:predicate_1> ?object1; " + " <urn:predicate_2> ?object2 } ";
try (FluoClient fluoClient = new FluoClientImpl(getFluoConfiguration())) {
RyaURI subj = new RyaURI("urn:subject_1");
RyaStatement statement2 = new RyaStatement(subj, new RyaURI("urn:predicate_2"), null);
Set<RyaStatement> statements2 = getRyaStatements(statement2, 5);
// Create the PCJ table.
final PrecomputedJoinStorage pcjStorage = new AccumuloPcjStorage(getAccumuloConnector(), getRyaInstanceName());
final String pcjId = pcjStorage.createPcj(sparql);
// Tell the Fluo app to maintain the PCJ.
String queryId = new CreateFluoPcj().withRyaIntegration(pcjId, pcjStorage, fluoClient, getAccumuloConnector(), getRyaInstanceName()).getQueryId();
List<String> ids = getNodeIdStrings(fluoClient, queryId);
String joinId = ids.get(2);
String rightSp = ids.get(4);
QueryBindingSet bs = new QueryBindingSet();
bs.addBinding("subject", vf.createURI("urn:subject_1"));
bs.addBinding("object1", vf.createURI("urn:object_0"));
VisibilityBindingSet vBs = new VisibilityBindingSet(bs);
URI uri = vf.createURI("urn:subject_1");
Bytes prefixBytes = BindingHashShardingFunction.getShardedScanPrefix(rightSp, uri);
Span span = Span.prefix(prefixBytes);
// Stream the data into Fluo.
InsertTriples inserter = new InsertTriples();
inserter.insert(fluoClient, statements2, Optional.absent());
getMiniFluo().waitForObservers();
verifyCounts(fluoClient, ids, Arrays.asList(0, 0, 0, 0, 5));
JoinBatchInformation batch = JoinBatchInformation.builder().setBatchSize(1).setColumn(FluoQueryColumns.STATEMENT_PATTERN_BINDING_SET).setSpan(span).setTask(Task.Add).setJoinType(JoinType.NATURAL_JOIN).setSide(Side.LEFT).setBs(vBs).build();
// Verify the end results of the query match the expected results.
createSpanBatch(fluoClient, joinId, batch);
getMiniFluo().waitForObservers();
verifyCounts(fluoClient, ids, Arrays.asList(5, 5, 5, 0, 5));
}
}
use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class BatchIT method simpleJoinDelete.
@Test
public void simpleJoinDelete() throws Exception {
final String sparql = "SELECT ?subject ?object1 ?object2 WHERE { ?subject <urn:predicate_1> ?object1; " + " <urn:predicate_2> ?object2 } ";
try (FluoClient fluoClient = new FluoClientImpl(getFluoConfiguration())) {
RyaURI subj = new RyaURI("urn:subject_1");
RyaStatement statement1 = new RyaStatement(subj, new RyaURI("urn:predicate_1"), null);
RyaStatement statement2 = new RyaStatement(subj, new RyaURI("urn:predicate_2"), null);
Set<RyaStatement> statements1 = getRyaStatements(statement1, 5);
Set<RyaStatement> statements2 = getRyaStatements(statement2, 5);
// Create the PCJ table.
final PrecomputedJoinStorage pcjStorage = new AccumuloPcjStorage(getAccumuloConnector(), getRyaInstanceName());
final String pcjId = pcjStorage.createPcj(sparql);
// Tell the Fluo app to maintain the PCJ.
String queryId = new CreateFluoPcj().withRyaIntegration(pcjId, pcjStorage, fluoClient, getAccumuloConnector(), getRyaInstanceName()).getQueryId();
List<String> ids = getNodeIdStrings(fluoClient, queryId);
String joinId = ids.get(2);
String rightSp = ids.get(4);
QueryBindingSet bs = new QueryBindingSet();
bs.addBinding("subject", vf.createURI("urn:subject_1"));
bs.addBinding("object1", vf.createURI("urn:object_0"));
VisibilityBindingSet vBs = new VisibilityBindingSet(bs);
// create sharded span for deletion
URI uri = vf.createURI("urn:subject_1");
Bytes prefixBytes = BindingHashShardingFunction.getShardedScanPrefix(rightSp, uri);
Span span = Span.prefix(prefixBytes);
// Stream the data into Fluo.
InsertTriples inserter = new InsertTriples();
inserter.insert(fluoClient, statements1, Optional.absent());
inserter.insert(fluoClient, statements2, Optional.absent());
getMiniFluo().waitForObservers();
verifyCounts(fluoClient, ids, Arrays.asList(25, 25, 25, 5, 5));
JoinBatchInformation batch = JoinBatchInformation.builder().setBatchSize(1).setColumn(FluoQueryColumns.STATEMENT_PATTERN_BINDING_SET).setSpan(span).setTask(Task.Delete).setJoinType(JoinType.NATURAL_JOIN).setSide(Side.LEFT).setBs(vBs).build();
// Verify the end results of the query match the expected results.
createSpanBatch(fluoClient, joinId, batch);
getMiniFluo().waitForObservers();
verifyCounts(fluoClient, ids, Arrays.asList(25, 25, 20, 5, 5));
}
}
use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class PeriodicNotificationBinPrunerIT method compareFluoCounts.
private void compareFluoCounts(FluoClient client, String pcjId, long bin) {
QueryBindingSet bs = new QueryBindingSet();
bs.addBinding(IncrementalUpdateConstants.PERIODIC_BIN_ID, new LiteralImpl(Long.toString(bin), XMLSchema.LONG));
VariableOrder varOrder = new VariableOrder(IncrementalUpdateConstants.PERIODIC_BIN_ID);
try (Snapshot sx = client.newSnapshot()) {
String fluoQueryId = NodeType.generateNewIdForType(NodeType.QUERY, pcjId);
Set<String> ids = new HashSet<>();
PeriodicQueryUtil.getPeriodicQueryNodeAncestorIds(sx, fluoQueryId, ids);
for (String id : ids) {
NodeType optNode = NodeType.fromNodeId(id).orNull();
if (optNode == null)
throw new RuntimeException("Invalid NodeType.");
Bytes prefix = RowKeyUtil.makeRowKey(id, varOrder, bs);
RowScanner scanner = sx.scanner().fetch(optNode.getResultColumn()).over(Span.prefix(prefix)).byRow().build();
int count = 0;
Iterator<ColumnScanner> colScannerIter = scanner.iterator();
while (colScannerIter.hasNext()) {
ColumnScanner colScanner = colScannerIter.next();
String row = colScanner.getRow().toString();
Iterator<ColumnValue> values = colScanner.iterator();
while (values.hasNext()) {
values.next();
count++;
}
}
Assert.assertEquals(0, count);
}
}
}
use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class BindingHashShardingFunctionTest method shardAddAndRemoveTest.
@Test
public void shardAddAndRemoveTest() {
String nodeId = NodeType.generateNewFluoIdForType(NodeType.STATEMENT_PATTERN);
QueryBindingSet bs = new QueryBindingSet();
bs.addBinding("entity", vf.createURI("urn:entity"));
bs.addBinding("location", vf.createLiteral("location_1"));
VisibilityBindingSet vBs = new VisibilityBindingSet(bs);
VariableOrder varOrder = new VariableOrder("entity", "location");
Bytes row = RowKeyUtil.makeRowKey(nodeId, varOrder, vBs);
Bytes shardedRow = BindingHashShardingFunction.addShard(nodeId, varOrder, vBs);
Bytes shardlessRow = BindingHashShardingFunction.removeHash(Bytes.of(SP_PREFIX), shardedRow);
Assert.assertEquals(row, shardlessRow);
}
Aggregations