Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

TestKeyRange.cpp

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1997-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: TestKeyRange.cpp,v 12.1 2005/06/16 20:24:11 bostic Exp $
00008  */
00009 
00010 /*
00011  * NOTE: AccessExample changed to test Db.key_range.  We made a global
00012  * change of /AccessExample/TestKeyRange/, the only other changes are
00013  * marked with comments that are notated as 'ADDED'.
00014  */
00015 
00016 #ifndef NO_SYSTEM_INCLUDES
00017 #include <sys/types.h>
00018 
00019 #include <iostream.h>
00020 #include <errno.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #ifndef _MSC_VER
00024 #include <unistd.h>
00025 #endif
00026 #endif
00027 
00028 #include <iomanip.h>
00029 #include <db_cxx.h>
00030 
00031 class TestKeyRange
00032 {
00033 public:
00034         TestKeyRange();
00035         void run();
00036 
00037 private:
00038         static const char FileName[];
00039 
00040         // no need for copy and assignment
00041         TestKeyRange(const TestKeyRange &);
00042         void operator = (const TestKeyRange &);
00043 };
00044 
00045 static void usage();          // forward
00046 
00047 int main(int argc, char *argv[])
00048 {
00049         if (argc > 1) {
00050                 usage();
00051         }
00052 
00053         // Use a try block just to report any errors.
00054         // An alternate approach to using exceptions is to
00055         // use error models (see DbEnv::set_error_model()) so
00056         // that error codes are returned for all Berkeley DB methods.
00057         //
00058         try {
00059                 TestKeyRange app;
00060                 app.run();
00061                 return 0;
00062         }
00063         catch (DbException &dbe) {
00064                 cerr << "TestKeyRange: " << dbe.what() << "\n";
00065                 return 1;
00066         }
00067 }
00068 
00069 static void usage()
00070 {
00071         cerr << "usage: TestKeyRange\n";
00072         exit(1);
00073 }
00074 
00075 const char TestKeyRange::FileName[] = "access.db";
00076 
00077 TestKeyRange::TestKeyRange()
00078 {
00079 }
00080 
00081 void TestKeyRange::run()
00082 {
00083         // Remove the previous database.
00084         (void)unlink(FileName);
00085 
00086         // Create the database object.
00087         // There is no environment for this simple example.
00088         Db db(0, 0);
00089 
00090         db.set_error_stream(&cerr);
00091         db.set_errpfx("TestKeyRange");
00092         db.set_pagesize(1024);          /* Page size: 1K. */
00093         db.set_cachesize(0, 32 * 1024, 0);
00094         db.open(NULL, FileName, NULL, DB_BTREE, DB_CREATE, 0664);
00095 
00096         //
00097         // Insert records into the database, where the key is the user
00098         // input and the data is the user input in reverse order.
00099         //
00100         char buf[1024];
00101         char rbuf[1024];
00102         char *t;
00103         char *p;
00104         int ret;
00105         int len;
00106         Dbt *firstkey = NULL;
00107         char firstbuf[1024];
00108 
00109         for (;;) {
00110                 cout << "input>";
00111                 cout.flush();
00112 
00113                 cin.getline(buf, sizeof(buf));
00114                 if (cin.eof())
00115                         break;
00116 
00117                 if ((len = strlen(buf)) <= 0)
00118                         continue;
00119                 for (t = rbuf, p = buf + (len - 1); p >= buf;)
00120                         *t++ = *p--;
00121                 *t++ = '\0';
00122 
00123                 Dbt key(buf, len + 1);
00124                 Dbt data(rbuf, len + 1);
00125                 if (firstkey == NULL) {
00126                         strcpy(firstbuf, buf);
00127                         firstkey = new Dbt(firstbuf, len + 1);
00128                 }
00129 
00130                 ret = db.put(0, &key, &data, DB_NOOVERWRITE);
00131                 if (ret == DB_KEYEXIST) {
00132                         cout << "Key " << buf << " already exists.\n";
00133                 }
00134                 cout << "\n";
00135         }
00136 
00137         // We put a try block around this section of code
00138         // to ensure that our database is properly closed
00139         // in the event of an error.
00140         //
00141         try {
00142                 // Acquire a cursor for the table.
00143                 Dbc *dbcp;
00144                 db.cursor(NULL, &dbcp, 0);
00145 
00146                 /*ADDED...*/
00147                 DB_KEY_RANGE range;
00148                 memset(&range, 0, sizeof(range));
00149 
00150                 db.key_range(NULL, firstkey, &range, 0);
00151                 printf("less: %f\n", range.less);
00152                 printf("equal: %f\n", range.equal);
00153                 printf("greater: %f\n", range.greater);
00154                 /*end ADDED*/
00155 
00156                 Dbt key;
00157                 Dbt data;
00158 
00159                 // Walk through the table, printing the key/data pairs.
00160                 while (dbcp->get(&key, &data, DB_NEXT) == 0) {
00161                         char *key_string = (char *)key.get_data();
00162                         char *data_string = (char *)data.get_data();
00163                         cout << key_string << " : " << data_string << "\n";
00164                 }
00165                 dbcp->close();
00166         }
00167         catch (DbException &dbe) {
00168                 cerr << "TestKeyRange: " << dbe.what() << "\n";
00169         }
00170 
00171         db.close(0);
00172 }

Generated on Sun Dec 25 12:14:52 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2