• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-WNDR4500v2-V1.0.0.60_1.0.38/ap/gpl/timemachine/db-4.7.25.NC/examples_java/src/db/
1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1997,2008 Oracle.  All rights reserved.
5 *
6 * $Id: EnvExample.java,v 12.7 2008/01/08 20:58:32 bostic Exp $
7 */
8
9package db;
10
11import com.sleepycat.db.*;
12import java.io.File;
13import java.io.FileNotFoundException;
14import java.io.OutputStream;
15
16/*
17 * An example of a program configuring a database environment.
18 *
19 * For comparison purposes, this example uses a similar structure
20 * as examples/ex_env.c and examples_cxx/EnvExample.cpp.
21 */
22public class EnvExample {
23    private static final String progname = "EnvExample";
24    private static final File DATABASE_HOME = new File("/tmp/database");
25
26    private static void runApplication(Environment dbenv)
27        throws DatabaseException {
28
29        // Do something interesting...
30        // Your application goes here.
31    }
32
33    private static void setupEnvironment(File home,
34                                         String dataDir,
35                                         OutputStream errs)
36        throws DatabaseException, FileNotFoundException {
37
38        // Create an environment object and initialize it for error reporting.
39        EnvironmentConfig config = new EnvironmentConfig();
40        config.setErrorStream(errs);
41        config.setErrorPrefix(progname);
42
43        //
44        // We want to specify the shared memory buffer pool cachesize,
45        // but everything else is the default.
46        //
47        config.setCacheSize(64 * 1024);
48
49        // Databases are in a separate directory.
50        config.addDataDir(dataDir);
51
52        // Open the environment with full transactional support.
53        config.setAllowCreate(true);
54        config.setInitializeCache(true);
55        config.setTransactional(true);
56        config.setInitializeLocking(true);
57
58        //
59        // open is declared to throw a FileNotFoundException, which normally
60        // shouldn't occur when allowCreate is set.
61        //
62        Environment dbenv = new Environment(home, config);
63
64        try {
65            // Start your application.
66            runApplication(dbenv);
67        } finally {
68            // Close the environment.  Doing this in the finally block ensures
69            // it is done, even if an error is thrown.
70            dbenv.close();
71        }
72    }
73
74    private static void teardownEnvironment(File home,
75                                            String dataDir,
76                                            OutputStream errs)
77        throws DatabaseException, FileNotFoundException {
78
79        // Remove the shared database regions.
80        EnvironmentConfig config = new EnvironmentConfig();
81
82        config.setErrorStream(errs);
83        config.setErrorPrefix(progname);
84        config.addDataDir(dataDir);
85        Environment.remove(home, true, config);
86    }
87
88    public static void main(String[] args) {
89        //
90        // All of the shared database files live in /tmp/database,
91        // but data files live in /database/files.
92        //
93        // Using Berkeley DB in C/C++, we need to allocate two elements
94        // in the array and set config[1] to NULL.  This is not
95        // necessary in Java.
96        //
97        File home = DATABASE_HOME;
98        String dataDir = "/database/files";
99
100        try {
101            System.out.println("Setup env");
102            setupEnvironment(home, dataDir, System.err);
103
104            System.out.println("Teardown env");
105            teardownEnvironment(home, dataDir, System.err);
106        } catch (DatabaseException dbe) {
107            System.err.println(progname + ": environment open: " + dbe.toString());
108            System.exit (1);
109        } catch (FileNotFoundException fnfe) {
110            System.err.println(progname + ": unexpected open environment error  " + fnfe);
111            System.exit (1);
112        }
113    }
114
115}
116