2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1997-2003
5
* Sleepycat Software. All rights reserved.
7
* $Id: EnvExample.cpp,v 11.25 2003/01/08 04:46:55 bostic Exp $
10
#include <sys/types.h>
26
#define DATABASE_HOME ":database"
27
#define CONFIG_DATA_DIR ":database"
30
#define DATABASE_HOME "\\tmp\\database"
31
#define CONFIG_DATA_DIR "\\database\\files"
33
#define DATABASE_HOME "/tmp/database"
34
#define CONFIG_DATA_DIR "/database/files"
38
void db_setup(const char *, const char *, ostream&);
39
void db_teardown(const char *, const char *, ostream&);
41
const char *progname = "EnvExample"; /* Program name. */
44
// An example of a program creating/configuring a Berkeley DB environment.
50
// Note: it may be easiest to put all Berkeley DB operations in a
51
// try block, as seen here. Alternatively, you can change the
52
// ErrorModel in the DbEnv so that exceptions are never thrown
53
// and check error returns from all methods.
56
const char *data_dir, *home;
59
// All of the shared database files live in /home/database,
60
// but data files live in /database.
63
data_dir = CONFIG_DATA_DIR;
65
cout << "Setup env\n";
66
db_setup(home, data_dir, cerr);
68
cout << "Teardown env\n";
69
db_teardown(home, data_dir, cerr);
70
return (EXIT_SUCCESS);
72
catch (DbException &dbe) {
73
cerr << "EnvExample: " << dbe.what() << "\n";
74
return (EXIT_FAILURE);
78
// Note that any of the db calls can throw DbException
80
db_setup(const char *home, const char *data_dir, ostream& err_stream)
83
// Create an environment object and initialize it for error
86
DbEnv *dbenv = new DbEnv(0);
87
dbenv->set_error_stream(&err_stream);
88
dbenv->set_errpfx(progname);
91
// We want to specify the shared memory buffer pool cachesize,
92
// but everything else is the default.
94
dbenv->set_cachesize(0, 64 * 1024, 0);
96
// Databases are in a subdirectory.
97
(void)dbenv->set_data_dir(data_dir);
99
// Open the environment with full transactional support.
101
DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN, 0);
103
// Do something interesting...
110
db_teardown(const char *home, const char *data_dir, ostream& err_stream)
112
// Remove the shared database regions.
113
DbEnv *dbenv = new DbEnv(0);
115
dbenv->set_error_stream(&err_stream);
116
dbenv->set_errpfx(progname);
118
(void)dbenv->set_data_dir(data_dir);
119
dbenv->remove(home, 0);