2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1997-2001
5
* Sleepycat Software. All rights reserved.
7
* $Id: MpoolExample.cpp,v 11.16 2001/05/10 17:14:07 bostic Exp $
10
#include <sys/types.h>
24
void init(char *, int, int);
25
void run(DB_ENV *, int, int, int);
29
char *progname = "MpoolExample"; // Program name.
31
class MpoolExample : public DbEnv
35
void initdb(const char *home, int cachesize);
36
void run(int hits, int pagesize, int npages);
39
static const char FileName[];
41
// no need for copy and assignment
42
MpoolExample(const MpoolExample &);
43
void operator = (const MpoolExample &);
46
int main(int argc, char *argv[])
48
int cachesize = 20 * 1024;
53
for (int i = 1; i < argc; ++i) {
54
if (strcmp(argv[i], "-c") == 0) {
55
if ((cachesize = atoi(argv[++i])) < 20 * 1024)
58
else if (strcmp(argv[i], "-h") == 0) {
59
if ((hits = atoi(argv[++i])) <= 0)
62
else if (strcmp(argv[i], "-n") == 0) {
63
if ((npages = atoi(argv[++i])) <= 0)
66
else if (strcmp(argv[i], "-p") == 0) {
67
if ((pagesize = atoi(argv[++i])) <= 0)
75
// Initialize the file.
76
init(MPOOL, pagesize, npages);
82
<< ": cachesize: " << cachesize
83
<< "; pagesize: " << pagesize
84
<< "; N pages: " << npages << "\n";
86
app.initdb(NULL, cachesize);
87
app.run(hits, pagesize, npages);
88
cout << "MpoolExample: completed\n";
91
catch (DbException &dbe) {
92
cerr << "MpoolExample: " << dbe.what() << "\n";
99
// Create a backing file.
102
init(char *file, int pagesize, int npages)
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);
109
cerr << "MpoolExample: " << file << ": open failed\n";
112
char *p = new char[pagesize];
113
memset(p, 0, pagesize);
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);
120
cerr << "MpoolExample: " << file << ": write failed\n";
130
cerr << "usage: MpoolExample [-c cachesize] "
131
<< "[-h hits] [-n npages] [-p pagesize]\n";
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.
139
MpoolExample::MpoolExample()
140
: DbEnv(DB_CXX_NO_EXCEPTIONS)
144
void MpoolExample::initdb(const char *home, int cachesize)
146
set_error_stream(&cerr);
147
set_errpfx("MpoolExample");
148
set_cachesize(0, cachesize, 0);
150
open(home, DB_CREATE | DB_INIT_MPOOL, 0);
155
// Get a set of pages.
158
MpoolExample::run(int hits, int pagesize, int npages)
164
// Open the file in the pool.
167
DbMpoolFile::open(this, MPOOL, 0, 0, pagesize, NULL, &dbmfp);
169
cout << "retrieve " << hits << " random pages... ";
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";
180
if (*(db_pgno_t *)p != pageno) {
181
cerr << "MpoolExample: wrong page retrieved ("
182
<< (unsigned long)pageno << " != "
183
<< *(int *)p << ")\n";
186
if ((errno = dbmfp->put(p, 0)) != 0) {
187
cerr << "MpoolExample: unable to return page "
188
<< (unsigned long)pageno << ": "
189
<< strerror(errno) << "\n";
194
cout << "successful.\n";
197
if ((errno = close(0)) != 0) {
198
cerr << "MpoolExample: " << strerror(errno) << "\n";