hamsterdb Embedded Database 1.1.13
|
00001 00017 #include <stdio.h> 00018 #include <string.h> 00019 #include <ham/hamsterdb.h> 00020 00021 static int 00022 my_string_compare(ham_db_t *db, const ham_u8_t *lhs, ham_size_t lhs_length, 00023 const ham_u8_t *rhs, ham_size_t rhs_length) 00024 { 00025 (void)db; 00026 00027 return strncmp((const char *)lhs, (const char *)rhs, 00028 lhs_length<rhs_length ? lhs_length : rhs_length); 00029 } 00030 00031 int 00032 main(int argc, char **argv) 00033 { 00034 ham_status_t st; /* status variable */ 00035 ham_db_t *db; /* hamsterdb database object */ 00036 ham_cursor_t *cursor; /* a database cursor */ 00037 char line[1024*4]; /* a buffer for reading lines */ 00038 ham_key_t key; 00039 ham_record_t record; 00040 00041 memset(&key, 0, sizeof(key)); 00042 memset(&record, 0, sizeof(record)); 00043 00044 printf("This sample uses hamsterdb to sort data.\n"); 00045 printf("Reading from stdin...\n"); 00046 00047 /* 00048 * first step: create a new hamsterdb handle 00049 */ 00050 st=ham_new(&db); 00051 if (st!=HAM_SUCCESS) { 00052 printf("ham_new() failed with error %d\n", st); 00053 return (-1); 00054 } 00055 00056 /* 00057 * second step: since we use strings as our database-keys, we use 00058 * our own comparison function based on strcmp instead of the default 00059 * memcmp function. 00060 */ 00061 st=ham_set_compare_func(db, my_string_compare); 00062 if (st) { 00063 printf("ham_set_compare_func() failed with error %d\n", st); 00064 return (-1); 00065 } 00066 00067 /* 00068 * third step: create a new hamsterdb database 00069 * 00070 * we could create an in-memory-database to speed up the sorting. 00071 */ 00072 st=ham_create_ex(db, "test.db", 0, 0664, 0); 00073 if (st!=HAM_SUCCESS) { 00074 printf("ham_create() failed with error %d\n", st); 00075 return (-1); 00076 } 00077 00078 /* 00079 * now we 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_insert(db, 0, &key, &record, 0); 00095 if (st!=HAM_SUCCESS && st!=HAM_DUPLICATE_KEY) { 00096 printf("ham_insert() failed with error %d\n", st); 00097 return (-1); 00098 } 00099 printf("."); 00100 00101 start=0; 00102 } 00103 } 00104 00105 /* 00106 * create a cursor 00107 */ 00108 st=ham_cursor_create(db, 0, 0, &cursor); 00109 if (st!=HAM_SUCCESS) { 00110 printf("ham_cursor_create() failed with error %d\n", st); 00111 return (-1); 00112 } 00113 00114 /* 00115 * iterate over all items with HAM_CURSOR_NEXT, and print the words 00116 */ 00117 while (1) { 00118 st=ham_cursor_move(cursor, &key, &record, HAM_CURSOR_NEXT); 00119 if (st!=HAM_SUCCESS) { 00120 /* reached end of the database? */ 00121 if (st==HAM_KEY_NOT_FOUND) 00122 break; 00123 else { 00124 printf("ham_cursor_next() failed with error %d\n", st); 00125 return (-1); 00126 } 00127 } 00128 00129 /* print the word */ 00130 printf("%s\n", (const char *)key.data); 00131 } 00132 00133 /* 00134 * then close the database handle; the flag 00135 * HAM_AUTO_CLEANUP will automatically close all cursors, and we 00136 * do not need to call ham_cursor_close 00137 */ 00138 st=ham_close(db, HAM_AUTO_CLEANUP); 00139 if (st!=HAM_SUCCESS) { 00140 printf("ham_close() failed with error %d\n", st); 00141 return (-1); 00142 } 00143 00144 /* 00145 * delete the database object to avoid memory leaks 00146 */ 00147 ham_delete(db); 00148 00149 /* 00150 * success! 00151 */ 00152 return (0); 00153 } 00154