[Mulgara-svn] r900 - in projects/xa2/object-pool/src: data trie

andrae at mulgara.org andrae at mulgara.org
Tue May 6 10:36:52 UTC 2008


Author: andrae
Date: 2008-05-06 03:36:51 -0700 (Tue, 06 May 2008)
New Revision: 900

Modified:
   projects/xa2/object-pool/src/data/DataEntry.java
   projects/xa2/object-pool/src/data/DataFile.java
   projects/xa2/object-pool/src/trie/DiskTrie.java
Log:
Continuing progress towards an ISAM-leaf.



Modified: projects/xa2/object-pool/src/data/DataEntry.java
===================================================================
--- projects/xa2/object-pool/src/data/DataEntry.java	2008-05-06 06:39:34 UTC (rev 899)
+++ projects/xa2/object-pool/src/data/DataEntry.java	2008-05-06 10:36:51 UTC (rev 900)
@@ -15,26 +15,45 @@
  * Abstracts the concept of a data file entry.
  */
 public abstract class DataEntry {
-  private long value;
+  protected long value;
 
   public abstract int size();
+
   public abstract int totalSize() {
     return 8 + 4 + size(); // VALUE + LENGTH + DATA
   }
+
   public abstract void write(FileChannel chan) throws IOException;
+  public abstract DataEntry canonicalize();
 
-  protected class DiskBackedDataEntry extends DataEntry {
+  public abstract void rewind();
+  public abstract byte get(int position) throws IOException;
+  public abstract byte get() throws IOException;
+
+  public long getValue() {
+    return value;
+  }
+
+  protected static class DiskBackedDataEntry extends DataEntry {
+    private FileHandle handle;
+    private long position;
+    private ByteBuffer entry;
     private int length;
-    private ByteBuffer entry;
-    private ByteBuffer data;
 
     // FIXME: Need to replace this with a background populated ConcurrentLinkedQueue.
     private Block[] blocks;
 
+    private Block currBlock;
+
+
     DiskBackedDataEntry(FileHandle handle, long position) {
-      Block block = handle.readBlock(position);
-      this.entry = block.offset(position & (BLOCK_SIZE - 1));  // block size is a power of 2 therefore mask is size - 1.
+      this.handle = handle;
+      this.position = position;
+      this.curr = 0;
 
+      this.currBlock = handle.readBlock(position);
+      this.entry = currBlock.offset(position & (BLOCK_SIZE - 1));  // block size is a power of 2 therefore mask is size - 1.
+
       this.value = entry.getLong();
       this.length = entry.getInt();
 
@@ -49,9 +68,46 @@
         blocks[0] = block;
       }
     }
+
+    public DataEntry canonicalize() {
+      if (blocks.length == 1) {
+        byte[] data = new byte[entry.reset().remaining()];
+        entry.get(data);
+        return new MemoryBackedDataEntry(value, data);
+      } else {
+        return this;
+      }
+    }
+
+    public int size() {
+      return length;
+    }
+
+    public void rewrind() {
+      curr = 0;
+    }
+
+    public byte get() throws IOException {
+      byte b = get(curr);
+      curr++;
+      return b;
+    }
+
+    public byte get(int off) {
+      int bN = (off + 12) / BLOCK_SIZE;
+      if (blocks[bN] == null) {
+        blocks[bN] = handle.readBlock(off + 12);
+      }
+
+        
+    }
+
+    public void write(FileChannel chan) throws IOException {
+      handle.transferTo(chan, position, 12+length);
+    }
   }
 
-  protected class MemoryBackedDataEntry extends DataEntry {
+  protected static class MemoryBackedDataEntry extends DataEntry {
     private ByteBuffer data;
 
     MemoryBackedDataEntry(long value, byte[] data) {
@@ -72,5 +128,18 @@
     public int size() {
       return data.capacity();
     }
+
+    public DataEntry canonicalize() {
+      return this;
+    }
+
+    public void write(FileChannel chan) throws IOException {
+      ByteBuffer bb = ByteBuffer.allocateDirect(12 + data.length);
+      bb.clear();
+      bb.putLong(value);
+      bb.putInt(data.length);
+      bb.put(data);
+      chan.write((ByteBuffer)bb.flip());
+    }
   }
 }

Modified: projects/xa2/object-pool/src/data/DataFile.java
===================================================================
--- projects/xa2/object-pool/src/data/DataFile.java	2008-05-06 06:39:34 UTC (rev 899)
+++ projects/xa2/object-pool/src/data/DataFile.java	2008-05-06 10:36:51 UTC (rev 900)
@@ -16,13 +16,16 @@
  */
 public class DataFile {
   public static final int MAGIC = 0x00030002;
-  public static final int LOG2_BLOCK_SIZE = 21;
+  public static final int LOG2_BLOCK_SIZE = 15;
   public static final int BLOCK_SIZE = 0x01 << LOG2_BLOCK_SIZE;
 
-  private int blockSize;
+  private FileHandle handle;
 
-  public DataFile(File file) {
-    super();
-    FileHandle handle = IOScheduler.open(file, MAGIC, LOG2_BLOCK_SIZE);
+  public DataFile(File file, IOScheduler scheduler) {
+    this.handle = scheduler.open(file, MAGIC, LOG2_BLOCK_SIZE);
   }
+
+  public DataEntry getEntry(long position) throws IOException {
+    return new DataEntry(this, position);
+  }
 }

Modified: projects/xa2/object-pool/src/trie/DiskTrie.java
===================================================================
--- projects/xa2/object-pool/src/trie/DiskTrie.java	2008-05-06 06:39:34 UTC (rev 899)
+++ projects/xa2/object-pool/src/trie/DiskTrie.java	2008-05-06 10:36:51 UTC (rev 900)
@@ -262,7 +262,7 @@
     index.get();  // skip padding.
     long keyLocation = index.getLong();
 
-    DataEntry entry = dataFile.getEntry(keyLocation);
+    DataEntry entry = dataFile.getEntry(keyLocation).canonicalize();
 
     return new TrieLeaf(entry, false);
   }




More information about the Mulgara-svn mailing list