00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <sys/types.h>
00011
00012 #include <errno.h>
00013 #include <iostream>
00014 #include <stddef.h>
00015 #include <stdio.h>
00016 #include <stdlib.h>
00017 #include <string.h>
00018
00019 #include <db_cxx.h>
00020
00021 using std::ostream;
00022 using std::cout;
00023 using std::cerr;
00024
00025 #ifdef macintosh
00026 #define DATABASE_HOME ":database"
00027 #define CONFIG_DATA_DIR ":database"
00028 #else
00029 #ifdef DB_WIN32
00030 #define DATABASE_HOME "\\tmp\\database"
00031 #define CONFIG_DATA_DIR "\\database\\files"
00032 #else
00033 #define DATABASE_HOME "/tmp/database"
00034 #define CONFIG_DATA_DIR "/database/files"
00035 #endif
00036 #endif
00037
00038 void db_setup(const char *, const char *, ostream&);
00039 void db_teardown(const char *, const char *, ostream&);
00040
00041 const char *progname = "EnvExample";
00042
00043
00044
00045
00046 int
00047 main(int, char **)
00048 {
00049
00050
00051
00052
00053
00054
00055 try {
00056 const char *data_dir, *home;
00057
00058
00059
00060
00061
00062 home = DATABASE_HOME;
00063 data_dir = CONFIG_DATA_DIR;
00064
00065 cout << "Setup env\n";
00066 db_setup(home, data_dir, cerr);
00067
00068 cout << "Teardown env\n";
00069 db_teardown(home, data_dir, cerr);
00070 return (EXIT_SUCCESS);
00071 }
00072 catch (DbException &dbe) {
00073 cerr << "EnvExample: " << dbe.what() << "\n";
00074 return (EXIT_FAILURE);
00075 }
00076 }
00077
00078
00079 void
00080 db_setup(const char *home, const char *data_dir, ostream& err_stream)
00081 {
00082
00083
00084
00085
00086 DbEnv *dbenv = new DbEnv(0);
00087 dbenv->set_error_stream(&err_stream);
00088 dbenv->set_errpfx(progname);
00089
00090
00091
00092
00093
00094 dbenv->set_cachesize(0, 64 * 1024, 0);
00095
00096
00097 (void)dbenv->set_data_dir(data_dir);
00098
00099
00100 dbenv->open(home,
00101 DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN, 0);
00102
00103
00104
00105
00106 dbenv->close(0);
00107 }
00108
00109 void
00110 db_teardown(const char *home, const char *data_dir, ostream& err_stream)
00111 {
00112
00113 DbEnv *dbenv = new DbEnv(0);
00114
00115 dbenv->set_error_stream(&err_stream);
00116 dbenv->set_errpfx(progname);
00117
00118 (void)dbenv->set_data_dir(data_dir);
00119 dbenv->remove(home, 0);
00120 delete dbenv;
00121 }