[Mulgara-svn] r1999 - trunk/src/jar/util/java/org/mulgara/util/io

pag at mulgara.org pag at mulgara.org
Mon Jul 18 20:43:30 UTC 2011


Author: pag
Date: 2011-07-18 20:43:30 +0000 (Mon, 18 Jul 2011)
New Revision: 1999

Modified:
   trunk/src/jar/util/java/org/mulgara/util/io/LBufferedFile.java
   trunk/src/jar/util/java/org/mulgara/util/io/LIOBufferedFile.java
   trunk/src/jar/util/java/org/mulgara/util/io/LMappedBufferedFile.java
   trunk/src/jar/util/java/org/mulgara/util/io/LReadOnlyIOBufferedFile.java
Log:
Fixed problems in truncating files when the truncation is still larger than the mapped area (thanks to Alex Hall). Added in a method to retrieve the page size

Modified: trunk/src/jar/util/java/org/mulgara/util/io/LBufferedFile.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/io/LBufferedFile.java	2011-07-18 20:41:56 UTC (rev 1998)
+++ trunk/src/jar/util/java/org/mulgara/util/io/LBufferedFile.java	2011-07-18 20:43:30 UTC (rev 1999)
@@ -24,7 +24,7 @@
 import org.apache.log4j.Logger;
 
 /**
- * An abstraction for reading and writing to files. 
+ * An abstraction for reading and writing to files with Long offsets.
  * @author Paul Gearon
  *
  */
@@ -56,6 +56,8 @@
   private static final IOType ioType;
 
   static {
+    // initialize the type of memory block to be used: heap or direct
+    // configured with mulgara.xa.memoryType
     String defBlockType = System.getProperty(MEM_TYPE_PROP, DEFAULT.name());
     if (defBlockType.equalsIgnoreCase(BlockMemoryType.DIRECT.name())) {
       BLOCK_TYPE = BlockMemoryType.DIRECT;
@@ -66,7 +68,9 @@
       BLOCK_TYPE = DEFAULT;
     }
 
-    String forceIOTypeProp = System.getProperty("mulgara.xa.forceIOType", "mapped");
+    // initialize the type of file access to use: memory mapped files, or explicit read/write operations
+    // configured with mulgara.xa.forceIOType
+    String forceIOTypeProp = System.getProperty(IO_TYPE_PROP, "mapped");
 
     if (forceIOTypeProp.equalsIgnoreCase(IOType.MAPPED.name())) {
       ioType = IOType.MAPPED;
@@ -121,6 +125,11 @@
   public abstract void seek(long offset) throws IOException;
 
   /**
+   * @return The page size used internally by the implementation, or 0 if not paged.
+   */
+  public abstract int getPageSize();
+
+  /**
    * Truncates the file to a given size, or prepares the file for truncation
    * if it is read-only.
    * @param offset The offset into the file to trancate the size to.
@@ -181,9 +190,7 @@
    * @return the allocated buffer.
    */
   protected ByteBuffer allocate(int size) {
-    return BLOCK_TYPE == BlockMemoryType.DIRECT ?
-        ByteBuffer.allocateDirect(size).order(NATIVE_ORDER) :
-        ByteBuffer.allocate(size).order(NATIVE_ORDER);
+    return allocate(size, NATIVE_ORDER);
   }
 
   /**

Modified: trunk/src/jar/util/java/org/mulgara/util/io/LIOBufferedFile.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/io/LIOBufferedFile.java	2011-07-18 20:41:56 UTC (rev 1998)
+++ trunk/src/jar/util/java/org/mulgara/util/io/LIOBufferedFile.java	2011-07-18 20:43:30 UTC (rev 1999)
@@ -81,6 +81,11 @@
     file.seek(offset - 1);
   }
 
+  @Override
+  public int getPageSize() {
+    return 0;
+  }
+
   /**
    * A wrapper class for allowing ByteBuffers to be mapped by == instead of their internal method.
    */

Modified: trunk/src/jar/util/java/org/mulgara/util/io/LMappedBufferedFile.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/io/LMappedBufferedFile.java	2011-07-18 20:41:56 UTC (rev 1998)
+++ trunk/src/jar/util/java/org/mulgara/util/io/LMappedBufferedFile.java	2011-07-18 20:43:30 UTC (rev 1999)
@@ -123,6 +123,9 @@
 
     if (size == fc.size()) return;
     if (size > fc.size()) throw new IOException("Unable to truncate a mapped file larger");
+    // if truncating to an address above the mapping, then return
+    int fullBuffers = buffers.length - 1;
+    if (fullBuffers >= 0 && size >= fullBuffers * PAGE_SIZE + buffers[fullBuffers].limit()) return;
 
     // get all the pages, including a possible partial page
     int pages = (int)((size + PAGE_SIZE - 1) / PAGE_SIZE);
@@ -134,7 +137,7 @@
     if (fullPages == buffers.length) return;
 
     // check that this really is a truncation
-    assert pages >= buffers.length;
+    assert pages <= buffers.length;
 
     // This will be the set of buffers to use in the end
     MappedByteBuffer[] newBuffers;
@@ -192,6 +195,11 @@
     listeners.add(l);
   }
 
+  @Override
+  public int getPageSize() {
+    return PAGE_SIZE;
+  }
+
   /**
    * Map the entire file
    * @throws IOException If there is an error mapping the file

Modified: trunk/src/jar/util/java/org/mulgara/util/io/LReadOnlyIOBufferedFile.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/io/LReadOnlyIOBufferedFile.java	2011-07-18 20:41:56 UTC (rev 1998)
+++ trunk/src/jar/util/java/org/mulgara/util/io/LReadOnlyIOBufferedFile.java	2011-07-18 20:43:30 UTC (rev 1999)
@@ -65,4 +65,9 @@
     /* no-op */
   }
 
+  @Override
+  public int getPageSize() {
+    return 0;
+  }
+
 }



More information about the Mulgara-svn mailing list