1
/*NOTE: AccessExample changed to test Db.key_range.
2
* We made a global change of /AccessExample/TestKeyRange/,
3
* the only other changes are marked with comments that
4
* are notated as 'ADDED'.
7
* See the file LICENSE for redistribution information.
9
* Copyright (c) 1997-2002
10
* Sleepycat Software. All rights reserved.
15
#ifndef NO_SYSTEM_INCLUDES
16
#include <sys/types.h>
37
static const char FileName[];
39
// no need for copy and assignment
40
TestKeyRange(const TestKeyRange &);
41
void operator = (const TestKeyRange &);
44
static void usage(); // forward
46
int main(int argc, char *argv[])
52
// Use a try block just to report any errors.
53
// An alternate approach to using exceptions is to
54
// use error models (see DbEnv::set_error_model()) so
55
// that error codes are returned for all Berkeley DB methods.
62
catch (DbException &dbe) {
63
cerr << "TestKeyRange: " << dbe.what() << "\n";
70
cerr << "usage: TestKeyRange\n";
74
const char TestKeyRange::FileName[] = "access.db";
76
TestKeyRange::TestKeyRange()
80
void TestKeyRange::run()
82
// Remove the previous database.
83
(void)unlink(FileName);
85
// Create the database object.
86
// There is no environment for this simple example.
89
db.set_error_stream(&cerr);
90
db.set_errpfx("TestKeyRange");
91
db.set_pagesize(1024); /* Page size: 1K. */
92
db.set_cachesize(0, 32 * 1024, 0);
93
db.open(NULL, FileName, NULL, DB_BTREE, DB_CREATE, 0664);
96
// Insert records into the database, where the key is the user
97
// input and the data is the user input in reverse order.
105
Dbt *firstkey = NULL;
112
cin.getline(buf, sizeof(buf));
116
if ((len = strlen(buf)) <= 0)
118
for (t = rbuf, p = buf + (len - 1); p >= buf;)
122
Dbt key(buf, len + 1);
123
Dbt data(rbuf, len + 1);
124
if (firstkey == NULL) {
125
strcpy(firstbuf, buf);
126
firstkey = new Dbt(firstbuf, len + 1);
129
ret = db.put(0, &key, &data, DB_NOOVERWRITE);
130
if (ret == DB_KEYEXIST) {
131
cout << "Key " << buf << " already exists.\n";
136
// We put a try block around this section of code
137
// to ensure that our database is properly closed
138
// in the event of an error.
141
// Acquire a cursor for the table.
143
db.cursor(NULL, &dbcp, 0);
147
memset(&range, 0, sizeof(range));
149
db.key_range(NULL, firstkey, &range, 0);
150
printf("less: %f\n", range.less);
151
printf("equal: %f\n", range.equal);
152
printf("greater: %f\n", range.greater);
158
// Walk through the table, printing the key/data pairs.
159
while (dbcp->get(&key, &data, DB_NEXT) == 0) {
160
char *key_string = (char *)key.get_data();
161
char *data_string = (char *)data.get_data();
162
cout << key_string << " : " << data_string << "\n";
166
catch (DbException &dbe) {
167
cerr << "TestKeyRange: " << dbe.what() << "\n";