提交 d4cbb6ce authored 作者: Thomas Mueller's avatar Thomas Mueller

A new sample application that shows how to create a read-only database in a zip…

A new sample application that shows how to create a read-only database in a zip file where the database file is split into multiple smaller parts.
上级 1571a992
......@@ -18,7 +18,11 @@ Change Log
<h1>Change Log</h1>
<h2>Next Version (unreleased)</h2>
<ul><li>The wrong error message was thrown when trying to open a database where the database file
<ul><li>A new sample application that shows how to create
a read-only database in a zip file where the database file is split into multiple smaller parts.
</li><li>The Backup tool now supports compressing all files in a given directory.
</li><li>H2 Console: the special syntax @list and @meta can not be combined.
</li><li>The wrong error message was thrown when trying to open a database where the database file
could not be read for some reason (for example because the split file system was used, and the file
was split at the wrong position).
</li><li>There was a memory leak in the trace system. Opening and closing many connections could run out of memory.
......
......@@ -1291,6 +1291,7 @@ the database can also be opened in read-only mode, even if the database file is
<p>
To create a read-only database in a zip file, first create a regular persistent database, and then create a backup.
The database must not have pending changes, that means you need to close all connections to the database first.
To speed up opening the read-only database and running queries, the database should be closed using <code>SHUTDOWN DEFRAG</code>.
If you are using a database named <code>test</code>, an easy way to create a zip file is using the
<code>Backup</code> tool. You can start the tool from the command line, or from within the
H2 Console (Tools - Backup). Please note that the database must be closed when the backup
......@@ -1309,6 +1310,11 @@ affects the performance depends on the queries and the data. The database
is not read in memory; therefore large databases are supported as well. The same indexes are used as when using
a regular database.
</p>
<p>
If the database is larger than a few megabytes, performance is much better if the database file is split into multiple smaller files,
because random access in compressed files is not possible.
See also the sample application <a href="http://code.google.com/p/h2database/source/browse/trunk/h2/src/test/org/h2/samples/ReadOnlyDatabaseInZip.java">ReadOnlyDatabaseInZip</a>.
</p>
<h2 id="low_disk_space">Graceful Handling of Low Disk Space Situations</h2>
<p>
......@@ -1633,17 +1639,17 @@ overrides this value (even if larger than the physical memory).
To get the current used maximum cache size, use the query
<code>SELECT * FROM INFORMATION_SCHEMA.SETTINGS WHERE NAME = 'info.CACHE_MAX_SIZE'</code>
</p><p>
An experimental scan-resistant cache algorithm "Two Queue" (2Q) is available.
An experimental scan-resistant cache algorithm "Two Queue" (2Q) is available.
To enable it, append <code>;CACHE_TYPE=TQ</code> to the database URL.
The cache might not actually improve performance.
The cache might not actually improve performance.
If you plan to use it, please run your own test cases first.
</p><p>
Also included is an experimental second level soft reference cache.
Rows in this cache are only garbage collected on low memory.
By default the second level cache is disabled.
To enable it, use the prefix <code>SOFT_</code>.
Also included is an experimental second level soft reference cache.
Rows in this cache are only garbage collected on low memory.
By default the second level cache is disabled.
To enable it, use the prefix <code>SOFT_</code>.
Example: <code>jdbc:h2:~/test;CACHE_TYPE=SOFT_LRU</code>.
The cache might not actually improve performance.
The cache might not actually improve performance.
If you plan to use it, please run your own test cases first.
</p><p>
To get information about page reads and writes, and the current caching algorithm in use,
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -66,7 +66,7 @@ public class FileObjectZip implements FileObject {
try {
IOUtils.skipFully(in, skip);
} catch (NullPointerException e) {
// workaround for Google Android
// workaround for Android
skipUsingRead = true;
}
}
......
......@@ -60,4 +60,5 @@ public class Compact {
RunScript.execute(url, user, password, file, null, false);
IOUtils.delete(file);
}
}
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.samples;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import org.h2.store.fs.FileSystem;
import org.h2.tools.Backup;
import org.h2.tools.DeleteDbFiles;
/**
* This sample application shows how to create and use a read-only database in a
* zip file. The database file is split into multiple smaller files, to speed up
* random-access. Splitting up the file is only needed if the database file is
* larger than a few megabytes.
*/
public class ReadOnlyDatabaseInZip {
/**
* This method is called when executing this sample application from the
* command line.
*
* @param args the command line parameters
*/
public static void main(String... args) throws Exception {
// delete all files in this directory
FileSystem.getInstance("~/temp").deleteRecursive("~/temp", false);
Connection conn;
Class.forName("org.h2.Driver");
// create a database where the database file is split into
// multiple small files, 4 MB each (2^22). The larger the
// parts, the faster opening the database, but also the
// more files. 4 MB seems to be a good compromise, so
// the prefix split:22: is used, which means each part is
// 2^22 bytes long
conn = DriverManager.getConnection(
"jdbc:h2:split:22:~/temp/test");
System.out.println("adding test data...");
Statement stat = conn.createStatement();
stat.execute(
"create table test(id int primary key, name varchar) " +
"as select x, space(1000) from system_range(1, 2000)");
System.out.println("defrag to reduce random access...");
stat.execute("shutdown defrag");
conn.close();
System.out.println("create the zip file...");
Backup.execute("~/temp/test.zip", "~/temp", "", true);
// delete the old database files
DeleteDbFiles.execute("split:~/temp", "test", true);
System.out.println("open the database from the zip file...");
conn = DriverManager.getConnection(
"jdbc:h2:split:zip:~/temp/test.zip!/test");
// the database can now be used
conn.close();
}
}
......@@ -22,6 +22,8 @@ import org.h2.store.fs.FileObject;
import org.h2.store.fs.FileSystem;
import org.h2.store.fs.FileSystemMemory;
import org.h2.test.TestBase;
import org.h2.tools.Backup;
import org.h2.tools.DeleteDbFiles;
import org.h2.util.IOUtils;
/**
......@@ -41,6 +43,7 @@ public class TestFileSystem extends TestBase {
}
public void test() throws Exception {
testSplitDatabaseInZip();
testDatabaseInMemFileSys();
testDatabaseInJar();
// set default part size to 1 << 10
......@@ -72,6 +75,27 @@ public class TestFileSystem extends TestBase {
}
}
private void testSplitDatabaseInZip() throws SQLException {
String dir = getBaseDir() + "/fs";
FileSystem.getInstance(dir).deleteRecursive(dir, false);
Connection conn;
Statement stat;
conn = DriverManager.getConnection("jdbc:h2:split:18:"+dir+"/test");
stat = conn.createStatement();
stat.execute(
"create table test(id int primary key, name varchar) " +
"as select x, space(10000) from system_range(1, 100)");
stat.execute("shutdown defrag");
conn.close();
Backup.execute(dir + "/test.zip", dir, "", true);
DeleteDbFiles.execute("split:" + dir, "test", true);
conn = DriverManager.getConnection(
"jdbc:h2:split:zip:"+dir+"/test.zip!/test");
conn.createStatement().execute("select * from test where id=1");
conn.close();
FileSystem.getInstance(dir).deleteRecursive(dir, false);
}
private void testDatabaseInMemFileSys() throws SQLException {
org.h2.Driver.load();
deleteDb("fsMem");
......
......@@ -62,6 +62,12 @@ public class TestSampleApps extends TestBase {
// process)
testApp("The sum is 20.00", org.h2.samples.TriggerSample.class);
testApp("Hello: 1\nWorld: 2", org.h2.samples.TriggerPassData.class);
testApp(
"adding test data...\n" +
"defrag to reduce random access...\n" +
"create the zip file...\n" +
"open the database from the zip file...",
org.h2.samples.ReadOnlyDatabaseInZip.class);
// tools
testApp("Allows changing the database file encryption password or algorithm*",
......
......@@ -665,4 +665,5 @@ mbeans unregisters subtracting multiplying dividing contended bindings
projection managing observer misuse windowed discriminator abort familiar rice
reachable mind develop disposition extras arithmetics readwrite syncable
requeried requery closable curr outdated market accurate borg theis welford
ooq exceeded eye hannibal stels garringer czech prevention propagate
\ No newline at end of file
ooq exceeded eye hannibal stels garringer czech prevention propagate
compromise portion
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论