00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <sys/types.h>
00011
00012 #include <errno.h>
00013 #include <fcntl.h>
00014 #include <iostream>
00015 #include <fstream>
00016 #include <stdlib.h>
00017 #include <string.h>
00018 #include <time.h>
00019
00020 #include <db_cxx.h>
00021
00022 using std::cout;
00023 using std::cerr;
00024 using std::ios;
00025 using std::ofstream;
00026
00027 #define MPOOL "mpool"
00028
00029 int init(const char *, int, int);
00030 int run(DB_ENV *, int, int, int);
00031
00032 static int usage();
00033
00034 const char *progname = "MpoolExample";
00035
00036 class MpoolExample : public DbEnv
00037 {
00038 public:
00039 MpoolExample();
00040 int initdb(const char *home, int cachesize);
00041 int run(int hits, int pagesize, int npages);
00042
00043 private:
00044 static const char FileName[];
00045
00046
00047 MpoolExample(const MpoolExample &);
00048 void operator = (const MpoolExample &);
00049 };
00050
00051 int main(int argc, char *argv[])
00052 {
00053 int ret;
00054 int cachesize = 20 * 1024;
00055 int hits = 1000;
00056 int npages = 50;
00057 int pagesize = 1024;
00058
00059 for (int i = 1; i < argc; ++i) {
00060 if (strcmp(argv[i], "-c") == 0) {
00061 if ((cachesize = atoi(argv[++i])) < 20 * 1024)
00062 usage();
00063 }
00064 else if (strcmp(argv[i], "-h") == 0) {
00065 if ((hits = atoi(argv[++i])) <= 0)
00066 usage();
00067 }
00068 else if (strcmp(argv[i], "-n") == 0) {
00069 if ((npages = atoi(argv[++i])) <= 0)
00070 usage();
00071 }
00072 else if (strcmp(argv[i], "-p") == 0) {
00073 if ((pagesize = atoi(argv[++i])) <= 0)
00074 usage();
00075 }
00076 else {
00077 usage();
00078 }
00079 }
00080
00081
00082 if ((ret = init(MPOOL, pagesize, npages)) != 0)
00083 return (ret);
00084
00085 try {
00086 MpoolExample app;
00087
00088 cout << progname
00089 << ": cachesize: " << cachesize
00090 << "; pagesize: " << pagesize
00091 << "; N pages: " << npages << "\n";
00092
00093 if ((ret = app.initdb(NULL, cachesize)) != 0)
00094 return (ret);
00095 if ((ret = app.run(hits, pagesize, npages)) != 0)
00096 return (ret);
00097 cout << "MpoolExample: completed\n";
00098 return (EXIT_SUCCESS);
00099 }
00100 catch (DbException &dbe) {
00101 cerr << "MpoolExample: " << dbe.what() << "\n";
00102 return (EXIT_FAILURE);
00103 }
00104 }
00105
00106
00107
00108
00109
00110 int
00111 init(const char *file, int pagesize, int npages)
00112 {
00113
00114
00115 ofstream of(file, ios::out | ios::binary);
00116
00117 if (of.fail()) {
00118 cerr << "MpoolExample: " << file << ": open failed\n";
00119 return (EXIT_FAILURE);
00120 }
00121 char *p = new char[pagesize];
00122 memset(p, 0, pagesize);
00123
00124
00125 for (int cnt = 0; cnt <= npages; ++cnt) {
00126 *(db_pgno_t *)p = cnt;
00127 of.write(p, pagesize);
00128 if (of.fail()) {
00129 cerr << "MpoolExample: " << file << ": write failed\n";
00130 return (EXIT_FAILURE);
00131 }
00132 }
00133 delete [] p;
00134 return (EXIT_SUCCESS);
00135 }
00136
00137 static int
00138 usage()
00139 {
00140 cerr << "usage: MpoolExample [-c cachesize] "
00141 << "[-h hits] [-n npages] [-p pagesize]\n";
00142 return (EXIT_FAILURE);
00143 }
00144
00145
00146
00147
00148
00149 MpoolExample::MpoolExample()
00150 : DbEnv(DB_CXX_NO_EXCEPTIONS)
00151 {
00152 }
00153
00154 int MpoolExample::initdb(const char *home, int cachesize)
00155 {
00156 set_error_stream(&cerr);
00157 set_errpfx("MpoolExample");
00158 set_cachesize(0, cachesize, 0);
00159
00160 open(home, DB_CREATE | DB_INIT_MPOOL, 0);
00161 return (EXIT_SUCCESS);
00162 }
00163
00164
00165
00166
00167
00168 int
00169 MpoolExample::run(int hits, int pagesize, int npages)
00170 {
00171 db_pgno_t pageno;
00172 int cnt, ret;
00173 void *p;
00174
00175
00176 DbMpoolFile *mfp;
00177
00178 if ((ret = memp_fcreate(&mfp, 0)) != 0) {
00179 cerr << "MpoolExample: memp_fcreate failed: "
00180 << strerror(ret) << "\n";
00181 return (EXIT_FAILURE);
00182 }
00183 mfp->open(MPOOL, 0, 0, pagesize);
00184
00185 cout << "retrieve " << hits << " random pages... ";
00186
00187 srand((unsigned int)time(NULL));
00188 for (cnt = 0; cnt < hits; ++cnt) {
00189 pageno = (rand() % npages) + 1;
00190 if ((ret = mfp->get(&pageno, 0, &p)) != 0) {
00191 cerr << "MpoolExample: unable to retrieve page "
00192 << (unsigned long)pageno << ": "
00193 << strerror(ret) << "\n";
00194 return (EXIT_FAILURE);
00195 }
00196 if (*(db_pgno_t *)p != pageno) {
00197 cerr << "MpoolExample: wrong page retrieved ("
00198 << (unsigned long)pageno << " != "
00199 << *(int *)p << ")\n";
00200 return (EXIT_FAILURE);
00201 }
00202 if ((ret = mfp->put(p, 0)) != 0) {
00203 cerr << "MpoolExample: unable to return page "
00204 << (unsigned long)pageno << ": "
00205 << strerror(ret) << "\n";
00206 return (EXIT_FAILURE);
00207 }
00208 }
00209
00210 cout << "successful.\n";
00211
00212
00213 if ((ret = close(0)) != 0) {
00214 cerr << "MpoolExample: " << strerror(ret) << "\n";
00215 return (EXIT_FAILURE);
00216 }
00217 return (EXIT_SUCCESS);
00218 }