hamsterdb Embedded Database  2.1.7
db2.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 
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h> /* for exit() */
26 #include <ham/hamsterdb.h>
27 
28 void
29 error(const char *foo, ham_status_t st) {
30  printf("%s() returned error %d: %s\n", foo, st, ham_strerror(st));
31  exit(-1);
32 }
33 
34 void
35 usage() {
36  printf("usage: ./db2 <environment> <source-db> <destination-db>\n");
37  exit(-1);
38 }
39 
40 void
41 copy_db(ham_db_t *source, ham_db_t *dest) {
42  ham_cursor_t *cursor; /* hamsterdb cursor object */
43  ham_status_t st;
44  ham_key_t key;
45  ham_record_t rec;
46 
47  memset(&key, 0, sizeof(key));
48  memset(&rec, 0, sizeof(rec));
49 
50  /* create a new cursor */
51  st = ham_cursor_create(&cursor, source, 0, 0);
52  if (st)
53  error("ham_cursor_create", st);
54 
55  /* get a cursor to the source database */
56  st = ham_cursor_move(cursor, &key, &rec, HAM_CURSOR_FIRST);
57  if (st == HAM_KEY_NOT_FOUND) {
58  printf("database is empty!\n");
59  return;
60  }
61  else if (st)
62  error("ham_cursor_move", st);
63 
64  do {
65  /* insert this element into the new database */
66  st = ham_db_insert(dest, 0, &key, &rec, HAM_DUPLICATE);
67  if (st)
68  error("ham_db_insert", st);
69 
70  /* give some feedback to the user */
71  printf(".");
72 
73  /* fetch the next item, and repeat till we've reached the end
74  * of the database */
75  st = ham_cursor_move(cursor, &key, &rec, HAM_CURSOR_NEXT);
76  if (st && st != HAM_KEY_NOT_FOUND)
77  error("ham_cursor_move", st);
78 
79  } while (st == 0);
80 
81  /* clean up and return */
82  ham_cursor_close(cursor);
83 }
84 
85 int
86 main(int argc, char **argv) {
87  ham_status_t st;
88  ham_env_t *env = 0;
89  ham_db_t *src_db = 0;
90  ham_db_t *dest_db = 0;
91  ham_u16_t src_name;
92  ham_u16_t dest_name;
93  const char *env_path = 0;
94 
95  /* check and parse the command line parameters */
96  if (argc != 4)
97  usage();
98  env_path = argv[1];
99  src_name = atoi(argv[2]);
100  dest_name = atoi(argv[3]);
101  if (src_name == 0 || dest_name == 0)
102  usage();
103 
104  /* open the Environment */
105  st = ham_env_open(&env, env_path, 0, 0);
106  if (st)
107  error("ham_env_open", st);
108 
109  /* open the source database */
110  st = ham_env_open_db(env, &src_db, src_name, 0, 0);
111  if (st)
112  error("ham_env_open_db", st);
113 
114  /* create the destination database */
115  st = ham_env_create_db(env, &dest_db, dest_name,
117  if (st)
118  error("ham_env_create_db", st);
119 
120  /* copy the data */
121  copy_db(src_db, dest_db);
122 
123  /* clean up and return */
124  st = ham_env_close(env, HAM_AUTO_CLEANUP);
125  if (st)
126  error("ham_env_close", st);
127 
128  printf("\nsuccess!\n");
129  return (0);
130 }
131