client1.c

Go to the documentation of this file.
00001 
00016 #include <stdio.h>
00017 #include <string.h>
00018 #include <stdlib.h> /* for exit() */
00019 #include <ham/hamsterdb.h>
00020 
00021 #define LOOP 10
00022 
00023 void 
00024 error(const char *foo, ham_status_t st)
00025 {
00026     printf("%s() returned error %d: %s\n", foo, st, ham_strerror(st));
00027     exit(-1);
00028 }
00029 
00030 int 
00031 main(int argc, char **argv)
00032 {
00033     int i;
00034     ham_status_t st;       /* status variable */
00035     ham_env_t *env;        /* hamsterdb Environment object */
00036     ham_db_t *db;          /* hamsterdb Database object */
00037     ham_key_t key;         /* the structure for a key */
00038     ham_record_t record;   /* the structure for a record */
00039 
00040     memset(&key, 0, sizeof(key));
00041     memset(&record, 0, sizeof(record));
00042 
00043     st=ham_new(&db);
00044     if (st!=HAM_SUCCESS)
00045         error("ham_new", st);
00046     st=ham_env_new(&env);
00047     if (st!=HAM_SUCCESS)
00048         error("ham_env_new", st);
00049 
00050     /*
00051      * now connect to the server which should listen at 8080
00052      *
00053      * ham_env_create_ex() will not really create a new Environment but rather 
00054      * connect to an already existing one
00055      */
00056     st=ham_env_create_ex(env, "http://localhost:8080/env1.db", 0, 0, 0);
00057     if (st!=HAM_SUCCESS)
00058         error("ham_env_create_ex", st);
00059 
00060     /*
00061      * now open a Database in this Environment
00062      */
00063     st=ham_env_open_db(env, db, 13, 0, 0);
00064     if (st!=HAM_SUCCESS)
00065         error("ham_env_open_db", st);
00066 
00067     /*
00068      * now we can insert, delete or lookup values in the database
00069      */
00070     for (i=0; i<LOOP; i++) {
00071         key.data=&i;
00072         key.size=sizeof(i);
00073 
00074         record.size=key.size;
00075         record.data=key.data;
00076 
00077         st=ham_insert(db, 0, &key, &record, 0);
00078         if (st!=HAM_SUCCESS)
00079             error("ham_insert", st);
00080     }
00081 
00082     /*
00083      * now lookup all values
00084      */
00085     for (i=0; i<LOOP; i++) {
00086         key.data=&i;
00087         key.size=sizeof(i);
00088 
00089         st=ham_find(db, 0, &key, &record, 0);
00090         if (st!=HAM_SUCCESS)
00091             error("ham_find", st);
00092 
00093         /*
00094          * check if the value is ok
00095          */
00096         if (*(int *)record.data!=i) {
00097             printf("ham_find() ok, but returned bad value\n");
00098             return (-1);
00099         }
00100     }
00101 
00102     /*
00103      * erase everything
00104      */
00105     for (i=0; i<LOOP; i++) {
00106         key.data=&i;
00107         key.size=sizeof(i);
00108 
00109         st=ham_erase(db, 0, &key, 0);
00110         if (st!=HAM_SUCCESS)
00111             error("ham_erase", st);
00112     }
00113 
00114     /*
00115      * and make sure that the database is empty
00116      */
00117     for (i=0; i<LOOP; i++) {
00118         key.data=&i;
00119         key.size=sizeof(i);
00120 
00121         st=ham_find(db, 0, &key, &record, 0);
00122         if (st!=HAM_KEY_NOT_FOUND)
00123             error("ham_find", st);
00124     }
00125 
00126     /*
00127      * close the database handle
00128      */
00129     st=ham_close(db, 0);
00130     if (st!=HAM_SUCCESS)
00131         error("ham_close", st);
00132 
00133     /*
00134      * delete the database object to avoid memory leaks
00135      */
00136     ham_delete(db);
00137 
00138     printf("success!\n");
00139     return (0);
00140 }
00141 
Generated by  doxygen 1.6.3