mariadb/bdb/examples_cxx/MpoolExample.cpp

219 lines
4.6 KiB
C++
Raw Normal View History

2001-03-04 19:42:05 -05:00
/*-
* See the file LICENSE for redistribution information.
*
2002-10-30 15:57:05 +04:00
* Copyright (c) 1997-2002
2001-03-04 19:42:05 -05:00
* Sleepycat Software. All rights reserved.
*
2002-10-30 15:57:05 +04:00
* $Id: MpoolExample.cpp,v 11.23 2002/01/11 15:52:15 bostic Exp $
2001-03-04 19:42:05 -05:00
*/
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
2002-10-30 15:57:05 +04:00
#include <iostream>
#include <fstream>
2001-03-04 19:42:05 -05:00
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <db_cxx.h>
2002-10-30 15:57:05 +04:00
using std::cout;
using std::cerr;
using std::ios;
using std::ofstream;
2001-03-04 19:42:05 -05:00
#define MPOOL "mpool"
2002-10-30 15:57:05 +04:00
int init(const char *, int, int);
int run(DB_ENV *, int, int, int);
2001-03-04 19:42:05 -05:00
2002-10-30 15:57:05 +04:00
static int usage();
2001-03-04 19:42:05 -05:00
2002-10-30 15:57:05 +04:00
const char *progname = "MpoolExample"; // Program name.
2001-03-04 19:42:05 -05:00
class MpoolExample : public DbEnv
{
public:
MpoolExample();
2002-10-30 15:57:05 +04:00
int initdb(const char *home, int cachesize);
int run(int hits, int pagesize, int npages);
2001-03-04 19:42:05 -05:00
private:
static const char FileName[];
// no need for copy and assignment
MpoolExample(const MpoolExample &);
void operator = (const MpoolExample &);
};
int main(int argc, char *argv[])
{
2002-10-30 15:57:05 +04:00
int ret;
2001-03-04 19:42:05 -05:00
int cachesize = 20 * 1024;
int hits = 1000;
int npages = 50;
int pagesize = 1024;
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-c") == 0) {
if ((cachesize = atoi(argv[++i])) < 20 * 1024)
usage();
}
else if (strcmp(argv[i], "-h") == 0) {
if ((hits = atoi(argv[++i])) <= 0)
usage();
}
else if (strcmp(argv[i], "-n") == 0) {
if ((npages = atoi(argv[++i])) <= 0)
usage();
}
else if (strcmp(argv[i], "-p") == 0) {
if ((pagesize = atoi(argv[++i])) <= 0)
usage();
}
else {
usage();
}
}
// Initialize the file.
2002-10-30 15:57:05 +04:00
if ((ret = init(MPOOL, pagesize, npages)) != 0)
return (ret);
2001-03-04 19:42:05 -05:00
try {
MpoolExample app;
cout << progname
<< ": cachesize: " << cachesize
<< "; pagesize: " << pagesize
<< "; N pages: " << npages << "\n";
2002-10-30 15:57:05 +04:00
if ((ret = app.initdb(NULL, cachesize)) != 0)
return (ret);
if ((ret = app.run(hits, pagesize, npages)) != 0)
return (ret);
2001-03-04 19:42:05 -05:00
cout << "MpoolExample: completed\n";
2002-10-30 15:57:05 +04:00
return (EXIT_SUCCESS);
2001-03-04 19:42:05 -05:00
}
catch (DbException &dbe) {
cerr << "MpoolExample: " << dbe.what() << "\n";
2002-10-30 15:57:05 +04:00
return (EXIT_FAILURE);
2001-03-04 19:42:05 -05:00
}
}
//
// init --
// Create a backing file.
//
2002-10-30 15:57:05 +04:00
int
init(const char *file, int pagesize, int npages)
2001-03-04 19:42:05 -05:00
{
// Create a file with the right number of pages, and store a page
// number on each page.
2002-10-30 15:57:05 +04:00
ofstream of(file, ios::out | ios::binary);
if (of.fail()) {
cerr << "MpoolExample: " << file << ": open failed\n";
return (EXIT_FAILURE);
2001-03-04 19:42:05 -05:00
}
char *p = new char[pagesize];
memset(p, 0, pagesize);
// The pages are numbered from 0.
for (int cnt = 0; cnt <= npages; ++cnt) {
*(db_pgno_t *)p = cnt;
2002-10-30 15:57:05 +04:00
of.write(p, pagesize);
if (of.fail()) {
cerr << "MpoolExample: " << file << ": write failed\n";
return (EXIT_FAILURE);
2001-03-04 19:42:05 -05:00
}
}
delete [] p;
2002-10-30 15:57:05 +04:00
return (EXIT_SUCCESS);
2001-03-04 19:42:05 -05:00
}
2002-10-30 15:57:05 +04:00
static int
2001-03-04 19:42:05 -05:00
usage()
{
cerr << "usage: MpoolExample [-c cachesize] "
<< "[-h hits] [-n npages] [-p pagesize]\n";
2002-10-30 15:57:05 +04:00
return (EXIT_FAILURE);
2001-03-04 19:42:05 -05:00
}
// Note: by using DB_CXX_NO_EXCEPTIONS, we get explicit error returns
// from various methods rather than exceptions so we can report more
// information with each error.
//
MpoolExample::MpoolExample()
: DbEnv(DB_CXX_NO_EXCEPTIONS)
{
}
2002-10-30 15:57:05 +04:00
int MpoolExample::initdb(const char *home, int cachesize)
2001-03-04 19:42:05 -05:00
{
set_error_stream(&cerr);
set_errpfx("MpoolExample");
set_cachesize(0, cachesize, 0);
open(home, DB_CREATE | DB_INIT_MPOOL, 0);
2002-10-30 15:57:05 +04:00
return (EXIT_SUCCESS);
2001-03-04 19:42:05 -05:00
}
//
// run --
// Get a set of pages.
//
2002-10-30 15:57:05 +04:00
int
2001-03-04 19:42:05 -05:00
MpoolExample::run(int hits, int pagesize, int npages)
{
db_pgno_t pageno;
2002-10-30 15:57:05 +04:00
int cnt, ret;
2001-03-04 19:42:05 -05:00
void *p;
2002-10-30 15:57:05 +04:00
// Open the file in the environment.
DbMpoolFile *mfp;
2001-03-04 19:42:05 -05:00
2002-10-30 15:57:05 +04:00
if ((ret = memp_fcreate(&mfp, 0)) != 0) {
cerr << "MpoolExample: memp_fcreate failed: "
<< strerror(ret) << "\n";
return (EXIT_FAILURE);
}
mfp->open(MPOOL, 0, 0, pagesize);
2001-03-04 19:42:05 -05:00
cout << "retrieve " << hits << " random pages... ";
srand((unsigned int)time(NULL));
for (cnt = 0; cnt < hits; ++cnt) {
pageno = (rand() % npages) + 1;
2002-10-30 15:57:05 +04:00
if ((ret = mfp->get(&pageno, 0, &p)) != 0) {
2001-03-04 19:42:05 -05:00
cerr << "MpoolExample: unable to retrieve page "
<< (unsigned long)pageno << ": "
2002-10-30 15:57:05 +04:00
<< strerror(ret) << "\n";
return (EXIT_FAILURE);
2001-03-04 19:42:05 -05:00
}
if (*(db_pgno_t *)p != pageno) {
cerr << "MpoolExample: wrong page retrieved ("
<< (unsigned long)pageno << " != "
<< *(int *)p << ")\n";
2002-10-30 15:57:05 +04:00
return (EXIT_FAILURE);
2001-03-04 19:42:05 -05:00
}
2002-10-30 15:57:05 +04:00
if ((ret = mfp->put(p, 0)) != 0) {
2001-03-04 19:42:05 -05:00
cerr << "MpoolExample: unable to return page "
<< (unsigned long)pageno << ": "
2002-10-30 15:57:05 +04:00
<< strerror(ret) << "\n";
return (EXIT_FAILURE);
2001-03-04 19:42:05 -05:00
}
}
cout << "successful.\n";
// Close the pool.
2002-10-30 15:57:05 +04:00
if ((ret = close(0)) != 0) {
cerr << "MpoolExample: " << strerror(ret) << "\n";
return (EXIT_FAILURE);
2001-03-04 19:42:05 -05:00
}
2002-10-30 15:57:05 +04:00
return (EXIT_SUCCESS);
2001-03-04 19:42:05 -05:00
}