Search in sources :

Example 1 with NodeId

use of org.apache.jena.tdb2.store.NodeId in project jena by apache.

the class tdbstats method stats$.

private static StatsResults stats$(DatasetGraphTDB dsg, Node gn) {
    NodeTable nt = dsg.getTripleTable().getNodeTupleTable().getNodeTable();
    StatsCollectorNodeId stats = new StatsCollectorNodeId(nt);
    if (gn == null) {
        Iterator<Tuple<NodeId>> iter = dsg.getTripleTable().getNodeTupleTable().findAll();
        for (; iter.hasNext(); ) {
            Tuple<NodeId> t = iter.next();
            stats.record(null, t.get(0), t.get(1), t.get(2));
        }
    } else {
        // If the union graph, then we need to scan all quads but with uniqueness.
        boolean unionGraph = Quad.isUnionGraph(gn);
        NodeId gnid = null;
        if (!unionGraph) {
            gnid = nt.getNodeIdForNode(gn);
            if (NodeId.isDoesNotExist(gnid))
                Log.warn(tdbstats.class, "No such graph: " + gn);
        }
        NodeTupleTable ntt = dsg.getQuadTable().getNodeTupleTable();
        Iterator<Tuple<NodeId>> iter = unionGraph ? SolverLibTDB.unionGraph(ntt) : ntt.find(gnid, null, null, null);
        for (; iter.hasNext(); ) {
            Tuple<NodeId> t = iter.next();
            stats.record(t.get(0), t.get(1), t.get(2), t.get(3));
        }
    }
    return stats.results();
}
Also used : NodeTupleTable(org.apache.jena.tdb2.store.nodetupletable.NodeTupleTable) NodeId(org.apache.jena.tdb2.store.NodeId) StatsCollectorNodeId(org.apache.jena.tdb2.solver.stats.StatsCollectorNodeId) NodeTable(org.apache.jena.tdb2.store.nodetable.NodeTable) Tuple(org.apache.jena.atlas.lib.tuple.Tuple) StatsCollectorNodeId(org.apache.jena.tdb2.solver.stats.StatsCollectorNodeId)

Example 2 with NodeId

use of org.apache.jena.tdb2.store.NodeId in project jena by apache.

the class PatternMatchTDB2 method execute.

// The worker.  Callers choose the NodeTupleTable.
// graphNode may be Node.ANY, meaning we should make triples unique.
// graphNode may be null, meaning default graph
private static QueryIterator execute(NodeTupleTable nodeTupleTable, Node graphNode, BasicPattern pattern, QueryIterator input, Predicate<Tuple<NodeId>> filter, ExecutionContext execCxt) {
    if (Quad.isUnionGraph(graphNode))
        graphNode = Node.ANY;
    if (Quad.isDefaultGraph(graphNode))
        graphNode = null;
    List<Triple> triples = pattern.getList();
    boolean anyGraph = (graphNode == null ? false : (Node.ANY.equals(graphNode)));
    int tupleLen = nodeTupleTable.getTupleTable().getTupleLen();
    if (graphNode == null) {
        if (3 != tupleLen)
            throw new TDBException("SolverLib: Null graph node but tuples are of length " + tupleLen);
    } else {
        if (4 != tupleLen)
            throw new TDBException("SolverLib: Graph node specified but tuples are of length " + tupleLen);
    }
    // Convert from a QueryIterator (Bindings of Var/Node) to BindingNodeId
    NodeTable nodeTable = nodeTupleTable.getNodeTable();
    Iterator<BindingNodeId> chain = Iter.map(input, SolverLibTDB.convFromBinding(nodeTable));
    List<Abortable> killList = new ArrayList<>();
    for (Triple triple : triples) {
        Tuple<Node> patternTuple = null;
        if (graphNode == null)
            // 3-tuples
            patternTuple = TupleFactory.create3(triple.getSubject(), triple.getPredicate(), triple.getObject());
        else
            // 4-tuples.
            patternTuple = TupleFactory.create4(graphNode, triple.getSubject(), triple.getPredicate(), triple.getObject());
        // Plain RDF, no RDF-star
        // chain = solve(nodeTupleTable, tuple, anyGraph, chain, filter, execCxt)
        // ;
        // RDF-star SA
        chain = matchQuadPattern(chain, graphNode, triple, nodeTupleTable, patternTuple, anyGraph, filter, execCxt);
        chain = makeAbortable(chain, killList);
    }
    Iterator<Binding> iterBinding = SolverLibTDB.convertToNodes(chain, nodeTable);
    // "killList" will be aborted on timeout.
    return new QueryIterAbortable(iterBinding, killList, input, execCxt);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) QueryIterAbortable(org.apache.jena.sparql.engine.iterator.QueryIterAbortable) TDBException(org.apache.jena.tdb2.TDBException) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) Triple(org.apache.jena.graph.Triple) QueryIterAbortable(org.apache.jena.sparql.engine.iterator.QueryIterAbortable) SolverLib.makeAbortable(org.apache.jena.sparql.engine.main.solver.SolverLib.makeAbortable) Abortable(org.apache.jena.sparql.engine.iterator.Abortable) NodeTable(org.apache.jena.tdb2.store.nodetable.NodeTable)

Example 3 with NodeId

use of org.apache.jena.tdb2.store.NodeId in project jena by apache.

the class SolverRX method matchQuadPattern.

// Entry point from SolverLib.
/*package*/
static Iterator<BindingNodeId> matchQuadPattern(Iterator<BindingNodeId> chain, Node graphNode, Triple tPattern, NodeTupleTable nodeTupleTable, Tuple<Node> patternTuple, boolean anyGraph, Predicate<Tuple<NodeId>> filter, ExecutionContext execCxt) {
    if (DATAPATH) {
        if (!tripleHasEmbTripleWithVars(tPattern))
            // No RDF-star <<>> with variables.
            return StageMatchTuple.access(nodeTupleTable, chain, patternTuple, filter, anyGraph, execCxt);
    }
    // RDF-star <<>> with variables.
    // This path should work regardless.
    boolean isTriple = (patternTuple.len() == 3);
    NodeTable nodeTable = nodeTupleTable.getNodeTable();
    Function<BindingNodeId, Iterator<BindingNodeId>> step = bnid -> find(bnid, nodeTupleTable, graphNode, tPattern, anyGraph, filter, execCxt);
    return Iter.flatMap(chain, step);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Iterator(java.util.Iterator) Predicate(java.util.function.Predicate) NodeId(org.apache.jena.tdb2.store.NodeId) SolverLib.nodeTopLevel(org.apache.jena.sparql.engine.main.solver.SolverLib.nodeTopLevel) Triple(org.apache.jena.graph.Triple) SolverLib.tripleHasEmbTripleWithVars(org.apache.jena.sparql.engine.main.solver.SolverLib.tripleHasEmbTripleWithVars) NodeTupleTable(org.apache.jena.tdb2.store.nodetupletable.NodeTupleTable) Function(java.util.function.Function) NodeTable(org.apache.jena.tdb2.store.nodetable.NodeTable) SolverRX4(org.apache.jena.sparql.engine.main.solver.SolverRX4) Substitute(org.apache.jena.sparql.core.Substitute) Node(org.apache.jena.graph.Node) Quad(org.apache.jena.sparql.core.Quad) TupleLib(org.apache.jena.tdb2.lib.TupleLib) TupleFactory(org.apache.jena.atlas.lib.tuple.TupleFactory) SolverLibTDB.convFromBinding(org.apache.jena.tdb2.solver.SolverLibTDB.convFromBinding) Iter(org.apache.jena.atlas.iterator.Iter) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException) Tuple(org.apache.jena.atlas.lib.tuple.Tuple) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) BindingFactory(org.apache.jena.sparql.engine.binding.BindingFactory) Iterator(java.util.Iterator) NodeTable(org.apache.jena.tdb2.store.nodetable.NodeTable)

Example 4 with NodeId

use of org.apache.jena.tdb2.store.NodeId in project jena by apache.

the class SolverRX method find.

private static Iterator<BindingNodeId> find(BindingNodeId bnid, NodeTupleTable nodeTupleTable, Node xGraphNode, Triple xPattern, boolean anyGraph, Predicate<Tuple<NodeId>> filter, ExecutionContext execCxt) {
    NodeTable nodeTable = nodeTupleTable.getNodeTable();
    Binding input = bnid.isEmpty() ? BindingFactory.empty() : new BindingTDB(bnid, nodeTable);
    Triple tPattern = Substitute.substitute(xPattern, input);
    Node graphNode = Substitute.substitute(xGraphNode, input);
    Node tGraphNode = anyGraph ? Quad.unionGraph : graphNode;
    // graphNode is ANY for union graph and null for default graph.
    // Var to ANY, Triple Term to ANY.
    Node g = (graphNode == null) ? null : nodeTopLevel(graphNode);
    Node s = nodeTopLevel(tPattern.getSubject());
    Node p = nodeTopLevel(tPattern.getPredicate());
    Node o = nodeTopLevel(tPattern.getObject());
    Tuple<Node> patternTuple = (g == null) ? TupleFactory.create3(s, p, o) : TupleFactory.create4(g, s, p, o);
    Iterator<Quad> dsgIter = accessData(patternTuple, nodeTupleTable, anyGraph, filter, execCxt);
    Iterator<Binding> matched = Iter.iter(dsgIter).map(dQuad -> SolverRX4.matchQuad(input, dQuad, tGraphNode, tPattern)).removeNulls();
    return convFromBinding(matched, nodeTable);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) SolverLibTDB.convFromBinding(org.apache.jena.tdb2.solver.SolverLibTDB.convFromBinding) Triple(org.apache.jena.graph.Triple) Binding(org.apache.jena.sparql.engine.binding.Binding) Iterator(java.util.Iterator) Predicate(java.util.function.Predicate) NodeId(org.apache.jena.tdb2.store.NodeId) SolverLib.nodeTopLevel(org.apache.jena.sparql.engine.main.solver.SolverLib.nodeTopLevel) Triple(org.apache.jena.graph.Triple) SolverLib.tripleHasEmbTripleWithVars(org.apache.jena.sparql.engine.main.solver.SolverLib.tripleHasEmbTripleWithVars) NodeTupleTable(org.apache.jena.tdb2.store.nodetupletable.NodeTupleTable) Function(java.util.function.Function) NodeTable(org.apache.jena.tdb2.store.nodetable.NodeTable) SolverRX4(org.apache.jena.sparql.engine.main.solver.SolverRX4) Substitute(org.apache.jena.sparql.core.Substitute) Node(org.apache.jena.graph.Node) Quad(org.apache.jena.sparql.core.Quad) TupleLib(org.apache.jena.tdb2.lib.TupleLib) TupleFactory(org.apache.jena.atlas.lib.tuple.TupleFactory) SolverLibTDB.convFromBinding(org.apache.jena.tdb2.solver.SolverLibTDB.convFromBinding) Iter(org.apache.jena.atlas.iterator.Iter) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException) Tuple(org.apache.jena.atlas.lib.tuple.Tuple) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) BindingFactory(org.apache.jena.sparql.engine.binding.BindingFactory) Quad(org.apache.jena.sparql.core.Quad) Node(org.apache.jena.graph.Node) NodeTable(org.apache.jena.tdb2.store.nodetable.NodeTable)

Example 5 with NodeId

use of org.apache.jena.tdb2.store.NodeId in project jena by apache.

the class StageMatchTuple method prepare.

/**
 * Prepare a pattern (tuple of nodes), and an existing binding of NodeId, into
 * NodeIds and Variables. A variable in the pattern is replaced by its binding or
 * null in the NodeIds. A variable that is not bound by the binding is placed in
 * the var array. Return false if preparation detects the pattern can not match.
 */
private static boolean prepare(NodeTable nodeTable, Tuple<Node> patternTuple, BindingNodeId input, NodeId[] ids, Var[] var) {
    // we wish to abort if an unknown node is seen.
    for (int i = 0; i < patternTuple.len(); i++) {
        Node n = patternTuple.get(i);
        // Substitution and turning into NodeIds
        // Variables unsubstituted are null NodeIds
        NodeId nId = idFor(nodeTable, input, n);
        if (NodeId.isDoesNotExist(nId))
            return false;
        ids[i] = nId;
        if (nId == null)
            var[i] = asVar(n);
    }
    return true;
}
Also used : Node(org.apache.jena.graph.Node) NodeId(org.apache.jena.tdb2.store.NodeId)

Aggregations

NodeId (org.apache.jena.tdb2.store.NodeId)47 Node (org.apache.jena.graph.Node)24 Tuple (org.apache.jena.atlas.lib.tuple.Tuple)16 TDBException (org.apache.jena.tdb2.TDBException)12 NodeTable (org.apache.jena.tdb2.store.nodetable.NodeTable)11 Quad (org.apache.jena.sparql.core.Quad)7 ArrayList (java.util.ArrayList)6 Var (org.apache.jena.sparql.core.Var)6 Triple (org.apache.jena.graph.Triple)5 NodeTupleTable (org.apache.jena.tdb2.store.nodetupletable.NodeTupleTable)5 Iterator (java.util.Iterator)4 Binding (org.apache.jena.sparql.engine.binding.Binding)4 StatsCollectorNodeId (org.apache.jena.tdb2.solver.stats.StatsCollectorNodeId)4 Predicate (java.util.function.Predicate)3 Iter (org.apache.jena.atlas.iterator.Iter)3 Record (org.apache.jena.dboe.base.record.Record)3 TupleIndex (org.apache.jena.tdb2.store.tupletable.TupleIndex)3 Test (org.junit.Test)3 List (java.util.List)2 Function (java.util.function.Function)2