Search in sources :

Example 16 with Pair

use of org.apache.jena.atlas.lib.Pair in project jena by apache.

the class TransformFilterImplicitJoin method apply.

private static Op apply(ExprList exprs, Op subOp) {
    // ---- Find and extract any implicit join filters.
    Pair<List<Pair<Var, Var>>, ExprList> p = preprocessFilterImplicitJoin(subOp, exprs);
    if (p == null || p.getLeft().size() == 0)
        return null;
    List<Pair<Var, Var>> joins = p.getLeft();
    Collection<Var> varsMentioned = varsMentionedInImplictJoins(joins);
    ExprList remaining = p.getRight();
    // enhancement to this optimizer
    if (varsMentioned.size() != joins.size() * 2)
        return null;
    // ---- Check if the subOp is the right shape to transform.
    Op op = subOp;
    // hence eliminate all rows. Return the empty table.
    if (testSpecialCaseUnused(subOp, joins, remaining))
        return OpTable.empty();
    // This is { OPTIONAL{P1} OPTIONAL{P2} ... FILTER(?x = :x) }
    if (testSpecialCaseOptional(subOp, joins, remaining)) {
        // Find backbone of ops
        List<Op> ops = extractOptionals(subOp);
        ops = processSpecialCaseOptional(ops, joins);
        // 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;
    }
    // Special case : filter is over a union where one/both sides are always false
    if (testSpecialCaseUnion(subOp, joins)) {
        // This will attempt to eliminate the sides that are always false
        op = processSpecialCaseUnion(subOp, joins);
        // operator at this point and can return immediately
        if (op instanceof OpTable)
            return op;
    }
    if (!safeToTransform(joins, varsMentioned, op))
        return null;
    for (Pair<Var, Var> implicitJoin : joins) {
        // TODO Where there are multiple conditions it may be necessary to
        // apply the substitutions more intelligently
        op = processFilterWorker(op, implicitJoin.getLeft(), implicitJoin.getRight());
    }
    // ---- Place any filter expressions around the processed sub op.
    if (remaining.size() > 0)
        op = OpFilter.filterBy(remaining, op);
    return op;
}
Also used : Op(org.apache.jena.sparql.algebra.Op) VarExprList(org.apache.jena.sparql.core.VarExprList) Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList) List(java.util.List) VarExprList(org.apache.jena.sparql.core.VarExprList) Pair(org.apache.jena.atlas.lib.Pair)

Example 17 with Pair

use of org.apache.jena.atlas.lib.Pair in project jena by apache.

the class TransformFilterInequality method preprocessFilterInequality.

// --- find and extract
private static Pair<List<Pair<Var, NodeValue>>, ExprList> preprocessFilterInequality(ExprList exprs) {
    List<Pair<Var, NodeValue>> exprsFilterInequality = new ArrayList<>();
    ExprList exprsOther = new ExprList();
    for (Expr e : exprs.getList()) {
        Pair<Var, NodeValue> p = preprocess(e);
        if (p != null)
            exprsFilterInequality.add(p);
        else
            exprsOther.add(e);
    }
    if (exprsFilterInequality.size() == 0)
        return null;
    return Pair.create(exprsFilterInequality, exprsOther);
}
Also used : VarExprList(org.apache.jena.sparql.core.VarExprList) Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList) Pair(org.apache.jena.atlas.lib.Pair)

Example 18 with Pair

use of org.apache.jena.atlas.lib.Pair in project jena by apache.

the class ObjectFileTrans method append.

/** Copy from the temporary file to the real file */
private void append() {
    // We could write directly to the real file if:
    //    we record the truncate point needed for an abort
    //    manage partial final writes
    //    deny the existence of nodes after the transaction mark.
    // Later - stay simple for now.
    // Truncate/position the ObjectFile.
    base.reposition(otherAllocOffset);
    Iterator<Pair<Long, ByteBuffer>> iter = transObjects.all();
    for (; iter.hasNext(); ) {
        Pair<Long, ByteBuffer> p = iter.next();
        String s = StrUtils.fromUTF8bytes(p.getRight().array());
        long x = base.write(p.getRight());
        if (p.getLeft() + otherAllocOffset != x)
            throw new FileException("Expected id of " + (p.getLeft() + otherAllocOffset) + ", got an id of " + x);
    }
}
Also used : FileException(org.apache.jena.tdb.base.file.FileException) ByteBuffer(java.nio.ByteBuffer) Pair(org.apache.jena.atlas.lib.Pair)

Example 19 with Pair

use of org.apache.jena.atlas.lib.Pair in project jena by apache.

the class NodeTableTrans method dump.

// Debugging only
private void dump() {
    System.err.println(">>>>>>>>>>");
    System.err.println("label = " + label);
    System.err.println("txn = " + txn);
    System.err.println("offset = " + allocOffset);
    System.err.println("journalStartOffset = " + journalObjFileStartOffset);
    System.err.println("journal = " + journalObjFile.getLabel());
    if (true)
        return;
    System.err.println("nodeTableJournal >>>");
    Iterator<Pair<NodeId, Node>> iter = nodeTableJournal.all();
    for (; iter.hasNext(); ) {
        Pair<NodeId, Node> x = iter.next();
        NodeId nodeId = x.getLeft();
        Node node = x.getRight();
        NodeId mapped = mapFromJournal(nodeId);
        //debug("append: %s -> %s", x, mapFromJournal(nodeId)) ;
        // This does the write.
        NodeId nodeId2 = base.getAllocateNodeId(node);
        System.err.println(x + "  mapped=" + mapped + " getAlloc=" + nodeId2);
    }
    System.err.println("journal >>>");
    Iterator<Pair<Long, ByteBuffer>> iter1 = this.journalObjFile.all();
    for (; iter1.hasNext(); ) {
        Pair<Long, ByteBuffer> p = iter1.next();
        System.err.println(p.getLeft() + " : " + p.getRight());
        ByteBufferLib.print(System.err, p.getRight());
    }
    System.err.println("nodeIndex >>>");
    Iterator<Record> iter2 = this.nodeIndex.iterator();
    for (; iter2.hasNext(); ) {
        Record r = iter2.next();
        System.err.println(r);
    }
    System.err.println("<<<<<<<<<<");
}
Also used : Node(org.apache.jena.graph.Node) NodeId(org.apache.jena.tdb.store.NodeId) Record(org.apache.jena.tdb.base.record.Record) ByteBuffer(java.nio.ByteBuffer) Pair(org.apache.jena.atlas.lib.Pair)

Aggregations

Pair (org.apache.jena.atlas.lib.Pair)19 Var (org.apache.jena.sparql.core.Var)9 VarExprList (org.apache.jena.sparql.core.VarExprList)8 ArrayList (java.util.ArrayList)7 Node (org.apache.jena.graph.Node)6 NodeId (org.apache.jena.tdb.store.NodeId)5 Record (org.apache.jena.tdb.base.record.Record)4 List (java.util.List)3 Op (org.apache.jena.sparql.algebra.Op)3 RecordBufferPage (org.apache.jena.tdb.base.recordbuffer.RecordBufferPage)3 ByteBuffer (java.nio.ByteBuffer)2 Iterator (java.util.Iterator)1 Function (java.util.function.Function)1 IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)1 Iter (org.apache.jena.atlas.iterator.Iter)1 IteratorDelayedInitialization (org.apache.jena.atlas.iterator.IteratorDelayedInitialization)1 IteratorWithBuffer (org.apache.jena.atlas.iterator.IteratorWithBuffer)1 Binding (org.apache.jena.sparql.engine.binding.Binding)1 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)1 ExprAggregator (org.apache.jena.sparql.expr.ExprAggregator)1