Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

TestLogc.cpp

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2000-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: TestLogc.cpp,v 12.1 2005/06/16 20:24:11 bostic Exp $
00008  */
00009 
00010 /*
00011  * A basic regression test for the Logc class.
00012  */
00013 
00014 #include <db_cxx.h>
00015 #include <iostream.h>
00016 
00017 static void show_dbt(ostream &os, Dbt *dbt)
00018 {
00019         int i;
00020         int size = dbt->get_size();
00021         unsigned char *data = (unsigned char *)dbt->get_data();
00022 
00023         os << "size: " << size << " data: ";
00024         for (i=0; i<size && i<10; i++) {
00025                 os << (int)data[i] << " ";
00026         }
00027         if (i<size)
00028                 os << "...";
00029 }
00030 
00031 int main(int argc, char *argv[])
00032 {
00033         try {
00034                 DbEnv *env = new DbEnv(0);
00035                 env->open(".", DB_CREATE | DB_INIT_LOG | DB_INIT_MPOOL, 0);
00036 
00037                 // Do some database activity to get something into the log.
00038                 Db *db1 = new Db(env, 0);
00039                 db1->open(NULL, "first.db", NULL, DB_BTREE, DB_CREATE, 0);
00040                 Dbt *key = new Dbt((char *)"a", 1);
00041                 Dbt *data = new Dbt((char *)"b", 1);
00042                 db1->put(NULL, key, data, 0);
00043                 key->set_data((char *)"c");
00044                 data->set_data((char *)"d");
00045                 db1->put(NULL, key, data, 0);
00046                 db1->close(0);
00047 
00048                 Db *db2 = new Db(env, 0);
00049                 db2->open(NULL, "second.db", NULL, DB_BTREE, DB_CREATE, 0);
00050                 key->set_data((char *)"w");
00051                 data->set_data((char *)"x");
00052                 db2->put(NULL, key, data, 0);
00053                 key->set_data((char *)"y");
00054                 data->set_data((char *)"z");
00055                 db2->put(NULL, key, data, 0);
00056                 db2->close(0);
00057 
00058                 // Now get a log cursor and walk through.
00059                 DbLogc *logc;
00060 
00061                 env->log_cursor(&logc, 0);
00062                 int ret = 0;
00063                 DbLsn lsn;
00064                 Dbt *dbt = new Dbt();
00065                 u_int32_t flags = DB_FIRST;
00066 
00067                 int count = 0;
00068                 while ((ret = logc->get(&lsn, dbt, flags)) == 0) {
00069 
00070                         // We ignore the contents of the log record,
00071                         // it's not portable.  Even the exact count
00072                         // is may change when the underlying implementation
00073                         // changes, we'll just make sure at the end we saw
00074                         // 'enough'.
00075                         //
00076                         //     cout << "logc.get: " << count;
00077                         //     show_dbt(cout, dbt);
00078                         //      cout << "\n";
00079                         //
00080                         count++;
00081                         flags = DB_NEXT;
00082                 }
00083                 if (ret != DB_NOTFOUND) {
00084                         cerr << "*** FAIL: logc.get returned: "
00085                              << DbEnv::strerror(ret) << "\n";
00086                 }
00087                 logc->close(0);
00088 
00089                 // There has to be at *least* four log records,
00090                 // since we did four separate database operations.
00091                 //
00092                 if (count < 4)
00093                         cerr << "*** FAIL: not enough log records\n";
00094 
00095                 cout << "TestLogc done.\n";
00096         }
00097         catch (DbException &dbe) {
00098                 cerr << "*** FAIL: " << dbe.what() <<"\n";
00099         }
00100         return 0;
00101 }

Generated on Sun Dec 25 12:14:52 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2