1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1997,2008 Oracle.  All rights reserved.
5 *
6 * $Id: EnvExample.cpp,v 12.6 2008/01/08 20:58:26 bostic Exp $
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
24#ifdef macintosh
25#define	DATABASE_HOME	":database"
26#define	CONFIG_DATA_DIR	":database"
27#else
28#ifdef DB_WIN32
29#define	DATABASE_HOME	"\\tmp\\database"
30#define	CONFIG_DATA_DIR	"\\database\\files"
31#else
32#define	DATABASE_HOME	"/tmp/database"
33#define	CONFIG_DATA_DIR	"/database/files"
34#endif
35#endif
36
37void	db_setup(const char *, const char *, ostream&);
38void	db_teardown(const char *, const char *, ostream&);
39
40const char *progname = "EnvExample";			/* Program name. */
41
42//
43// An example of a program creating/configuring a Berkeley DB environment.
44//
45int
46main(int, char **)
47{
48	//
49	// Note: it may be easiest to put all Berkeley DB operations in a
50	// try block, as seen here.  Alternatively, you can change the
51	// ErrorModel in the DbEnv so that exceptions are never thrown
52	// and check error returns from all methods.
53	//
54	try {
55		const char *data_dir, *home;
56
57		//
58		// All of the shared database files live in /home/database,
59		// but data files live in /database.
60		//
61		home = DATABASE_HOME;
62		data_dir = CONFIG_DATA_DIR;
63
64		cout << "Setup env\n";
65		db_setup(home, data_dir, cerr);
66
67		cout << "Teardown env\n";
68		db_teardown(home, data_dir, cerr);
69		return (EXIT_SUCCESS);
70	}
71	catch (DbException &dbe) {
72		cerr << "EnvExample: " << dbe.what() << "\n";
73		return (EXIT_FAILURE);
74	}
75}
76
77// Note that any of the db calls can throw DbException
78void
79db_setup(const char *home, const char *data_dir, ostream& err_stream)
80{
81	//
82	// Create an environment object and initialize it for error
83	// reporting.
84	//
85	DbEnv *dbenv = new DbEnv(0);
86	dbenv->set_error_stream(&err_stream);
87	dbenv->set_errpfx(progname);
88
89	//
90	// We want to specify the shared memory buffer pool cachesize,
91	// but everything else is the default.
92	//
93	dbenv->set_cachesize(0, 64 * 1024, 0);
94
95	// Databases are in a subdirectory.
96	(void)dbenv->set_data_dir(data_dir);
97
98	// Open the environment with full transactional support.
99	dbenv->open(home,
100    DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN, 0);
101
102	// Do something interesting...
103
104	// Close the handle.
105	dbenv->close(0);
106}
107
108void
109db_teardown(const char *home, const char *data_dir, ostream& err_stream)
110{
111	// Remove the shared database regions.
112	DbEnv *dbenv = new DbEnv(0);
113
114	dbenv->set_error_stream(&err_stream);
115	dbenv->set_errpfx(progname);
116
117	(void)dbenv->set_data_dir(data_dir);
118	dbenv->remove(home, 0);
119	delete dbenv;
120}
121