~ubuntu-branches/ubuntu/maverick/evolution-data-server/maverick-proposed

« back to all changes in this revision

Viewing changes to libdb/test/scr015/TestKeyRange.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-05-17 17:02:06 UTC
  • mfrom: (1.1.79 upstream) (1.6.12 experimental)
  • Revision ID: james.westby@ubuntu.com-20100517170206-4ufr52vwrhh26yh0
Tags: 2.30.1-1ubuntu1
* Merge from debian experimental. Remaining change:
  (LP: #42199, #229669, #173703, #360344, #508494)
  + debian/control:
    - add Vcs-Bzr tag
    - don't use libgnome
    - Use Breaks instead of Conflicts against evolution 2.25 and earlier.
  + debian/evolution-data-server.install,
    debian/patches/45_libcamel_providers_version.patch:
    - use the upstream versioning, not a Debian-specific one 
  + debian/libedata-book1.2-dev.install, debian/libebackend-1.2-dev.install,
    debian/libcamel1.2-dev.install, debian/libedataserverui1.2-dev.install:
    - install html documentation
  + debian/rules:
    - don't build documentation it's shipped with the tarball

Show diffs side-by-side

added added

removed removed

Lines of Context:
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'.
5
 
 */
6
 
/*-
7
 
 * See the file LICENSE for redistribution information.
8
 
 *
9
 
 * Copyright (c) 1997-2002
10
 
 *      Sleepycat Software.  All rights reserved.
11
 
 *
12
 
 * $Id$
13
 
 */
14
 
 
15
 
#ifndef NO_SYSTEM_INCLUDES
16
 
#include <sys/types.h>
17
 
 
18
 
#include <iostream.h>
19
 
#include <errno.h>
20
 
#include <stdlib.h>
21
 
#include <string.h>
22
 
#ifndef _MSC_VER
23
 
#include <unistd.h>
24
 
#endif
25
 
#endif
26
 
 
27
 
#include <iomanip.h>
28
 
#include <db_cxx.h>
29
 
 
30
 
class TestKeyRange
31
 
{
32
 
public:
33
 
        TestKeyRange();
34
 
        void run();
35
 
 
36
 
private:
37
 
        static const char FileName[];
38
 
 
39
 
        // no need for copy and assignment
40
 
        TestKeyRange(const TestKeyRange &);
41
 
        void operator = (const TestKeyRange &);
42
 
};
43
 
 
44
 
static void usage();          // forward
45
 
 
46
 
int main(int argc, char *argv[])
47
 
{
48
 
        if (argc > 1) {
49
 
                usage();
50
 
        }
51
 
 
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.
56
 
        //
57
 
        try {
58
 
                TestKeyRange app;
59
 
                app.run();
60
 
                return 0;
61
 
        }
62
 
        catch (DbException &dbe) {
63
 
                cerr << "TestKeyRange: " << dbe.what() << "\n";
64
 
                return 1;
65
 
        }
66
 
}
67
 
 
68
 
static void usage()
69
 
{
70
 
        cerr << "usage: TestKeyRange\n";
71
 
        exit(1);
72
 
}
73
 
 
74
 
const char TestKeyRange::FileName[] = "access.db";
75
 
 
76
 
TestKeyRange::TestKeyRange()
77
 
{
78
 
}
79
 
 
80
 
void TestKeyRange::run()
81
 
{
82
 
        // Remove the previous database.
83
 
        (void)unlink(FileName);
84
 
 
85
 
        // Create the database object.
86
 
        // There is no environment for this simple example.
87
 
        Db db(0, 0);
88
 
 
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);
94
 
 
95
 
        //
96
 
        // Insert records into the database, where the key is the user
97
 
        // input and the data is the user input in reverse order.
98
 
        //
99
 
        char buf[1024];
100
 
        char rbuf[1024];
101
 
        char *t;
102
 
        char *p;
103
 
        int ret;
104
 
        int len;
105
 
        Dbt *firstkey = NULL;
106
 
        char firstbuf[1024];
107
 
 
108
 
        for (;;) {
109
 
                cout << "input>";
110
 
                cout.flush();
111
 
 
112
 
                cin.getline(buf, sizeof(buf));
113
 
                if (cin.eof())
114
 
                        break;
115
 
 
116
 
                if ((len = strlen(buf)) <= 0)
117
 
                        continue;
118
 
                for (t = rbuf, p = buf + (len - 1); p >= buf;)
119
 
                        *t++ = *p--;
120
 
                *t++ = '\0';
121
 
 
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);
127
 
                }
128
 
 
129
 
                ret = db.put(0, &key, &data, DB_NOOVERWRITE);
130
 
                if (ret == DB_KEYEXIST) {
131
 
                        cout << "Key " << buf << " already exists.\n";
132
 
                }
133
 
                cout << "\n";
134
 
        }
135
 
 
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.
139
 
        //
140
 
        try {
141
 
                // Acquire a cursor for the table.
142
 
                Dbc *dbcp;
143
 
                db.cursor(NULL, &dbcp, 0);
144
 
 
145
 
                /*ADDED...*/
146
 
                DB_KEY_RANGE range;
147
 
                memset(&range, 0, sizeof(range));
148
 
 
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);
153
 
                /*end ADDED*/
154
 
 
155
 
                Dbt key;
156
 
                Dbt data;
157
 
 
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";
163
 
                }
164
 
                dbcp->close();
165
 
        }
166
 
        catch (DbException &dbe) {
167
 
                cerr << "TestKeyRange: " << dbe.what() << "\n";
168
 
        }
169
 
 
170
 
        db.close(0);
171
 
}