My favorites | Sign in
Project Home Downloads Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* 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.FileUtils;
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
FileUtils.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();
}

}

Change log

r3923 by thomas.tom.mueller on Sep 12, 2011   Diff
Improved compatibility with the Java 7
FileSystem abstraction.
Go to: 
Project members, sign in to write a code review

Older revisions

r3306 by thomas.tom.mueller on Jan 20, 2011   Diff
TCP Server: a base directory with a
prefix with a database name with
prefix didn't work as expected (this
only applies to non-disk and wrapped
file systems).
r3301 by thomas.tom.mueller on Jan 16, 2011   Diff
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.
All revisions of this file

File info

Size: 2457 bytes, 70 lines
Powered by Google Project Hosting