upscaledb  2.2.1
db2.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2016 Christoph Rupp (chris@crupp.de).
3  * All Rights Reserved.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * See the file COPYING for License information.
16  */
17 
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h> /* for exit() */
27 #include <ups/upscaledb.h>
28 
29 void
30 error(const char *foo, ups_status_t st) {
31  printf("%s() returned error %d: %s\n", foo, st, ups_strerror(st));
32  exit(-1);
33 }
34 
35 void
36 usage() {
37  printf("usage: ./db2 <environment> <source-db> <destination-db>\n");
38  exit(-1);
39 }
40 
41 void
42 copy_db(ups_db_t *source, ups_db_t *dest) {
43  ups_cursor_t *cursor; /* upscaledb cursor object */
44  ups_status_t st;
45  ups_key_t key;
46  ups_record_t rec;
47 
48  memset(&key, 0, sizeof(key));
49  memset(&rec, 0, sizeof(rec));
50 
51  /* create a new cursor */
52  st = ups_cursor_create(&cursor, source, 0, 0);
53  if (st)
54  error("ups_cursor_create", st);
55 
56  /* get a cursor to the source database */
57  st = ups_cursor_move(cursor, &key, &rec, UPS_CURSOR_FIRST);
58  if (st == UPS_KEY_NOT_FOUND) {
59  printf("database is empty!\n");
60  return;
61  }
62  else if (st)
63  error("ups_cursor_move", st);
64 
65  do {
66  /* insert this element into the new database */
67  st = ups_db_insert(dest, 0, &key, &rec, UPS_DUPLICATE);
68  if (st)
69  error("ups_db_insert", st);
70 
71  /* give some feedback to the user */
72  printf(".");
73 
74  /* fetch the next item, and repeat till we've reached the end
75  * of the database */
76  st = ups_cursor_move(cursor, &key, &rec, UPS_CURSOR_NEXT);
77  if (st && st != UPS_KEY_NOT_FOUND)
78  error("ups_cursor_move", st);
79 
80  } while (st == 0);
81 
82  /* clean up and return */
83  ups_cursor_close(cursor);
84 }
85 
86 int
87 main(int argc, char **argv) {
88  ups_status_t st;
89  ups_env_t *env = 0;
90  ups_db_t *src_db = 0;
91  ups_db_t *dest_db = 0;
92  uint16_t src_name;
93  uint16_t dest_name;
94  const char *env_path = 0;
95 
96  /* check and parse the command line parameters */
97  if (argc != 4)
98  usage();
99  env_path = argv[1];
100  src_name = atoi(argv[2]);
101  dest_name = atoi(argv[3]);
102  if (src_name == 0 || dest_name == 0)
103  usage();
104 
105  /* open the Environment */
106  st = ups_env_open(&env, env_path, 0, 0);
107  if (st)
108  error("ups_env_open", st);
109 
110  /* open the source database */
111  st = ups_env_open_db(env, &src_db, src_name, 0, 0);
112  if (st)
113  error("ups_env_open_db", st);
114 
115  /* create the destination database */
116  st = ups_env_create_db(env, &dest_db, dest_name,
118  if (st)
119  error("ups_env_create_db", st);
120 
121  /* copy the data */
122  copy_db(src_db, dest_db);
123 
124  /* clean up and return */
125  st = ups_env_close(env, UPS_AUTO_CLEANUP);
126  if (st)
127  error("ups_env_close", st);
128 
129  printf("\nsuccess!\n");
130  return (0);
131 }
132 
int main(int argc, char **argv)
Definition: db2.c:87
UPS_EXPORT ups_status_t UPS_CALLCONV ups_env_open(ups_env_t **env, const char *filename, uint32_t flags, const ups_parameter_t *param)
UPS_EXPORT const char *UPS_CALLCONV ups_strerror(ups_status_t status)
unsigned short uint16_t
Definition: msstdint.h:84
void error(const char *foo, ups_status_t st)
Definition: db2.c:30
UPS_EXPORT ups_status_t UPS_CALLCONV ups_env_close(ups_env_t *env, uint32_t flags)
UPS_EXPORT ups_status_t UPS_CALLCONV ups_db_insert(ups_db_t *db, ups_txn_t *txn, ups_key_t *key, ups_record_t *record, uint32_t flags)
void usage()
Definition: db2.c:36
UPS_EXPORT ups_status_t UPS_CALLCONV ups_cursor_move(ups_cursor_t *cursor, ups_key_t *key, ups_record_t *record, uint32_t flags)
Include file for upscaledb embedded database.
struct ups_db_t ups_db_t
Definition: upscaledb.h:156
#define UPS_CURSOR_NEXT
Definition: upscaledb.h:2051
struct ups_cursor_t ups_cursor_t
Definition: upscaledb.h:177
#define UPS_AUTO_CLEANUP
Definition: upscaledb.h:1883
UPS_EXPORT ups_status_t UPS_CALLCONV ups_cursor_close(ups_cursor_t *cursor)
#define UPS_ENABLE_DUPLICATE_KEYS
Definition: upscaledb.h:1309
void copy_db(ups_db_t *source, ups_db_t *dest)
Definition: db2.c:42
UPS_EXPORT ups_status_t UPS_CALLCONV ups_env_open_db(ups_env_t *env, ups_db_t **db, uint16_t name, uint32_t flags, const ups_parameter_t *params)
#define UPS_CURSOR_FIRST
Definition: upscaledb.h:2045
#define UPS_DUPLICATE
Definition: upscaledb.h:1566
int ups_status_t
Definition: types.h:139
UPS_EXPORT ups_status_t UPS_CALLCONV ups_env_create_db(ups_env_t *env, ups_db_t **db, uint16_t name, uint32_t flags, const ups_parameter_t *params)
UPS_EXPORT ups_status_t UPS_CALLCONV ups_cursor_create(ups_cursor_t **cursor, ups_db_t *db, ups_txn_t *txn, uint32_t flags)
#define UPS_KEY_NOT_FOUND
Definition: upscaledb.h:361
struct ups_env_t ups_env_t
Definition: upscaledb.h:165