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

TestMulti.cpp

00001 #include "db_cxx.h"
00002 #include "stdlib.h"
00003 
00004 void test1()
00005 {
00006         int numberOfKeysToWrite= 10000;
00007         Db db(0,DB_CXX_NO_EXCEPTIONS);
00008         db.set_pagesize(512);
00009         int err= db.open(0, "test1.db", 0, DB_BTREE, DB_CREATE, 0);
00010         {
00011                 int i= 0;
00012                 Dbt key(&i,sizeof(i));
00013                 Dbt data(&i,sizeof(i));
00014                 for(;i<numberOfKeysToWrite;++i)
00015                 {
00016                         db.put(0,&key,&data,0);
00017                 }
00018         }
00019 
00020         {
00021                 Dbc *dbc;
00022                 err= db.cursor(0,&dbc,0);
00023 
00024                 char *check= (char*)calloc(numberOfKeysToWrite,1);
00025                 char buffer[8192];
00026                 int numberOfKeysRead= 0;
00027                 Dbt multikey(&numberOfKeysRead,sizeof(numberOfKeysRead));
00028                 Dbt multidata(&buffer,sizeof(buffer));
00029                 multidata.set_flags(DB_DBT_USERMEM);
00030                 multidata.set_ulen(sizeof(buffer));
00031                 err= 0;
00032                 while(err==0)
00033                 {
00034                         err= dbc->get(&multikey,&multidata,DB_NEXT|DB_MULTIPLE_KEY);
00035                         if(err==0)
00036                         {
00037                                 Dbt key, data;
00038                                 DbMultipleKeyDataIterator i(multidata);
00039                                 while(err==0 && i.next(key,data))
00040                                 {
00041                                         int actualKey= *((int*)key.get_data());
00042                                         int actualData= *((int*)data.get_data());
00043                                         if(actualKey!=actualData)
00044                                         {
00045                                                 std::cout << "Error: key/data mismatch. " << actualKey << "!=" << actualData << std::endl;
00046                                                 err= -1;
00047                                         }
00048                                         else
00049                                         {
00050                                                 check[actualKey]++;
00051                                         }
00052                                         numberOfKeysRead++;
00053                                 }
00054                         } else if(err!=DB_NOTFOUND)
00055                                 std::cout << "Error: dbc->get: " << db_strerror(err) << std::endl;
00056                 }
00057                 if(numberOfKeysRead!=numberOfKeysToWrite)
00058                 {
00059                         std::cout << "Error: key count mismatch. " << numberOfKeysRead << "!=" << numberOfKeysToWrite << std::endl;
00060                 }
00061                 for(int n=0;n<numberOfKeysToWrite;++n)
00062                 {
00063                         if(check[n]!=1)
00064                         {
00065                                 std::cout << "Error: key " << n << " was written to the database, but not read back." << std::endl;
00066                         }
00067                 }
00068                 free(check);
00069                 dbc->close();
00070         }
00071 
00072         db.close(0);
00073 }
00074 
00075 void test2()
00076 {
00077         int numberOfKeysToWrite= 10000;
00078         Db db(0,DB_CXX_NO_EXCEPTIONS);
00079         db.set_flags(DB_DUP);
00080         db.set_pagesize(512);
00081         int err= db.open(0, "test2.db", 0, DB_BTREE, DB_CREATE, 0);
00082 
00083         {
00084                 int i= 0;
00085                 int k= 0;
00086                 Dbt key(&k,sizeof(k));
00087                 Dbt data(&i,sizeof(i));
00088                 for(;i<numberOfKeysToWrite;++i)
00089                 {
00090                         err= db.put(0,&key,&data,0);
00091                 }
00092         }
00093 
00094         {
00095                 Dbc *dbc;
00096                 err= db.cursor(0,&dbc,0);
00097 
00098                 char buffer[8192];
00099                 int numberOfKeysRead= 0;
00100                 Dbt multikey(&numberOfKeysRead,sizeof(numberOfKeysRead));
00101                 Dbt multidata(&buffer,sizeof(buffer));
00102                 multidata.set_flags(DB_DBT_USERMEM);
00103                 multidata.set_ulen(sizeof(buffer));
00104                 err= 0;
00105                 while(err==0)
00106                 {
00107                         err= dbc->get(&multikey,&multidata,DB_NEXT|DB_MULTIPLE);
00108                         if(err==0)
00109                         {
00110                                 Dbt data;
00111                                 DbMultipleDataIterator i(multidata);
00112                                 while(err==0 && i.next(data))
00113                                 {
00114                                         int actualData= *((int*)data.get_data());
00115                                         if(numberOfKeysRead!=actualData)
00116                                         {
00117                                                 std::cout << "Error: key/data mismatch. " << numberOfKeysRead << "!=" << actualData << std::endl;
00118                                                 err= -1;
00119                                         }
00120                                         numberOfKeysRead++;
00121                                 }
00122                         } else if(err!=DB_NOTFOUND)
00123                                 std::cout << "Error: dbc->get: " << db_strerror(err) << std::endl;
00124                 }
00125                 if(numberOfKeysRead!=numberOfKeysToWrite)
00126                 {
00127                         std::cout << "Error: key count mismatch. " << numberOfKeysRead << "!=" << numberOfKeysToWrite << std::endl;
00128                 }
00129                 dbc->close();
00130         }
00131         db.close(0);
00132 }
00133 
00134 void test3()
00135 {
00136         int numberOfKeysToWrite= 10000;
00137         Db db(0,DB_CXX_NO_EXCEPTIONS);
00138         db.set_pagesize(512);
00139         int err= db.open(0, "test3.db", 0, DB_RECNO, DB_CREATE, 0);
00140 
00141         {
00142                 int i= 0;
00143                 Dbt key;
00144                 Dbt data(&i,sizeof(i));
00145                 for(;i<numberOfKeysToWrite;++i)
00146                 {
00147                         err= db.put(0,&key,&data,DB_APPEND);
00148                 }
00149         }
00150 
00151         {
00152                 Dbc *dbc;
00153                 err= db.cursor(0,&dbc,0);
00154 
00155                 char buffer[8192];
00156                 int numberOfKeysRead= 0;
00157                 Dbt multikey(&numberOfKeysRead,sizeof(numberOfKeysRead));
00158                 Dbt multidata(&buffer,sizeof(buffer));
00159                 multidata.set_flags(DB_DBT_USERMEM);
00160                 multidata.set_ulen(sizeof(buffer));
00161                 err= 0;
00162                 while(err==0)
00163                 {
00164                         err= dbc->get(&multikey,&multidata,DB_NEXT|DB_MULTIPLE_KEY);
00165                         if(err==0)
00166                         {
00167                                 u_int32_t recno= 0;
00168                                 Dbt data;
00169                                 DbMultipleRecnoDataIterator i(multidata);
00170                                 while(err==0 && i.next(recno,data))
00171                                 {
00172                                         int actualData= *((int*)data.get_data());
00173                                         if(recno!=actualData+1)
00174                                         {
00175                                                 std::cout << "Error: recno/data mismatch. " << recno << "!=" << actualData << "+1" << std::endl;
00176                                                 err= -1;
00177                                         }
00178                                         numberOfKeysRead++;
00179                                 }
00180                         } else if(err!=DB_NOTFOUND)
00181                                 std::cout << "Error: dbc->get: " << db_strerror(err) << std::endl;
00182                 }
00183                 if(numberOfKeysRead!=numberOfKeysToWrite)
00184                 {
00185                         std::cout << "Error: key count mismatch. " << numberOfKeysRead << "!=" << numberOfKeysToWrite << std::endl;
00186                 }
00187                 dbc->close();
00188         }
00189 
00190         db.close(0);
00191 }
00192 
00193 int main()
00194 {
00195         test1();
00196         test2();
00197         test3();
00198         return (0);
00199 }
00200 

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