~jspashett/+junk/sossnt-import

« back to all changes in this revision

Viewing changes to src/db-4.2.52.NC/examples_cxx/EnvExample.cpp

  • Committer: jason.spashett
  • Date: 2009-09-24 13:40:02 UTC
  • Revision ID: jason.spashett@sw-dev-jspash1-20090924134002-biz3nn1401317vkb
Initial import from sourceforge

Show diffs side-by-side

added added

removed removed

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