/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1997, 1998, 1999
 *	Sleepycat Software.  All rights reserved.
 *
 * $Id: EnvExample.java,v 11.3 1999/11/21 23:04:52 bostic Exp $
 */

package com.sleepycat.examples;

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

/*
 * An example of a program using DbEnv to configure its DB
 * 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 final String DATABASE_HOME = "/tmp/database";

    private static void db_application()
        throws DbException
    {
        // Do something interesting...
        // Your application goes here.
    }

    private static void db_setup(String home, String config[],
                                  OutputStream errs)
         throws DbException, FileNotFoundException
    {
        //
        // Create an environment object and initialize it for error
        // reporting.
        //
        DbEnv dbenv = new DbEnv(0);

        dbenv.set_error_stream(errs);
        dbenv.set_errpfx(progname);

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

        //
        // We have multiple processes reading/writing these files, so
        // we need concurrency control and a shared buffer pool, but
        // not logging or transactions.
        //
        // open() will throw a DbException if there is an error.
        //
        // open is declared to throw a FileNotFoundException, which normally
        // shouldn't occur with the DB_CREATE option.
        //
        dbenv.open(DATABASE_HOME, config,
                   Db.DB_CREATE | Db.DB_INIT_LOCK | Db.DB_INIT_MPOOL, 0);

        try {

            // Start your application.
            db_application();

        }
        finally {

            // Close the environment.  Doing this in the
            // finally block ensures it is done, even if
            // an error is thrown.
            //
            dbenv.close(0);
        }
    }

    private static void db_teardown(String home, String config[],
                                    OutputStream errs)
         throws DbException, FileNotFoundException
    {
	// Remove the shared database regions.

        DbEnv dbenv = new DbEnv(0);

        dbenv.set_error_stream(errs);
        dbenv.set_errpfx(progname);
        dbenv.remove(home, config, 0);
    }

    public static void main(String[] args)
    {
	System.out.println("beginning env example");

        //
        // All of the shared database files live in /tmp/database,
        // but data files live in /database.
        //
        // 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.
        //
        String home = DATABASE_HOME;
        String config[] = new String[1];

        config[0] = "DB_DATA_DIR /database/files";

        try {
            db_setup(home, config, System.err);

            db_teardown(home, config, System.err);
        }
        catch (DbException 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);
        }
	System.out.println("completed env example");
    }

}
