[Mulgara-svn] r865 - projects/xa2/object-pool/src

andrae at mulgara.org andrae at mulgara.org
Tue Apr 29 03:59:04 UTC 2008


Author: andrae
Date: 2008-04-28 20:59:02 -0700 (Mon, 28 Apr 2008)
New Revision: 865

Modified:
   projects/xa2/object-pool/src/CompBlockTrie.java
   projects/xa2/object-pool/src/CompMemTrie.java
Log:
Almost finished migrating IO code out of memory-trie.



Modified: projects/xa2/object-pool/src/CompBlockTrie.java
===================================================================
--- projects/xa2/object-pool/src/CompBlockTrie.java	2008-04-29 03:32:48 UTC (rev 864)
+++ projects/xa2/object-pool/src/CompBlockTrie.java	2008-04-29 03:59:02 UTC (rev 865)
@@ -80,13 +80,13 @@
       byte type = index.get();
       switch (type) {
         case TYPE_BRANCH_TERM:
-          nodeMap.put(location, new TrieBranch(index, true, nodeMap));
+          nodeMap.put(location, readTrieBranch(index, true, nodeMap));
           break;
         case TYPE_BRANCH_NOTERM:
-          nodeMap.put(location, new TrieBranch(index, false, nodeMap));
+          nodeMap.put(location, readTrieBranch(index, false, nodeMap));
           break;
         case TYPE_LEAF:
-          nodeMap.put(location, new TrieLeaf(index, data));
+          nodeMap.put(location, readTrieLeaf(index, data));
           break;
         case TYPE_ROOT_LOC:
           index.get();
@@ -221,4 +221,46 @@
       index.putLong(leaf.keyLocation);
     }
   }
+
+  private TrieBranch readTrieBranch(ByteBuffer index, boolean hasTerm, Map<Short, TrieNode> nodeMap) throws IOException {
+    TrieBranch branch = new TrieBranch();
+
+    branch.location = (short)(index.position() - 1);
+
+    index.get();  // skip padding.
+    branch.offset = index.getInt();
+    if (hasTerm) {
+      branch.term = (TrieLeaf)nodeMap.get(index.getShort());
+    }
+    branch.children = new ByteMap<TrieNode>(index, nodeMap);
+    if (branch.term == null) {
+      // This two-step process is required to avoid the cast from Object[] to TrieNode[] inserted by java
+      // generics which triggers the obvious ClassCastException.  By extracting explicitly to an Object[]
+      // first; then indexing to obtain an Object; then casting the Object to TrieNode the compiler doesn't
+      // insert the erroneous cast.
+      Object[] d = branch.children.data;
+      branch.aLeaf = ((TrieNode)(d[0])).aLeaf;
+    } else {
+      branch.aLeaf = branch.term;
+    }
+
+    return branch;
+  }
+
+  private TrieLeaf readTrieLeaf(ByteBuffer index, FileChannel data) throws IOException {
+    short location = (short)(index.position() - 1);
+    index.get();
+    long keyLocation = index.getLong();
+    data.position(keyLocation);
+    // FIXME: I need to cache these to reduce the allocation cost which is showing up under profiling
+    ByteBuffer bb = ByteBuffer.allocateDirect(8+4); // sizeof(value) + sizof(length).
+    data.read(bb);
+    bb.flip();
+    long value = bb.getLong();
+    int length = bb.getInt();
+    byte[] key = new byte[length]; // FIXME: I eventually need to replace this with a lazy load.
+    data.read(ByteBuffer.wrap(key));
+
+    return new TrieLeaf(key, value, location, keyLocation);
+  }
 }

Modified: projects/xa2/object-pool/src/CompMemTrie.java
===================================================================
--- projects/xa2/object-pool/src/CompMemTrie.java	2008-04-29 03:32:48 UTC (rev 864)
+++ projects/xa2/object-pool/src/CompMemTrie.java	2008-04-29 03:59:02 UTC (rev 865)
@@ -4,13 +4,7 @@
  * Date 15th April 2008
  */
 
-import java.io.IOException;
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-import java.nio.channels.FileChannel;
 import java.util.Arrays;
-import java.util.HashMap;
-import java.util.Map;
 
 /**
  * Implements an in-memory trie - uses ByteMaps to implement trie nodes.
@@ -108,8 +102,9 @@
     protected ByteMap<TrieNode> children;
     protected TrieLeaf term;
     
+    protected TrieBranch() {}
+
     public TrieBranch(byte[] key, long value) {
-      super();
       this.children = new ByteMap<TrieNode>();
       this.term = new TrieLeaf(key, value);
       this.offset = term.key.length;
@@ -148,28 +143,6 @@
       return total;
     }
 
-    protected TrieBranch(ByteBuffer index, boolean hasTerm, Map<Short, TrieNode> nodeMap) throws IOException {
-      location = (short)(index.position() - 1);
-
-      index.get();  // skip padding.
-      offset = index.getInt();
-      if (hasTerm) {
-        term = (TrieLeaf)nodeMap.get(index.getShort());
-      }
-      children = new ByteMap<TrieNode>(index, nodeMap);
-      if (term == null) {
-        // This two-step process is required to avoid the cast from Object[] to TrieNode[] inserted by java
-        // generics which triggers the obvious ClassCastException.  By extracting explicitly to an Object[]
-        // first; then indexing to obtain an Object; then casting the Object to TrieNode the compiler doesn't
-        // insert the erroneous cast.
-        Object[] d = children.data;
-        aLeaf = ((TrieNode)(d[0])).aLeaf;
-      } else {
-        aLeaf = term;
-      }
-//      aLeaf = (term == null) ? ((TrieNode)(children.data[0])).aLeaf : term;
-    }
-
     private TrieBranch(TrieNode oldNode, TrieLeaf newNode) {
       super();
       assert oldNode != null;
@@ -286,29 +259,21 @@
 
     protected long keyLocation = 0;
 
+    protected TrieLeaf(byte[] key, long value, short location, long keyLocation) {
+      this.key = key;
+      this.value = value;
+      this.location = location;
+      this.keyLocation = keyLocation;
+      this.aLeaf = this;
+    }
+
     public TrieLeaf(byte[] key, long value) {
-      super();
       this.key = new byte[key.length];
       System.arraycopy(key, 0, this.key, 0, key.length);
       this.value = value;
       this.aLeaf = this;
     }
 
-    protected TrieLeaf(ByteBuffer index, FileChannel data) throws IOException {
-      location = (short)(index.position() - 1);
-      index.get();
-      this.keyLocation = index.getLong();
-      data.position(keyLocation);
-      ByteBuffer bb = ByteBuffer.allocateDirect(8+4); // sizeof(value) + sizof(length).
-      data.read(bb);
-      bb.flip();
-      value = bb.getLong();
-      int length = bb.getInt();
-      this.key = new byte[length]; // FIXME: I eventually need to replace this with a lazy load.
-      data.read(ByteBuffer.wrap(key));
-      this.aLeaf = this;
-    }
-
     protected boolean insert(TrieLeaf node, int parentLcp) {
       if (key.length != node.key.length) {
         return false;




More information about the Mulgara-svn mailing list