~ubuntu-branches/ubuntu/edgy/rpm/edgy

« back to all changes in this revision

Viewing changes to db/examples_cxx/MpoolExample.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Joey Hess
  • Date: 2002-01-22 20:56:57 UTC
  • Revision ID: james.westby@ubuntu.com-20020122205657-l74j50mr9z8ofcl5
Tags: upstream-4.0.3
ImportĀ upstreamĀ versionĀ 4.0.3

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-2001
 
5
 *      Sleepycat Software.  All rights reserved.
 
6
 *
 
7
 * $Id: MpoolExample.cpp,v 11.16 2001/05/10 17:14:07 bostic Exp $
 
8
 */
 
9
 
 
10
#include <sys/types.h>
 
11
 
 
12
#include <errno.h>
 
13
#include <fcntl.h>
 
14
#include <iostream.h>
 
15
#include <fstream.h>
 
16
#include <stdlib.h>
 
17
#include <string.h>
 
18
#include <time.h>
 
19
 
 
20
#include <db_cxx.h>
 
21
 
 
22
#define MPOOL   "mpool"
 
23
 
 
24
void init(char *, int, int);
 
25
void run(DB_ENV *, int, int, int);
 
26
 
 
27
static void usage();
 
28
 
 
29
char *progname = "MpoolExample";                        // Program name.
 
30
 
 
31
class MpoolExample : public DbEnv
 
32
{
 
33
public:
 
34
        MpoolExample();
 
35
        void initdb(const char *home, int cachesize);
 
36
        void run(int hits, int pagesize, int npages);
 
37
 
 
38
private:
 
39
        static const char FileName[];
 
40
 
 
41
        // no need for copy and assignment
 
42
        MpoolExample(const MpoolExample &);
 
43
        void operator = (const MpoolExample &);
 
44
};
 
45
 
 
46
int main(int argc, char *argv[])
 
47
{
 
48
        int cachesize = 20 * 1024;
 
49
        int hits = 1000;
 
50
        int npages = 50;
 
51
        int pagesize = 1024;
 
52
 
 
53
        for (int i = 1; i < argc; ++i) {
 
54
                if (strcmp(argv[i], "-c") == 0) {
 
55
                        if ((cachesize = atoi(argv[++i])) < 20 * 1024)
 
56
                                usage();
 
57
                }
 
58
                else if (strcmp(argv[i], "-h") == 0) {
 
59
                        if ((hits = atoi(argv[++i])) <= 0)
 
60
                                usage();
 
61
                }
 
62
                else if (strcmp(argv[i], "-n") == 0) {
 
63
                        if ((npages = atoi(argv[++i])) <= 0)
 
64
                                usage();
 
65
                }
 
66
                else if (strcmp(argv[i], "-p") == 0) {
 
67
                        if ((pagesize = atoi(argv[++i])) <= 0)
 
68
                                usage();
 
69
                }
 
70
                else {
 
71
                        usage();
 
72
                }
 
73
        }
 
74
 
 
75
        // Initialize the file.
 
76
        init(MPOOL, pagesize, npages);
 
77
 
 
78
        try {
 
79
                MpoolExample app;
 
80
 
 
81
                cout << progname
 
82
                     << ": cachesize: " << cachesize
 
83
                     << "; pagesize: " << pagesize
 
84
                     << "; N pages: " << npages << "\n";
 
85
 
 
86
                app.initdb(NULL, cachesize);
 
87
                app.run(hits, pagesize, npages);
 
88
                cout << "MpoolExample: completed\n";
 
89
                return EXIT_SUCCESS;
 
90
        }
 
91
        catch (DbException &dbe) {
 
92
                cerr << "MpoolExample: " << dbe.what() << "\n";
 
93
                return EXIT_FAILURE;
 
94
        }
 
95
}
 
96
 
 
97
//
 
98
// init --
 
99
//      Create a backing file.
 
100
//
 
101
void
 
102
init(char *file, int pagesize, int npages)
 
103
{
 
104
        // Create a file with the right number of pages, and store a page
 
105
        // number on each page.
 
106
        ofstream of(file, ios::out | ios::binary);
 
107
 
 
108
        if (of.fail()) {
 
109
                cerr << "MpoolExample: " << file << ": open failed\n";
 
110
                exit(EXIT_FAILURE);
 
111
        }
 
112
        char *p = new char[pagesize];
 
113
        memset(p, 0, pagesize);
 
114
 
 
115
        // The pages are numbered from 0.
 
116
        for (int cnt = 0; cnt <= npages; ++cnt) {
 
117
                *(db_pgno_t *)p = cnt;
 
118
                of.write(p, pagesize);
 
119
                if (of.fail()) {
 
120
                        cerr << "MpoolExample: " << file << ": write failed\n";
 
121
                        exit(EXIT_FAILURE);
 
122
                }
 
123
        }
 
124
        delete [] p;
 
125
}
 
126
 
 
127
static void
 
128
usage()
 
129
{
 
130
        cerr << "usage: MpoolExample [-c cachesize] "
 
131
             << "[-h hits] [-n npages] [-p pagesize]\n";
 
132
        exit(EXIT_FAILURE);
 
133
}
 
134
 
 
135
// Note: by using DB_CXX_NO_EXCEPTIONS, we get explicit error returns
 
136
// from various methods rather than exceptions so we can report more
 
137
// information with each error.
 
138
//
 
139
MpoolExample::MpoolExample()
 
140
:       DbEnv(DB_CXX_NO_EXCEPTIONS)
 
141
{
 
142
}
 
143
 
 
144
void MpoolExample::initdb(const char *home, int cachesize)
 
145
{
 
146
        set_error_stream(&cerr);
 
147
        set_errpfx("MpoolExample");
 
148
        set_cachesize(0, cachesize, 0);
 
149
 
 
150
        open(home, DB_CREATE | DB_INIT_MPOOL, 0);
 
151
}
 
152
 
 
153
//
 
154
// run --
 
155
//      Get a set of pages.
 
156
//
 
157
void
 
158
MpoolExample::run(int hits, int pagesize, int npages)
 
159
{
 
160
        db_pgno_t pageno;
 
161
        int cnt;
 
162
        void *p;
 
163
 
 
164
        // Open the file in the pool.
 
165
        DbMpoolFile *dbmfp;
 
166
 
 
167
        DbMpoolFile::open(this, MPOOL, 0, 0, pagesize, NULL, &dbmfp);
 
168
 
 
169
        cout << "retrieve " << hits << " random pages... ";
 
170
 
 
171
        srand((unsigned int)time(NULL));
 
172
        for (cnt = 0; cnt < hits; ++cnt) {
 
173
                pageno = (rand() % npages) + 1;
 
174
                if ((errno = dbmfp->get(&pageno, 0, &p)) != 0) {
 
175
                        cerr << "MpoolExample: unable to retrieve page "
 
176
                             << (unsigned long)pageno << ": "
 
177
                             << strerror(errno) << "\n";
 
178
                        exit(EXIT_FAILURE);
 
179
                }
 
180
                if (*(db_pgno_t *)p != pageno) {
 
181
                        cerr << "MpoolExample: wrong page retrieved ("
 
182
                             << (unsigned long)pageno << " != "
 
183
                             << *(int *)p << ")\n";
 
184
                        exit(EXIT_FAILURE);
 
185
                }
 
186
                if ((errno = dbmfp->put(p, 0)) != 0) {
 
187
                        cerr << "MpoolExample: unable to return page "
 
188
                             << (unsigned long)pageno << ": "
 
189
                             << strerror(errno) << "\n";
 
190
                        exit(EXIT_FAILURE);
 
191
                }
 
192
        }
 
193
 
 
194
        cout << "successful.\n";
 
195
 
 
196
        // Close the pool.
 
197
        if ((errno = close(0)) != 0) {
 
198
                cerr << "MpoolExample: " << strerror(errno) << "\n";
 
199
                exit(EXIT_FAILURE);
 
200
        }
 
201
}