You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2012/05/22 15:08:24 UTC

svn commit: r1341459 - in /jena/trunk/jena-tdb: src-examples/tdb/examples/ExQuadFilter.java src/main/java/com/hp/hpl/jena/tdb/sys/TDBInternal.java

Author: andy
Date: Tue May 22 13:08:24 2012
New Revision: 1341459

URL: http://svn.apache.org/viewvc?rev=1341459&view=rev
Log:
Start function library to access TDB internals.
Align quad filter example to new library.

Added:
    jena/trunk/jena-tdb/src/main/java/com/hp/hpl/jena/tdb/sys/TDBInternal.java
Modified:
    jena/trunk/jena-tdb/src-examples/tdb/examples/ExQuadFilter.java

Modified: jena/trunk/jena-tdb/src-examples/tdb/examples/ExQuadFilter.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-tdb/src-examples/tdb/examples/ExQuadFilter.java?rev=1341459&r1=1341458&r2=1341459&view=diff
==============================================================================
--- jena/trunk/jena-tdb/src-examples/tdb/examples/ExQuadFilter.java (original)
+++ jena/trunk/jena-tdb/src-examples/tdb/examples/ExQuadFilter.java Tue May 22 13:08:24 2012
@@ -28,11 +28,9 @@ import com.hp.hpl.jena.sparql.core.Quad 
 import com.hp.hpl.jena.sparql.sse.SSE ;
 import com.hp.hpl.jena.tdb.TDB ;
 import com.hp.hpl.jena.tdb.TDBFactory ;
-import com.hp.hpl.jena.tdb.nodetable.NodeTable ;
-import com.hp.hpl.jena.tdb.store.DatasetGraphTDB ;
 import com.hp.hpl.jena.tdb.store.NodeId ;
 import com.hp.hpl.jena.tdb.sys.SystemTDB ;
-import com.hp.hpl.jena.tdb.transaction.DatasetGraphTransaction ;
+import com.hp.hpl.jena.tdb.sys.TDBInternal ;
 
 /** Example of how to filter quads as they are accessed at the lowest level.
  * Can be used to exclude daat from specific graphs.   
@@ -71,14 +69,9 @@ public class ExQuadFilter
     /** Create a filter to exclude the graph http://example/g2 */
     private static Filter<Tuple<NodeId>> createFilter(Dataset ds)
     {
-        Object x = ds.asDatasetGraph() ;
-        
-        DatasetGraphTransaction dst = (DatasetGraphTransaction)(ds.asDatasetGraph()) ;
-        DatasetGraphTDB dsg = dst.getBaseDatasetGraph();
-        final NodeTable nodeTable = dsg.getQuadTable().getNodeTupleTable().getNodeTable() ;
         // Filtering operates at a very low level: 
-        // need to know the internal identifier for the graph name. 
-        final NodeId target = nodeTable.getNodeIdForNode(Node.createURI(graphToHide)) ;
+        // Need to know the internal identifier for the graph name. 
+        final NodeId target = TDBInternal.getNodeId(ds, Node.createURI(graphToHide)) ;
 
         System.out.println("Hide graph: "+graphToHide+" --> "+target) ;
         
@@ -89,7 +82,7 @@ public class ExQuadFilter
             public boolean accept(Tuple<NodeId> item)
             {
                 // Reverse the lookup as a demo
-                //Node n = nodeTable.getNodeForNodeId(target) ;
+                //Node n = TDBInternal.getNode(target) ;
                 //System.err.println(item) ;
                 if ( item.size() == 4 && item.get(0).equals(target) )
                 {
@@ -108,7 +101,7 @@ public class ExQuadFilter
         String[] x = {
             "SELECT * { GRAPH ?g { ?s ?p ?o } }",
             "SELECT * { ?s ?p ?o }",
-            // THis filter does not hide the graph itself, just the quads associated with the graph.
+            // This filter does not hide the graph itself, just the quads associated with the graph.
             "SELECT * { GRAPH ?g {} }"
             } ;
         

Added: jena/trunk/jena-tdb/src/main/java/com/hp/hpl/jena/tdb/sys/TDBInternal.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-tdb/src/main/java/com/hp/hpl/jena/tdb/sys/TDBInternal.java?rev=1341459&view=auto
==============================================================================
--- jena/trunk/jena-tdb/src/main/java/com/hp/hpl/jena/tdb/sys/TDBInternal.java (added)
+++ jena/trunk/jena-tdb/src/main/java/com/hp/hpl/jena/tdb/sys/TDBInternal.java Tue May 22 13:08:24 2012
@@ -0,0 +1,102 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.hp.hpl.jena.tdb.sys;
+
+import com.hp.hpl.jena.graph.Node ;
+import com.hp.hpl.jena.query.Dataset ;
+import com.hp.hpl.jena.sparql.core.DatasetGraph ;
+import com.hp.hpl.jena.tdb.nodetable.NodeTable ;
+import com.hp.hpl.jena.tdb.store.DatasetGraphTDB ;
+import com.hp.hpl.jena.tdb.store.NodeId ;
+import com.hp.hpl.jena.tdb.transaction.DatasetGraphTransaction ;
+
+/** A collection of helpers to abstract away from internal details of TDB. 
+ * Use with care.
+ */
+public class TDBInternal
+{
+    /** 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(Dataset ds, Node node)
+    {
+        return  getNodeId(ds.asDatasetGraph(), node) ;
+    }
+    
+    /** 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(DatasetGraph ds, Node node)
+    {
+        DatasetGraphTDB dsg = getDatasetGraphTDB(ds) ;
+        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(Dataset ds, NodeId nodeId)
+    {
+        return getNode(ds.asDatasetGraph(), nodeId) ;
+    }
+    
+    /** Return the node for a NodeId (if any).
+     *  Returns null if the NodeId does not exist in the dataset. 
+     */
+    public static Node getNode(DatasetGraph ds, NodeId nodeId)
+    {
+        DatasetGraphTDB dsg = getDatasetGraphTDB(ds) ;
+        if ( dsg == null )
+            return null ;
+        NodeTable nodeTable = dsg.getQuadTable().getNodeTupleTable().getNodeTable() ;
+        Node node = nodeTable.getNodeForNodeId(nodeId) ;
+        return node ;
+    }
+
+    /**
+     * Return the DatasetGraphTDB for a Dataset, or null.
+     */
+    public static DatasetGraphTDB getDatasetGraphTDB(Dataset ds)
+    {
+        return getDatasetGraphTDB(ds.asDatasetGraph()) ;
+    }
+
+    
+    /**
+     * Return the DatasetGraphTDB for a DatasetGraph, or null.
+     */
+    public static DatasetGraphTDB getDatasetGraphTDB(DatasetGraph dsg)
+    {
+        if ( dsg instanceof DatasetGraphTransaction )
+            return ((DatasetGraphTransaction)dsg).getBaseDatasetGraph() ;
+        
+        if ( dsg instanceof DatasetGraphTDB )
+            return (DatasetGraphTDB)dsg ;
+        
+        return null ;
+    }
+    
+}
+