1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1997,2007 Oracle. All rights reserved.
*
* $Id: BtRecExample.cpp,v 12.5 2007/05/17 15:15:31 bostic Exp $
*/
#include <sys/types.h>
#include <errno.h>
#include <iostream>
#include <iomanip>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <db_cxx.h>
using std::cout;
using std::cerr;
#define DATABASE "access.db"
#define WORDLIST "../test/wordlist"
const char *progname = "BtRecExample"; // Program name.
class BtRecExample
{
public:
BtRecExample(FILE *fp);
~BtRecExample();
void run();
void stats();
void show(const char *msg, Dbt *key, Dbt *data);
private:
Db *dbp;
Dbc *dbcp;
};
BtRecExample::BtRecExample(FILE *fp)
{
char *p, *t, buf[1024], rbuf[1024];
int ret;
// Remove the previous database.
(void)remove(DATABASE);
dbp = new Db(NULL, 0);
dbp->set_error_stream(&cerr);
dbp->set_errpfx(progname);
dbp->set_pagesize(1024); // 1K page sizes.
dbp->set_flags(DB_RECNUM); // Record numbers.
dbp->open(NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664);
//
// Insert records into the database, where the key is the word
// preceded by its record number, and the data is the same, but
// in reverse order.
//
for (int cnt = 1; cnt <= 1000; ++cnt) {
(void)sprintf(buf, "%04d_", cnt);
if (fgets(buf + 4, sizeof(buf) - 4, fp) == NULL)
break;
u_int32_t len = (u_int32_t)strlen(buf);
buf[len - 1] = '\0';
for (t = rbuf, p = buf + (len - 2); p >= buf;)
*t++ = *p--;
*t++ = '\0';
// As a convenience for printing, we include the null terminator
// in the stored data.
//
Dbt key(buf, len);
Dbt data(rbuf, len);
if ((ret = dbp->put(NULL, &key, &data, DB_NOOVERWRITE)) != 0) {
dbp->err(ret, "Db::put");
if (ret != DB_KEYEXIST)
throw DbException(ret);
}
}
}
BtRecExample::~BtRecExample()
{
if (dbcp != 0)
dbcp->close();
dbp->close(0);
delete dbp;
}
//
// Print out the number of records in the database.
//
void BtRecExample::stats()
{
DB_BTREE_STAT *statp;
dbp->stat(NULL, &statp, 0);
cout << progname << ": database contains "
<< (u_long)statp->bt_ndata << " records\n";
// Note: must use free, not delete.
// This struct is allocated by C.
//
free(statp);
}
void BtRecExample::run()
{
db_recno_t recno;
int ret;
char buf[1024];
// Acquire a cursor for the database.
dbp->cursor(NULL, &dbcp, 0);
//
// Prompt the user for a record number, then retrieve and display
// that record.
//
for (;;) {
// Get a record number.
cout << "recno #> ";
cout.flush();
if (fgets(buf, sizeof(buf), stdin) == NULL)
break;
recno = atoi(buf);
//
// Start with a fresh key each time,
// the dbp->get() routine returns
// the key and data pair, not just the key!
//
Dbt key(&recno, sizeof(recno));
Dbt data;
if ((ret = dbcp->get(&key, &data, DB_SET_RECNO)) != 0) {
dbp->err(ret, "DBcursor->get");
throw DbException(ret);
}
// Display the key and data.
show("k/d\t", &key, &data);
// Move the cursor a record forward.
if ((ret = dbcp->get(&key, &data, DB_NEXT)) != 0) {
dbp->err(ret, "DBcursor->get");
throw DbException(ret);
}
// Display the key and data.
show("next\t", &key, &data);
//
// Retrieve the record number for the following record into
// local memory.
//
data.set_data(&recno);
data.set_size(sizeof(recno));
data.set_ulen(sizeof(recno));
data.set_flags(data.get_flags() | DB_DBT_USERMEM);
if ((ret = dbcp->get(&key, &data, DB_GET_RECNO)) != 0) {
if (ret != DB_NOTFOUND && ret != DB_KEYEMPTY) {
dbp->err(ret, "DBcursor->get");
throw DbException(ret);
}
}
else {
cout << "retrieved recno: " << (u_long)recno << "\n";
}
}
dbcp->close();
dbcp = NULL;
}
//
// show --
// Display a key/data pair.
//
void BtRecExample::show(const char *msg, Dbt *key, Dbt *data)
{
cout << msg << (char *)key->get_data()
<< " : " << (char *)data->get_data() << "\n";
}
int
main()
{
FILE *fp;
// Open the word database.
if ((fp = fopen(WORDLIST, "r")) == NULL) {
fprintf(stderr, "%s: open %s: %s\n",
progname, WORDLIST, db_strerror(errno));
return (EXIT_FAILURE);
}
try {
BtRecExample app(fp);
// Close the word database.
(void)fclose(fp);
fp = NULL;
app.stats();
app.run();
}
catch (DbException &dbe) {
cerr << "Exception: " << dbe.what() << "\n";
return (EXIT_FAILURE);
}
return (EXIT_SUCCESS);
}
|