hamsterdb Embedded Database 1.1.13

db4.c

Go to the documentation of this file.
00001 
00018 #include <stdio.h>
00019 #include <string.h>
00020 #include <ham/hamsterdb.h>
00021 
00022 int 
00023 main(int argc, char **argv)
00024 {
00025     ham_status_t st;      /* status variable */
00026     ham_db_t *db;         /* hamsterdb database object */
00027     ham_cursor_t *cursor; /* a database cursor */
00028     char line[1024*4];    /* a buffer for reading lines */
00029     ham_key_t key;
00030     ham_record_t record;
00031 
00032     memset(&key, 0, sizeof(key));
00033     memset(&record, 0, sizeof(record));
00034 
00035     printf("This sample uses hamsterdb to list all words in the "
00036             "original order.\n");
00037     printf("Reading from stdin...\n");
00038 
00039     /*
00040      * first step: create a new hamsterdb object 
00041      */
00042     st=ham_new(&db);
00043     if (st!=HAM_SUCCESS) {
00044         printf("ham_new() failed with error %d\n", st);
00045         return (-1);
00046     }
00047 
00048     /*
00049      * second step: create a new hamsterdb "record number" Database
00050      *
00051      * we could create an in-memory-database to speed up the sorting.
00052      */
00053     st=ham_create(db, "test.db", HAM_RECORD_NUMBER, 0664);
00054     if (st!=HAM_SUCCESS) {
00055         printf("ham_create() failed with error %d\n", st);
00056         return (-1);
00057     }
00058 
00059     /*
00060      * now we read each line from stdin and split it in words; then each 
00061      * word is inserted into the database
00062      */
00063     while (fgets(line, sizeof(line), stdin)) {
00064         char *start=line, *p;
00065 
00066         /*
00067          * strtok is not the best function because it's not threadsafe
00068          * and not flexible, but it's good enough for this example.
00069          */
00070         while ((p=strtok(start, " \t\r\n"))) {
00071             ham_u64_t recno;
00072 
00073             key.flags=HAM_KEY_USER_ALLOC;
00074             key.data=&recno;
00075             key.size=sizeof(recno);
00076 
00077             record.data=p;
00078             record.size=(ham_size_t)strlen(p)+1; /* also store terminating 0 */
00079 
00080             st=ham_insert(db, 0, &key, &record, 0);
00081             if (st!=HAM_SUCCESS && st!=HAM_DUPLICATE_KEY) {
00082                 printf("ham_insert() failed with error %d\n", st);
00083                 return (-1);
00084             }
00085             printf(".");
00086 
00087             start=0;
00088         }
00089     }
00090 
00091     /* 
00092      * create a cursor 
00093      */
00094     st=ham_cursor_create(db, 0, 0, &cursor);
00095     if (st!=HAM_SUCCESS) {
00096         printf("ham_cursor_create() failed with error %d\n", st);
00097         return (-1);
00098     }
00099 
00100     /*
00101      * iterate over all items and print the records
00102      */
00103     while (1) {
00104         st=ham_cursor_move(cursor, &key, &record, HAM_CURSOR_NEXT);
00105         if (st!=HAM_SUCCESS) {
00106             /* reached end of the database? */
00107             if (st==HAM_KEY_NOT_FOUND)
00108                 break;
00109             else {
00110                 printf("ham_cursor_next() failed with error %d\n", st);
00111                 return (-1);
00112             }
00113         }
00114 
00115         /* 
00116          * print the record number and the word 
00117          */
00118 #if WIN32
00119         printf("%I64u: %s\n", *(ham_u64_t *)key.data, 
00120                 (const char *)record.data);
00121 #else
00122         printf("%llu: %s\n", *(unsigned long long *)key.data, 
00123                 (const char *)record.data);
00124 #endif
00125     }
00126 
00127     /*
00128      * then close the database handle; the flag
00129      * HAM_AUTO_CLEANUP will automatically close all cursors, and we
00130      * do not need to call ham_cursor_close
00131      */
00132     st=ham_close(db, HAM_AUTO_CLEANUP);
00133     if (st!=HAM_SUCCESS) {
00134         printf("ham_close() failed with error %d\n", st);
00135         return (-1);
00136     }
00137 
00138     /*
00139      * delete the database object to avoid memory leaks
00140      */
00141     ham_delete(db);
00142 
00143     /* 
00144      * success!
00145      */
00146     return (0);
00147 }
00148