summaryrefslogtreecommitdiff
path: root/db-4.8.30/examples_java/src/db/EnvExample.java
blob: a8c1f758874509f90d35e7ee224fc2be32215320 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1997-2009 Oracle.  All rights reserved.
 *
 * $Id$
 */

package db;

import com.sleepycat.db.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.OutputStream;

/*
 * An example of a program configuring a database environment.
 *
 * For comparison purposes, this example uses a similar structure
 * as examples/ex_env.c and examples_cxx/EnvExample.cpp.
 */
public class EnvExample {
    private static final String progname = "EnvExample";

    private static void runApplication(Environment dbenv)
        throws DatabaseException, FileNotFoundException {
         
        // Open a database in the environment to verify the data_dir
        // has been set correctly.
        DatabaseConfig dbconfig = new DatabaseConfig();

        // The database is DB_BTREE.
        dbconfig.setAllowCreate(true);
        dbconfig.setMode(0644);
        dbconfig.setType(DatabaseType.BTREE);
        Database db=dbenv.openDatabase(null, 
            "jEnvExample_db1.db", null, dbconfig);

        // Close the database.
        db.close();
    }

    private static void setupEnvironment(File home,
                                         File dataDir,
                                         OutputStream errs)
        throws DatabaseException, FileNotFoundException {

        // Create an environment object and initialize it for error reporting.
        EnvironmentConfig config = new EnvironmentConfig();
        config.setErrorStream(errs);
        config.setErrorPrefix(progname);

        //
        // We want to specify the shared memory buffer pool cachesize,
        // but everything else is the default.
        //
        config.setCacheSize(64 * 1024);

        // Databases are in a separate directory.
        config.addDataDir(dataDir);

        // Open the environment with full transactional support.
        config.setAllowCreate(true);
        config.setInitializeCache(true);
        config.setTransactional(true);
        config.setInitializeLocking(true);

        //
        // open is declared to throw a FileNotFoundException, which normally
        // shouldn't occur when allowCreate is set.
        //
        Environment dbenv = new Environment(home, config);

        try {
            // Start your application.
            runApplication(dbenv);
        } finally {
            // Close the environment.  Doing this in the finally block ensures
            // it is done, even if an error is thrown.
            dbenv.close();
        }
    }

    private static void teardownEnvironment(File home,
                                            File dataDir,
                                            OutputStream errs)
        throws DatabaseException, FileNotFoundException {

        // Remove the shared database regions.
        EnvironmentConfig config = new EnvironmentConfig();

        config.setErrorStream(errs);
        config.setErrorPrefix(progname);
        config.addDataDir(dataDir);
        Environment.remove(home, true, config);
    }

    private static void usage() {
        System.err.println("usage: java db.EnvExample [-h home] [-d data_dir]");
        System.exit(1);
    }

    public static void main(String[] argv) {
        //
        // All of the shared database files live in home,
        // but data files live in dataDir.
        //
        // Using Berkeley DB in C/C++, we need to allocate two elements
        // in the array and set config[1] to NULL.  This is not
        // necessary in Java.
        //
        File home = new File("TESTDIR");
        File dataDir = new File("data");

        for (int i = 0; i < argv.length; ++i) {
            if (argv[i].equals("-h")) {
                if (++i >= argv.length)
                    usage();
                home = new File(argv[i]);
            } else if (argv[i].equals("-d")) {
                if (++i >= argv.length)
                    usage();
                dataDir = new File(argv[i]);
            } else if (argv[i].equals("-u")) {
                usage();
            }
        }

        try {
            System.out.println("Setup env");
            setupEnvironment(home, dataDir, System.err);

            System.out.println("Teardown env");
            teardownEnvironment(home, dataDir, System.err);
        } catch (DatabaseException dbe) {
            System.err.println(progname + ": environment open: " + dbe.toString());
            System.exit (1);
        } catch (FileNotFoundException fnfe) {
            System.err.println(progname + ": unexpected open environment error  " + fnfe);
            System.exit (1);
        }
    }

}