hamsterdb Embedded Database  2.1.7
client1.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 1000
28 
29 void
30 error(const char *foo, ham_status_t st) {
31  printf("%s() returned error %d: %s\n", foo, st, ham_strerror(st));
32  exit(-1);
33 }
34 
35 int
36 main(int argc, char **argv) {
37  int i;
38  ham_status_t st; /* status variable */
39  ham_env_t *env; /* hamsterdb Environment object */
40  ham_db_t *db; /* hamsterdb Database object */
41  ham_key_t key = {0}; /* the structure for a key */
42  ham_record_t record = {0}; /* the structure for a record */
43 
44  /*
45  * Connect to the server which should listen at 8080. The server is
46  * implemented in server1.c.
47  */
48  st = ham_env_create(&env, "ham://localhost:8080/env1.db", 0, 0, 0);
49  if (st != HAM_SUCCESS)
50  error("ham_env_create", st);
51 
52  /* now open a Database in this Environment */
53  st = ham_env_open_db(env, &db, 13, 0, 0);
54  if (st != HAM_SUCCESS)
55  error("ham_env_open_db", st);
56 
57  /* now we can insert, delete or lookup values in the database */
58  for (i = 0; i < LOOP; i++) {
59  key.data = &i;
60  key.size = sizeof(i);
61 
62  record.size = key.size;
63  record.data = key.data;
64 
65  st = ham_db_insert(db, 0, &key, &record, 0);
66  if (st != HAM_SUCCESS)
67  error("ham_db_insert", st);
68  }
69 
70  /* now lookup all values */
71  for (i = 0; i < LOOP; i++) {
72  key.data = &i;
73  key.size = sizeof(i);
74 
75  st = ham_db_find(db, 0, &key, &record, 0);
76  if (st != HAM_SUCCESS)
77  error("ham_db_find", st);
78 
79  /* check if the value is ok */
80  if (*(int *)record.data != i) {
81  printf("ham_db_find() ok, but returned bad value\n");
82  return (-1);
83  }
84  }
85 
86  /* erase everything */
87  for (i = 0; i < LOOP; i++) {
88  key.data = &i;
89  key.size = sizeof(i);
90 
91  st = ham_db_erase(db, 0, &key, 0);
92  if (st != HAM_SUCCESS)
93  error("ham_db_erase", st);
94  }
95 
96  /* and make sure that the database is empty */
97  for (i = 0; i < LOOP; i++) {
98  key.data = &i;
99  key.size = sizeof(i);
100 
101  st = ham_db_find(db, 0, &key, &record, 0);
102  if (st != HAM_KEY_NOT_FOUND)
103  error("ham_db_find", st);
104  }
105 
106  /* close the database handle */
107  st = ham_db_close(db, 0);
108  if (st != HAM_SUCCESS)
109  error("ham_db_close", st);
110 
111  /* close the environment handle */
112  st = ham_env_close(env, 0);
113  if (st != HAM_SUCCESS)
114  error("ham_env_close", st);
115 
116  printf("success!\n");
117  return (0);
118 }
119