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

andrae at mulgara.org andrae at mulgara.org
Thu Apr 24 04:10:12 UTC 2008


Author: andrae
Date: 2008-04-23 21:10:11 -0700 (Wed, 23 Apr 2008)
New Revision: 834

Modified:
   projects/xa2/object-pool/src/ByteMap.java
   projects/xa2/object-pool/src/CompBlockTrie.java
   projects/xa2/object-pool/src/CompBlockTrieTest.java
   projects/xa2/object-pool/src/CompMemTrie.java
Log:
The reading code now compiles - tests still fail.



Modified: projects/xa2/object-pool/src/ByteMap.java
===================================================================
--- projects/xa2/object-pool/src/ByteMap.java	2008-04-24 03:10:41 UTC (rev 833)
+++ projects/xa2/object-pool/src/ByteMap.java	2008-04-24 04:10:11 UTC (rev 834)
@@ -8,6 +8,7 @@
 
 import java.nio.ByteBuffer;
 import java.util.Iterator;
+import java.util.Map;
 import java.util.NoSuchElementException;
 
 
@@ -27,12 +28,13 @@
     data = (T[])new Object[0];
   }
 
+  @SuppressWarnings("unchecked")
   public ByteMap(ByteBuffer index, Map<Short, T> nodeMap) {
-    high = bb.getShort();
+    high = index.getShort();
     low = new short[ones(high)];
-    dataCount = 0;
+    short dataCount = 0;
     for (int i = 0; i < low.length; i++) {
-      low[i] = bb.getShort();
+      low[i] = index.getShort();
       dataCount += ones(low[i]);
     }
     data = (T[])new Object[dataCount];

Modified: projects/xa2/object-pool/src/CompBlockTrie.java
===================================================================
--- projects/xa2/object-pool/src/CompBlockTrie.java	2008-04-24 03:10:41 UTC (rev 833)
+++ projects/xa2/object-pool/src/CompBlockTrie.java	2008-04-24 04:10:11 UTC (rev 834)
@@ -4,6 +4,9 @@
  * Date 22nd April 2008
  */
 
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
 import java.util.Arrays;
 
 /**
@@ -46,6 +49,11 @@
     this.space = (short)((blockSize - 6) / WORST_CASE_ENTRY_SIZE); // -6 leaves room for header info.
   }
 
+  public CompBlockTrie(ByteBuffer index, FileChannel data) throws IOException {
+    super(index, data);
+    this.space = 0; // If this gets modified it will be recalculated automaticly then.
+  }
+
   public void insert(byte[] key, long value) throws CompMemTrie.InsufficientSpace {
     // Space was calculated on the basis of worst-case - if we were worst case we have now filled the
     // trie-block, but if we haven't we should recalculate the space available. 

Modified: projects/xa2/object-pool/src/CompBlockTrieTest.java
===================================================================
--- projects/xa2/object-pool/src/CompBlockTrieTest.java	2008-04-24 03:10:41 UTC (rev 833)
+++ projects/xa2/object-pool/src/CompBlockTrieTest.java	2008-04-24 04:10:11 UTC (rev 834)
@@ -73,12 +73,28 @@
 
     indexFile.force(true);
     dataFile.force(true);
+    indexFile.close();
+    dataFile.close();
 
     long _endInsert = System.currentTimeMillis();
     names.close();
 
+    indexFile = new RandomAccessFile("tmp/sp_idx", "rw").getChannel().truncate(0);
+    dataFile = new RandomAccessFile("tmp/sp_dat", "rw").getChannel().truncate(0);
+    indexBuffer = ByteBuffer.allocateDirect(BLOCK_SIZE);
+    ArrayList<CompBlockTrie> readTries = new ArrayList<CompBlockTrie>();
+
+    long _startRead = System.currentTimeMillis();
+
+    while (indexFile.read((ByteBuffer)indexBuffer.clear()) != -1) {
+      indexBuffer.flip();
+      CompBlockTrie ctrie = new CompBlockTrie(indexBuffer, dataFile);
+      readTries.add(ctrie);
+    }
+
     System.out.println("Checking lines from " + file);
     long _startLookup = System.currentTimeMillis();
+
     for (byte[] key : namesMap.keySet()) {
       boolean found = false;
       for (CompBlockTrie t : tries) {
@@ -89,7 +105,17 @@
       if (!found) {
         throw new IllegalStateException("Trie doesn't match Map");
       }
+      found = false;
+      for (CompBlockTrie t : readTries) {
+        if (t.lookup(key) == namesMap.get(key)) {
+          found = true;
+        }
+      }
+      if (!found) {
+        throw new IllegalStateException("Read-Trie doesn't match Map");
+      }
     }
+
     long _endLookup = System.currentTimeMillis();
     
     System.out.println("Test Succeeded with " + file +

Modified: projects/xa2/object-pool/src/CompMemTrie.java
===================================================================
--- projects/xa2/object-pool/src/CompMemTrie.java	2008-04-24 03:10:41 UTC (rev 833)
+++ projects/xa2/object-pool/src/CompMemTrie.java	2008-04-24 04:10:11 UTC (rev 834)
@@ -6,8 +6,11 @@
 
 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.
@@ -67,7 +70,7 @@
     index.putShort(index.limit() - 2, root.location);
   }
 
-  public static CompMemTrie read(ByteBuffer index, FileChannel data) throws IOException {
+  public CompMemTrie(ByteBuffer index, FileChannel data) throws IOException {
     index.rewind(); // Note sure if I should doing this here or delegating to the caller.
     int magic = index.getInt();
     if (magic != MAGIC) {
@@ -92,13 +95,13 @@
       byte type = index.get();
       switch (type) {
         case TYPE_BRANCH_TERM:
-          nodeMap.put(location, TrieBranch.readTerm(index, true, nodeMap));
+          nodeMap.put(location, new TrieBranch(index, true, nodeMap));
           break;
         case TYPE_BRANCH_NOTERM:
-          nodeMap.put(location, TrieBranch.readNoTerm(index, false, nodeMap));
+          nodeMap.put(location, new TrieBranch(index, false, nodeMap));
           break;
-        case TYPE_BRANCH_LEAF:
-          nodeMap.put(location, TrieLeaf.readLeaf(index, data));
+        case TYPE_LEAF:
+          nodeMap.put(location, new TrieLeaf(index, data));
           break;
       }
     }
@@ -199,8 +202,8 @@
       return total;
     }
 
-    protected TrieBranch(ByteBuffer index, Map<Short, TrieNode> nodeMap, boolean hasTerm) throws IOException {
-      location = (short)index.position() - 1;
+    protected TrieBranch(ByteBuffer index, boolean hasTerm, Map<Short, TrieNode> nodeMap) throws IOException {
+      location = (short)(index.position() - 1);
 
       index.get();  // skip padding.
       offset = index.getInt();
@@ -353,15 +356,15 @@
     }
 
     protected TrieLeaf(ByteBuffer index, FileChannel data) throws IOException {
-      location = (short)index.position() - 1;
+      location = (short)(index.position() - 1);
       index.get();
-      keyLocation = getLong();
+      this.keyLocation = index.getLong();
       data.position(keyLocation);
       ByteBuffer bb = ByteBuffer.allocateDirect(14);
       data.read(bb);
       value = bb.getLong();
-      length = bb.getInt();
-      key = new byte[length]; // FIXME: I eventually need to replace this with a lazy load.
+      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));
     }
 




More information about the Mulgara-svn mailing list