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

pag at mulgara.org pag at mulgara.org
Fri Jul 2 20:39:56 UTC 2010


Author: pag
Date: 2010-07-02 20:39:56 +0000 (Fri, 02 Jul 2010)
New Revision: 1962

Modified:
   trunk/src/jar/util/java/org/mulgara/util/io/LBufferedFile.java
   trunk/src/jar/util/java/org/mulgara/util/io/LMappedBufferedFile.java
   trunk/src/jar/util/java/org/mulgara/util/io/LReadOnlyIOBufferedFile.java
Log:
Unit tests (not included) now work for read-only operations

Modified: trunk/src/jar/util/java/org/mulgara/util/io/LBufferedFile.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/io/LBufferedFile.java	2010-07-02 17:15:20 UTC (rev 1961)
+++ trunk/src/jar/util/java/org/mulgara/util/io/LBufferedFile.java	2010-07-02 20:39:56 UTC (rev 1962)
@@ -30,20 +30,14 @@
  */
 public abstract class LBufferedFile {
 
-  private final static Logger logger = Logger.getLogger(LBufferedFile.class);
+  private final static Logger logger = Logger.getLogger(LBufferedFileTest.class);
 
   /** The property for the block type. May be "direct" or "javaHeap" */
   public static final String MEM_TYPE_PROP = "mulgara.xa.memoryType";
-  
-  /** The property for the IO type. May be "mapped" or "explicit" */
-  public static final String IO_TYPE_PROP = "mulgara.xa.forceIOType";
 
   /** Enumeration of the different memory types for blocks */
   enum BlockMemoryType { DIRECT, HEAP };
 
-  /** Enumeration of the different memory types for blocks */
-  enum IOType { MAPPED, EXPLICIT };
-
   /** Native ordering of the bytes */
   ByteOrder NATIVE_ORDER = ByteOrder.nativeOrder();
 
@@ -78,28 +72,6 @@
   }
 
   /**
-   * Create read only buffered file access. This will be a mapped file, unless overridden.
-   * @param file The file to access.
-   * @return The file for buffered access.
-   * @throws IOException If there is an error accessing the file.
-   */
-  public static LBufferedFile newReadOnlyFile(RandomAccessFile file) throws IOException {
-    String ioTypeProp = System.getProperty("mulgara.xa.forceIOType", "mapped");
-
-    IOType ioType = IOType.MAPPED;
-    if (ioTypeProp.equalsIgnoreCase(IOType.MAPPED.name())) {
-      ioType = IOType.MAPPED;
-    } else if (ioTypeProp.equalsIgnoreCase(IOType.EXPLICIT.name())) {
-      ioType = IOType.EXPLICIT;
-    } else {
-      logger.warn("Invalid value for property mulgara.xa.forceIOType: " + ioTypeProp);
-    }
-
-    if (ioType == IOType.MAPPED) return new LMappedBufferedFile(file);
-    else return new LReadOnlyIOBufferedFile(file);
-  }
-
-  /**
    * Reads a buffer from a file, at a given position.
    * @param offset The offset to get the buffer from.
    * @param length The required size of the buffer.

Modified: trunk/src/jar/util/java/org/mulgara/util/io/LMappedBufferedFile.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/io/LMappedBufferedFile.java	2010-07-02 17:15:20 UTC (rev 1961)
+++ trunk/src/jar/util/java/org/mulgara/util/io/LMappedBufferedFile.java	2010-07-02 20:39:56 UTC (rev 1962)
@@ -12,9 +12,24 @@
  */
 public class LMappedBufferedFile extends LBufferedFile {
 
+  /** The name of the property for setting the page size */
+  static final String PAGE_SIZE_PROP = "org.mulgara.io.pagesize";
+
   /** The size of a page to be mapped */
-  private static final int PAGE_SIZE = 33554432; // 32 MB
+  private static final int DEFAULT_PAGE_SIZE = 33554432; // 32 MB
 
+  /** The page size to use. */
+  private static final int PAGE_SIZE;
+  
+  static {
+    String pageSizeStr = System.getProperty(PAGE_SIZE_PROP);
+    int tmp = DEFAULT_PAGE_SIZE;
+    try {
+      if (pageSizeStr != null) tmp = Integer.parseInt(pageSizeStr);
+    } catch (NumberFormatException e) { }
+    PAGE_SIZE = tmp;
+  }
+  
   /** The channel to the file being accessed */
   private FileChannel fc;
 
@@ -34,16 +49,21 @@
 
   @Override
   public ByteBuffer read(long offset, int length) throws IOException {
-    if (offset + length > buffers.length * PAGE_SIZE) mapFile();
+    // get the offset into the last buffer, this may be negative if before the last page
+    long lastPageOffset = (offset + length) - (PAGE_SIZE * (buffers.length - 1));
+    // if the offset is larger than the final page, then remap
+    if (lastPageOffset > PAGE_SIZE || lastPageOffset > buffers[buffers.length - 1].limit()) {
+      mapFile();
+    }
     int page = (int)(offset / PAGE_SIZE);
     int page_offset = (int)(offset % PAGE_SIZE);
     if (page_offset + length <= PAGE_SIZE) {
       ByteBuffer bb = buffers[page].asReadOnlyBuffer();
       bb.position(page_offset);
-      bb.limit(length);
+      bb.limit(page_offset + length);
       return bb.slice();
     } else {
-      ByteBuffer data = allocate(length);
+      ByteBuffer data = ByteBuffer.allocate(length);
       byte[] dataArray = data.array();
       ByteBuffer tmp = buffers[page].asReadOnlyBuffer();
       tmp.position(page_offset);
@@ -93,7 +113,7 @@
         topBuffer++;
       }
       System.arraycopy(buffers, 0, newBuffers, 0, topBuffer);
-      start = buffers.length;
+      start = topBuffer;
     }
 
     // fill in the rest of the new array

Modified: trunk/src/jar/util/java/org/mulgara/util/io/LReadOnlyIOBufferedFile.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/io/LReadOnlyIOBufferedFile.java	2010-07-02 17:15:20 UTC (rev 1961)
+++ trunk/src/jar/util/java/org/mulgara/util/io/LReadOnlyIOBufferedFile.java	2010-07-02 20:39:56 UTC (rev 1962)
@@ -32,7 +32,7 @@
 
   @Override
   public ByteBuffer read(long offset, int length) throws IOException {
-    ByteBuffer data = allocate(length);
+    ByteBuffer data = ByteBuffer.allocate(length);
     synchronized (file) {
       file.seek(offset);
       file.readFully(data.array());



More information about the Mulgara-svn mailing list