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

andrae at mulgara.org andrae at mulgara.org
Tue Apr 29 04:37:48 UTC 2008


Author: andrae
Date: 2008-04-28 21:37:47 -0700 (Mon, 28 Apr 2008)
New Revision: 867

Modified:
   projects/xa2/object-pool/src/CompBlockTrie.java
   projects/xa2/object-pool/src/CompMemTrie.java
Log:
CompMemTrie now contains absolutely no reference to IO.  All IO has been migrated into CompBlockTrie.

This allows us to test and debug the Trie operation independently of the seralization code.



Modified: projects/xa2/object-pool/src/CompBlockTrie.java
===================================================================
--- projects/xa2/object-pool/src/CompBlockTrie.java	2008-04-29 04:27:51 UTC (rev 866)
+++ projects/xa2/object-pool/src/CompBlockTrie.java	2008-04-29 04:37:47 UTC (rev 867)
@@ -154,13 +154,19 @@
       throw new InsufficientSpace("Attempt to write trie index larger than 64K");
     }
 
+    HashMap<TrieNode, Short> locationMap = new HashMap<TrieNode, Short>();
+
     index.putInt(MAGIC);
     if (root != null) {
-      writeTrieNode(root, index, data);
+      writeTrieNode(root, index, data, locationMap);
     }
     index.put(TYPE_ROOT_LOC);
     index.put((byte)0xFF);
-    index.putShort(root.location);
+    if (root != null) {
+      index.putShort(locationMap.get(root));
+    } else {
+      index.putShort((short)0x0000);
+    }
 
     // Set the position to the block size to ensure whole blocks are written.
     index.position(index.limit());
@@ -174,8 +180,9 @@
     writerMap.put(TrieLeaf.class, new TrieLeafWriter());
   }
 
-  protected static void writeTrieNode(TrieNode node, ByteBuffer index, FileChannel data) throws IOException {
-    writerMap.get(node.getClass()).write(node, index, data);
+  protected static void writeTrieNode(TrieNode node, ByteBuffer index, FileChannel data,
+      Map<TrieNode, Short> locationMap) throws IOException {
+    writerMap.get(node.getClass()).write(node, index, data, locationMap);
   }
 
   private static interface TrieNodeWriter {
@@ -186,36 +193,36 @@
      * The data buffer is the target of the byte[]/value pairs stored in the leaves.  We want to keep these
      * separate for caching purposes.
      */
-    public void write(TrieNode node, ByteBuffer index, FileChannel data) throws IOException;
+    public void write(TrieNode node, ByteBuffer index, FileChannel data, Map<TrieNode, Short> locationMap) throws IOException;
   }
 
   private static class TrieBranchWriter implements TrieNodeWriter {
-    public void write(TrieNode node, ByteBuffer index, FileChannel data) throws IOException {
+    public void write(TrieNode node, ByteBuffer index, FileChannel data, Map<TrieNode, Short> locationMap) throws IOException {
       TrieBranch branch = (TrieBranch)node;
       if (branch.term != null) {
-        writeTrieNode(branch.term, index, data);
+        writeTrieNode(branch.term, index, data, locationMap);
       }
       for (TrieNode child : branch.children) {
-        writeTrieNode(child, index, data);
+        writeTrieNode(child, index, data, locationMap);
       }
 
-      branch.location = (short)index.position();
+      locationMap.put(branch, (short)index.position());
       
       index.put((branch.term == null) ? TYPE_BRANCH_NOTERM : TYPE_BRANCH_TERM);
       index.put((byte)0xFF);  // Padding to keep things short-aligned.
       index.putInt(branch.offset);
       if (branch.term != null) {
-        index.putShort(branch.term.location);
+        index.putShort(locationMap.get(branch.term));
       }
       branch.children.writeHeader(index);
       for (TrieNode child : branch.children) {
-        index.putShort(child.location);
+        index.putShort(locationMap.get(child));
       }
     }
   }
 
   private static class TrieLeafWriter implements TrieNodeWriter {
-    public void write(TrieNode node, ByteBuffer index, FileChannel data) throws IOException {
+    public void write(TrieNode node, ByteBuffer index, FileChannel data, Map<TrieNode, Short> locationMap) throws IOException {
       TrieLeaf leaf = (TrieLeaf)node;
 
       ByteBuffer dataBuf = ByteBuffer.allocateDirect(8 + 4 + leaf.key.length);
@@ -225,7 +232,7 @@
       dataBuf.put(leaf.key);
       data.write((ByteBuffer)dataBuf.flip());
 
-      leaf.location = (short)index.position();
+      locationMap.put(leaf, (short)index.position());
       index.put(TYPE_LEAF);
       index.put((byte)0xFF); // Pad to 16-bit aligned.
       index.putLong(keyLocation);
@@ -235,8 +242,6 @@
   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) {
@@ -258,7 +263,6 @@
   }
 
   private TrieLeaf readTrieLeaf(ByteBuffer index, FileChannel data) throws IOException {
-    short location = (short)(index.position() - 1);
     index.get();
     long keyLocation = index.getLong();
     data.position(keyLocation);
@@ -271,7 +275,7 @@
     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);
+    return new TrieLeaf(key, value, false);
   }
 
   private static Map<Class, TrieNodeSizer> sizerMap = new HashMap<Class, TrieNodeSizer>();

Modified: projects/xa2/object-pool/src/CompMemTrie.java
===================================================================
--- projects/xa2/object-pool/src/CompMemTrie.java	2008-04-29 04:27:51 UTC (rev 866)
+++ projects/xa2/object-pool/src/CompMemTrie.java	2008-04-29 04:37:47 UTC (rev 867)
@@ -53,11 +53,6 @@
 
   protected abstract static class TrieNode {
     protected TrieLeaf aLeaf;
-    /**
-     * Offset into block of this TrieNode.
-     * 0 if uninitialized.  Measured in bytes.
-     */
-    protected short location = 0;
 
     /**
      * @return false if we need to insert key above this node.
@@ -220,18 +215,19 @@
     final byte[] key;
     final long value;
 
-    protected TrieLeaf(byte[] key, long value, short location) {
-      this.key = key;
+    protected TrieLeaf(byte[] key, long value, boolean copyKey) {
+      if (copyKey) {
+        this.key = new byte[key.length];
+        System.arraycopy(key, 0, this.key, 0, key.length);
+      } else {
+        this.key = key;
+      }
       this.value = value;
-      this.location = location;
       this.aLeaf = this;
     }
 
     public TrieLeaf(byte[] key, long value) {
-      this.key = new byte[key.length];
-      System.arraycopy(key, 0, this.key, 0, key.length);
-      this.value = value;
-      this.aLeaf = this;
+      this(key, value, true);
     }
 
     protected boolean insert(TrieLeaf node, int parentLcp) {




More information about the Mulgara-svn mailing list