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 ; }
protected QuadTable makeQuadTable(Location location, NodeTable nodeTable, DatasetControl policy, StoreParams params) { String primary = params.getPrimaryIndexQuads() ; String[] indexes = params.getQuadIndexes() ; // Allow experimentation of other index layouts. // if ( indexes.length != 6 ) // error(log, // "Wrong number of quad table indexes: "+StrUtils.strjoin(",", // indexes)) ; log.debug("Quad table: " + primary + " :: " + String.join(",", indexes)) ; TupleIndex quadIndexes[] = makeTupleIndexes(location, primary, indexes, params) ; if ( quadIndexes.length != indexes.length ) error(log, "Wrong number of quad table tuples indexes: " + quadIndexes.length) ; QuadTable quadTable = new QuadTable(quadIndexes, nodeTable, policy) ; return quadTable ; }
@Override protected void addToNamedGraph(Node g, Node s, Node p, Node o) { getQuadTable().add(g, s, p, o) ; }
@Override protected void deleteFromNamedGraph(Node g, Node s, Node p, Node o) { getQuadTable().delete(g, s, p, o) ; }
@Override protected Iterator<Quad> findInSpecificNamedGraph(Node g, Node s, Node p, Node o) { return getQuadTable().find(g, s, p, o) ; }
@Override public void close() { if ( closed ) return ; closed = true ; tripleTable.close() ; quadTable.close() ; prefixes.close(); // Which will cause reuse to throw exceptions early. tripleTable = null ; quadTable = null ; prefixes = null ; }
@Override public void clear() { // Leave the node table alone. getTripleTable().clearTriples() ; getQuadTable().clearQuads() ; }
/** Delete a quad (as graph node and triple) - return true if it was deleted, false if it didn't exist */ public boolean delete( Node gn, Triple triple ) { return delete(gn, triple.getSubject(), triple.getPredicate(), triple.getObject()) ; }
@Override protected Iterator<Quad> findInAnyNamedGraphs(Node s, Node p, Node o) { return getQuadTable().find(Node.ANY, s, p, o) ; }
@Override // Empty graphs don't "exist" public boolean containsGraph(Node graphNode) { if ( Quad.isDefaultGraph(graphNode) || Quad.isUnionGraph(graphNode) ) return true ; // 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 = quadTable.getNodeTupleTable().findAsNodeIds(graphNode, null, null, null) ; if ( x == null ) return false ; // NodeId graphNodeId = quadTable.getNodeTupleTable().getNodeTable().getNodeIdForNode(graphNode) ; // Tuple<NodeId> pattern = Tuple.create(graphNodeId, null, null, null) ; // Iterator<Tuple<NodeId>> x = quadTable.getNodeTupleTable().getTupleTable().find(pattern) ; boolean result = x.hasNext() ; return result ; }
/** Add a quad (as graph node and triple) - return true if it was added, false if it already existed */ public boolean add(Node gn, Triple triple ) { return add(gn, triple.getSubject(), triple.getPredicate(), triple.getObject()) ; }
/** Delete a quad - return true if it was deleted, false if it didn't exist */ public boolean delete( Quad quad ) { return delete(quad.getGraph(), quad.getSubject(), quad.getPredicate(), quad.getObject()) ; }
/** * 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 ; }
/** Add a quad - return true if it was added, false if it already existed */ public boolean add( Quad quad ) { return add(quad.getGraph(), quad.getSubject(), quad.getPredicate(), quad.getObject()) ; }
/** * 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() { Iterator<Tuple<NodeId>> x = quadTable.getNodeTupleTable().findAll() ; Iterator<NodeId> z = Iter.iter(x).map(t -> t.get(0)).distinct() ; return NodeLib.nodes(quadTable.getNodeTupleTable().getNodeTable(), z) ; }
public NodeTupleTable chooseNodeTupleTable(Node graphNode) { if ( graphNode == null || Quad.isDefaultGraph(graphNode) ) return getTripleTable().getNodeTupleTable() ; else // Includes Node.ANY and union graph return getQuadTable().getNodeTupleTable() ; }
/** * Create a graph filter for a TDB1 {@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 GraphFilterTDB1 graphFilter(DatasetGraph dsg, Collection<Node> namedGraphs, boolean matchDefaultGraph) { if ( ! TDBInternal.isTDB1(dsg) ) throw new IllegalArgumentException("DatasetGraph is not TDB1-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 GraphFilterTDB1(x, matchDefaultGraph); } }
public static TupleIndex getIndex(String idxName, DatasetGraphTDB dsg) { System.out.println(idxName) ; TupleIndex[] indexes1 = dsg.getTripleTable().getNodeTupleTable().getTupleTable().getIndexes() ; TupleIndex[] indexes2 = dsg.getQuadTable().getNodeTupleTable().getTupleTable().getIndexes() ; TupleIndex idx = null ; for ( TupleIndex i : indexes1 ) { if ( i.getName().equals(idxName) ) return i ; } for ( TupleIndex i : indexes2 ) { if ( i.getName().equals(idxName) ) return i ; } return null ; }
/** 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) ; } }