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

cxx_env.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: cxx_env.cpp,v 12.14 2005/10/18 14:49:27 mjc Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #include <errno.h>
00013 #include <stdio.h>              // needed for set_error_stream
00014 #include <string.h>
00015 
00016 #include "db_cxx.h"
00017 #include "dbinc/cxx_int.h"
00018 
00019 #include "db_int.h"
00020 #include "dbinc/db_page.h"
00021 #include "dbinc/db_am.h"
00022 #include "dbinc/log.h"
00023 #include "dbinc_auto/common_ext.h"
00024 #include "dbinc_auto/log_ext.h"
00025 
00026 #ifdef HAVE_CXX_STDHEADERS
00027 using std::cerr;
00028 #endif
00029 
00030 // Helper macros for simple methods that pass through to the
00031 // underlying C method.  They may return an error or raise an exception.
00032 // These macros expect that input _argspec is an argument
00033 // list element (e.g., "char *arg") and that _arglist is the arguments
00034 // that should be passed through to the C method (e.g., "(dbenv, arg)")
00035 //
00036 #define DBENV_METHOD_ERR(_name, _argspec, _arglist, _on_err)      \
00037 int DbEnv::_name _argspec                                         \
00038 {                                                                 \
00039         DB_ENV *dbenv = unwrap(this);                             \
00040         int ret;                                                  \
00041                                                                   \
00042         if ((ret = dbenv->_name _arglist) != 0) {                 \
00043                 _on_err;                                          \
00044         }                                                         \
00045         return (ret);                                             \
00046 }
00047 
00048 #define DBENV_METHOD(_name, _argspec, _arglist)                   \
00049         DBENV_METHOD_ERR(_name, _argspec, _arglist,               \
00050         DB_ERROR(this, "DbEnv::" # _name, ret, error_policy()))
00051 
00052 #define DBENV_METHOD_QUIET(_name, _argspec, _arglist)             \
00053 int DbEnv::_name _argspec                                         \
00054 {                                                                 \
00055         DB_ENV *dbenv = unwrap(this);                             \
00056                                                                   \
00057         return (dbenv->_name _arglist);                           \
00058 }
00059 
00060 #define DBENV_METHOD_VOID(_name, _argspec, _arglist)              \
00061 void DbEnv::_name _argspec                                        \
00062 {                                                                 \
00063         DB_ENV *dbenv = unwrap(this);                             \
00064                                                                   \
00065         dbenv->_name _arglist;                                    \
00066 }
00067 
00068 // The reason for a static variable is that some structures
00069 // (like Dbts) have no connection to any Db or DbEnv, so when
00070 // errors occur in their methods, we must have some reasonable
00071 // way to determine whether to throw or return errors.
00072 //
00073 // This variable is taken from flags whenever a DbEnv is constructed.
00074 // Normally there is only one DbEnv per program, and even if not,
00075 // there is typically a single policy of throwing or returning.
00076 //
00077 static int last_known_error_policy = ON_ERROR_UNKNOWN;
00078 
00079 // These 'glue' function are declared as extern "C" so they will
00080 // be compatible with picky compilers that do not allow mixing
00081 // of function pointers to 'C' functions with function pointers
00082 // to C++ functions.
00083 //
00084 extern "C"
00085 void _feedback_intercept_c(DB_ENV *env, int opcode, int pct)
00086 {
00087         DbEnv::_feedback_intercept(env, opcode, pct);
00088 }
00089 
00090 extern "C"
00091 void _paniccall_intercept_c(DB_ENV *env, int errval)
00092 {
00093         DbEnv::_paniccall_intercept(env, errval);
00094 }
00095 
00096 extern "C"
00097 void _stream_error_function_c(const DB_ENV *env,
00098     const char *prefix, const char *message)
00099 {
00100         DbEnv::_stream_error_function(env, prefix, message);
00101 }
00102 
00103 extern "C"
00104 void _stream_message_function_c(const DB_ENV *env, const char *message)
00105 {
00106         DbEnv::_stream_message_function(env, message);
00107 }
00108 
00109 extern "C"
00110 int _app_dispatch_intercept_c(DB_ENV *env, DBT *dbt, DB_LSN *lsn, db_recops op)
00111 {
00112         return (DbEnv::_app_dispatch_intercept(env, dbt, lsn, op));
00113 }
00114 
00115 extern "C"
00116 int _rep_send_intercept_c(DB_ENV *env, const DBT *cntrl, const DBT *data,
00117     const DB_LSN *lsn, int id, u_int32_t flags)
00118 {
00119         return (DbEnv::_rep_send_intercept(env,
00120             cntrl, data, lsn, id, flags));
00121 }
00122 
00123 extern "C"
00124 int _isalive_intercept_c(DB_ENV *env, pid_t pid, db_threadid_t thrid)
00125 {
00126         return (DbEnv::_isalive_intercept(env, pid, thrid));
00127 }
00128 
00129 extern "C"
00130 void _thread_id_intercept_c(DB_ENV *env, pid_t *pidp, db_threadid_t *thridp)
00131 {
00132         DbEnv::_thread_id_intercept(env, pidp, thridp);
00133 }
00134 
00135 extern "C"
00136 char *_thread_id_string_intercept_c(DB_ENV *env, pid_t pid,
00137     db_threadid_t thrid, char *buf)
00138 {
00139         return (DbEnv::_thread_id_string_intercept(env, pid, thrid, buf));
00140 }
00141 
00142 void DbEnv::_feedback_intercept(DB_ENV *env, int opcode, int pct)
00143 {
00144         DbEnv *cxxenv = DbEnv::get_DbEnv(env);
00145         if (cxxenv == 0) {
00146                 DB_ERROR(0,
00147                     "DbEnv::feedback_callback", EINVAL, ON_ERROR_UNKNOWN);
00148                 return;
00149         }
00150         if (cxxenv->feedback_callback_ == 0) {
00151                 DB_ERROR(DbEnv::get_DbEnv(env),
00152                     "DbEnv::feedback_callback", EINVAL, cxxenv->error_policy());
00153                 return;
00154         }
00155         (*cxxenv->feedback_callback_)(cxxenv, opcode, pct);
00156 }
00157 
00158 void DbEnv::_paniccall_intercept(DB_ENV *env, int errval)
00159 {
00160         DbEnv *cxxenv = DbEnv::get_DbEnv(env);
00161         if (cxxenv == 0) {
00162                 DB_ERROR(0,
00163                     "DbEnv::paniccall_callback", EINVAL, ON_ERROR_UNKNOWN);
00164                 return;
00165         }
00166         if (cxxenv->paniccall_callback_ == 0) {
00167                 DB_ERROR(cxxenv, "DbEnv::paniccall_callback", EINVAL,
00168                     cxxenv->error_policy());
00169                 return;
00170         }
00171         (*cxxenv->paniccall_callback_)(cxxenv, errval);
00172 }
00173 
00174 int DbEnv::_app_dispatch_intercept(DB_ENV *env, DBT *dbt, DB_LSN *lsn,
00175     db_recops op)
00176 {
00177         DbEnv *cxxenv = DbEnv::get_DbEnv(env);
00178         if (cxxenv == 0) {
00179                 DB_ERROR(DbEnv::get_DbEnv(env),
00180                     "DbEnv::app_dispatch_callback", EINVAL, ON_ERROR_UNKNOWN);
00181                 return (EINVAL);
00182         }
00183         if (cxxenv->app_dispatch_callback_ == 0) {
00184                 DB_ERROR(DbEnv::get_DbEnv(env),
00185                     "DbEnv::app_dispatch_callback", EINVAL,
00186                     cxxenv->error_policy());
00187                 return (EINVAL);
00188         }
00189         Dbt *cxxdbt = (Dbt *)dbt;
00190         DbLsn *cxxlsn = (DbLsn *)lsn;
00191         return ((*cxxenv->app_dispatch_callback_)(cxxenv, cxxdbt, cxxlsn, op));
00192 }
00193 
00194 int DbEnv::_isalive_intercept(DB_ENV *env, pid_t pid, db_threadid_t thrid)
00195 {
00196         DbEnv *cxxenv = DbEnv::get_DbEnv(env);
00197         if (cxxenv == 0) {
00198                 DB_ERROR(DbEnv::get_DbEnv(env),
00199                         "DbEnv::isalive_callback", EINVAL, ON_ERROR_UNKNOWN);
00200                 return (0);
00201         }
00202         return ((*cxxenv->isalive_callback_)(cxxenv, pid, thrid));
00203 }
00204 
00205 int DbEnv::_rep_send_intercept(DB_ENV *env, const DBT *cntrl, const DBT *data,
00206     const DB_LSN *lsn, int id, u_int32_t flags)
00207 {
00208         DbEnv *cxxenv = DbEnv::get_DbEnv(env);
00209         if (cxxenv == 0) {
00210                 DB_ERROR(DbEnv::get_DbEnv(env),
00211                         "DbEnv::rep_send_callback", EINVAL, ON_ERROR_UNKNOWN);
00212                 return (EINVAL);
00213         }
00214         const Dbt *cxxcntrl = (const Dbt *)cntrl;
00215         const DbLsn *cxxlsn = (const DbLsn *)lsn;
00216         Dbt *cxxdata = (Dbt *)data;
00217         return ((*cxxenv->rep_send_callback_)(cxxenv,
00218             cxxcntrl, cxxdata, cxxlsn, id, flags));
00219 }
00220 
00221 void DbEnv::_thread_id_intercept(DB_ENV *env, pid_t *pidp, db_threadid_t *thridp)
00222 {
00223         DbEnv *cxxenv = DbEnv::get_DbEnv(env);
00224         if (cxxenv == 0) {
00225                 DB_ERROR(DbEnv::get_DbEnv(env),
00226                         "DbEnv::thread_id_callback", EINVAL, ON_ERROR_UNKNOWN);
00227         } else
00228                 cxxenv->thread_id_callback_(cxxenv, pidp, thridp);
00229 }
00230 
00231 char *DbEnv::_thread_id_string_intercept(DB_ENV *env, pid_t pid,
00232     db_threadid_t thrid, char *buf)
00233 {
00234         DbEnv *cxxenv = DbEnv::get_DbEnv(env);
00235         if (cxxenv == 0) {
00236                 DB_ERROR(DbEnv::get_DbEnv(env),
00237                         "DbEnv::thread_id_string_callback", EINVAL, ON_ERROR_UNKNOWN);
00238                 return (NULL);
00239         }
00240         return (cxxenv->thread_id_string_callback_(cxxenv, pid, thrid, buf));
00241 }
00242 
00243 // A truism for the DbEnv object is that there is a valid
00244 // DB_ENV handle from the constructor until close().
00245 // After the close, the DB_ENV handle is invalid and
00246 // no operations are permitted on the DbEnv (other than
00247 // destructor).  Leaving the DbEnv handle open and not
00248 // doing a close is generally considered an error.
00249 //
00250 // We used to allow DbEnv objects to be closed and reopened.
00251 // This implied always keeping a valid DB_ENV object, and
00252 // coordinating the open objects between Db/DbEnv turned
00253 // out to be overly complicated.  Now we do not allow this.
00254 
00255 DbEnv::DbEnv(u_int32_t flags)
00256 :       imp_(0)
00257 ,       construct_error_(0)
00258 ,       construct_flags_(flags)
00259 ,       error_stream_(0)
00260 ,       message_stream_(0)
00261 ,       app_dispatch_callback_(0)
00262 ,       feedback_callback_(0)
00263 ,       paniccall_callback_(0)
00264 ,       rep_send_callback_(0)
00265 {
00266         if ((construct_error_ = initialize(0)) != 0)
00267                 DB_ERROR(this, "DbEnv::DbEnv", construct_error_,
00268                     error_policy());
00269 }
00270 
00271 DbEnv::DbEnv(DB_ENV *env, u_int32_t flags)
00272 :       imp_(0)
00273 ,       construct_error_(0)
00274 ,       construct_flags_(flags)
00275 ,       error_stream_(0)
00276 ,       message_stream_(0)
00277 ,       app_dispatch_callback_(0)
00278 ,       feedback_callback_(0)
00279 ,       paniccall_callback_(0)
00280 ,       rep_send_callback_(0)
00281 {
00282         if ((construct_error_ = initialize(env)) != 0)
00283                 DB_ERROR(this, "DbEnv::DbEnv", construct_error_,
00284                     error_policy());
00285 }
00286 
00287 // If the DB_ENV handle is still open, we close it.  This is to make stack
00288 // allocation of DbEnv objects easier so that they are cleaned up in the error
00289 // path.  Note that the C layer catches cases where handles are open in the
00290 // environment at close time and reports an error.  Applications should call
00291 // close explicitly in normal (non-exceptional) cases to check the return
00292 // value.
00293 //
00294 DbEnv::~DbEnv()
00295 {
00296         DB_ENV *env = unwrap(this);
00297 
00298         if (env != NULL) {
00299                 cleanup();
00300                 (void)env->close(env, 0);
00301         }
00302 }
00303 
00304 // called by destructors before the DB_ENV is destroyed.
00305 void DbEnv::cleanup()
00306 {
00307         DB_ENV *env = unwrap(this);
00308 
00309         if (env != NULL) {
00310                 env->api1_internal = 0;
00311                 imp_ = 0;
00312         }
00313 }
00314 
00315 int DbEnv::close(u_int32_t flags)
00316 {
00317         int ret;
00318         DB_ENV *env = unwrap(this);
00319 
00320         // after a close (no matter if success or failure),
00321         // the underlying DB_ENV object must not be accessed,
00322         // so we clean up in advance.
00323         //
00324         cleanup();
00325 
00326         // It's safe to throw an error after the close,
00327         // since our error mechanism does not peer into
00328         // the DB* structures.
00329         //
00330         if ((ret = env->close(env, flags)) != 0)
00331                 DB_ERROR(this, "DbEnv::close", ret, error_policy());
00332 
00333         return (ret);
00334 }
00335 
00336 DBENV_METHOD(dbremove,
00337     (DbTxn *txn, const char *name, const char *subdb, u_int32_t flags),
00338     (dbenv, unwrap(txn), name, subdb, flags))
00339 DBENV_METHOD(dbrename, (DbTxn *txn, const char *name, const char *subdb,
00340     const char *newname, u_int32_t flags),
00341     (dbenv, unwrap(txn), name, subdb, newname, flags))
00342 
00343 void DbEnv::err(int error, const char *format, ...)
00344 {
00345         DB_ENV *env = unwrap(this);
00346 
00347         DB_REAL_ERR(env, error, 1, 1, format);
00348 }
00349 
00350 // Return a tristate value corresponding to whether we should
00351 // throw exceptions on errors:
00352 //   ON_ERROR_RETURN
00353 //   ON_ERROR_THROW
00354 //   ON_ERROR_UNKNOWN
00355 //
00356 int DbEnv::error_policy()
00357 {
00358         if ((construct_flags_ & DB_CXX_NO_EXCEPTIONS) != 0) {
00359                 return (ON_ERROR_RETURN);
00360         }
00361         else {
00362                 return (ON_ERROR_THROW);
00363         }
00364 }
00365 
00366 void DbEnv::errx(const char *format, ...)
00367 {
00368         DB_ENV *env = unwrap(this);
00369 
00370         DB_REAL_ERR(env, 0, 0, 1, format);
00371 }
00372 
00373 void *DbEnv::get_app_private() const
00374 {
00375         return unwrapConst(this)->app_private;
00376 }
00377 
00378 DBENV_METHOD(failchk, (u_int32_t flags), (dbenv, flags))
00379 DBENV_METHOD(fileid_reset, (const char *file, u_int32_t flags),
00380     (dbenv, file, flags))
00381 DBENV_METHOD(get_home, (const char **homep), (dbenv, homep))
00382 DBENV_METHOD(get_open_flags, (u_int32_t *flagsp), (dbenv, flagsp))
00383 DBENV_METHOD(get_data_dirs, (const char ***dirspp), (dbenv, dirspp))
00384 
00385 bool DbEnv::is_bigendian()
00386 {
00387         return unwrap(this)->is_bigendian() ? true : false;
00388 }
00389 
00390 DBENV_METHOD(set_thread_count, (u_int32_t count), (dbenv, count))
00391 
00392 // used internally during constructor
00393 // to associate an existing DB_ENV with this DbEnv,
00394 // or create a new one.
00395 //
00396 int DbEnv::initialize(DB_ENV *env)
00397 {
00398         int ret;
00399 
00400         last_known_error_policy = error_policy();
00401 
00402         if (env == 0) {
00403                 // Create a new DB_ENV environment.
00404                 if ((ret = ::db_env_create(&env,
00405                     construct_flags_ & ~DB_CXX_NO_EXCEPTIONS)) != 0)
00406                         return (ret);
00407         }
00408         imp_ = env;
00409         env->api1_internal = this;      // for DB_ENV* to DbEnv* conversion
00410         return (0);
00411 }
00412 
00413 // lock methods
00414 DBENV_METHOD(lock_detect, (u_int32_t flags, u_int32_t atype, int *aborted),
00415     (dbenv, flags, atype, aborted))
00416 DBENV_METHOD_ERR(lock_get,
00417     (u_int32_t locker, u_int32_t flags, const Dbt *obj,
00418     db_lockmode_t lock_mode, DbLock *lock),
00419     (dbenv, locker, flags, obj, lock_mode, &lock->lock_),
00420     DbEnv::runtime_error_lock_get(this, "DbEnv::lock_get", ret,
00421                                   DB_LOCK_GET, lock_mode, obj, *lock,
00422                                   -1, error_policy()))
00423 DBENV_METHOD(lock_id, (u_int32_t *idp), (dbenv, idp))
00424 DBENV_METHOD(lock_id_free, (u_int32_t id), (dbenv, id))
00425 DBENV_METHOD(lock_put, (DbLock *lock), (dbenv, &lock->lock_))
00426 DBENV_METHOD(lock_stat, (DB_LOCK_STAT **statp, u_int32_t flags),
00427     (dbenv, statp, flags))
00428 DBENV_METHOD(lock_stat_print, (u_int32_t flags), (dbenv, flags))
00429 DBENV_METHOD_ERR(lock_vec,
00430     (u_int32_t locker, u_int32_t flags, DB_LOCKREQ list[],
00431     int nlist, DB_LOCKREQ **elist_returned),
00432     (dbenv, locker, flags, list, nlist, elist_returned),
00433     DbEnv::runtime_error_lock_get(this, "DbEnv::lock_vec", ret,
00434         (*elist_returned)->op, (*elist_returned)->mode,
00435         Dbt::get_Dbt((*elist_returned)->obj), DbLock((*elist_returned)->lock),
00436         (*elist_returned) - list, error_policy()))
00437 // log methods
00438 DBENV_METHOD(log_archive, (char **list[], u_int32_t flags),
00439     (dbenv, list, flags))
00440 
00441 int DbEnv::log_compare(const DbLsn *lsn0, const DbLsn *lsn1)
00442 {
00443         return (::log_compare(lsn0, lsn1));
00444 }
00445 
00446 // The following cast implies that DbLogc can be no larger than DB_LOGC
00447 DBENV_METHOD(log_cursor, (DbLogc **cursorp, u_int32_t flags),
00448     (dbenv, (DB_LOGC **)cursorp, flags))
00449 DBENV_METHOD(log_file, (DbLsn *lsn, char *namep, size_t len),
00450     (dbenv, lsn, namep, len))
00451 DBENV_METHOD(log_flush, (const DbLsn *lsn), (dbenv, lsn))
00452 DBENV_METHOD(log_put, (DbLsn *lsn, const Dbt *data, u_int32_t flags),
00453     (dbenv, lsn, data, flags))
00454 
00455 int DbEnv::log_printf(DbTxn *txn, const char *fmt, ...)
00456 {
00457         DB_ENV *env = unwrap(this);
00458         va_list ap;
00459         int ret;
00460 
00461         va_start(ap, fmt);
00462         ret = __log_printf_pp(env, unwrap(txn), fmt, ap);
00463         va_end(ap);
00464 
00465         return (ret);
00466 }
00467 
00468 DBENV_METHOD(log_stat, (DB_LOG_STAT **spp, u_int32_t flags),
00469     (dbenv, spp, flags))
00470 DBENV_METHOD(log_stat_print, (u_int32_t flags), (dbenv, flags))
00471 
00472 DBENV_METHOD(lsn_reset, (const char *file, u_int32_t flags),
00473     (dbenv, file, flags))
00474 
00475 int DbEnv::memp_fcreate(DbMpoolFile **dbmfp, u_int32_t flags)
00476 {
00477         DB_ENV *env = unwrap(this);
00478         int ret;
00479         DB_MPOOLFILE *mpf;
00480 
00481         if (env == NULL)
00482                 ret = EINVAL;
00483         else
00484                 ret = env->memp_fcreate(env, &mpf, flags);
00485 
00486         if (DB_RETOK_STD(ret)) {
00487                 *dbmfp = new DbMpoolFile();
00488                 (*dbmfp)->imp_ = mpf;
00489         } else
00490                 DB_ERROR(this, "DbMpoolFile::f_create", ret, ON_ERROR_UNKNOWN);
00491 
00492         return (ret);
00493 }
00494 
00495 DBENV_METHOD(memp_register,
00496     (int ftype, pgin_fcn_type pgin_fcn, pgout_fcn_type pgout_fcn),
00497     (dbenv, ftype, pgin_fcn, pgout_fcn))
00498 
00499 // memory pool methods
00500 DBENV_METHOD(memp_stat,
00501     (DB_MPOOL_STAT **gsp, DB_MPOOL_FSTAT ***fsp, u_int32_t flags),
00502     (dbenv, gsp, fsp, flags))
00503 DBENV_METHOD(memp_stat_print, (u_int32_t flags), (dbenv, flags))
00504 DBENV_METHOD(memp_sync, (DbLsn *sn), (dbenv, sn))
00505 DBENV_METHOD(memp_trickle, (int pct, int *nwrotep), (dbenv, pct, nwrotep))
00506 
00507 // If an error occurred during the constructor, report it now.
00508 // Otherwise, call the underlying DB->open method.
00509 //
00510 int DbEnv::open(const char *db_home, u_int32_t flags, int mode)
00511 {
00512         int ret;
00513         DB_ENV *env = unwrap(this);
00514 
00515         if (construct_error_ != 0)
00516                 ret = construct_error_;
00517         else
00518                 ret = env->open(env, db_home, flags, mode);
00519 
00520         if (!DB_RETOK_STD(ret))
00521                 DB_ERROR(this, "DbEnv::open", ret, error_policy());
00522 
00523         return (ret);
00524 }
00525 
00526 int DbEnv::remove(const char *db_home, u_int32_t flags)
00527 {
00528         int ret;
00529         DB_ENV *env = unwrap(this);
00530 
00531         // after a remove (no matter if success or failure),
00532         // the underlying DB_ENV object must not be accessed,
00533         // so we clean up in advance.
00534         //
00535         cleanup();
00536 
00537         if ((ret = env->remove(env, db_home, flags)) != 0)
00538                 DB_ERROR(this, "DbEnv::remove", ret, error_policy());
00539 
00540         return (ret);
00541 }
00542 
00543 // Report an error associated with the DbEnv.
00544 // error_policy is one of:
00545 //   ON_ERROR_THROW     throw an error
00546 //   ON_ERROR_RETURN    do nothing here, the caller will return an error
00547 //   ON_ERROR_UNKNOWN   defer the policy to policy saved in DbEnv::DbEnv
00548 //
00549 void DbEnv::runtime_error(DbEnv *env,
00550     const char *caller, int error, int error_policy)
00551 {
00552         if (error_policy == ON_ERROR_UNKNOWN)
00553                 error_policy = last_known_error_policy;
00554         if (error_policy == ON_ERROR_THROW) {
00555                 // Creating and throwing the object in two separate
00556                 // statements seems to be necessary for HP compilers.
00557                 switch (error) {
00558                 case DB_LOCK_DEADLOCK:
00559                         {
00560                                 DbDeadlockException dl_except(caller);
00561                                 dl_except.set_env(env);
00562                                 throw dl_except;
00563                         }
00564                         break;
00565                 case DB_RUNRECOVERY:
00566                         {
00567                                 DbRunRecoveryException rr_except(caller);
00568                                 rr_except.set_env(env);
00569                                 throw rr_except;
00570                         }
00571                         break;
00572                 case DB_LOCK_NOTGRANTED:
00573                         {
00574                                 DbLockNotGrantedException lng_except(caller);
00575                                 lng_except.set_env(env);
00576                                 throw lng_except;
00577                         }
00578                         break;
00579                 case DB_REP_HANDLE_DEAD:
00580                         {
00581                                 DbRepHandleDeadException dl_except(caller);
00582                                 dl_except.set_env(env);
00583                                 throw dl_except;
00584                         }
00585                 default:
00586                         {
00587                                 DbException except(caller, error);
00588                                 except.set_env(env);
00589                                 throw except;
00590                         }
00591                         break;
00592                 }
00593         }
00594 }
00595 
00596 // Like DbEnv::runtime_error, but issue a DbMemoryException
00597 // based on the fact that this Dbt is not large enough.
00598 void DbEnv::runtime_error_dbt(DbEnv *env,
00599     const char *caller, Dbt *dbt, int error_policy)
00600 {
00601         if (error_policy == ON_ERROR_UNKNOWN)
00602                 error_policy = last_known_error_policy;
00603         if (error_policy == ON_ERROR_THROW) {
00604                 // Creating and throwing the object in two separate
00605                 // statements seems to be necessary for HP compilers.
00606                 DbMemoryException except(caller, dbt);
00607                 except.set_env(env);
00608                 throw except;
00609         }
00610 }
00611 
00612 // Like DbEnv::runtime_error, but issue a DbLockNotGrantedException,
00613 // or a regular runtime error.
00614 // call regular runtime_error if it
00615 void DbEnv::runtime_error_lock_get(DbEnv *env,
00616     const char *caller, int error,
00617     db_lockop_t op, db_lockmode_t mode, const Dbt *obj,
00618     DbLock lock, int index, int error_policy)
00619 {
00620         if (error != DB_LOCK_NOTGRANTED) {
00621                 runtime_error(env, caller, error, error_policy);
00622                 return;
00623         }
00624 
00625         if (error_policy == ON_ERROR_UNKNOWN)
00626                 error_policy = last_known_error_policy;
00627         if (error_policy == ON_ERROR_THROW) {
00628                 // Creating and throwing the object in two separate
00629                 // statements seems to be necessary for HP compilers.
00630                 DbLockNotGrantedException except(caller, op, mode,
00631                     obj, lock, index);
00632                 except.set_env(env);
00633                 throw except;
00634         }
00635 }
00636 
00637 void DbEnv::_stream_error_function(
00638     const DB_ENV *env, const char *prefix, const char *message)
00639 {
00640         const DbEnv *cxxenv = DbEnv::get_const_DbEnv(env);
00641         if (cxxenv == 0) {
00642                 DB_ERROR(0,
00643                     "DbEnv::stream_error", EINVAL, ON_ERROR_UNKNOWN);
00644                 return;
00645         }
00646 
00647         if (cxxenv->error_callback_)
00648                 cxxenv->error_callback_(cxxenv, prefix, message);
00649         else if (cxxenv->error_stream_) {
00650                 // HP compilers need the extra casts, we don't know why.
00651                 if (prefix) {
00652                         (*cxxenv->error_stream_) << prefix;
00653                         (*cxxenv->error_stream_) << (const char *)": ";
00654                 }
00655                 if (message)
00656                         (*cxxenv->error_stream_) << (const char *)message;
00657                 (*cxxenv->error_stream_) << (const char *)"\n";
00658         }
00659 }
00660 
00661 void DbEnv::_stream_message_function(const DB_ENV *env, const char *message)
00662 {
00663         const DbEnv *cxxenv = DbEnv::get_const_DbEnv(env);
00664         if (cxxenv == 0) {
00665                 DB_ERROR(0,
00666                     "DbEnv::stream_message", EINVAL, ON_ERROR_UNKNOWN);
00667                 return;
00668         }
00669 
00670         if (cxxenv->message_callback_)
00671                 cxxenv->message_callback_(cxxenv, message);
00672         else if (cxxenv->message_stream_) {
00673                 // HP compilers need the extra casts, we don't know why.
00674                 (*cxxenv->message_stream_) << (const char *)message;
00675                 (*cxxenv->message_stream_) << (const char *)"\n";
00676         }
00677 }
00678 
00679 // static method
00680 char *DbEnv::strerror(int error)
00681 {
00682         return (db_strerror(error));
00683 }
00684 
00685 // We keep these alphabetical by field name,
00686 // for comparison with Java's list.
00687 //
00688 DBENV_METHOD(set_data_dir, (const char *dir), (dbenv, dir))
00689 DBENV_METHOD(get_encrypt_flags, (u_int32_t *flagsp),
00690     (dbenv, flagsp))
00691 DBENV_METHOD(set_encrypt, (const char *passwd, u_int32_t flags),
00692     (dbenv, passwd, flags))
00693 DBENV_METHOD_VOID(get_errfile, (FILE **errfilep), (dbenv, errfilep))
00694 DBENV_METHOD_VOID(set_errfile, (FILE *errfile), (dbenv, errfile))
00695 DBENV_METHOD_VOID(get_errpfx, (const char **errpfxp), (dbenv, errpfxp))
00696 DBENV_METHOD_VOID(set_errpfx, (const char *errpfx), (dbenv, errpfx))
00697 DBENV_METHOD(set_intermediate_dir, (int mode, u_int32_t flags),
00698     (dbenv, mode, flags))
00699 DBENV_METHOD(get_lg_bsize, (u_int32_t *bsizep), (dbenv, bsizep))
00700 DBENV_METHOD(set_lg_bsize, (u_int32_t bsize), (dbenv, bsize))
00701 DBENV_METHOD(get_lg_dir, (const char **dirp), (dbenv, dirp))
00702 DBENV_METHOD(set_lg_dir, (const char *dir), (dbenv, dir))
00703 DBENV_METHOD(get_lg_filemode, (int *modep), (dbenv, modep))
00704 DBENV_METHOD(set_lg_filemode, (int mode), (dbenv, mode))
00705 DBENV_METHOD(get_lg_max, (u_int32_t *maxp), (dbenv, maxp))
00706 DBENV_METHOD(set_lg_max, (u_int32_t max), (dbenv, max))
00707 DBENV_METHOD(get_lg_regionmax, (u_int32_t *regionmaxp), (dbenv, regionmaxp))
00708 DBENV_METHOD(set_lg_regionmax, (u_int32_t regionmax), (dbenv, regionmax))
00709 DBENV_METHOD(get_lk_conflicts, (const u_int8_t **lk_conflictsp, int *lk_maxp),
00710     (dbenv, lk_conflictsp, lk_maxp))
00711 DBENV_METHOD(set_lk_conflicts, (u_int8_t *lk_conflicts, int lk_max),
00712     (dbenv, lk_conflicts, lk_max))
00713 DBENV_METHOD(get_lk_detect, (u_int32_t *detectp), (dbenv, detectp))
00714 DBENV_METHOD(set_lk_detect, (u_int32_t detect), (dbenv, detect))
00715 DBENV_METHOD(set_lk_max, (u_int32_t max), (dbenv, max))
00716 DBENV_METHOD(get_lk_max_lockers, (u_int32_t *max_lockersp),
00717     (dbenv, max_lockersp))
00718 DBENV_METHOD(set_lk_max_lockers, (u_int32_t max_lockers), (dbenv, max_lockers))
00719 DBENV_METHOD(get_lk_max_locks, (u_int32_t *max_locksp), (dbenv, max_locksp))
00720 DBENV_METHOD(set_lk_max_locks, (u_int32_t max_locks), (dbenv, max_locks))
00721 DBENV_METHOD(get_lk_max_objects, (u_int32_t *max_objectsp),
00722     (dbenv, max_objectsp))
00723 DBENV_METHOD(set_lk_max_objects, (u_int32_t max_objects), (dbenv, max_objects))
00724 DBENV_METHOD(get_mp_max_openfd, (int *maxopenfdp), (dbenv, maxopenfdp))
00725 DBENV_METHOD(set_mp_max_openfd, (int maxopenfd), (dbenv, maxopenfd))
00726 DBENV_METHOD(get_mp_max_write, (int *maxwritep, int *maxwrite_sleepp), (dbenv, maxwritep, maxwrite_sleepp))
00727 DBENV_METHOD(set_mp_max_write, (int maxwrite, int maxwrite_sleep), (dbenv, maxwrite, maxwrite_sleep))
00728 DBENV_METHOD(get_mp_mmapsize, (size_t *mmapsizep), (dbenv, mmapsizep))
00729 DBENV_METHOD(set_mp_mmapsize, (size_t mmapsize), (dbenv, mmapsize))
00730 DBENV_METHOD_VOID(get_msgfile, (FILE **msgfilep), (dbenv, msgfilep))
00731 DBENV_METHOD_VOID(set_msgfile, (FILE *msgfile), (dbenv, msgfile))
00732 DBENV_METHOD(get_tmp_dir, (const char **tmp_dirp), (dbenv, tmp_dirp))
00733 DBENV_METHOD(set_tmp_dir, (const char *tmp_dir), (dbenv, tmp_dir))
00734 DBENV_METHOD(get_tx_max, (u_int32_t *tx_maxp), (dbenv, tx_maxp))
00735 DBENV_METHOD(set_tx_max, (u_int32_t tx_max), (dbenv, tx_max))
00736 
00737 DBENV_METHOD(stat_print, (u_int32_t flags), (dbenv, flags))
00738 
00739 DBENV_METHOD_QUIET(set_alloc,
00740     (db_malloc_fcn_type malloc_fcn, db_realloc_fcn_type realloc_fcn,
00741     db_free_fcn_type free_fcn),
00742     (dbenv, malloc_fcn, realloc_fcn, free_fcn))
00743 
00744 void DbEnv::set_app_private(void *value)
00745 {
00746         unwrap(this)->app_private = value;
00747 }
00748 
00749 DBENV_METHOD(get_cachesize,
00750     (u_int32_t *gbytesp, u_int32_t *bytesp, int *ncachep),
00751     (dbenv, gbytesp, bytesp, ncachep))
00752 DBENV_METHOD(set_cachesize,
00753     (u_int32_t gbytes, u_int32_t bytes, int ncache),
00754     (dbenv, gbytes, bytes, ncache))
00755 
00756 void DbEnv::set_errcall(void (*arg)(const DbEnv *, const char *, const char *))
00757 {
00758         DB_ENV *dbenv = unwrap(this);
00759 
00760         error_callback_ = arg;
00761         error_stream_ = 0;
00762 
00763         dbenv->set_errcall(dbenv, (arg == 0) ? 0 :
00764                            _stream_error_function_c);
00765 }
00766 
00767 __DB_STD(ostream) *DbEnv::get_error_stream()
00768 {
00769         return (error_stream_);
00770 }
00771 
00772 void DbEnv::set_error_stream(__DB_STD(ostream) *stream)
00773 {
00774         DB_ENV *dbenv = unwrap(this);
00775 
00776         error_stream_ = stream;
00777         error_callback_ = 0;
00778 
00779         dbenv->set_errcall(dbenv, (stream == 0) ? 0 :
00780                            _stream_error_function_c);
00781 }
00782 
00783 int DbEnv::set_feedback(void (*arg)(DbEnv *, int, int))
00784 {
00785         DB_ENV *dbenv = unwrap(this);
00786 
00787         feedback_callback_ = arg;
00788 
00789         return (dbenv->set_feedback(dbenv,
00790             arg == 0 ? 0 : _feedback_intercept_c));
00791 }
00792 
00793 DBENV_METHOD(get_flags, (u_int32_t *flagsp), (dbenv, flagsp))
00794 DBENV_METHOD(set_flags, (u_int32_t flags, int onoff), (dbenv, flags, onoff))
00795 
00796 void DbEnv::set_msgcall(void (*arg)(const DbEnv *, const char *))
00797 {
00798         DB_ENV *dbenv = unwrap(this);
00799 
00800         message_callback_ = arg;
00801         message_stream_ = 0;
00802 
00803         dbenv->set_msgcall(dbenv, (arg == 0) ? 0 :
00804             _stream_message_function_c);
00805 }
00806 
00807 __DB_STD(ostream) *DbEnv::get_message_stream()
00808 {
00809         return (message_stream_);
00810 }
00811 
00812 void DbEnv::set_message_stream(__DB_STD(ostream) *stream)
00813 {
00814         DB_ENV *dbenv = unwrap(this);
00815 
00816         message_stream_ = stream;
00817         message_callback_ = 0;
00818 
00819         dbenv->set_msgcall(dbenv, (stream == 0) ? 0 :
00820                            _stream_message_function_c);
00821 }
00822 
00823 int DbEnv::set_paniccall(void (*arg)(DbEnv *, int))
00824 {
00825         DB_ENV *dbenv = unwrap(this);
00826 
00827         paniccall_callback_ = arg;
00828 
00829         return (dbenv->set_paniccall(dbenv,
00830             arg == 0 ? 0 : _paniccall_intercept_c));
00831 }
00832 
00833 DBENV_METHOD(set_rpc_server,
00834     (void *cl, char *host, long tsec, long ssec, u_int32_t flags),
00835     (dbenv, cl, host, tsec, ssec, flags))
00836 DBENV_METHOD(get_shm_key, (long *shm_keyp), (dbenv, shm_keyp))
00837 DBENV_METHOD(set_shm_key, (long shm_key), (dbenv, shm_key))
00838 
00839 int DbEnv::set_app_dispatch
00840     (int (*arg)(DbEnv *, Dbt *, DbLsn *, db_recops))
00841 {
00842         DB_ENV *dbenv = unwrap(this);
00843         int ret;
00844 
00845         app_dispatch_callback_ = arg;
00846         if ((ret = dbenv->set_app_dispatch(dbenv,
00847             arg == 0 ? 0 : _app_dispatch_intercept_c)) != 0)
00848                 DB_ERROR(this, "DbEnv::set_app_dispatch", ret, error_policy());
00849 
00850         return (ret);
00851 }
00852 
00853 int DbEnv::set_isalive
00854     (int (*arg)(DbEnv *, pid_t, db_threadid_t))
00855 {
00856         DB_ENV *dbenv = unwrap(this);
00857         int ret;
00858 
00859         isalive_callback_ = arg;
00860         if ((ret = dbenv->set_isalive(dbenv,
00861             arg == 0 ? 0 : _isalive_intercept_c)) != 0)
00862                 DB_ERROR(this, "DbEnv::set_isalive", ret, error_policy());
00863 
00864         return (ret);
00865 }
00866 
00867 DBENV_METHOD(get_tx_timestamp, (time_t *timestamp), (dbenv, timestamp))
00868 DBENV_METHOD(set_tx_timestamp, (time_t *timestamp), (dbenv, timestamp))
00869 DBENV_METHOD(get_verbose, (u_int32_t which, int *onoffp),
00870     (dbenv, which, onoffp))
00871 DBENV_METHOD(set_verbose, (u_int32_t which, int onoff), (dbenv, which, onoff))
00872 
00873 DBENV_METHOD(mutex_alloc,
00874     (u_int32_t flags, db_mutex_t *mutexp), (dbenv, flags, mutexp))
00875 DBENV_METHOD(mutex_free, (db_mutex_t mutex), (dbenv, mutex))
00876 DBENV_METHOD(mutex_get_align, (u_int32_t *argp), (dbenv, argp))
00877 DBENV_METHOD(mutex_get_increment, (u_int32_t *argp), (dbenv, argp))
00878 DBENV_METHOD(mutex_get_max, (u_int32_t *argp), (dbenv, argp))
00879 DBENV_METHOD(mutex_get_tas_spins, (u_int32_t *argp), (dbenv, argp))
00880 DBENV_METHOD(mutex_lock, (db_mutex_t mutex), (dbenv, mutex))
00881 DBENV_METHOD(mutex_set_align, (u_int32_t arg), (dbenv, arg))
00882 DBENV_METHOD(mutex_set_increment, (u_int32_t arg), (dbenv, arg))
00883 DBENV_METHOD(mutex_set_max, (u_int32_t arg), (dbenv, arg))
00884 DBENV_METHOD(mutex_set_tas_spins, (u_int32_t arg), (dbenv, arg))
00885 DBENV_METHOD(mutex_stat,
00886     (DB_MUTEX_STAT **statp, u_int32_t flags), (dbenv, statp, flags))
00887 DBENV_METHOD(mutex_stat_print, (u_int32_t flags), (dbenv, flags))
00888 DBENV_METHOD(mutex_unlock, (db_mutex_t mutex), (dbenv, mutex))
00889 
00890 int DbEnv::set_thread_id(void (*arg)(DbEnv *, pid_t *, db_threadid_t *))
00891 {
00892         DB_ENV *dbenv = unwrap(this);
00893         int ret;
00894 
00895         thread_id_callback_ = arg;
00896         if ((ret = dbenv->set_thread_id(dbenv,
00897             arg == 0 ? 0 : _thread_id_intercept_c)) != 0)
00898                 DB_ERROR(this, "DbEnv::set_thread_id", ret, error_policy());
00899 
00900         return (ret);
00901 }
00902 
00903 int DbEnv::set_thread_id_string(
00904     char *(*arg)(DbEnv *, pid_t, db_threadid_t, char *))
00905 {
00906         DB_ENV *dbenv = unwrap(this);
00907         int ret;
00908 
00909         thread_id_string_callback_ = arg;
00910         if ((ret = dbenv->set_thread_id_string(dbenv,
00911             arg == 0 ? 0 : _thread_id_string_intercept_c)) != 0)
00912                 DB_ERROR(this, "DbEnv::set_thread_id_string", ret,
00913                     error_policy());
00914 
00915         return (ret);
00916 }
00917 
00918 int DbEnv::txn_begin(DbTxn *pid, DbTxn **tid, u_int32_t flags)
00919 {
00920         DB_ENV *env = unwrap(this);
00921         DB_TXN *txn;
00922         int ret;
00923 
00924         ret = env->txn_begin(env, unwrap(pid), &txn, flags);
00925         if (DB_RETOK_STD(ret))
00926                 *tid = new DbTxn(txn);
00927         else
00928                 DB_ERROR(this, "DbEnv::txn_begin", ret, error_policy());
00929 
00930         return (ret);
00931 }
00932 
00933 DBENV_METHOD(txn_checkpoint, (u_int32_t kbyte, u_int32_t min, u_int32_t flags),
00934     (dbenv, kbyte, min, flags))
00935 
00936 int DbEnv::txn_recover(DbPreplist *preplist, long count,
00937     long *retp, u_int32_t flags)
00938 {
00939         DB_ENV *dbenv = unwrap(this);
00940         DB_PREPLIST *c_preplist;
00941         long i;
00942         int ret;
00943 
00944         /*
00945          * We need to allocate some local storage for the
00946          * returned preplist, and that requires us to do
00947          * our own argument validation.
00948          */
00949         if (count <= 0)
00950                 ret = EINVAL;
00951         else
00952                 ret = __os_malloc(dbenv, sizeof(DB_PREPLIST) * count,
00953                     &c_preplist);
00954 
00955         if (ret != 0) {
00956                 DB_ERROR(this, "DbEnv::txn_recover", ret, error_policy());
00957                 return (ret);
00958         }
00959 
00960         if ((ret =
00961             dbenv->txn_recover(dbenv, c_preplist, count, retp, flags)) != 0) {
00962                 __os_free(dbenv, c_preplist);
00963                 DB_ERROR(this, "DbEnv::txn_recover", ret, error_policy());
00964                 return (ret);
00965         }
00966 
00967         for (i = 0; i < *retp; i++) {
00968                 preplist[i].txn = new DbTxn();
00969                 preplist[i].txn->imp_ = c_preplist[i].txn;
00970                 memcpy(preplist[i].gid, c_preplist[i].gid,
00971                     sizeof(preplist[i].gid));
00972         }
00973 
00974         __os_free(dbenv, c_preplist);
00975 
00976         return (0);
00977 }
00978 
00979 DBENV_METHOD(txn_stat, (DB_TXN_STAT **statp, u_int32_t flags),
00980     (dbenv, statp, flags))
00981 DBENV_METHOD(txn_stat_print, (u_int32_t flags), (dbenv, flags))
00982 
00983 int DbEnv::set_rep_transport(int myid,
00984     int (*arg)(DbEnv *, const Dbt *, const Dbt *, const DbLsn *, int, u_int32_t))
00985 {
00986         DB_ENV *dbenv = unwrap(this);
00987         int ret;
00988 
00989         rep_send_callback_ = arg;
00990         if ((ret = dbenv->set_rep_transport(dbenv, myid,
00991             arg == 0 ? 0 : _rep_send_intercept_c)) != 0)
00992                 DB_ERROR(this, "DbEnv::set_rep_transport", ret, error_policy());
00993 
00994         return (ret);
00995 }
00996 
00997 DBENV_METHOD(rep_elect,
00998     (int nsites,
00999     int nvotes, int priority, u_int32_t timeout, int *eidp, u_int32_t flags),
01000     (dbenv, nsites, nvotes, priority, timeout, eidp, flags))
01001 DBENV_METHOD(rep_flush, (), (dbenv))
01002 DBENV_METHOD(rep_get_config, (u_int32_t which, int *onoffp),
01003     (dbenv, which, onoffp))
01004 DBENV_METHOD(set_rep_request, (u_int32_t min, u_int32_t max), (dbenv, min, max))
01005 
01006 int DbEnv::rep_process_message(Dbt *control,
01007     Dbt *rec, int *idp, DbLsn *ret_lsnp)
01008 {
01009         DB_ENV *dbenv = unwrap(this);
01010         int ret;
01011 
01012         ret = dbenv->rep_process_message(dbenv, control, rec, idp, ret_lsnp);
01013         if (!DB_RETOK_REPPMSG(ret))
01014                 DB_ERROR(this, "DbEnv::rep_process_message", ret,
01015                     error_policy());
01016 
01017         return (ret);
01018 }
01019 
01020 DBENV_METHOD(rep_set_config,
01021     (u_int32_t which, int onoff), (dbenv, which, onoff))
01022 DBENV_METHOD(rep_start,
01023     (Dbt *cookie, u_int32_t flags),
01024     (dbenv, (DBT *)cookie, flags))
01025 
01026 DBENV_METHOD(rep_stat, (DB_REP_STAT **statp, u_int32_t flags),
01027     (dbenv, statp, flags))
01028 DBENV_METHOD(rep_stat_print, (u_int32_t flags), (dbenv, flags))
01029 DBENV_METHOD(rep_sync, (u_int32_t flags), (dbenv, flags))
01030 
01031 DBENV_METHOD(get_rep_limit, (u_int32_t *gbytesp, u_int32_t *bytesp),
01032     (dbenv, gbytesp, bytesp))
01033 DBENV_METHOD(set_rep_limit, (u_int32_t gbytes, u_int32_t bytes),
01034     (dbenv, gbytes, bytes))
01035 
01036 DBENV_METHOD(get_timeout,
01037     (db_timeout_t *timeoutp, u_int32_t flags),
01038     (dbenv, timeoutp, flags))
01039 DBENV_METHOD(set_timeout,
01040     (db_timeout_t timeout, u_int32_t flags),
01041     (dbenv, timeout, flags))
01042 
01043 // static method
01044 char *DbEnv::version(int *major, int *minor, int *patch)
01045 {
01046         return (db_version(major, minor, patch));
01047 }
01048 
01049 // static method
01050 DbEnv *DbEnv::wrap_DB_ENV(DB_ENV *dbenv)
01051 {
01052         DbEnv *wrapped_env = get_DbEnv(dbenv);
01053         return (wrapped_env != NULL) ? wrapped_env : new DbEnv(dbenv, 0);
01054 }

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