hamsterdb Embedded Database  2.1.7
db4.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2014 Christoph Rupp (chris@crupp.de).
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
24 #include <stdio.h>
25 #include <string.h>
26 #include <ham/hamsterdb.h>
27 
28 #define DATABASE_NAME 1
29 
30 int
31 main(int argc, char **argv) {
32  ham_status_t st; /* status variable */
33  ham_env_t *env; /* hamsterdb environment object */
34  ham_db_t *db; /* hamsterdb database object */
35  ham_cursor_t *cursor; /* a database cursor */
36  char line[1024 * 4]; /* a buffer for reading lines */
37  ham_key_t key;
38  ham_record_t record;
39 
40  memset(&key, 0, sizeof(key));
41  memset(&record, 0, sizeof(record));
42 
43  printf("This sample uses hamsterdb to list all words in the "
44  "original order.\n");
45  printf("Reading from stdin...\n");
46 
47  /*
48  * Create a new hamsterdb "record number" Database.
49  * We could create an in-memory-Environment to speed up the sorting.
50  */
51  st = ham_env_create(&env, "test.db", 0, 0664, 0);
52  if (st != HAM_SUCCESS) {
53  printf("ham_env_create() failed with error %d\n", st);
54  return (-1);
55  }
56 
58  if (st != HAM_SUCCESS) {
59  printf("ham_env_create_db() failed with error %d\n", st);
60  return (-1);
61  }
62 
63  /*
64  * Now read each line from stdin and split it in words; then each
65  * word is inserted into the database
66  */
67  while (fgets(line, sizeof(line), stdin)) {
68  char *start = line, *p;
69 
70  /*
71  * strtok is not the best function because it's not threadsafe
72  * and not flexible, but it's good enough for this example.
73  */
74  while ((p = strtok(start, " \t\r\n"))) {
75  ham_u64_t recno;
76 
78  key.data = &recno;
79  key.size = sizeof(recno);
80 
81  record.data = p;
82  record.size = (ham_u32_t)strlen(p) + 1; /* also store
83  * terminating 0 */
84 
85  st = ham_db_insert(db, 0, &key, &record, 0);
86  if (st != HAM_SUCCESS && st != HAM_DUPLICATE_KEY) {
87  printf("ham_db_insert() failed with error %d\n", st);
88  return (-1);
89  }
90  printf(".");
91 
92  start = 0;
93  }
94  }
95 
96  /* Create a cursor */
97  st = ham_cursor_create(&cursor, db, 0, 0);
98  if (st != HAM_SUCCESS) {
99  printf("ham_cursor_create() failed with error %d\n", st);
100  return (-1);
101  }
102 
103  memset(&key, 0, sizeof(key));
104 
105  /* Iterate over all items and print the records */
106  while (1) {
107  st = ham_cursor_move(cursor, &key, &record, HAM_CURSOR_NEXT);
108  if (st != HAM_SUCCESS) {
109  /* reached end of the database? */
110  if (st == HAM_KEY_NOT_FOUND)
111  break;
112  else {
113  printf("ham_cursor_next() failed with error %d\n", st);
114  return (-1);
115  }
116  }
117 
118  /* print the record number and the word */
119 #ifdef WIN32
120  printf("%I64u: %s\n", *(ham_u64_t *)key.data,
121  (const char *)record.data);
122 #else
123  printf("%llu: %s\n", *(unsigned long long *)key.data,
124  (const char *)record.data);
125 #endif
126  }
127 
128  /*
129  * Then close the handles; the flag HAM_AUTO_CLEANUP will automatically
130  * close all databases and cursors and we do not need to
131  * call ham_cursor_close and ham_db_close
132  */
133  st = ham_env_close(env, HAM_AUTO_CLEANUP);
134  if (st != HAM_SUCCESS) {
135  printf("ham_env_close() failed with error %d\n", st);
136  return (-1);
137  }
138 
139  /* success! */
140  return (0);
141 }
142