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