1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2000,2008 Oracle.  All rights reserved.
5 *
6 * $Id: TestLogc.cpp,v 12.6 2008/01/08 20:58:54 bostic Exp $
7 */
8
9/*
10 * A basic regression test for the Logc class.
11 */
12
13#include <db_cxx.h>
14#include <iostream.h>
15
16static void show_dbt(ostream &os, Dbt *dbt)
17{
18	int i;
19	int size = dbt->get_size();
20	unsigned char *data = (unsigned char *)dbt->get_data();
21
22	os << "size: " << size << " data: ";
23	for (i=0; i<size && i<10; i++) {
24		os << (int)data[i] << " ";
25	}
26	if (i<size)
27		os << "...";
28}
29
30int main(int argc, char *argv[])
31{
32	try {
33		DbEnv *env = new DbEnv(0);
34		env->open(".", DB_CREATE | DB_INIT_LOG | DB_INIT_MPOOL, 0);
35
36		// Do some database activity to get something into the log.
37		Db *db1 = new Db(env, 0);
38		db1->open(NULL, "first.db", NULL, DB_BTREE, DB_CREATE, 0);
39		Dbt *key = new Dbt((char *)"a", 1);
40		Dbt *data = new Dbt((char *)"b", 1);
41		db1->put(NULL, key, data, 0);
42		key->set_data((char *)"c");
43		data->set_data((char *)"d");
44		db1->put(NULL, key, data, 0);
45		db1->close(0);
46
47		Db *db2 = new Db(env, 0);
48		db2->open(NULL, "second.db", NULL, DB_BTREE, DB_CREATE, 0);
49		key->set_data((char *)"w");
50		data->set_data((char *)"x");
51		db2->put(NULL, key, data, 0);
52		key->set_data((char *)"y");
53		data->set_data((char *)"z");
54		db2->put(NULL, key, data, 0);
55		db2->close(0);
56
57		// Now get a log cursor and walk through.
58		DbLogc *logc;
59
60		env->log_cursor(&logc, 0);
61		int ret = 0;
62		DbLsn lsn;
63		Dbt *dbt = new Dbt();
64		u_int32_t flags = DB_FIRST;
65
66		int count = 0;
67		while ((ret = logc->get(&lsn, dbt, flags)) == 0) {
68
69			// We ignore the contents of the log record,
70			// it's not portable.  Even the exact count
71			// is may change when the underlying implementation
72			// changes, we'll just make sure at the end we saw
73			// 'enough'.
74			//
75			//     cout << "logc.get: " << count;
76			//     show_dbt(cout, dbt);
77			//	cout << "\n";
78			//
79			count++;
80			flags = DB_NEXT;
81		}
82		if (ret != DB_NOTFOUND) {
83			cerr << "*** FAIL: logc.get returned: "
84			     << DbEnv::strerror(ret) << "\n";
85		}
86		logc->close(0);
87
88		// There has to be at *least* four log records,
89		// since we did four separate database operations.
90		//
91		if (count < 4)
92			cerr << "*** FAIL: not enough log records\n";
93
94		cout << "TestLogc done.\n";
95	}
96	catch (DbException &dbe) {
97		cerr << "*** FAIL: " << dbe.what() <<"\n";
98	}
99	return 0;
100}
101