1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1997-2009 Oracle.  All rights reserved.
5 *
6 * $Id$
7 */
8
9#include <sys/types.h>
10
11#include <errno.h>
12#include <iostream>
13#include <stddef.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17
18#include <db_cxx.h>
19
20using std::ostream;
21using std::cout;
22using std::cerr;
23
24void db_setup(const char *, const char *, ostream&);
25void db_teardown(const char *, const char *, ostream&);
26static int usage();
27
28const char *progname = "EnvExample";			/* Program name. */
29
30//
31// An example of a program creating/configuring a Berkeley DB environment.
32//
33int
34main(int argc, char *argv[])
35{
36	//
37	// Note: it may be easiest to put all Berkeley DB operations in a
38	// try block, as seen here.  Alternatively, you can change the
39	// ErrorModel in the DbEnv so that exceptions are never thrown
40	// and check error returns from all methods.
41	//
42	try {
43		const char *data_dir, *home;
44
45		//
46		// All of the shared database files live in home,
47		// but data files live in data_dir.
48		//
49		home = "TESTDIR";
50		data_dir = "data";
51
52		for (int argnum = 1; argnum < argc; ++argnum) {
53			if (strcmp(argv[argnum], "-h") == 0) {
54				if (++argnum >= argc)
55					return (usage());
56				home = argv[argnum];
57			}
58			else if (strcmp(argv[argnum], "-d") == 0) {
59				if (++argnum >= argc)
60					return (usage());
61				data_dir = argv[argnum];
62			}
63			else {
64				return (usage());
65			}
66		}
67
68		cout << "Setup env\n";
69		db_setup(home, data_dir, cerr);
70
71		cout << "Teardown env\n";
72		db_teardown(home, data_dir, cerr);
73		return (EXIT_SUCCESS);
74	}
75	catch (DbException &dbe) {
76		cerr << "EnvExample: " << dbe.what() << "\n";
77		return (EXIT_FAILURE);
78	}
79}
80
81// Note that any of the db calls can throw DbException
82void
83db_setup(const char *home, const char *data_dir, ostream& err_stream)
84{
85	//
86	// Create an environment object and initialize it for error
87	// reporting.
88	//
89	DbEnv *dbenv = new DbEnv(0);
90	dbenv->set_error_stream(&err_stream);
91	dbenv->set_errpfx(progname);
92
93	//
94	// We want to specify the shared memory buffer pool cachesize,
95	// but everything else is the default.
96	//
97	dbenv->set_cachesize(0, 64 * 1024, 0);
98
99	// Databases are in a subdirectory.
100	(void)dbenv->set_data_dir(data_dir);
101
102	// Open the environment with full transactional support.
103	dbenv->open(home,
104	    DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL |
105	    DB_INIT_TXN, 0);
106
107	// Open a database in the environment to verify the data_dir
108	// has been set correctly.
109	// Create a database handle, using the environment.
110	Db *db = new Db(dbenv, 0) ;
111
112	// Open the database.
113	db->open(NULL, "EvnExample_db1.db", NULL, DB_BTREE, DB_CREATE, 0644);
114
115	// Close the database handle.
116	db->close(0) ;
117	delete db;
118
119	// Close the handle.
120	dbenv->close(0);
121	delete dbenv;
122}
123
124void
125db_teardown(const char *home, const char *data_dir, ostream& err_stream)
126{
127	// Remove the shared database regions.
128	DbEnv *dbenv = new DbEnv(0);
129
130	dbenv->set_error_stream(&err_stream);
131	dbenv->set_errpfx(progname);
132
133	(void)dbenv->set_data_dir(data_dir);
134	dbenv->remove(home, 0);
135	delete dbenv;
136}
137
138static int
139usage()
140{
141	cerr << "usage: excxx_env [-h home] [-d data_dir]\n";
142	return (EXIT_FAILURE);
143}
144