Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

EnvExample.cpp

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1997-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: EnvExample.cpp,v 12.1 2005/06/16 20:22:14 bostic Exp $
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";                    /* Program name. */
00042 
00043 //
00044 // An example of a program creating/configuring a Berkeley DB environment.
00045 //
00046 int
00047 main(int, char **)
00048 {
00049         //
00050         // Note: it may be easiest to put all Berkeley DB operations in a
00051         // try block, as seen here.  Alternatively, you can change the
00052         // ErrorModel in the DbEnv so that exceptions are never thrown
00053         // and check error returns from all methods.
00054         //
00055         try {
00056                 const char *data_dir, *home;
00057 
00058                 //
00059                 // All of the shared database files live in /home/database,
00060                 // but data files live in /database.
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 // Note that any of the db calls can throw DbException
00079 void
00080 db_setup(const char *home, const char *data_dir, ostream& err_stream)
00081 {
00082         //
00083         // Create an environment object and initialize it for error
00084         // reporting.
00085         //
00086         DbEnv *dbenv = new DbEnv(0);
00087         dbenv->set_error_stream(&err_stream);
00088         dbenv->set_errpfx(progname);
00089 
00090         //
00091         // We want to specify the shared memory buffer pool cachesize,
00092         // but everything else is the default.
00093         //
00094         dbenv->set_cachesize(0, 64 * 1024, 0);
00095 
00096         // Databases are in a subdirectory.
00097         (void)dbenv->set_data_dir(data_dir);
00098 
00099         // Open the environment with full transactional support.
00100         dbenv->open(home,
00101     DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN, 0);
00102 
00103         // Do something interesting...
00104 
00105         // Close the handle.
00106         dbenv->close(0);
00107 }
00108 
00109 void
00110 db_teardown(const char *home, const char *data_dir, ostream& err_stream)
00111 {
00112         // Remove the shared database regions.
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 }

Generated on Sun Dec 25 12:14:25 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2