hamsterdb Embedded Database 1.1.15
db2.c
Go to the documentation of this file.
00001 
00016 #include <stdio.h>
00017 #include <string.h>
00018 #include <stdlib.h> /* for exit() */
00019 #include <ham/hamsterdb.h>
00020 
00021 void 
00022 error(const char *foo, ham_status_t st)
00023 {
00024     printf("%s() returned error %d: %s\n", foo, st, ham_strerror(st));
00025     exit(-1);
00026 }
00027 
00028 void
00029 usage(void)
00030 {
00031     printf("usage: ./db2 <source> <destination>\n");
00032     exit(-1);
00033 }
00034 
00035 void
00036 copy_db(ham_db_t *source, ham_db_t *dest)
00037 {
00038     ham_cursor_t *cursor;    /* hamsterdb cursor object */
00039     ham_status_t st;
00040     ham_key_t key;
00041     ham_record_t rec;
00042 
00043     memset(&key, 0, sizeof(key));
00044     memset(&rec, 0, sizeof(rec));
00045 
00046     /* create a new cursor */
00047     st=ham_cursor_create(source, 0, 0, &cursor); 
00048     if (st)
00049         error("ham_cursor_create", st);
00050 
00051     /* get a cursor to the source database */
00052     st=ham_cursor_move(cursor, &key, &rec, HAM_CURSOR_FIRST);
00053     if (st==HAM_KEY_NOT_FOUND) {
00054         printf("database is empty!\n");
00055         return;
00056     }
00057     else if (st)
00058         error("ham_cursor_move", st);
00059 
00060     do {
00061         /* insert this element into the new database */
00062         st=ham_insert(dest, 0, &key, &rec, HAM_DUPLICATE);
00063         if (st)
00064             error("ham_insert", st);
00065 
00066         /* give some feedback to the user */
00067         printf(".");
00068 
00069         /* fetch the next item, and repeat till we've reached the end
00070          * of the database */
00071         st=ham_cursor_move(cursor, &key, &rec, HAM_CURSOR_NEXT);
00072         if (st && st!=HAM_KEY_NOT_FOUND)
00073             error("ham_cursor_move", st);
00074 
00075     } while (st==0);
00076 
00077     /* clean up and return */
00078     ham_cursor_close(cursor);
00079 }
00080 
00081 int 
00082 main(int argc, char **argv)
00083 {
00084     ham_status_t st;
00085     ham_db_t *src, *dest;
00086     const char *src_path=0, *dest_path=0;
00087 
00088     /*
00089      * check and parse the command line parameters
00090      */
00091     if (argc!=3)
00092         usage();
00093     src_path =argv[1];
00094     dest_path=argv[2];
00095 
00096     /*
00097      * open the source database
00098      */
00099     st=ham_new(&src);
00100     if (st)
00101         error("ham_new", st);
00102 
00103     st=ham_open(src, src_path, 0);
00104     if (st)
00105         error("ham_open", st);
00106 
00107     /*
00108      * create the destination database
00109      */
00110     st=ham_new(&dest);
00111     if (st)
00112         error("ham_new", st);
00113 
00114     st=ham_create_ex(dest, dest_path, HAM_ENABLE_DUPLICATES, 0664, 0);
00115     if (st)
00116         error("ham_create", st);
00117 
00118     /*
00119      * copy the data
00120      */
00121     copy_db(src, dest);
00122 
00123     /*
00124      * clean up and return
00125      */
00126     st=ham_close(src, 0);
00127     if (st)
00128         error("ham_close", st);
00129     st=ham_close(dest, 0);
00130     if (st)
00131         error("ham_close", st);
00132     ham_delete(src);
00133     ham_delete(dest);
00134 
00135     printf("\nsuccess!\n");
00136     return (0);
00137 }
00138