hamsterdb Embedded Database 1.1.13

db5.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     unsigned lineno=0;    /* the current line number */
00030     ham_key_t key;
00031     ham_record_t record;
00032 
00033     memset(&key, 0, sizeof(key));
00034     memset(&record, 0, sizeof(record));
00035 
00036     printf("This sample uses hamsterdb and duplicate keys to list all words "
00037             "in the\noriginal order, together with their line number.\n");
00038     printf("Reading from stdin...\n");
00039 
00040     /*
00041      * first step: create a new hamsterdb object 
00042      */
00043     st=ham_new(&db);
00044     if (st!=HAM_SUCCESS) {
00045         printf("ham_new() failed with error %d\n", st);
00046         return (-1);
00047     }
00048 
00049     /*
00050      * second step: create a new Database with support for duplicate keys
00051      *
00052      * we could create an in-memory-database to speed up the sorting.
00053      */
00054     st=ham_create_ex(db, "test.db", HAM_ENABLE_DUPLICATES, 0664, 0);
00055     if (st!=HAM_SUCCESS) {
00056         printf("ham_create_ex() failed with error %d\n", st);
00057         return (-1);
00058     }
00059 
00060     /*
00061      * now we read each line from stdin and split it in words; then each 
00062      * word is inserted into the database
00063      */
00064     while (fgets(line, sizeof(line), stdin)) {
00065         char *start=line, *p;
00066         lineno++;
00067 
00068         /*
00069          * strtok is not the best function because it's not threadsafe
00070          * and not flexible, but it's good enough for this example.
00071          */
00072         while ((p=strtok(start, " \t\r\n"))) {
00073             key.data=p;
00074             key.size=(ham_size_t)strlen(p)+1; /* also store the terminating 
00075                                                  0-byte */
00076             record.data=&lineno;
00077             record.size=sizeof(lineno);
00078 
00079             st=ham_insert(db, 0, &key, &record, HAM_DUPLICATE);
00080             if (st!=HAM_SUCCESS) {
00081                 printf("ham_insert() failed with error %d\n", st);
00082                 return (-1);
00083             }
00084             printf(".");
00085 
00086             start=0;
00087         }
00088     }
00089 
00090     /* 
00091      * create a cursor 
00092      */
00093     st=ham_cursor_create(db, 0, 0, &cursor);
00094     if (st!=HAM_SUCCESS) {
00095         printf("ham_cursor_create() failed with error %d\n", st);
00096         return (-1);
00097     }
00098 
00099     /*
00100      * iterate over all items and print them
00101      */
00102     while (1) {
00103         st=ham_cursor_move(cursor, &key, &record, HAM_CURSOR_NEXT);
00104         if (st!=HAM_SUCCESS) {
00105             /* reached end of the database? */
00106             if (st==HAM_KEY_NOT_FOUND)
00107                 break;
00108             else {
00109                 printf("ham_cursor_next() failed with error %d\n", st);
00110                 return (-1);
00111             }
00112         }
00113 
00114         /* 
00115          * print the word and the line number
00116          */
00117         printf("%s: appeared in line %u\n", (const char *)key.data, 
00118                 *(unsigned *)record.data);
00119     }
00120 
00121     /*
00122      * then close the database handle; the flag
00123      * HAM_AUTO_CLEANUP will automatically close all cursors, and we
00124      * do not need to call ham_cursor_close
00125      */
00126     st=ham_close(db, HAM_AUTO_CLEANUP);
00127     if (st!=HAM_SUCCESS) {
00128         printf("ham_close() failed with error %d\n", st);
00129         return (-1);
00130     }
00131 
00132     /*
00133      * delete the database object to avoid memory leaks
00134      */
00135     ham_delete(db);
00136 
00137     /* 
00138      * success!
00139      */
00140     return (0);
00141 }
00142