use of org.apache.jena.atlas.lib.Pair in project jena by apache.
the class NodeTableTrans method append.
/** Copy from the journal file to the real file */
/*package*/
void append() {
Iterator<Pair<NodeId, Node>> iter = nodeTableJournal.all();
Pair<NodeId, Node> firstPair = null;
Pair<NodeId, Node> lastPair = null;
for (; iter.hasNext(); ) {
Pair<NodeId, Node> x = iter.next();
if (firstPair == null)
firstPair = x;
lastPair = x;
NodeId nodeId = x.getLeft();
Node node = x.getRight();
debug(" append: %s -> %s", x, mapFromJournal(nodeId));
// This does the write.
NodeId nodeId2 = base.getAllocateNodeId(node);
if (!nodeId2.equals(mapFromJournal(nodeId)))
inconsistent(node, nodeId, nodeId2);
}
}
use of org.apache.jena.atlas.lib.Pair in project jena by apache.
the class TransformFilterEquality method preprocessFilterEquality.
// --- find and extract
private static Pair<List<Pair<Var, NodeValue>>, ExprList> preprocessFilterEquality(ExprList exprs) {
List<Pair<Var, NodeValue>> exprsFilterEquality = new ArrayList<>();
ExprList exprsOther = new ExprList();
for (Expr e : exprs.getList()) {
Pair<Var, NodeValue> p = preprocess(e);
if (p != null)
exprsFilterEquality.add(p);
else
exprsOther.add(e);
}
if (exprsFilterEquality.size() == 0)
return null;
return Pair.create(exprsFilterEquality, exprsOther);
}
use of org.apache.jena.atlas.lib.Pair in project jena by apache.
the class TransformFilterInequality method apply.
private static Op apply(ExprList exprs, Op subOp) {
// ---- Find and extract any inequality filters.
Pair<List<Pair<Var, NodeValue>>, ExprList> p = preprocessFilterInequality(exprs);
if (p == null || p.getLeft().size() == 0)
return null;
List<Pair<Var, NodeValue>> inequalities = p.getLeft();
Collection<Var> varsMentioned = varsMentionedInInequalityFilters(inequalities);
ExprList remaining = p.getRight();
// If any of the conditions overlap the optimization is unsafe
// (the query is also likely incorrect but that isn't our problem)
// TODO There is actually a special case here, if the inequality
// constraints are conflicting then we can special case to table empty.
// ---- Check if the subOp is the right shape to transform.
Op op = subOp;
// hence eliminate all rows. Return the empty table.
if (testSpecialCaseUnused(subOp, inequalities, remaining))
return OpTable.empty();
// { OPTIONAL{P1} OPTIONAL{P2} ... FILTER(?x = :x) }
if (testSpecialCase1(subOp, inequalities, remaining)) {
// Find backbone of ops
List<Op> ops = extractOptionals(subOp);
ops = processSpecialCase1(ops, inequalities);
// Put back together
op = rebuild((Op2) subOp, ops);
// Put all filters - either we optimized, or we left alone.
// Either way, the complete set of filter expressions.
op = OpFilter.filterBy(exprs, op);
return op;
}
if (!safeToTransform(varsMentioned, op))
return null;
op = processFilterWorker(op, inequalities);
// ---- Place any filter expressions around the processed sub op.
if (remaining.size() > 0)
op = OpFilter.filterBy(remaining, op);
return op;
}
use of org.apache.jena.atlas.lib.Pair in project jena by apache.
the class NodeTableLib method print.
public static void print(String label, NodeTable nodeTable) {
if (label != null)
System.out.println(label);
Iterator<Pair<NodeId, Node>> iter = nodeTable.all();
for (; iter.hasNext(); ) {
Pair<NodeId, Node> x = iter.next();
NodeId nodeId = x.getLeft();
Node node = x.getRight();
System.out.println(nodeId + " " + node);
}
}
use of org.apache.jena.atlas.lib.Pair in project jena by apache.
the class QueryIterGroup method calc.
private static Iterator<Binding> calc(final QueryIterator iter, final VarExprList groupVarExpr, final List<ExprAggregator> aggregators, final ExecutionContext execCxt) {
return new IteratorDelayedInitialization<Binding>() {
@Override
protected Iterator<Binding> initializeIterator() {
boolean noAggregators = (aggregators == null || aggregators.isEmpty());
// Phase 1 : assign bindings to buckets by key and pump through the aggregators.
Multimap<Binding, Pair<Var, Accumulator>> accumulators = HashMultimap.create();
while (iter.hasNext()) {
Binding b = iter.nextBinding();
Binding key = genKey(groupVarExpr, b, execCxt);
if (noAggregators) {
// Put in a dummy to remember the input.
accumulators.put(key, placeholder);
continue;
}
// Create if does not exist.
if (!accumulators.containsKey(key)) {
for (ExprAggregator agg : aggregators) {
Accumulator x = agg.getAggregator().createAccumulator();
Var v = agg.getVar();
accumulators.put(key, Pair.create(v, x));
}
}
// Do the per-accumulator calculation.
for (Pair<Var, Accumulator> pair : accumulators.get(key)) pair.getRight().accumulate(b, execCxt);
}
if (accumulators.isEmpty()) {
if (noAggregators) {
// ==> No result rows.
return Iter.nullIterator();
}
BindingMap binding = BindingFactory.create();
for (ExprAggregator agg : aggregators) {
Var v = agg.getVar();
Node value = agg.getAggregator().getValueEmpty();
if (value != null) {
binding.add(v, value);
}
}
if (binding == null)
// This does not happen if there are any aggregators.
return Iter.nullIterator();
// cast to get the static type inference to work.
return Iter.singletonIter((Binding) binding);
}
if (noAggregators)
// We used placeholder so there are always the key.
return accumulators.keySet().iterator();
List<Binding> results = new ArrayList<>();
for (Binding k : accumulators.keySet()) {
Collection<Pair<Var, Accumulator>> accs = accumulators.get(k);
BindingMap b = BindingFactory.create(k);
for (Pair<Var, Accumulator> pair : accs) {
Var v = pair.getLeft();
NodeValue value = pair.getRight().getValue();
Node n = (value == null) ? null : value.asNode();
if (v == null || n == null) {
} else
b.add(v, n);
}
results.add(b);
}
return results.iterator();
}
};
}
Aggregations