From: James Ahlborn Date: Tue, 10 Jul 2007 21:48:33 +0000 (+0000) Subject: add flush method to Database; add unit test for new usage map functionality X-Git-Tag: rel_1_1_9~5 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a05f660f7b0d93d8e4e739686cec112650ca84d2;p=jackcess.git add flush method to Database; add unit test for new usage map functionality git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@157 f203690c-595d-4dc9-a70b-905162fa7fd2 --- diff --git a/src/java/com/healthmarketscience/jackcess/Database.java b/src/java/com/healthmarketscience/jackcess/Database.java index 0a94e0a..7c68f21 100644 --- a/src/java/com/healthmarketscience/jackcess/Database.java +++ b/src/java/com/healthmarketscience/jackcess/Database.java @@ -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 + implements Iterable
, 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 diff --git a/src/java/com/healthmarketscience/jackcess/PageChannel.java b/src/java/com/healthmarketscience/jackcess/PageChannel.java index ca824db..9f394f0 100644 --- a/src/java/com/healthmarketscience/jackcess/PageChannel.java +++ b/src/java/com/healthmarketscience/jackcess/PageChannel.java @@ -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(); } diff --git a/src/java/com/healthmarketscience/jackcess/UsageMap.java b/src/java/com/healthmarketscience/jackcess/UsageMap.java index 55c2015..1ba9a6f 100644 --- a/src/java/com/healthmarketscience/jackcess/UsageMap.java +++ b/src/java/com/healthmarketscience/jackcess/UsageMap.java @@ -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 index 0000000..901c520 Binary files /dev/null and b/test/data/testPromotion.mdb differ diff --git a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java index 7524133..5083577 100644 --- a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java @@ -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 ids = new HashSet(); + for(Map 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,