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