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

andrae at mulgara.org andrae at mulgara.org
Fri Apr 11 07:00:13 UTC 2008


Author: andrae
Date: 2008-04-11 00:00:13 -0700 (Fri, 11 Apr 2008)
New Revision: 772

Modified:
   projects/xa2/object-pool/src/MemoryTrie.java
   projects/xa2/object-pool/src/MemoryTrieTest.java
Log:
Added random binary data test.



Modified: projects/xa2/object-pool/src/MemoryTrie.java
===================================================================
--- projects/xa2/object-pool/src/MemoryTrie.java	2008-04-11 04:52:20 UTC (rev 771)
+++ projects/xa2/object-pool/src/MemoryTrie.java	2008-04-11 07:00:13 UTC (rev 772)
@@ -22,6 +22,7 @@
   }
 
   public void insert(byte[] key, long value) {
+//    System.err.println("MemoryTrie # Inserting: " + Arrays.hashCode(key) + " :: " + value);
     if (root == null) {
       root = new TrieBranch(key, value);
     } else {
@@ -34,6 +35,7 @@
   }
 
   public long lookup(byte[] key) throws NotFound {
+//    System.err.println("MemoryTrie # Lookup: " + Arrays.hashCode(key));
     if (root == null) {
       throw new NotFound();
     }
@@ -70,7 +72,7 @@
     private int offset;
     private Map<Byte, TrieNode> children;
     private TrieLeaf term;
-
+    
     public TrieBranch(byte[] key, long value) {
       super();
       this.children = new HashMap<Byte, TrieNode>();
@@ -195,6 +197,10 @@
         }
       }
     }
+    
+    public String toString() {
+      return "Trie-BRANCH[" + (term != null) + " on " + offset + " with " + children.size() + " and least[" + least + "]";
+    }
   }
 
   private static class TrieLeaf extends TrieNode {
@@ -225,5 +231,9 @@
       assert Arrays.equals(this.key, key);
       return value;
     }
+    
+    public String toString() {
+      return "Trie-LEAF: " + Arrays.toString(key) + " -> " + value;
+    }
   }
 }

Modified: projects/xa2/object-pool/src/MemoryTrieTest.java
===================================================================
--- projects/xa2/object-pool/src/MemoryTrieTest.java	2008-04-11 04:52:20 UTC (rev 771)
+++ projects/xa2/object-pool/src/MemoryTrieTest.java	2008-04-11 07:00:13 UTC (rev 772)
@@ -4,11 +4,15 @@
  * Date 9th April 2008
  */
 
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileReader;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
 
 /**
  * Basic tests of the MemoryTrie.
@@ -19,6 +23,7 @@
     testWithFile(new File("../scratch/connectives.uniq"));
     testWithFile(new File("../scratch/web2a.uniq"));
     testWithFile(new File("../scratch/web2.uniq"));
+    testWithRandomFile(new File("/dev/urandom"));
   }
 
   public static void testWithFile(File file) throws Exception {
@@ -46,47 +51,86 @@
     
     System.out.println("Test Succeeded with " + file);
   }
-/*
+
+  public static class Bytes {
+    public final byte[] bytes;
+    
+    public Bytes(byte[] bytes) {
+      this.bytes = new byte[bytes.length];
+      System.arraycopy(bytes, 0, this.bytes, 0, bytes.length);
+    }
+    
+    public boolean equals(Object o) {
+//      System.err.print("Bytes::equals() # ");
+      if (o instanceof Bytes) {
+//        System.err.println("Comparing " + Arrays.hashCode(bytes) + " with " + Arrays.hashCode(((Bytes)o).bytes));
+        return Arrays.equals(bytes, ((Bytes)o).bytes);
+      } else {
+        return false;
+      }
+    }
+    
+    public int hashCode() {
+      int hc = Arrays.hashCode(bytes);
+//      System.err.println("Bytes # Calculated hashcode for: " + hc);
+      return hc;
+    }
+  }
+  
   public static void testWithRandomFile(File file) throws Exception {
-    Map<byte[], Long> namesMap = new HashMap<byte[], Long[]>();
+    Map<Bytes, Long> namesMap = new HashMap<Bytes, Long>();
     MemoryTrie namesTrie = new MemoryTrie();
 
     System.out.println("Inserting random bytes from " + file);
     FileChannel chan = new FileInputStream(file).getChannel();
-    ByteBuffer buffer = ByteBuffer.allocate(134*1024).clear().limit(0);
-    // Set limit to 0 so initial compact works correctly.
+    ByteBuffer buffer = ByteBuffer.allocate(134*1024);
 
     long n = 0;
     byte[] key;
+    // Set limit to 0 so initial compact works correctly.
+    buffer.clear().limit(0);
     while (n < 5000) {
-      if (buffer.hasRemaining() < 67*1024 && chan.read(buffer.compact()) == -1) {
+      if (buffer.remaining() < 67*1024 && chan.read(buffer.compact()) == -1) {
         break;
       }
       buffer.flip();
 
-      key = new byte[buffer.getShort()];
+      key = new byte[((int)buffer.getShort()) & 0x0000FFFF];
       buffer.get(key);
-      namesMap.put(key, n++);
-      namesTrie.insert(key, n);
+      Bytes keyBytes = new Bytes(key);
+      
+//      System.err.println("TEST: read - " + keyBytes.hashCode());
+      
+      if (namesMap.containsKey(keyBytes)) {
+        namesTrie.insert(key, namesMap.get(keyBytes));
+      } else {
+        n++;
+        namesMap.put(keyBytes, n);
+        namesTrie.insert(key, n);
+      }
 
-      for (i = 0; i < 10; i++) {
-        key = new byte[buffer.getByte()];
+      for (int i = 0; i < 10; i++) {
+        key = new byte[((int)buffer.get()) & 0x000000FF];
         buffer.get(key);
-        namesMap.put(key, n++);
-        namesTrie.insert(key, n);
+        keyBytes = new Bytes(key);
+        if (namesMap.containsKey(keyBytes)) {
+          namesTrie.insert(key, namesMap.get(keyBytes));
+        } else {
+          n++;
+          namesMap.put(keyBytes, n);
+          namesTrie.insert(key, n);
+        }
       }
     }
     chan.close();
 
     System.out.println("Checking random bytes from " + file);
-    for (byte[] key : namesMap.keySet()) {
-      if (namesTrie.lookup(key) != namesMap.get(key)) {
+    for (Bytes k : namesMap.keySet()) {
+      if (namesTrie.lookup(k.bytes) != namesMap.get(k)) {
         throw new IllegalStateException("Trie doesn't match Map");
       }
     }
     
     System.out.println("Test Succeeded with " + file);
   }
-  */
-
 }




More information about the Mulgara-svn mailing list