@Override protected Binding moveToNextBinding() { return iterator.next() ; }
public LinearIndex(Set<Var> commonVars, QueryIterator data) { this.commonVars = commonVars ; while ( data.hasNext() ) table.add(data.next()) ; data.close() ; }
/** * Creates a new index table * * @param commonVars * Common Variables * @param data * Data */ public SetIndexTable(Set<Var> commonVars, QueryIterator data) { if (commonVars.size() != 1) throw new IllegalArgumentException("Common Variables must be of size 1"); this.var = commonVars.iterator().next(); while (data.hasNext()) { Binding binding = data.next(); Node value = binding.get(this.var); if (value == null) continue; this.values.add(value); } }
/** Return the binding from the input, hiding any variables to be ignored. */ private Binding getInputNext() { Binding b = getInput().next() ; // Hide unnamed and internal variables. b = new BindingProjectNamed(b) ; return b ; }
public LinearIndex(Set<Var> commonVars, QueryIterator data, Set<HashIndexTable.Key> loadedData, Map<Var,Integer> mappings) { this.commonVars = commonVars ; for ( HashIndexTable.Key key: loadedData ) table.add(toBinding(key, mappings)) ; while ( data.hasNext() ) table.add(data.next()) ; data.close() ; }
private static QueryIterator joinWorkerN(QueryIterator left, Table right, JoinType joinType, ExprList conditions, ExecutionContext execCxt) { List<Binding> out = new ArrayList<>() ; for ( ; left.hasNext() ; ) { Binding bindingLeft = left.next() ; int count = 0 ; for (Iterator<Binding> iter = right.rows() ; iter.hasNext();) { Binding bindingRight = iter.next() ; Binding r = Algebra.merge(bindingLeft, bindingRight) ; if ( r == null ) continue ; // This does the conditional part. Theta-join. if ( conditions == null || conditions.isSatisfied(r, execCxt) ) { count ++ ; out.add(r) ; } } if ( count == 0 && ( joinType == LEFT) ) // Conditions on left? out.add(bindingLeft) ; } return new QueryIterPlainWrapper(out.iterator(), execCxt) ; }
/** Very simple, materializing version - useful for debugging. * Builds output early. Materializes left, streams right. * Does <b>not</b> scale. * No cancellation, no stats. * * @see #nestedLoopJoin */ public static QueryIterator nestedLoopJoinBasic(QueryIterator left, QueryIterator right, ExecutionContext execCxt) { List<Binding> leftRows = Iter.toList(left) ; List<Binding> output = new ArrayList<>() ; for ( ; right.hasNext() ; ) { Binding row2 = right.next() ; for ( Binding row1 : leftRows ) { Binding r = Algebra.merge(row1, row2) ; if ( r != null ) output.add(r) ; } } return new QueryIterPlainWrapper(output.iterator(), execCxt) ; }
protected Binding moveToNextBindingOrNull() { if ( isFinished() ) return null; for ( ;; ) { // For rows from the right. if ( rowRight == null ) { if ( right.hasNext() ) { rowRight = right.next(); s_countRHS++; left = leftRows.iterator(); } else return null; } // There is a rowRight while (left.hasNext()) { Binding rowLeft = left.next(); Binding r = Algebra.merge(rowLeft, rowRight); if ( r != null ) { s_countResults++; return r; } } // Nothing more for this rowRight. rowRight = null; } }
private void buildHashTable(QueryIterator iter1) { state = Phase.HASH ; for (; iter1.hasNext();) { Binding row1 = iter1.next() ; s_countProbe ++ ; hashTable.put(row1) ; } iter1.close() ; state = Phase.STREAM ; }
@Override protected Iterator<Binding> initializeIterator() { try { while ( qIter.hasNext() ) { Binding binding = qIter.next() ; if ( heap.size() < limit ) add(binding) ; else { Binding currentMaxLeastN = heap.peek() ; if ( comparator.compare(binding, currentMaxLeastN) < 0 ) add(binding) ; } } qIter.close() ; Binding[] y = heap.toArray(new Binding[]{}) ; heap = null ; Arrays.sort(y, comparator) ; return asList(y).iterator() ; } catch (QueryCancelledException e) { QueryIterTopN.this.close(); this.close(); throw e; } } } ;
@Override protected Binding moveToNextBinding() { if ( isFinished() ) throw new NoSuchElementException(Lib.className(this)) ; if ( returnDefaultObject ) { haveReturnedSomeObject = true ; return defaultObject ; } Binding binding = null ; if ( iter != null && iter.hasNext() ) binding = iter.next() ; else { if ( haveReturnedSomeObject ) throw new NoSuchElementException("DefaultingIterator - without hasNext call first") ; binding = defaultObject ; } haveReturnedSomeObject = true ; return binding ; }
/** Very simple, materializing version for leftjoin - useful for debugging. * Builds output early. Materializes right, streams left. * Does <b>not</b> scale. */ public static QueryIterator nestedLoopLeftJoinBasic(QueryIterator left, QueryIterator right, ExprList conditions, ExecutionContext execCxt) { // Stream from left, materialize right. List<Binding> rightRows = Iter.toList(right) ; List<Binding> output = new ArrayList<>() ; long count = 0 ; for ( ; left.hasNext() ; ) { Binding row1 = left.next() ; boolean match = false ; for ( Binding row2 : rightRows ) { Binding r = Algebra.merge(row1, row2) ; if ( r != null && applyConditions(r, conditions, execCxt)) { output.add(r) ; match = true ; } } if ( ! match ) output.add(row1) ; } return new QueryIterPlainWrapper(output.iterator(), execCxt) ; }
if ( rowLeft == null ) { if ( left.hasNext() ) { rowLeft = left.next(); foundMatch = false ; s_countLHS++;
@Override public JsonObject next() { if (queryIterator == null) throw new NoSuchElementException(this.getClass() + ".next") ; try { Binding binding = queryIterator.next() ; JsonObject jsonObject = new JsonObject() ; for (String resultVar : resultVars) { Node n = binding.get(Var.alloc(resultVar)) ; JsonValue value = RDFTerm2Json.fromNode(n) ; jsonObject.put(resultVar, value); } return jsonObject ; } catch (NoSuchElementException ex) { close() ; throw ex ; } }
cIter.next() ;
@Override public boolean execAsk() { checkNotClosed(); if ( !query.isAskType() ) throw new QueryExecException("Attempt to have boolean from a " + labelForQuery(query) + " query"); startQueryIterator(); boolean r; try { // Not has next because setting timeout1 which applies to getting // the first result, not testing for it. queryIterator.next(); r = true; } catch (NoSuchElementException ex) { r = false; } this.close(); return r; }
@Override public void visit(OpTable opTable) { // This will go in a group so simply forget it. if ( opTable.isJoinIdentity() ) return ; // Put in a VALUES // This may be related to the grpup of the overall query. ElementData el = new ElementData() ; el.getVars().addAll(opTable.getTable().getVars()) ; QueryIterator qIter = opTable.getTable().iterator(null) ; while (qIter.hasNext()) el.getRows().add(qIter.next()) ; qIter.close() ; currentGroup().addElement(el) ; }
private QueryIterator makeNextStage() { count++ ; if ( getInput() == null ) return null ; if ( !getInput().hasNext() ) { getInput().close() ; return null ; } Binding binding = getInput().next() ; QueryIterator iter = nextStage(binding) ; return iter ; }
private Iterator<Triple> graphFindWorker(Graph graph, Node s, PropertyFunctionFactory f, Node p, Node o, Context context) { // Expensive? PropertyFunction pf = f.create(p.getURI()) ; PropFuncArg sv = arg(s, "S") ; PropFuncArg ov = arg(o, "O") ; QueryIterator r = QueryIterRoot.create(new ExecutionContext(context, graph, null, null)) ; QueryIterator qIter = pf.exec(r, sv, p, ov, new ExecutionContext(ARQ.getContext(), graph, null, null)) ; if ( ! qIter.hasNext() ) return Iter.nullIterator() ; List<Triple> array = new ArrayList<>() ; for ( ; qIter.hasNext() ; ) { Binding b = qIter.next() ; Node st = value(sv, b) ; Node ot = value(ov, b) ; array.add(Triple.create(st, p, ot)) ; } // Materialise so the inner QueryIterators are used up. return array.iterator() ; }
@Override public JsonArray execJson() { checkNotClosed() ; if ( ! query.isJsonType() ) throw new QueryExecException("Attempt to get a JSON result from a " + labelForQuery(query)+" query") ; startQueryIterator() ; JsonArray jsonArray = new JsonArray() ; List<String> resultVars = query.getResultVars() ; while (queryIterator.hasNext()) { Binding binding = queryIterator.next() ; JsonObject jsonObject = new JsonObject() ; for (String resultVar : resultVars) { Node n = binding.get(Var.alloc(resultVar)) ; JsonValue value = RDFTerm2Json.fromNode(n) ; jsonObject.put(resultVar, value) ; } jsonArray.add(jsonObject) ; } return jsonArray ; }