[Mulgara-svn] r875 - projects/xa2/object-pool/src/scheduler

andrae at mulgara.org andrae at mulgara.org
Thu May 1 09:37:53 UTC 2008


Author: andrae
Date: 2008-05-01 02:37:52 -0700 (Thu, 01 May 2008)
New Revision: 875

Modified:
   projects/xa2/object-pool/src/scheduler/Block.java
   projects/xa2/object-pool/src/scheduler/BlockCache.java
   projects/xa2/object-pool/src/scheduler/FileHandle.java
   projects/xa2/object-pool/src/scheduler/IOScheduler.java
Log:
Still needs work, specifically testing, however this is deferred until after the ISAM-tree data level code is
done.



Modified: projects/xa2/object-pool/src/scheduler/Block.java
===================================================================
--- projects/xa2/object-pool/src/scheduler/Block.java	2008-05-01 04:56:19 UTC (rev 874)
+++ projects/xa2/object-pool/src/scheduler/Block.java	2008-05-01 09:37:52 UTC (rev 875)
@@ -7,18 +7,39 @@
 
 public class Block {
   private FileHandle handle;
-  private Long position;
+  private ByteBuffer buffer;
+  private Long blockId;
 
-  public Block(FileHandle handle, Long position) {
+  public Block(FileHandle handle, int size) {
     this.handle = handle;
-    this.position = position;
+    this.blockId = null;
+    this.buffer.allocateDirect(size);
   }
 
   public FileHandle getHandle() {
     return handle;
   }
 
-  public Long getPosition() {
-    return position;
+  public Long getBlockId() {
+    if (blockId == null) {
+      throw new IllegalStateException("Attempt to obtain blockId for uninitialized block");
+    return blockId;
   }
+
+  /**
+   * Prepare the Block to be written to/from file.
+   * Sets the Buffer's position = 0; limit = size; and stores the blockId.
+   */
+  public ByteBuffer prepare(Long id) {
+    blockId = id;
+    return (ByteBuffer)(buffer.clear());
+  }
+
+  /**
+   * Prepare the Block to be written to/from memory.
+   * Sets the Buffer's position = 0; limit = size; and leaves the blockId unchanged.
+   */
+  public ByteBuffer clear() {
+    return (ByteBuffer)(buffer.clear());
+  }
 }

Modified: projects/xa2/object-pool/src/scheduler/BlockCache.java
===================================================================
--- projects/xa2/object-pool/src/scheduler/BlockCache.java	2008-05-01 04:56:19 UTC (rev 874)
+++ projects/xa2/object-pool/src/scheduler/BlockCache.java	2008-05-01 09:37:52 UTC (rev 875)
@@ -13,16 +13,18 @@
   private FileHandle handle;
   // The Block object is required to maintain a strong reference to the Long key obj.
   private WeakHashMap<Long, SoftReference<Block>> blockCache;
+  private Queue<SoftReference<Block> blockPool;
 
   private static final int LOG2_BLOCK_SIZE = 21;
 
-  BlockCache(FileHandle handle) {
+  BlockCache(FileHandle handle, Queue<SoftReference<Block>> blockPool) {
     this.handle = handle;
     this.blockCache = new WeakHashMap<Long, SoftReference<Block>>();
+    this.blockPool = blockPool;
   }
 
   public Block getBlock(long position) {
-    Long pos = new Long(position >> handle.log2BlockSize());
+    Long pos = new Long(position >> LOG2_BLOCK_SIZE);
     SoftReference<Block> blockRef = blockCache.get(pos);
     if (blockRef != null) {
       Block block = blockRef.get();
@@ -37,10 +39,22 @@
         blockCache.remove(pos);
       }
     }
-    Block newBlock = handle.getBlock(pos);
+
+    SoftReference newBlockRef;
+    Block newBlock = null;
+    while ((newBlockRef = blockPool.pool()) != null) {
+      if ((newBlock = newBlockRef.get()) != null) {
+        break;
+      }
+    }
+    if (newBlock == null) {
+      newBlock = new Block(handle, 0x01 << LOG2_BLOCK_SIZE);
+    }
+
+    handle.readBlock(pos >> LOG2_BLOCK_SIZE, newBlock);
     // Note we use the newBlock's position, not pos - this ensures the map's entry will be retained until
     // memory pressure forces the SoftReference to be reaped.
-    blockCache.put(newBlock.getPosition(), new SoftReference(newBlock));
+    blockCache.put(newBlock.getBlockId(), new SoftReference(newBlock));
 
     return newBlock;
   }

Modified: projects/xa2/object-pool/src/scheduler/FileHandle.java
===================================================================
--- projects/xa2/object-pool/src/scheduler/FileHandle.java	2008-05-01 04:56:19 UTC (rev 874)
+++ projects/xa2/object-pool/src/scheduler/FileHandle.java	2008-05-01 09:37:52 UTC (rev 875)
@@ -9,14 +9,12 @@
 
 public class FileHandle {
   private File file;
-  private FileHandleCMap fhmap;
   private FileChannel channel;
 
   private long seeks;
 
-  FileHandle(File file, FileHandleCMap fhmap) {
+  FileHandle(File file) {
     this.file = file;
-    this.fhmap = fhmap;
     this.seeks = 0;
 
     this.channel = file.exists() ?
@@ -28,11 +26,21 @@
     return file;
   }
 
-  Block getBlock(Long blockId) {
+  Block readBlock(Long blockId, Block block) {
     long position = blockId.longValue();
     if (channel.position() != position) {
       seeks++;
       channel.position(position);
     }
+
+    channel.read(block.prepare(blockId));
+
+    return block;
   }
+
+  Long writeBlock(Block block) {
+    Long position = new Long(channel.position());
+
+    channel.write(block.prepare(blockId));
+  }
 }

Modified: projects/xa2/object-pool/src/scheduler/IOScheduler.java
===================================================================
--- projects/xa2/object-pool/src/scheduler/IOScheduler.java	2008-05-01 04:56:19 UTC (rev 874)
+++ projects/xa2/object-pool/src/scheduler/IOScheduler.java	2008-05-01 09:37:52 UTC (rev 875)
@@ -12,9 +12,24 @@
 public class IOScheduler {
   private Map<FileHandle, FileCache> fileCache;
   private File[] disks;
+  private FileHandleCMap fileMap;
 
   public IOScheduler(File[] disks) {
     this.disks = disks;
     fileCache = new HashMap<FileHandle, FileCache>();
   }
+
+  public FileHandle newFile(File file) {
+    if (file.exists()) {
+      throw new IllegalArgumentException("File exists: " + file);
+    }
+    fileMap.put(file, new FileHandle(file));
+  }
+
+  public FileHandle openFile(File file) {
+    if (!file.exists()) {
+      throw new IllegalArgumentException("File does not exist: " + file);
+    }
+    fileMap.put(file, new FileHandle(file));
+  }
 }




More information about the Mulgara-svn mailing list