]> source.dussan.org Git - jackcess.git/commitdiff
add flush method to Database; add unit test for new usage map functionality
authorJames Ahlborn <jtahlborn@yahoo.com>
Tue, 10 Jul 2007 21:48:33 +0000 (21:48 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Tue, 10 Jul 2007 21:48:33 +0000 (21:48 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@157 f203690c-595d-4dc9-a70b-905162fa7fd2

src/java/com/healthmarketscience/jackcess/Database.java
src/java/com/healthmarketscience/jackcess/PageChannel.java
src/java/com/healthmarketscience/jackcess/UsageMap.java
test/data/testPromotion.mdb [new file with mode: 0644]
test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java

index 0a94e0af974c29c1b812881e38e20d4707756b0e..7c68f21666c7c80df49ae47f499bb7dc5eef66f9 100644 (file)
@@ -28,9 +28,11 @@ King of Prussia, PA 19406
 package com.healthmarketscience.jackcess;
 
 import java.io.BufferedReader;
+import java.io.Closeable;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
+import java.io.Flushable;
 import java.io.IOException;
 import java.io.RandomAccessFile;
 import java.nio.ByteBuffer;
@@ -39,7 +41,6 @@ import java.nio.channels.FileChannel;
 import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
-import java.sql.Types;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -63,7 +64,7 @@ import org.apache.commons.logging.LogFactory;
  * @author Tim McCune
  */
 public class Database
-  implements Iterable<Table>
+  implements Iterable<Table>, Closeable, Flushable
 {
   
   private static final Log LOG = LogFactory.getLog(Database.class);
@@ -824,6 +825,13 @@ public class Database
       throw (IOException)new IOException(e.getMessage()).initCause(e);
     }
   }
+
+  /**
+   * Flushes any current changes to the database file to disk.
+   */
+  public void flush() throws IOException {
+    _pageChannel.flush();
+  }
   
   /**
    * Close the database file
index ca824dbd2aaefefaf824ab21ebd182114df7151f..9f394f0dd224ef7e81391b837fa1cb522950947c 100644 (file)
@@ -27,11 +27,13 @@ King of Prussia, PA 19406
 
 package com.healthmarketscience.jackcess;
 
+import java.io.Flushable;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.nio.channels.Channel;
 import java.nio.channels.FileChannel;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -39,7 +41,7 @@ import org.apache.commons.logging.LogFactory;
  * Reads and writes individual pages in a database file
  * @author Tim McCune
  */
-public class PageChannel implements Channel {
+public class PageChannel implements Channel, Flushable {
   
   private static final Log LOG = LogFactory.getLog(PageChannel.class);
   
@@ -125,7 +127,7 @@ public class PageChannel implements Channel {
     _channel.write(page, (((long) pageNumber * (long) _format.PAGE_SIZE) +
                           (long) pageOffset));
     if(_autoSync) {
-      _channel.force(true);
+      flush();
     }
   }
   
@@ -165,9 +167,13 @@ public class PageChannel implements Channel {
     rtn.order(ByteOrder.LITTLE_ENDIAN);
     return rtn;
   }
+
+  public void flush() throws IOException {
+    _channel.force(true);
+  }
   
   public void close() throws IOException {
-    _channel.force(true);
+    flush();
     _channel.close();
   }
   
index 55c201576d2982568d439a5cbfc1a0fade4b629d..1ba9a6f8a252d0748244d0df81a7c2a546d2eaad 100644 (file)
@@ -29,9 +29,7 @@ package com.healthmarketscience.jackcess;
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
-import java.util.ArrayList;
 import java.util.BitSet;
-import java.util.List;
 import java.util.logging.Handler;
 
 import org.apache.commons.logging.Log;
diff --git a/test/data/testPromotion.mdb b/test/data/testPromotion.mdb
new file mode 100644 (file)
index 0000000..901c520
Binary files /dev/null and b/test/data/testPromotion.mdb differ
index 75241335452ab5ba7cfa1af491c7cf7c13ca124c..5083577b8bd93ad57c00f4d5d48164cdd306335b 100644 (file)
@@ -19,8 +19,10 @@ import java.util.Calendar;
 import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import junit.framework.TestCase;
 
@@ -636,6 +638,31 @@ public class DatabaseTest extends TestCase {
     assertEquals(tval, readRow.get("c"));
 
   }
+
+
+  public void testUsageMapPromotion() throws Exception {
+    File srcFile = new File("test/data/testPromotion.mdb");
+    File dbFile = File.createTempFile("databaseTest", ".mdb");
+    dbFile.deleteOnExit();
+    copyFile(srcFile, dbFile);
+    
+    Database db = Database.open(dbFile);
+    Table t = db.getTable("jobDB1");
+
+    String lval = createString(255); // "--255 chars long text--";
+
+    for(int i = 0; i < 1000; ++i) {
+      t.addRow(i, 13, 57, 47.0d, lval, lval, lval, lval, lval, lval);
+    }
+
+    Set<Integer> ids = new HashSet<Integer>();
+    for(Map<String,Object> row : t) {
+      ids.add((Integer)row.get("ID"));
+    }
+    assertEquals(1000, ids.size());
+
+    db.close();
+  }  
   
   static Object[] createTestRow(String col1Val) {
     return new Object[] {col1Val, "R", "McCune", 1234, (byte) 0xad, 555.66d,