hamsterdb Embedded Database 1.1.13

db1.c

Go to the documentation of this file.
00001 
00016 #include <stdio.h>
00017 #include <string.h>
00018 #include <stdlib.h> /* for exit() */
00019 #if UNDER_CE
00020 #   include <windows.h>
00021 #endif
00022 #include <ham/hamsterdb.h>
00023 
00024 #define LOOP 100
00025 
00026 void 
00027 error(const char *foo, ham_status_t st)
00028 {
00029 #if UNDER_CE
00030     wchar_t title[1024];
00031     wchar_t text[1024];
00032 
00033     MultiByteToWideChar(CP_ACP, 0, foo, -1, title, 
00034             sizeof(title)/sizeof(wchar_t));
00035     MultiByteToWideChar(CP_ACP, 0, ham_strerror(st), -1, text, 
00036             sizeof(text)/sizeof(wchar_t));
00037 
00038     MessageBox(0, title, text, 0);
00039 #endif
00040     printf("%s() returned error %d: %s\n", foo, st, ham_strerror(st));
00041     exit(-1);
00042 }
00043 
00044 int 
00045 main(int argc, char **argv)
00046 {
00047     int i;
00048     ham_status_t st;       /* status variable */
00049     ham_db_t *db;          /* hamsterdb database object */
00050     ham_key_t key;         /* the structure for a key */
00051     ham_record_t record;   /* the structure for a record */
00052 
00053     memset(&key, 0, sizeof(key));
00054     memset(&record, 0, sizeof(record));
00055 
00056     /*
00057      * first step: create a new hamsterdb object 
00058      */
00059     st=ham_new(&db);
00060     if (st!=HAM_SUCCESS)
00061         error("ham_new", st);
00062 
00063     /*
00064      * second step: create a new hamsterdb database
00065      *
00066      * we could also use ham_create_ex() if we wanted to specify the 
00067      * page size, key size or cache size limits
00068      */
00069     st=ham_create_ex(db, "test.db", 0, 0664, 0);
00070     if (st!=HAM_SUCCESS)
00071         error("ham_create", st);
00072 
00073     /*
00074      * now we can insert, delete or lookup values in the database
00075      *
00076      * for our test program, we just insert a few values, then look them 
00077      * up, then delete them and try to look them up again (which will fail).
00078      */
00079     for (i=0; i<LOOP; i++) {
00080         key.data=&i;
00081         key.size=sizeof(i);
00082 
00083         record.data=&i;
00084         record.size=sizeof(i);
00085 
00086         st=ham_insert(db, 0, &key, &record, 0);
00087         if (st!=HAM_SUCCESS)
00088             error("ham_insert", st);
00089     }
00090 
00091     /*
00092      * now lookup all values
00093      *
00094      * for ham_find(), we could use the flag HAM_RECORD_USER_ALLOC, if WE
00095      * allocate record.data (otherwise the memory is automatically allocated
00096      * by hamsterdb)
00097      */
00098     for (i=0; i<LOOP; i++) {
00099         key.data=&i;
00100         key.size=sizeof(i);
00101 
00102         st=ham_find(db, 0, &key, &record, 0);
00103         if (st!=HAM_SUCCESS)
00104             error("ham_find", st);
00105 
00106         /*
00107          * check if the value is ok
00108          */
00109         if (*(int *)record.data!=i) {
00110             printf("ham_find() ok, but returned bad value\n");
00111             return (-1);
00112         }
00113     }
00114 
00115     /*
00116      * close the database handle, then re-open it (to demonstrate the 
00117      * call ham_open)
00118      */
00119     st=ham_close(db, 0);
00120     if (st!=HAM_SUCCESS)
00121         error("ham_close", st);
00122     st=ham_open_ex(db, "test.db", 0, 0);
00123     if (st!=HAM_SUCCESS)
00124         error("ham_open", st);
00125 
00126     /*
00127      * now erase all values
00128      */
00129     for (i=0; i<LOOP; i++) {
00130         key.size=sizeof(i);
00131         key.data=&i;
00132 
00133         st=ham_erase(db, 0, &key, 0);
00134         if (st!=HAM_SUCCESS)
00135             error("ham_erase", st);
00136     }
00137 
00138     /*
00139      * once more we try to find all values... every ham_find() call must
00140      * now fail with HAM_KEY_NOT_FOUND
00141      */
00142     for (i=0; i<LOOP; i++) {
00143         key.size=sizeof(i);
00144         key.data=&i;
00145 
00146         st=ham_find(db, 0, &key, &record, 0);
00147         if (st!=HAM_KEY_NOT_FOUND)
00148             error("ham_find", st);
00149     }
00150 
00151     /*
00152      * we're done! close the database handle
00153      */
00154     st=ham_close(db, 0);
00155     if (st!=HAM_SUCCESS)
00156         error("ham_close", st);
00157 
00158     /*
00159      * delete the database object to avoid memory leaks
00160      */
00161     ham_delete(db);
00162 
00163 #if UNDER_CE
00164     error("success", 0);
00165 #endif
00166     printf("success!\n");
00167     return (0);
00168 }
00169 
00170 #if UNDER_CE
00171 int 
00172 _tmain(int argc, _TCHAR* argv[])
00173 {
00174     return (main(0, 0));
00175 }
00176 #endif