jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [2/4] jena git commit: JENA-1581: Don't use a global buffer.
Date Thu, 02 Aug 2018 13:39:57 GMT
JENA-1581: Don't use a global buffer.

Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/8627e79d
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/8627e79d
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/8627e79d

Branch: refs/heads/master
Commit: 8627e79deb4c58337e49be0e524e423b3789b102
Parents: 6ca7904
Author: Andy Seaborne <andy@apache.org>
Authored: Wed Aug 1 21:15:41 2018 +0100
Committer: Andy Seaborne <andy@apache.org>
Committed: Wed Aug 1 21:15:41 2018 +0100

----------------------------------------------------------------------
 .../java/org/apache/jena/tdb/lib/NodeLib.java   | 46 ++++++++++++--------
 .../tdb/store/nodetable/NodeTableNative.java    | 24 +++++-----
 .../jena/tdb/store/nodetable/NodecSSE.java      |  5 +--
 3 files changed, 40 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/8627e79d/jena-tdb/src/main/java/org/apache/jena/tdb/lib/NodeLib.java
----------------------------------------------------------------------
diff --git a/jena-tdb/src/main/java/org/apache/jena/tdb/lib/NodeLib.java b/jena-tdb/src/main/java/org/apache/jena/tdb/lib/NodeLib.java
index 3925b52..284f286 100644
--- a/jena-tdb/src/main/java/org/apache/jena/tdb/lib/NodeLib.java
+++ b/jena-tdb/src/main/java/org/apache/jena/tdb/lib/NodeLib.java
@@ -48,34 +48,44 @@ public class NodeLib
 {
     private static Nodec nodec = new NodecSSE() ;
     
-    // Characters in IRIs that are illegal and cause SSE problems, but we wish to keep.
-    final private static char MarkerChar = '_' ;
-    final private static char[] invalidIRIChars = { MarkerChar , ' ' } ; 
-    final private static int SIZE = 1024;
-    // Marshalling space.
-    // This buffer is used in encodeStore in a single threaded fashion.
-    // Callers of encodeStore must ensure writing is not concurrent.
-    final private static ByteBuffer workspace = ByteBuffer.allocate(SIZE);
-
     /**
      * Encode and write a {@link Node} to the {@link ObjectFile}. Returns the location,
      * suitable for use with {@link #fetchDecode}.
-     * <p>
-     * Callers must synchronize to ensure writing is not concurrent.
      */
     public static long encodeStore(Node node, ObjectFile file) {
+        return encodeStore(node, file, null);
+    }
+    
+    /**
+     * Encode and write a {@link Node} to the {@link ObjectFile}. 
+     * Uses the given {@link ByteBuffer} for encoding space if possible.
+     * Returns the location, suitable for use with {@link #fetchDecode}.
+     */
+    public static long encodeStore(Node node, ObjectFile file, ByteBuffer bb) {
         int maxSize = nodec.maxSize(node);
-        ByteBuffer bb = workspace;
-        if ( maxSize >= SIZE )
-            // Large object. Special buffer.
-            bb = ByteBuffer.allocate(maxSize);
-        else
-            bb.clear();
+        if ( bb == null )
+            return allocEncodeWrite(node, file, maxSize);  
+        if ( bb.capacity() < maxSize )
+            // Buffer may not be big enough.
+            return allocEncodeWrite(node, file, maxSize);
+        // Use buffer provided.
+        bb.clear();
+        return encodeWrite(node, file, bb);
+    }
+
+    /** Encode and write, allocating space as needed */
+    private static long allocEncodeWrite(Node node, ObjectFile file, int maxSize) {
+        ByteBuffer bb = ByteBuffer.allocate(maxSize);
+        return encodeWrite(node, file, bb);
+    }
+    
+    /** Encode and write, using the space provided which is assumed to be large enough. */
+    private static long encodeWrite(Node node, ObjectFile file, ByteBuffer bb) {
         int len = nodec.encode(node, bb, null);
         long x = file.write(bb);
         return x;
     }
-    
+
     /**
      * Read and decode a {@link Node} from the {@link ObjectFile}. The {@code id} must
      * have originally been generated by {@link #encodeStore}.

http://git-wip-us.apache.org/repos/asf/jena/blob/8627e79d/jena-tdb/src/main/java/org/apache/jena/tdb/store/nodetable/NodeTableNative.java
----------------------------------------------------------------------
diff --git a/jena-tdb/src/main/java/org/apache/jena/tdb/store/nodetable/NodeTableNative.java
b/jena-tdb/src/main/java/org/apache/jena/tdb/store/nodetable/NodeTableNative.java
index 9974c5d..2148bbd 100644
--- a/jena-tdb/src/main/java/org/apache/jena/tdb/store/nodetable/NodeTableNative.java
+++ b/jena-tdb/src/main/java/org/apache/jena/tdb/store/nodetable/NodeTableNative.java
@@ -38,12 +38,9 @@ import org.apache.jena.tdb.store.NodeId ;
 /** A concrete NodeTable based on native storage (string file and an index) */ 
 public class NodeTableNative implements NodeTable
 {
-    // TODO Split into a general accessor (get and put (node,NodeId) pairs)
-    // Abstracts the getAllocateNodeId requirements.
-    
     protected ObjectFile objects ;
-    protected Index nodeHashToId ;        // hash -> int
-    private boolean syncNeeded = false ;
+    protected Index nodeHashToId ;          // hash -> int
+    private boolean syncNeeded = false ;    // Non-transactional mode sync.
     
     // Delayed construction - must call init explicitly.
     protected NodeTableNative() {}
@@ -95,10 +92,10 @@ public class NodeTableNative implements NodeTable
     // accessIndex and readNodeFromTable
     
     // Cache around this class further out in NodeTableCache are synchronized
-    // to maintain cache validatity which indirectly sync access to the NodeTable.
+    // to maintain cache validity which indirectly sync access to the NodeTable.
     // But to be sure, we provide MRSW guarantees on this class.
     // (otherwise if no cache => disaster)
-    // synchonization happens in accessIndex() and readNodeByNodeId
+    // Synchronization happens in accessIndex() and readNodeByNodeId()
     
     // NodeId to Node worker.
     private Node _retrieveNodeByNodeId(NodeId id)
@@ -120,13 +117,11 @@ public class NodeTableNative implements NodeTable
     {
         if ( node == Node.ANY )
             return NodeId.NodeIdAny ;
-        
-        // synchronized in accessIndex
         NodeId nodeId = accessIndex(node, allocate) ;
         return nodeId ;
     }
     
-    protected final NodeId accessIndex(Node node, boolean create)
+    private final NodeId accessIndex(Node node, boolean create)
     {
         Hash hash = new Hash(nodeHashToId.getRecordFactory().keyLength()) ;
         setHash(hash, node) ;
@@ -163,19 +158,20 @@ public class NodeTableNative implements NodeTable
     }
     
     // -------- NodeId<->Node
+    // Workspace for "normal" sized nodes.
+    // Null means allocate a fresh buffer each time.
+    private final ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
     // Synchronization:
     //   write: in accessIndex
     //   read: synchronized here.
     // Only places for accessing the StringFile.
-    
+
     private final NodeId writeNodeToTable(Node node)
     {
         syncNeeded = true ;
-        // Synchronized in accessIndex
-        long x = NodeLib.encodeStore(node, getObjects()) ;
+        long x = NodeLib.encodeStore(node, getObjects(), writeBuffer) ;
         return NodeId.create(x);
     }
-    
 
     private final Node readNodeFromTable(NodeId id)
     {

http://git-wip-us.apache.org/repos/asf/jena/blob/8627e79d/jena-tdb/src/main/java/org/apache/jena/tdb/store/nodetable/NodecSSE.java
----------------------------------------------------------------------
diff --git a/jena-tdb/src/main/java/org/apache/jena/tdb/store/nodetable/NodecSSE.java b/jena-tdb/src/main/java/org/apache/jena/tdb/store/nodetable/NodecSSE.java
index 1b7febb..3a3284e 100644
--- a/jena-tdb/src/main/java/org/apache/jena/tdb/store/nodetable/NodecSSE.java
+++ b/jena-tdb/src/main/java/org/apache/jena/tdb/store/nodetable/NodecSSE.java
@@ -41,10 +41,9 @@ import org.apache.jena.tdb.lib.StringAbbrev ;
 
 public class NodecSSE implements Nodec
 {
-    private static boolean SafeChars = false ;
     // Characters in IRIs that are illegal and cause SSE problems, but we wish to keep.
-    final private static char MarkerChar = '_' ;
-    final private static char[] invalidIRIChars = { MarkerChar , ' ' } ; 
+    private final static char MarkerChar = '_' ;
+    private final static char[] invalidIRIChars = { MarkerChar , ' ' } ; 
     
     public NodecSSE() {}
     


Mime
View raw message