private boolean _containsGraph(Node graphNode) { // Have to look explicitly, which is a bit of a nuisance. // But does not normally happen for GRAPH <g> because that's rewritten to quads. // Only pattern with complex paths go via GRAPH. Iterator<Tuple<NodeId>> x = storage.quadTable.getNodeTupleTable().findAsNodeIds(graphNode, null, null, null) ; if ( x == null ) return false ; boolean result = x.hasNext() ; return result ; }
public static Set<NodeId> convertToNodeIds(Collection<Node> nodes, DatasetGraphTDB dataset) { Set<NodeId> graphIds = new HashSet<>() ; NodeTable nt = dataset.getQuadTable().getNodeTupleTable().getNodeTable() ; for ( Node n : nodes ) graphIds.add(nt.getNodeIdForNode(n)) ; return graphIds ; }
static Map<String, TupleIndex> indexMap(DatasetGraphTDB dsgtdb) { Map<String, TupleIndex> indexMap = new HashMap<>(); // All triple/quad indexes. Arrays.stream(dsgtdb.getTripleTable().getNodeTupleTable().getTupleTable().getIndexes()) .forEach(idx->indexMap.put(idx.getName(), idx)); Arrays.stream(dsgtdb.getQuadTable().getNodeTupleTable().getTupleTable().getIndexes()) .forEach(idx->indexMap.put(idx.getName(), idx)); return indexMap; }
/** * Return the NodeId for a node. Returns NodeId.NodeDoesNotExist when the node is not * found. Returns null when not a TDB-backed dataset. */ public static NodeId getNodeId(DatasetGraphTDB dsg, Node node) { if ( dsg == null ) return null; NodeTable nodeTable = dsg.getQuadTable().getNodeTupleTable().getNodeTable(); NodeId nodeId = nodeTable.getNodeIdForNode(node); return nodeId; }
/** * Return the node for a NodeId (if any). Returns null if the NodeId does not exist in * the dataset. */ public static Node getNode(DatasetGraphTDB dsg, NodeId nodeId) { if ( dsg == null ) return null; NodeTable nodeTable = dsg.getQuadTable().getNodeTupleTable().getNodeTable(); Node node = nodeTable.getNodeForNodeId(nodeId); return node; }
@Override public Iterator<Node> listGraphNodes() { checkNotClosed(); Iterator<Tuple<NodeId>> x = storage.quadTable.getNodeTupleTable().findAll(); Iterator<NodeId> z = Iter.iter(x).map(t -> t.get(0)).distinct(); return NodeLib.nodes(storage.quadTable.getNodeTupleTable().getNodeTable(), z); }
/** * Create a graph filter for a TDB2 {@link DatasetGraph}. The filter matches (returns * true) for Tuples where the graph slot in quad is in the collection or for triples in the default * graph according the boolean. */ public static GraphFilterTDB2 graphFilter(DatasetGraph dsg, Collection<Node> namedGraphs, boolean matchDefaultGraph) { if ( ! TDBInternal.isTDB2(dsg) ) throw new IllegalArgumentException("DatasetGraph is not TDB2-backed"); List<NodeId> x = Txn.calculateRead(dsg, ()->{ NodeTable nt = TDBInternal.getDatasetGraphTDB(dsg).getQuadTable().getNodeTupleTable().getNodeTable(); return ListUtils.toList( namedGraphs.stream() .map(n->nt.getNodeIdForNode(n)) .filter(Objects::nonNull) ); }); return new GraphFilterTDB2(x, matchDefaultGraph); } }
public NodeTupleTable chooseNodeTupleTable(Node graphNode) { checkNotClosed() ; if ( graphNode == null || Quad.isDefaultGraph(graphNode) ) return getTripleTable().getNodeTupleTable() ; else // Includes Node.ANY and union graph return getQuadTable().getNodeTupleTable() ; }
public DataToTuples(DatasetGraphTDB dsgtdb, Destination<Tuple<NodeId>> tuples3, Destination<Tuple<NodeId>> tuples4, MonitorOutput output) { this.dsgtdb = dsgtdb; this.dest3 = tuples3; this.dest4 = tuples4; this.input = new ArrayBlockingQueue<>(LoaderConst.QueueSizeData); this.nodeTable = dsgtdb.getQuadTable().getNodeTupleTable().getNodeTable(); this.prefixes = dsgtdb.getPrefixes(); this.output = output; NodeTable nodeTable2 = dsgtdb.getTripleTable().getNodeTupleTable().getNodeTable(); if ( nodeTable != nodeTable2 ) throw new BulkLoaderException("Different node tables"); }
/** Find whether a specific graph name is in the quads table. */ public static QueryIterator testForGraphName(DatasetGraphTDB ds, Node graphNode, QueryIterator input, Predicate<Tuple<NodeId>> filter, ExecutionContext execCxt) { NodeId nid = TDBInternal.getNodeId(ds, graphNode) ; boolean exists = !NodeId.isDoesNotExist(nid) ; if ( exists ) { // Node exists but is it used in the quad position? NodeTupleTable ntt = ds.getQuadTable().getNodeTupleTable() ; // Don't worry about abortable - this iterator should be fast // (with normal indexing - at least one G???). // Either it finds a starting point, or it doesn't. We are only // interested in the first .hasNext. Iterator<Tuple<NodeId>> iter1 = ntt.find(nid, NodeId.NodeIdAny, NodeId.NodeIdAny, NodeId.NodeIdAny) ; if ( filter != null ) iter1 = Iter.filter(iter1, filter) ; exists = iter1.hasNext() ; } if ( exists ) return input ; else { input.close() ; return QueryIterNullIterator.create(execCxt) ; } }
public DataToTuplesInlineSingle(DatasetGraphTDB dsgtdb, Consumer<Tuple<NodeId>> dest3, Consumer<Tuple<NodeId>> dest4, MonitorOutput output) { this.dsgtdb = dsgtdb; this.dest3 = dest3; this.dest4 = dest4; this.output = output; this.nodeTable = dsgtdb.getTripleTable().getNodeTupleTable().getNodeTable(); this.prefixes = dsgtdb.getPrefixes(); NodeTable nodeTable2 = dsgtdb.getQuadTable().getNodeTupleTable().getNodeTable(); if ( nodeTable != nodeTable2 ) throw new BulkLoaderException("Different node tables"); }
public DataToTuplesInline(DatasetGraphTDB dsgtdb, Destination<Tuple<NodeId>> dest3, Destination<Tuple<NodeId>> dest4, MonitorOutput output) { this.dsgtdb = dsgtdb; this.dest3 = dest3; this.dest4 = dest4; this.output = output; this.nodeTable = dsgtdb.getTripleTable().getNodeTupleTable().getNodeTable(); this.prefixes = dsgtdb.getPrefixes(); NodeTable nodeTable2 = dsgtdb.getQuadTable().getNodeTupleTable().getNodeTable(); if ( nodeTable != nodeTable2 ) throw new BulkLoaderException("Different node tables"); }
/** Find all the graph names in the quads table. */ public static QueryIterator graphNames(DatasetGraphTDB ds, Node graphNode, QueryIterator input, Predicate<Tuple<NodeId>> filter, ExecutionContext execCxt) { List<Abortable> killList = new ArrayList<>() ; Iterator<Tuple<NodeId>> iter1 = ds.getQuadTable().getNodeTupleTable().find(NodeId.NodeIdAny, NodeId.NodeIdAny, NodeId.NodeIdAny, NodeId.NodeIdAny) ; if ( filter != null ) iter1 = Iter.filter(iter1, filter) ; Iterator<NodeId> iter2 = Iter.map(iter1, t -> t.get(0)) ; // Project is cheap - don't brother wrapping iter1 iter2 = makeAbortable(iter2, killList) ; Iterator<NodeId> iter3 = Iter.distinct(iter2) ; iter3 = makeAbortable(iter3, killList) ; Iterator<Node> iter4 = NodeLib.nodes(ds.getQuadTable().getNodeTupleTable().getNodeTable(), iter3) ; final Var var = Var.alloc(graphNode) ; Iterator<Binding> iterBinding = Iter.map(iter4, node -> BindingFactory.binding(var, node)) ; // Not abortable. return new QueryIterTDB(iterBinding, killList, input, execCxt) ; }
@Override protected final int graphBaseSize() { if ( isDefaultGraph() ) return (int)getNodeTupleTable().size() ; Node gn = getGraphName() ; boolean unionGraph = isUnionGraph(gn) ; gn = unionGraph ? Node.ANY : gn ; Iterator<Tuple<NodeId>> iter = getDSG().getQuadTable().getNodeTupleTable().findAsNodeIds(gn, null, null, null) ; if ( unionGraph ) { iter = Iter.map(iter, project4TupleTo3Tuple) ; iter = Iter.distinctAdjacent(iter) ; } return (int)Iter.count(iter) ; }
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 ? SolverLib.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(); }
public LoaderSequential(DatasetGraph dsg, Node graphName, MonitorOutput output) { super(dsg, graphName, output); if ( ! TDBInternal.isBackedByTDB(dsg) ) throw new BulkLoaderException("Not a TDB2 database"); this.dsgtdb = TDBInternal.getDatasetGraphTDB(dsg); this.triplesLoader = new LoaderNodeTupleTable(dsgtdb.getTripleTable().getNodeTupleTable(), output, "Triples"); this.quadsLoader = new LoaderNodeTupleTable(dsgtdb.getQuadTable().getNodeTupleTable(), output, "Quads"); StreamRDF s = StreamRDFLib.dataset(dsg); s = new StreamRDFWrapper(s) { @Override public void triple(Triple triple) { triplesLoader.load(triple.getSubject(), triple.getPredicate(), triple.getObject()); countTriples++; } @Override public void quad(Quad quad) { quadsLoader.load(quad.getGraph(), quad.getSubject(), quad.getPredicate(), quad.getObject()); countQuads++; } }; this.stream = LoaderOps.toNamedGraph(s, graphName); }
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 ? SolverLib.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(); }
public TDBDatasetDetails(DatasetGraphTDB dsg) { ntTop = dsg.getTripleTable().getNodeTupleTable().getNodeTable() ; tripleIndexes = ArrayUtils.copy(dsg.getTripleTable().getNodeTupleTable().getTupleTable().getIndexes()) ; quadIndexes = ArrayUtils.copy(dsg.getQuadTable().getNodeTupleTable().getTupleTable().getIndexes()) ; fillInNodeTableDetails() ; fillInIndexDetails() ; }
/** Create a filter to exclude the graph http://example/g2 */ private static Predicate<Tuple<NodeId>> createFilter(Dataset ds) { DatasetGraphTDB dsg = (DatasetGraphTDB)(ds.asDatasetGraph()) ; final NodeTable nodeTable = dsg.getQuadTable().getNodeTupleTable().getNodeTable() ; final NodeId target = nodeTable.getNodeIdForNode(NodeFactory.createURI(graphToHide)) ; return item -> !( item.len() == 4 && item.get(0).equals(target) ); }
/** Create a filter to exclude the graph http://example/g2 */ private static Predicate<Tuple<NodeId>> createFilter(Dataset ds) { DatasetGraphTDB dsg = (DatasetGraphTDB)(ds.asDatasetGraph()) ; final NodeTable nodeTable = dsg.getQuadTable().getNodeTupleTable().getNodeTable() ; final NodeId target = nodeTable.getNodeIdForNode(NodeFactory.createURI(graphToHide)) ; return item -> !( item.len() == 4 && item.get(0).equals(target) ); }