Repository: jena
Updated Branches:
  refs/heads/master 339061538 -> ac0567d16


Add comments to NodeLib (from PR#391), reformat, remove dead code.

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

Branch: refs/heads/master
Commit: 97b7b28208dc64faaeb9595b0e7f636ba263f333
Parents: 3390615
Author: Andy Seaborne <a...@apache.org>
Authored: Thu Apr 5 17:50:17 2018 +0100
Committer: Andy Seaborne <a...@apache.org>
Committed: Thu Apr 5 17:50:17 2018 +0100

----------------------------------------------------------------------
 .../java/org/apache/jena/tdb/lib/NodeLib.java   | 220 +++++++++----------
 1 file changed, 102 insertions(+), 118 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/97b7b282/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 240a12e..3925b52 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
@@ -33,7 +33,6 @@ import org.apache.jena.atlas.lib.PoolBase ;
 import org.apache.jena.atlas.lib.PoolSync ;
 import org.apache.jena.atlas.logging.Log ;
 import org.apache.jena.graph.Node ;
-import org.apache.jena.riot.out.NodeFmtLib ;
 import org.apache.jena.sparql.util.NodeUtils ;
 import org.apache.jena.tdb.TDBException ;
 import org.apache.jena.tdb.base.objectfile.ObjectFile ;
@@ -54,178 +53,163 @@ public class NodeLib
     final private static char[] invalidIRIChars = { MarkerChar , ' ' } ; 
     final private static int SIZE = 1024;
     // Marshalling space.
-    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}.
+    // 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 synchonize to ensure writing is not concurrent.  
+     * Callers must synchronize to ensure writing is not concurrent.
      */
-    public static long encodeStore(Node node, ObjectFile file)
-    {
-        int maxSize = nodec.maxSize(node) ;
+    public static long encodeStore(Node node, ObjectFile file) {
+        int maxSize = nodec.maxSize(node);
         ByteBuffer bb = workspace;
         if ( maxSize >= SIZE )
             // Large object. Special buffer.
             bb = ByteBuffer.allocate(maxSize);
         else
             bb.clear();
-        int len = nodec.encode(node, bb, null) ;
+        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}.
+    /**
+     * Read and decode a {@link Node} from the {@link ObjectFile}. The {@code 
id} must
+     * have originally been generated by {@link #encodeStore}.
      */
-    public static Node fetchDecode(long id, ObjectFile file)
-    {
-        ByteBuffer bb = file.read(id) ;
+    public static Node fetchDecode(long id, ObjectFile file) {
+        ByteBuffer bb = file.read(id);
         if ( bb == null )
-            return null ;
-        return decode(bb) ;
+            return null;
+        return decode(bb);
     }
     
     /**
-     * Encode a node - it is better to use encodeStore which may avoid
-     * an additional copy in getting the node into the ObjectFile
+     * Encode a node - it is better to use encodeStore which may avoid an 
additional copy
+     * in getting the node into the ObjectFile and may avoid short-term byte 
buffer
+     * allocation.
      */
-    public static ByteBuffer encode(Node node)
-    {
-        int maxSize = nodec.maxSize(node) ;
-        ByteBuffer bb = ByteBuffer.allocate(maxSize) ;
-        int len = nodec.encode(node, bb, null) ;
-        bb.limit(len) ;
-        bb.position(0) ;
-        return bb ;
+    public static ByteBuffer encode(Node node) {
+        int maxSize = nodec.maxSize(node);
+        ByteBuffer bb = ByteBuffer.allocate(maxSize);
+        int len = nodec.encode(node, bb, null);
+        bb.limit(len);
+        bb.position(0);
+        return bb;
     }
-    
+
     /**
-     * Decode a node - it is better to use fetchDecode which may avoid an
-     * additional copy in getting the node from the ObjectFile.
+     * Decode a node - it is better to use fetchDecode which may avoid an 
additional copy
+     * in getting the node from the ObjectFile.
      */
-    public static Node decode(ByteBuffer bb)
-    {
-        bb.position(0) ;
-        Node n = nodec.decode(bb, null) ;
-        return n ;
+    public static Node decode(ByteBuffer bb) {
+        bb.position(0);
+        Node n = nodec.decode(bb, null);
+        return n;
     }
 
-    public static Hash hash(Node n)
-    { 
-        Hash h = new Hash(LenNodeHash) ;
-        setHash(h, n) ;
-        return h ;
+    public static Hash hash(Node n) {
+        Hash h = new Hash(LenNodeHash);
+        setHash(h, n);
+        return h;
     }
     
-    public static void setHash(Hash h, Node n) 
-    {
-        NodeType nt = NodeType.lookup(n) ;
-        switch(nt) 
-        {
-            case URI:
-                hash(h, n.getURI(), null, null, nt) ;
-                return ;
-            case BNODE:
-                hash(h, n.getBlankNodeLabel(), null, null, nt) ;
-                return ;
-            case LITERAL:
-                String dt = n.getLiteralDatatypeURI() ;
+    public static void setHash(Hash h, Node n) {
+        NodeType nt = NodeType.lookup(n);
+        switch (nt) {
+            case URI :
+                hash(h, n.getURI(), null, null, nt);
+                return;
+            case BNODE :
+                hash(h, n.getBlankNodeLabel(), null, null, nt);
+                return;
+            case LITERAL :
+                String dt = n.getLiteralDatatypeURI();
                 if ( NodeUtils.isSimpleString(n) || NodeUtils.isLangString(n) 
) {
                     // RDF 1.1 : No datatype for:
-                    //   xsd:String as simple literals
-                    //   rdf:langString and @ 
-                    dt = null ;
+                    // xsd:String as simple literals
+                    // rdf:langString and @
+                    dt = null;
                 }
-                hash(h, n.getLiteralLexicalForm(), n.getLiteralLanguage(), dt, 
nt) ;
-                return  ;
-            case OTHER:
-                throw new TDBException("Attempt to hash something strange: 
"+n) ; 
+                hash(h, n.getLiteralLexicalForm(), n.getLiteralLanguage(), dt, 
nt);
+                return;
+            case OTHER :
+                throw new TDBException("Attempt to hash something strange: " + 
n);
         }
-        throw new TDBException("NodeType broken: "+n) ; 
+        throw new TDBException("NodeType broken: " + n);
     }
     
-    /** This pattern is common - abstract */ 
-    private static int InitialPoolSize = 5 ;
-    private static Pool<MessageDigest> digesters = PoolSync.create(new 
PoolBase<MessageDigest>()) ;
+    private static int                 InitialPoolSize = 5;
+    private static Pool<MessageDigest> digesters       = PoolSync.create(new 
PoolBase<MessageDigest>());
     static {
         try {
             for ( int i = 0 ; i < InitialPoolSize ; i++ )
                 digesters.put(MessageDigest.getInstance("MD5"));
         }
-        catch (NoSuchAlgorithmException e)
-        { e.printStackTrace(); }
+        catch (NoSuchAlgorithmException e) {
+            e.printStackTrace();
+        }
     }
     
-    private static MessageDigest allocDigest()
-    {
+    private static MessageDigest allocDigest() {
         try {
-            MessageDigest disgest = digesters.get() ;
-            if ( disgest == null ) 
+            MessageDigest disgest = digesters.get();
+            if ( disgest == null )
                 disgest = MessageDigest.getInstance("MD5");
-            return disgest ;
+            return disgest;
+        }
+        catch (NoSuchAlgorithmException e) {
+            e.printStackTrace();
+            return null;
         }
-        catch (NoSuchAlgorithmException e)
-        { e.printStackTrace(); return null ; }
     }
 
-    private static void deallocDigest(MessageDigest digest) { digest.reset() ; 
digesters.put(digest) ; }
+    private static void deallocDigest(MessageDigest digest) {
+        digest.reset();
+        digesters.put(digest);
+    }    
     
-    
-    private static void hash(Hash h, String lex, String lang, String datatype, 
NodeType nodeType)
-    {
+    private static void hash(Hash h, String lex, String lang, String datatype, 
NodeType nodeType) {
         if ( datatype == null )
-            datatype = "" ;
+            datatype = "";
         if ( lang == null )
-            lang = "" ;
-        String toHash = lex + "|" + lang + "|" + 
datatype+"|"+nodeType.getName() ;
+            lang = "";
+        String toHash = lex + "|" + lang + "|" + datatype + "|" + 
nodeType.getName();
         MessageDigest digest;
-        try
-        {
-            digest = allocDigest() ; //MessageDigest.getInstance("MD5");
-            digest.update(Bytes.string2bytes(toHash)); 
//digest.update(toHash.getBytes("UTF8"));
+        try {
+            digest = allocDigest();
+            digest.update(Bytes.string2bytes(toHash));
             if ( h.getLen() == 16 )
                 // MD5 is 16 bytes.
-                digest.digest(h.getBytes(), 0, 16) ;
-            else
-            {
+                digest.digest(h.getBytes(), 0, 16);
+            else {
                 byte b[] = digest.digest(); // 16 bytes.
-                // Avoid the copy? If length is 16.  digest.digest(bytes, 0, 
length) needs 16 bytes
-                System.arraycopy(b, 0, h.getBytes(), 0, h.getLen()) ;
+                // Avoid the copy if length is 16? 
+                // digest.digest(bytes, 0, length) needs 16 bytes
+                System.arraycopy(b, 0, h.getBytes(), 0, h.getLen());
             }
-            deallocDigest(digest) ;
-            return ;
+            deallocDigest(digest);
+            return;
+        }
+        catch (DigestException ex) {
+            Log.error(NodeLib.class, "DigestException", ex);
         }
-        catch (DigestException ex) { Log.error(NodeLib.class, 
"DigestException", ex); } 
     }
-    
-    public static NodeId getNodeId(Record r, int idx)
-    {
-        return NodeId.create(Bytes.getLong(r.getKey(), idx)) ;
+
+    public static NodeId getNodeId(Record r, int idx) {
+        return NodeId.create(Bytes.getLong(r.getKey(), idx));
     }
-    
-    public static Node termOrAny(Node node)
-    {
+
+    public static Node termOrAny(Node node) {
         if ( node == null || node.isVariable() )
-            return Node.ANY ;
-        return node ;
+            return Node.ANY;
+        return node;
     }
-    
-    public static String format(String sep, Node[] nodes)
-    {
-        // Sigh ...
-        StringBuilder b = new StringBuilder() ;
-        for ( int i = 0 ; i < nodes.length ; i++ )
-        {
-            if ( i != 0 ) 
-                b.append(sep) ;
-            b.append(NodeFmtLib.str(nodes[i])) ;
-        }
-        return b.toString() ;
-    }
-    
-    public static Iterator<Node> nodes(final NodeTable nodeTable, 
Iterator<NodeId> iter)
-    {
-        return Iter.map(iter, nodeTable::getNodeForNodeId) ;
+
+    public static Iterator<Node> nodes(final NodeTable nodeTable, 
Iterator<NodeId> iter) {
+        return Iter.map(iter, nodeTable::getNodeForNodeId);
     }
 }

Reply via email to