[Mulgara-svn] r1914 - trunk/src/jar/util-xa/java/org/mulgara/store/xa

pag at mulgara.org pag at mulgara.org
Mon Feb 15 19:21:45 UTC 2010


Author: pag
Date: 2010-02-15 11:21:44 -0800 (Mon, 15 Feb 2010)
New Revision: 1914

Modified:
   trunk/src/jar/util-xa/java/org/mulgara/store/xa/Block.java
   trunk/src/jar/util-xa/java/org/mulgara/store/xa/BlockFile.java
   trunk/src/jar/util-xa/java/org/mulgara/store/xa/IOBlockFile.java
   trunk/src/jar/util-xa/java/org/mulgara/store/xa/ManagedBlockFile.java
   trunk/src/jar/util-xa/java/org/mulgara/store/xa/MappedBlockFile.java
Log:
Applied patch by Benjamin Armintor. Added a mechanism for recycling blocks rather than just reading them

Modified: trunk/src/jar/util-xa/java/org/mulgara/store/xa/Block.java
===================================================================
--- trunk/src/jar/util-xa/java/org/mulgara/store/xa/Block.java	2010-02-05 20:53:12 UTC (rev 1913)
+++ trunk/src/jar/util-xa/java/org/mulgara/store/xa/Block.java	2010-02-15 19:21:44 UTC (rev 1914)
@@ -62,9 +62,32 @@
 
   public static final long INVALID_BLOCK_ID = -1;
 
-  @SuppressWarnings("unused")
   private final static Logger logger = Logger.getLogger(Block.class);
 
+  /** The property for the block type. May be "direct" or "javaHeap" */
+  public static final String MEM_TYPE_PROP = "mulgara.xa.memoryType";
+
+  /** Enumeration of the different memory types for blocks */
+  enum BlockMemoryType { DIRECT, HEAP };
+
+  /** The default value to use for the block memory type. Used when nothing is configured. */
+  private static final BlockMemoryType DEFAULT = BlockMemoryType.DIRECT;
+
+  /** The configured type of block type to use. */
+  private static final BlockMemoryType BLOCK_TYPE;
+
+  static {
+    String defBlockType = System.getProperty(MEM_TYPE_PROP, DEFAULT.name());
+    if (defBlockType.equalsIgnoreCase(BlockMemoryType.DIRECT.name())) {
+      BLOCK_TYPE = BlockMemoryType.DIRECT;
+    } else if (defBlockType.equalsIgnoreCase(BlockMemoryType.HEAP.name())) {
+      BLOCK_TYPE = BlockMemoryType.HEAP;
+    } else {
+      logger.warn("Invalid value for property " + MEM_TYPE_PROP + ": " + defBlockType);
+      BLOCK_TYPE = DEFAULT;
+    }
+  }
+
   /** The file that this block is attached to. */
   private BlockFile blockFile;
 
@@ -165,15 +188,33 @@
    * @return A new block.
    */
   public static Block newInstance(BlockFile blockFile, int blockSize, long blockId, ByteOrder byteOrder) {
+    ByteBuffer buffer = BLOCK_TYPE == BlockMemoryType.DIRECT ?
+        ByteBuffer.allocateDirect(blockSize).order(byteOrder) :
+        ByteBuffer.allocate(blockSize).order(byteOrder);
+
     Block block = Block.newInstance(
         blockFile, blockSize, blockId, 0,
-        ByteBuffer.allocateDirect(blockSize).order(byteOrder), null, null, null
+        buffer, null, null, null
     );
     block.ownsBuffer = true;
     return block;
   }
 
   /**
+   * @author barmintor
+   * @param block The buffer to be re-used by a BlockFile
+   * @param blockId The ID of the block from the file.
+   */
+   static void recycleBuffer(Block block, long blockId) {
+    block.ownsBuffer = false;
+    block.bb.clear();
+    block.ib.rewind();
+    block.lb.rewind();
+    block.init(blockId, 0, block.bb, null, block.ib, block.lb);
+    block.ownsBuffer = true;
+  }
+
+  /**
    * Gets a byte from the block.
    *
    * @param offset The location of the byte within the data block.

Modified: trunk/src/jar/util-xa/java/org/mulgara/store/xa/BlockFile.java
===================================================================
--- trunk/src/jar/util-xa/java/org/mulgara/store/xa/BlockFile.java	2010-02-05 20:53:12 UTC (rev 1913)
+++ trunk/src/jar/util-xa/java/org/mulgara/store/xa/BlockFile.java	2010-02-15 19:21:44 UTC (rev 1914)
@@ -158,6 +158,17 @@
   public void modifyBlock(Block block) throws IOException;
 
   /**
+   * Attempt to re-use the given Block and wrapped ByteBuffer to read the indicated block.
+   * null ByteBuffer will behave like readBlock.
+   * @author barmintor
+   * @param blockId The block to read into the ByteBuffer.
+   * @param block The ByteBuffer to attempt to re-use
+   * @return The buffer that was read.
+   * @throws IOException if an I/O error occurs.
+   */
+  public Block recycleBlock(long blockId, Block block) throws IOException;
+
+  /**
    * Releases a block.  This is used when a block is no longer needed.
    *
    * @param blockId The block to free.

Modified: trunk/src/jar/util-xa/java/org/mulgara/store/xa/IOBlockFile.java
===================================================================
--- trunk/src/jar/util-xa/java/org/mulgara/store/xa/IOBlockFile.java	2010-02-05 20:53:12 UTC (rev 1913)
+++ trunk/src/jar/util-xa/java/org/mulgara/store/xa/IOBlockFile.java	2010-02-15 19:21:44 UTC (rev 1914)
@@ -228,4 +228,18 @@
     block.setBlockId(dstBlockId);
   }
 
+  /**
+   * Attempt to re-use the given Block and wrapped ByteBuffer to read the indicated block.
+   * null ByteBuffer will behave like readBlock.
+   * @author barmintor
+   * @param blockId The block to read into the ByteBuffer.
+   * @param block The ByteBuffer to attempt to re-use
+   * @return The buffer that was read.
+   * @throws IOException if an I/O error occurs.
+   */
+  public Block recycleBlock(long blockId, Block block) throws IOException {
+    if (block == null) return readBlock(blockId);
+    Block.recycleBuffer(block, blockId);
+    return block;
+  }
 }

Modified: trunk/src/jar/util-xa/java/org/mulgara/store/xa/ManagedBlockFile.java
===================================================================
--- trunk/src/jar/util-xa/java/org/mulgara/store/xa/ManagedBlockFile.java	2010-02-05 20:53:12 UTC (rev 1913)
+++ trunk/src/jar/util-xa/java/org/mulgara/store/xa/ManagedBlockFile.java	2010-02-15 19:21:44 UTC (rev 1914)
@@ -431,6 +431,13 @@
     }
 
     /**
+     * ManagedBlockFile effectively re-uses in the read
+     */
+    public Block recycleBlock(long blockId, Block block) throws IOException {
+      return readBlock(blockId);
+    }
+ 
+    /**
      * Used to unmap a file.  This is supported on the enclosing class rather than here.
      */
     public void unmap() {

Modified: trunk/src/jar/util-xa/java/org/mulgara/store/xa/MappedBlockFile.java
===================================================================
--- trunk/src/jar/util-xa/java/org/mulgara/store/xa/MappedBlockFile.java	2010-02-05 20:53:12 UTC (rev 1913)
+++ trunk/src/jar/util-xa/java/org/mulgara/store/xa/MappedBlockFile.java	2010-02-15 19:21:44 UTC (rev 1914)
@@ -320,7 +320,22 @@
     );
   }
 
+
   /**
+   * Attempt to re-use the given Block and wrapped ByteBuffer to read the indicated block.
+   * null ByteBuffer will behave like readBlock.
+   * @author barmintor
+   * @param blockId The block to read into the ByteBuffer.
+   * @param block The ByteBuffer to attempt to re-use
+   * @return The buffer that was read.
+   * @throws IOException if an I/O error occurs.
+   */
+  public Block recycleBlock(long blockId, Block block) throws IOException {
+   return readBlock(blockId);
+  }
+
+
+  /**
    * Discards all file mappings, allowing the garbage collector to unmap the file.
    */
   public synchronized void unmap() {




More information about the Mulgara-svn mailing list