2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1997-2002
5
* Sleepycat Software. All rights reserved.
10
#include <sys/types.h>
25
#define DATABASE "access.db"
26
#define WORDLIST "../test/wordlist"
28
const char *progname = "BtRecExample"; // Program name.
33
BtRecExample(FILE *fp);
37
void show(const char *msg, Dbt *key, Dbt *data);
44
BtRecExample::BtRecExample(FILE *fp)
46
char *p, *t, buf[1024], rbuf[1024];
49
// Remove the previous database.
50
(void)remove(DATABASE);
52
dbp = new Db(NULL, 0);
54
dbp->set_error_stream(&cerr);
55
dbp->set_errpfx(progname);
56
dbp->set_pagesize(1024); // 1K page sizes.
58
dbp->set_flags(DB_RECNUM); // Record numbers.
59
dbp->open(NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664);
62
// Insert records into the database, where the key is the word
63
// preceded by its record number, and the data is the same, but
67
for (int cnt = 1; cnt <= 1000; ++cnt) {
68
(void)sprintf(buf, "%04d_", cnt);
69
if (fgets(buf + 4, sizeof(buf) - 4, fp) == NULL)
71
u_int32_t len = strlen(buf);
73
for (t = rbuf, p = buf + (len - 2); p >= buf;)
77
// As a convenience for printing, we include the null terminator
78
// in the stored data.
83
if ((ret = dbp->put(NULL, &key, &data, DB_NOOVERWRITE)) != 0) {
84
dbp->err(ret, "Db::put");
85
if (ret != DB_KEYEXIST)
86
throw DbException(ret);
91
BtRecExample::~BtRecExample()
100
// Print out the number of records in the database.
102
void BtRecExample::stats()
104
DB_BTREE_STAT *statp;
106
dbp->stat(&statp, 0);
107
cout << progname << ": database contains "
108
<< (u_long)statp->bt_ndata << " records\n";
110
// Note: must use free, not delete.
111
// This struct is allocated by C.
116
void BtRecExample::run()
122
// Acquire a cursor for the database.
123
dbp->cursor(NULL, &dbcp, 0);
126
// Prompt the user for a record number, then retrieve and display
130
// Get a record number.
133
if (fgets(buf, sizeof(buf), stdin) == NULL)
138
// Start with a fresh key each time,
139
// the dbp->get() routine returns
140
// the key and data pair, not just the key!
142
Dbt key(&recno, sizeof(recno));
145
if ((ret = dbcp->get(&key, &data, DB_SET_RECNO)) != 0) {
146
dbp->err(ret, "DBcursor->get");
147
throw DbException(ret);
150
// Display the key and data.
151
show("k/d\t", &key, &data);
153
// Move the cursor a record forward.
154
if ((ret = dbcp->get(&key, &data, DB_NEXT)) != 0) {
155
dbp->err(ret, "DBcursor->get");
156
throw DbException(ret);
159
// Display the key and data.
160
show("next\t", &key, &data);
163
// Retrieve the record number for the following record into
166
data.set_data(&recno);
167
data.set_size(sizeof(recno));
168
data.set_ulen(sizeof(recno));
169
data.set_flags(data.get_flags() | DB_DBT_USERMEM);
171
if ((ret = dbcp->get(&key, &data, DB_GET_RECNO)) != 0) {
172
if (ret != DB_NOTFOUND && ret != DB_KEYEMPTY) {
173
dbp->err(ret, "DBcursor->get");
174
throw DbException(ret);
178
cout << "retrieved recno: " << (u_long)recno << "\n";
188
// Display a key/data pair.
190
void BtRecExample::show(const char *msg, Dbt *key, Dbt *data)
192
cout << msg << (char *)key->get_data()
193
<< " : " << (char *)data->get_data() << "\n";
201
// Open the word database.
202
if ((fp = fopen(WORDLIST, "r")) == NULL) {
203
fprintf(stderr, "%s: open %s: %s\n",
204
progname, WORDLIST, db_strerror(errno));
205
return (EXIT_FAILURE);
209
BtRecExample app(fp);
211
// Close the word database.
218
catch (DbException &dbe) {
219
cerr << "Exception: " << dbe.what() << "\n";
220
return (EXIT_FAILURE);
223
return (EXIT_SUCCESS);