hamsterdb Embedded Database  2.1.1
db3.c
Go to the documentation of this file.
00001 
00017 #include <stdio.h>
00018 #include <string.h>
00019 #include <ham/hamsterdb.h>
00020 
00021 #define DATABASE_NAME       1
00022 
00023 static int
00024 my_string_compare(ham_db_t *db, const ham_u8_t *lhs, ham_size_t lhs_length,
00025           const ham_u8_t *rhs, ham_size_t rhs_length) {
00026   int s = strncmp((const char *)lhs, (const char *)rhs,
00027       lhs_length < rhs_length ? lhs_length : rhs_length);
00028   if (s < 0)
00029     return -1;
00030   if (s > 0)
00031     return +1;
00032   return 0;
00033 }
00034 
00035 int
00036 main(int argc, char **argv) {
00037   ham_status_t st;    /* status variable */
00038   ham_env_t *env;     /* hamsterdb environment object */
00039   ham_db_t *db;     /* hamsterdb database object */
00040   ham_cursor_t *cursor; /* a database cursor */
00041   char line[1024 * 4];  /* a buffer for reading lines */
00042   ham_key_t key;
00043   ham_record_t record;
00044 
00045   memset(&key, 0, sizeof(key));
00046   memset(&record, 0, sizeof(record));
00047 
00048   printf("This sample uses hamsterdb to sort data.\n");
00049   printf("Reading from stdin...\n");
00050 
00051   /*
00052    * Create a new hamsterdb Environment.
00053    * We could create an In-Memory-Environment to speed up the sorting.
00054    */
00055   st = ham_env_create(&env, "test.db", 0, 0664, 0);
00056   if (st != HAM_SUCCESS) {
00057     printf("ham_env_create() failed with error %d\n", st);
00058     return (-1);
00059   }
00060 
00061   st = ham_env_create_db(env, &db, DATABASE_NAME,
00062           HAM_ENABLE_EXTENDED_KEYS | HAM_ENABLE_DUPLICATES, 0);
00063   if (st != HAM_SUCCESS) {
00064     printf("ham_env_create_db() failed with error %d\n", st);
00065     return (-1);
00066   }
00067 
00068   /*
00069    * Since we use strings as our database keys we use our own comparison
00070    * function based on strcmp instead of the default memcmp function.
00071    */
00072   st = ham_db_set_compare_func(db, my_string_compare);
00073   if (st) {
00074     printf("ham_set_compare_func() failed with error %d\n", st);
00075     return (-1);
00076   }
00077 
00078   /*
00079    * Now read each line from stdin and split it in words; then each
00080    * word is inserted into the database
00081    */
00082   while (fgets(line, sizeof(line), stdin)) {
00083     char *start = line, *p;
00084 
00085     /*
00086      * strtok is not the best function because it's not threadsafe
00087      * and not flexible, but it's good enough for this example.
00088      */
00089     while ((p = strtok(start, " \t\r\n"))) {
00090       key.data = p;
00091       key.size = (ham_size_t)strlen(p) + 1; /* also store the terminating
00092                            * 0-byte */
00093 
00094       st = ham_db_insert(db, 0, &key, &record, 0);
00095       if (st != HAM_SUCCESS && st!=HAM_DUPLICATE_KEY) {
00096         printf("ham_db_insert() failed with error %d\n", st);
00097         return (-1);
00098       }
00099       printf(".");
00100 
00101       start = 0;
00102     }
00103   }
00104 
00105   /* create a cursor */
00106   st = ham_cursor_create(&cursor, db, 0, 0);
00107   if (st != HAM_SUCCESS) {
00108     printf("ham_cursor_create() failed with error %d\n", st);
00109     return (-1);
00110   }
00111 
00112   /* iterate over all items with HAM_CURSOR_NEXT, and print the words */
00113   while (1) {
00114     st = ham_cursor_move(cursor, &key, &record, HAM_CURSOR_NEXT);
00115     if (st != HAM_SUCCESS) {
00116       /* reached end of the database? */
00117       if (st == HAM_KEY_NOT_FOUND)
00118         break;
00119       else {
00120         printf("ham_cursor_next() failed with error %d\n", st);
00121         return (-1);
00122       }
00123     }
00124 
00125     /* print the word */
00126     printf("%s\n", (const char *)key.data);
00127   }
00128 
00129   /*
00130    * Then close the handles; the flag HAM_AUTO_CLEANUP will automatically
00131    * close all database and cursors, and we do not need to call
00132    * ham_cursor_close and ham_db_close
00133    */
00134   st = ham_env_close(env, HAM_AUTO_CLEANUP);
00135   if (st != HAM_SUCCESS) {
00136     printf("ham_env_close() failed with error %d\n", st);
00137     return (-1);
00138   }
00139 
00140   /* success! */
00141   return (0);
00142 }
00143