hamsterdb Embedded Database  2.1.7
db1.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 
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h> /* for exit() */
25 #include <ham/hamsterdb.h>
26 
27 #define LOOP 10
28 #define DATABASE_NAME 1
29 
30 void
31 error(const char *foo, ham_status_t st) {
32  printf("%s() returned error %d: %s\n", foo, st, ham_strerror(st));
33  exit(-1);
34 }
35 
36 int
37 main(int argc, char **argv) {
38  ham_u32_t i;
39  ham_status_t st; /* status variable */
40  ham_env_t *env; /* hamsterdb environment object */
41  ham_db_t *db; /* hamsterdb database object */
42  ham_key_t key = {0}; /* the structure for a key */
43  ham_record_t record = {0}; /* the structure for a record */
44  ham_parameter_t params[] = { /* parameters for ham_env_create_db */
47  {0, }
48  };
49 
50  /* First create a new hamsterdb Environment */
51  st = ham_env_create(&env, "test.db", 0, 0664, 0);
52  if (st != HAM_SUCCESS)
53  error("ham_create", st);
54 
55  /* And in this Environment we create a new Database for uint32-keys
56  * and uint32-records. */
57  st = ham_env_create_db(env, &db, DATABASE_NAME, 0, &params[0]);
58  if (st != HAM_SUCCESS)
59  error("ham_create", st);
60 
61  /*
62  * now we can insert, delete or lookup values in the database
63  *
64  * for our test program, we just insert a few values, then look them
65  * up, then delete them and try to look them up again (which will fail).
66  */
67  for (i = 0; i < LOOP; i++) {
68  key.data = &i;
69  key.size = sizeof(i);
70 
71  record.size = key.size;
72  record.data = key.data;
73 
74  st = ham_db_insert(db, 0, &key, &record, 0);
75  if (st != HAM_SUCCESS)
76  error("ham_db_insert", st);
77  }
78 
79  /*
80  * now lookup all values
81  *
82  * for ham_db_find(), we could use the flag HAM_RECORD_USER_ALLOC, if WE
83  * allocate record.data (otherwise the memory is automatically allocated
84  * by hamsterdb)
85  */
86  for (i = 0; i < LOOP; i++) {
87  key.data = &i;
88  key.size = sizeof(i);
89 
90  st = ham_db_find(db, 0, &key, &record, 0);
91  if (st != HAM_SUCCESS)
92  error("ham_db_find", st);
93 
94  /*
95  * check if the value is ok
96  */
97  if (*(int *)record.data != i) {
98  printf("ham_db_find() ok, but returned bad value\n");
99  return (-1);
100  }
101  }
102 
103  /*
104  * close the database handle, then re-open it (to demonstrate how to open
105  * an Environment and a Database)
106  */
107  st = ham_db_close(db, 0);
108  if (st != HAM_SUCCESS)
109  error("ham_db_close", st);
110  st = ham_env_close(env, 0);
111  if (st != HAM_SUCCESS)
112  error("ham_env_close", st);
113 
114  st = ham_env_open(&env, "test.db", 0, 0);
115  if (st != HAM_SUCCESS)
116  error("ham_env_open", st);
117  st = ham_env_open_db(env, &db, DATABASE_NAME, 0, 0);
118  if (st != HAM_SUCCESS)
119  error("ham_env_open_db", st);
120 
121  /* now erase all values */
122  for (i = 0; i < LOOP; i++) {
123  key.size = sizeof(i);
124  key.data = &i;
125 
126  st = ham_db_erase(db, 0, &key, 0);
127  if (st != HAM_SUCCESS)
128  error("ham_db_erase", st);
129  }
130 
131  /*
132  * once more we try to find all values... every ham_db_find() call must
133  * now fail with HAM_KEY_NOT_FOUND
134  */
135  for (i = 0; i < LOOP; i++) {
136  key.size = sizeof(i);
137  key.data = &i;
138 
139  st = ham_db_find(db, 0, &key, &record, 0);
140  if (st != HAM_KEY_NOT_FOUND)
141  error("ham_db_find", st);
142  }
143 
144  /* we're done! close the handles. HAM_AUTO_CLEANUP will also close the
145  * 'db' handle */
146  st = ham_env_close(env, HAM_AUTO_CLEANUP);
147  if (st != HAM_SUCCESS)
148  error("ham_env_close", st);
149 
150  printf("success!\n");
151  return (0);
152 }
153