00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "db_config.h"
00011
00012 #include <errno.h>
00013 #include <string.h>
00014
00015 #include "db_cxx.h"
00016 #include "dbinc/cxx_int.h"
00017
00018 #include "db_int.h"
00019 #include "dbinc/db_page.h"
00020 #include "dbinc_auto/db_auto.h"
00021 #include "dbinc_auto/crdel_auto.h"
00022 #include "dbinc/db_dispatch.h"
00023 #include "dbinc_auto/db_ext.h"
00024 #include "dbinc_auto/common_ext.h"
00025
00026
00027
00028
00029
00030
00031
00032 #define DB_METHOD(_name, _argspec, _arglist, _retok) \
00033 int Db::_name _argspec \
00034 { \
00035 int ret; \
00036 DB *db = unwrap(this); \
00037 \
00038 ret = db->_name _arglist; \
00039 if (!_retok(ret)) \
00040 DB_ERROR(env_, "Db::" # _name, ret, error_policy()); \
00041 return (ret); \
00042 }
00043
00044 #define DB_DESTRUCTOR(_name, _argspec, _arglist, _retok) \
00045 int Db::_name _argspec \
00046 { \
00047 int ret; \
00048 DB *db = unwrap(this); \
00049 \
00050 if (!db) { \
00051 DB_ERROR(env_, "Db::" # _name, EINVAL, error_policy()); \
00052 return (EINVAL); \
00053 } \
00054 cleanup(); \
00055 ret = db->_name _arglist; \
00056 if (!_retok(ret)) \
00057 DB_ERROR(env_, "Db::" # _name, ret, error_policy()); \
00058 return (ret); \
00059 }
00060
00061 #define DB_METHOD_QUIET(_name, _argspec, _arglist) \
00062 int Db::_name _argspec \
00063 { \
00064 DB *db = unwrap(this); \
00065 \
00066 return (db->_name _arglist); \
00067 }
00068
00069 #define DB_METHOD_VOID(_name, _argspec, _arglist) \
00070 void Db::_name _argspec \
00071 { \
00072 DB *db = unwrap(this); \
00073 \
00074 db->_name _arglist; \
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 Db::Db(DbEnv *env, u_int32_t flags)
00090 : imp_(0)
00091 , env_(env)
00092 , mpf_(0)
00093 , construct_error_(0)
00094 , flags_(0)
00095 , construct_flags_(flags)
00096 , append_recno_callback_(0)
00097 , associate_callback_(0)
00098 , bt_compare_callback_(0)
00099 , bt_prefix_callback_(0)
00100 , dup_compare_callback_(0)
00101 , feedback_callback_(0)
00102 , h_hash_callback_(0)
00103 {
00104 if (env_ == 0)
00105 flags_ |= DB_CXX_PRIVATE_ENV;
00106
00107 if ((construct_error_ = initialize()) != 0)
00108 DB_ERROR(env_, "Db::Db", construct_error_, error_policy());
00109 }
00110
00111
00112
00113
00114
00115
00116
00117
00118 Db::~Db()
00119 {
00120 DB *db;
00121
00122 db = unwrap(this);
00123 if (db != NULL) {
00124 cleanup();
00125 (void)db->close(db, 0);
00126 }
00127 }
00128
00129
00130
00131
00132
00133 int Db::initialize()
00134 {
00135 DB *db;
00136 DB_ENV *cenv = unwrap(env_);
00137 int ret;
00138 u_int32_t cxx_flags;
00139
00140 cxx_flags = construct_flags_ & DB_CXX_NO_EXCEPTIONS;
00141
00142
00143
00144
00145
00146 if ((ret = db_create(&db, cenv,
00147 construct_flags_ & ~cxx_flags)) != 0)
00148 return (ret);
00149
00150
00151 imp_ = db;
00152 db->api_internal = this;
00153
00154
00155
00156
00157 if ((flags_ & DB_CXX_PRIVATE_ENV) != 0)
00158 env_ = new DbEnv(db->dbenv, cxx_flags);
00159
00160
00161 mpf_ = new DbMpoolFile();
00162 mpf_->imp_ = db->mpf;
00163
00164 return (0);
00165 }
00166
00167
00168
00169
00170
00171
00172 void Db::cleanup()
00173 {
00174 DB *db = unwrap(this);
00175
00176 if (db != NULL) {
00177
00178 imp_ = 0;
00179
00180
00181
00182
00183
00184
00185
00186 if ((flags_ & DB_CXX_PRIVATE_ENV) != 0) {
00187 env_->cleanup();
00188 delete env_;
00189 env_ = 0;
00190 }
00191
00192 delete mpf_;
00193 }
00194 }
00195
00196
00197
00198
00199
00200
00201
00202 int Db::error_policy()
00203 {
00204 if (env_ != NULL)
00205 return (env_->error_policy());
00206 else {
00207
00208
00209
00210
00211
00212 if ((construct_flags_ & DB_CXX_NO_EXCEPTIONS) != 0) {
00213 return (ON_ERROR_RETURN);
00214 }
00215 else {
00216 return (ON_ERROR_THROW);
00217 }
00218 }
00219 }
00220
00221 DB_DESTRUCTOR(close, (u_int32_t flags), (db, flags), DB_RETOK_STD)
00222 DB_METHOD(compact, (DbTxn *txnid, Dbt *start, Dbt *stop,
00223 DB_COMPACT *c_data, u_int32_t flags, Dbt *end),
00224 (db, unwrap(txnid), start, stop, c_data, flags, end), DB_RETOK_STD)
00225
00226
00227 DB_METHOD(cursor, (DbTxn *txnid, Dbc **cursorp, u_int32_t flags),
00228 (db, unwrap(txnid), (DBC **)cursorp, flags),
00229 DB_RETOK_STD)
00230
00231 DB_METHOD(del, (DbTxn *txnid, Dbt *key, u_int32_t flags),
00232 (db, unwrap(txnid), key, flags),
00233 DB_RETOK_DBDEL)
00234
00235 void Db::err(int error, const char *format, ...)
00236 {
00237 DB *db = unwrap(this);
00238
00239 DB_REAL_ERR(db->dbenv, error, 1, 1, format);
00240 }
00241
00242 void Db::errx(const char *format, ...)
00243 {
00244 DB *db = unwrap(this);
00245
00246 DB_REAL_ERR(db->dbenv, 0, 0, 1, format);
00247 }
00248
00249 DB_METHOD(fd, (int *fdp), (db, fdp), DB_RETOK_STD)
00250
00251 int Db::get(DbTxn *txnid, Dbt *key, Dbt *value, u_int32_t flags)
00252 {
00253 DB *db = unwrap(this);
00254 int ret;
00255
00256 ret = db->get(db, unwrap(txnid), key, value, flags);
00257
00258 if (!DB_RETOK_DBGET(ret)) {
00259 if (ret == DB_BUFFER_SMALL)
00260 DB_ERROR_DBT(env_, "Db::get", value, error_policy());
00261 else
00262 DB_ERROR(env_, "Db::get", ret, error_policy());
00263 }
00264
00265 return (ret);
00266 }
00267
00268 int Db::get_byteswapped(int *isswapped)
00269 {
00270 DB *db = (DB *)unwrapConst(this);
00271 return (db->get_byteswapped(db, isswapped));
00272 }
00273
00274 DbEnv *Db::get_env()
00275 {
00276 DB *db = (DB *)unwrapConst(this);
00277 DB_ENV *dbenv = db->get_env(db);
00278 return (dbenv != NULL ? DbEnv::get_DbEnv(dbenv) : NULL);
00279 }
00280
00281 DbMpoolFile *Db::get_mpf()
00282 {
00283 return (mpf_);
00284 }
00285
00286 DB_METHOD(get_dbname, (const char **filenamep, const char **dbnamep),
00287 (db, filenamep, dbnamep), DB_RETOK_STD)
00288
00289 DB_METHOD(get_open_flags, (u_int32_t *flagsp), (db, flagsp), DB_RETOK_STD)
00290
00291 int Db::get_type(DBTYPE *dbtype)
00292 {
00293 DB *db = (DB *)unwrapConst(this);
00294 return (db->get_type(db, dbtype));
00295 }
00296
00297
00298
00299
00300 DB_METHOD(join, (Dbc **curslist, Dbc **cursorp, u_int32_t flags),
00301 (db, (DBC **)curslist, (DBC **)cursorp, flags), DB_RETOK_STD)
00302
00303 DB_METHOD(key_range,
00304 (DbTxn *txnid, Dbt *key, DB_KEY_RANGE *results, u_int32_t flags),
00305 (db, unwrap(txnid), key, results, flags), DB_RETOK_STD)
00306
00307
00308
00309
00310 int Db::open(DbTxn *txnid, const char *file, const char *database,
00311 DBTYPE type, u_int32_t flags, int mode)
00312 {
00313 int ret;
00314 DB *db = unwrap(this);
00315
00316 if (construct_error_ != 0)
00317 ret = construct_error_;
00318 else
00319 ret = db->open(db, unwrap(txnid), file, database, type, flags,
00320 mode);
00321
00322 if (!DB_RETOK_STD(ret))
00323 DB_ERROR(env_, "Db::open", ret, error_policy());
00324
00325 return (ret);
00326 }
00327
00328 int Db::pget(DbTxn *txnid, Dbt *key, Dbt *pkey, Dbt *value, u_int32_t flags)
00329 {
00330 DB *db = unwrap(this);
00331 int ret;
00332
00333 ret = db->pget(db, unwrap(txnid), key, pkey, value, flags);
00334
00335
00336 if (!DB_RETOK_DBGET(ret)) {
00337 if (ret == DB_BUFFER_SMALL && DB_OVERFLOWED_DBT(value))
00338 DB_ERROR_DBT(env_, "Db::pget", value, error_policy());
00339 else
00340 DB_ERROR(env_, "Db::pget", ret, error_policy());
00341 }
00342
00343 return (ret);
00344 }
00345
00346 DB_METHOD(put, (DbTxn *txnid, Dbt *key, Dbt *value, u_int32_t flags),
00347 (db, unwrap(txnid), key, value, flags), DB_RETOK_DBPUT)
00348
00349 DB_DESTRUCTOR(rename,
00350 (const char *file, const char *database, const char *newname,
00351 u_int32_t flags),
00352 (db, file, database, newname, flags), DB_RETOK_STD)
00353
00354 DB_DESTRUCTOR(remove, (const char *file, const char *database, u_int32_t flags),
00355 (db, file, database, flags), DB_RETOK_STD)
00356
00357 DB_METHOD(truncate, (DbTxn *txnid, u_int32_t *countp, u_int32_t flags),
00358 (db, unwrap(txnid), countp, flags), DB_RETOK_STD)
00359
00360 DB_METHOD(stat, (DbTxn *txnid, void *sp, u_int32_t flags),
00361 (db, unwrap(txnid), sp, flags), DB_RETOK_STD)
00362
00363 DB_METHOD(stat_print, (u_int32_t flags), (db, flags), DB_RETOK_STD)
00364
00365 DB_METHOD(sync, (u_int32_t flags), (db, flags), DB_RETOK_STD)
00366
00367 DB_METHOD(upgrade,
00368 (const char *name, u_int32_t flags), (db, name, flags), DB_RETOK_STD)
00369
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405 #define DB_CALLBACK_C_INTERCEPT(_name, _rettype, _cargspec, \
00406 _return, _cxxargs) \
00407 extern "C" _rettype _db_##_name##_intercept_c _cargspec \
00408 { \
00409 Db *cxxthis; \
00410 \
00411 DB_ASSERT(cthis != NULL); \
00412 cxxthis = Db::get_Db(cthis); \
00413 DB_ASSERT(cxxthis != NULL); \
00414 DB_ASSERT(cxxthis->_name##_callback_ != 0); \
00415 \
00416 _return (*cxxthis->_name##_callback_) _cxxargs; \
00417 }
00418
00419 #define DB_SET_CALLBACK(_cxxname, _name, _cxxargspec, _cb) \
00420 int Db::_cxxname _cxxargspec \
00421 { \
00422 DB *cthis = unwrap(this); \
00423 \
00424 _name##_callback_ = _cb; \
00425 return ((*(cthis->_cxxname))(cthis, \
00426 (_cb) ? _db_##_name##_intercept_c : NULL)); \
00427 }
00428
00429
00430 DB_CALLBACK_C_INTERCEPT(associate,
00431 int, (DB *cthis, const DBT *key, const DBT *data, DBT *retval),
00432 return, (cxxthis, Dbt::get_const_Dbt(key), Dbt::get_const_Dbt(data),
00433 Dbt::get_Dbt(retval)))
00434
00435 int Db::associate(DbTxn *txn, Db *secondary, int (*callback)(Db *, const Dbt *,
00436 const Dbt *, Dbt *), u_int32_t flags)
00437 {
00438 DB *cthis = unwrap(this);
00439
00440
00441
00442
00443
00444 secondary->associate_callback_ = callback;
00445 return ((*(cthis->associate))(cthis, unwrap(txn), unwrap(secondary),
00446 (callback) ? _db_associate_intercept_c : NULL, flags));
00447 }
00448
00449 DB_CALLBACK_C_INTERCEPT(feedback,
00450 void, (DB *cthis, int opcode, int pct),
00451 (void), (cxxthis, opcode, pct))
00452
00453 DB_SET_CALLBACK(set_feedback, feedback,
00454 (void (*arg)(Db *cxxthis, int opcode, int pct)), arg)
00455
00456 DB_CALLBACK_C_INTERCEPT(append_recno,
00457 int, (DB *cthis, DBT *data, db_recno_t recno),
00458 return, (cxxthis, Dbt::get_Dbt(data), recno))
00459
00460 DB_SET_CALLBACK(set_append_recno, append_recno,
00461 (int (*arg)(Db *cxxthis, Dbt *data, db_recno_t recno)), arg)
00462
00463 DB_CALLBACK_C_INTERCEPT(bt_compare,
00464 int, (DB *cthis, const DBT *data1, const DBT *data2),
00465 return,
00466 (cxxthis, Dbt::get_const_Dbt(data1), Dbt::get_const_Dbt(data2)))
00467
00468 DB_SET_CALLBACK(set_bt_compare, bt_compare,
00469 (int (*arg)(Db *cxxthis, const Dbt *data1, const Dbt *data2)), arg)
00470
00471 DB_CALLBACK_C_INTERCEPT(bt_prefix,
00472 size_t, (DB *cthis, const DBT *data1, const DBT *data2),
00473 return,
00474 (cxxthis, Dbt::get_const_Dbt(data1), Dbt::get_const_Dbt(data2)))
00475
00476 DB_SET_CALLBACK(set_bt_prefix, bt_prefix,
00477 (size_t (*arg)(Db *cxxthis, const Dbt *data1, const Dbt *data2)), arg)
00478
00479 DB_CALLBACK_C_INTERCEPT(dup_compare,
00480 int, (DB *cthis, const DBT *data1, const DBT *data2),
00481 return,
00482 (cxxthis, Dbt::get_const_Dbt(data1), Dbt::get_const_Dbt(data2)))
00483
00484 DB_SET_CALLBACK(set_dup_compare, dup_compare,
00485 (int (*arg)(Db *cxxthis, const Dbt *data1, const Dbt *data2)), arg)
00486
00487 DB_CALLBACK_C_INTERCEPT(h_hash,
00488 u_int32_t, (DB *cthis, const void *data, u_int32_t len),
00489 return, (cxxthis, data, len))
00490
00491 DB_SET_CALLBACK(set_h_hash, h_hash,
00492 (u_int32_t (*arg)(Db *cxxthis, const void *data, u_int32_t len)), arg)
00493
00494
00495
00496
00497
00498
00499 extern "C"
00500 int _verify_callback_c(void *handle, const void *str_arg)
00501 {
00502 char *str;
00503 __DB_STD(ostream) *out;
00504
00505 str = (char *)str_arg;
00506 out = (__DB_STD(ostream) *)handle;
00507
00508 (*out) << str;
00509 if (out->fail())
00510 return (EIO);
00511
00512 return (0);
00513 }
00514
00515 int Db::verify(const char *name, const char *subdb,
00516 __DB_STD(ostream) *ostr, u_int32_t flags)
00517 {
00518 DB *db = unwrap(this);
00519 int ret;
00520
00521 if (!db)
00522 ret = EINVAL;
00523 else {
00524
00525
00526
00527
00528 cleanup();
00529
00530 ret = __db_verify_internal(db, name, subdb, ostr,
00531 _verify_callback_c, flags);
00532 }
00533
00534 if (!DB_RETOK_STD(ret))
00535 DB_ERROR(env_, "Db::verify", ret, error_policy());
00536
00537 return (ret);
00538 }
00539
00540 DB_METHOD(set_bt_compare, (bt_compare_fcn_type func),
00541 (db, func), DB_RETOK_STD)
00542 DB_METHOD(get_bt_minkey, (u_int32_t *bt_minkeyp),
00543 (db, bt_minkeyp), DB_RETOK_STD)
00544 DB_METHOD(set_bt_minkey, (u_int32_t bt_minkey),
00545 (db, bt_minkey), DB_RETOK_STD)
00546 DB_METHOD(set_bt_prefix, (bt_prefix_fcn_type func),
00547 (db, func), DB_RETOK_STD)
00548 DB_METHOD(set_dup_compare, (dup_compare_fcn_type func),
00549 (db, func), DB_RETOK_STD)
00550 DB_METHOD(get_encrypt_flags, (u_int32_t *flagsp),
00551 (db, flagsp), DB_RETOK_STD)
00552 DB_METHOD(set_encrypt, (const char *passwd, u_int32_t flags),
00553 (db, passwd, flags), DB_RETOK_STD)
00554 DB_METHOD_VOID(get_errfile, (FILE **errfilep), (db, errfilep))
00555 DB_METHOD_VOID(set_errfile, (FILE *errfile), (db, errfile))
00556 DB_METHOD_VOID(get_errpfx, (const char **errpfx), (db, errpfx))
00557 DB_METHOD_VOID(set_errpfx, (const char *errpfx), (db, errpfx))
00558 DB_METHOD(get_flags, (u_int32_t *flagsp), (db, flagsp),
00559 DB_RETOK_STD)
00560 DB_METHOD(set_flags, (u_int32_t flags), (db, flags),
00561 DB_RETOK_STD)
00562 DB_METHOD(get_h_ffactor, (u_int32_t *h_ffactorp),
00563 (db, h_ffactorp), DB_RETOK_STD)
00564 DB_METHOD(set_h_ffactor, (u_int32_t h_ffactor),
00565 (db, h_ffactor), DB_RETOK_STD)
00566 DB_METHOD(set_h_hash, (h_hash_fcn_type func),
00567 (db, func), DB_RETOK_STD)
00568 DB_METHOD(get_h_nelem, (u_int32_t *h_nelemp),
00569 (db, h_nelemp), DB_RETOK_STD)
00570 DB_METHOD(set_h_nelem, (u_int32_t h_nelem),
00571 (db, h_nelem), DB_RETOK_STD)
00572 DB_METHOD(get_lorder, (int *db_lorderp), (db, db_lorderp),
00573 DB_RETOK_STD)
00574 DB_METHOD(set_lorder, (int db_lorder), (db, db_lorder),
00575 DB_RETOK_STD)
00576 DB_METHOD_VOID(get_msgfile, (FILE **msgfilep), (db, msgfilep))
00577 DB_METHOD_VOID(set_msgfile, (FILE *msgfile), (db, msgfile))
00578 DB_METHOD(get_pagesize, (u_int32_t *db_pagesizep),
00579 (db, db_pagesizep), DB_RETOK_STD)
00580 DB_METHOD(set_pagesize, (u_int32_t db_pagesize),
00581 (db, db_pagesize), DB_RETOK_STD)
00582 DB_METHOD(get_re_delim, (int *re_delimp),
00583 (db, re_delimp), DB_RETOK_STD)
00584 DB_METHOD(set_re_delim, (int re_delim),
00585 (db, re_delim), DB_RETOK_STD)
00586 DB_METHOD(get_re_len, (u_int32_t *re_lenp),
00587 (db, re_lenp), DB_RETOK_STD)
00588 DB_METHOD(set_re_len, (u_int32_t re_len),
00589 (db, re_len), DB_RETOK_STD)
00590 DB_METHOD(get_re_pad, (int *re_padp),
00591 (db, re_padp), DB_RETOK_STD)
00592 DB_METHOD(set_re_pad, (int re_pad),
00593 (db, re_pad), DB_RETOK_STD)
00594 DB_METHOD(get_re_source, (const char **re_source),
00595 (db, re_source), DB_RETOK_STD)
00596 DB_METHOD(set_re_source, (const char *re_source),
00597 (db, re_source), DB_RETOK_STD)
00598 DB_METHOD(get_q_extentsize, (u_int32_t *extentsizep),
00599 (db, extentsizep), DB_RETOK_STD)
00600 DB_METHOD(set_q_extentsize, (u_int32_t extentsize),
00601 (db, extentsize), DB_RETOK_STD)
00602
00603 DB_METHOD_QUIET(set_alloc, (db_malloc_fcn_type malloc_fcn,
00604 db_realloc_fcn_type realloc_fcn, db_free_fcn_type free_fcn),
00605 (db, malloc_fcn, realloc_fcn, free_fcn))
00606
00607 void Db::set_errcall(void (*arg)(const DbEnv *, const char *, const char *))
00608 {
00609 env_->set_errcall(arg);
00610 }
00611
00612 void Db::set_msgcall(void (*arg)(const DbEnv *, const char *))
00613 {
00614 env_->set_msgcall(arg);
00615 }
00616
00617 void *Db::get_app_private() const
00618 {
00619 return unwrapConst(this)->app_private;
00620 }
00621
00622 void Db::set_app_private(void *value)
00623 {
00624 unwrap(this)->app_private = value;
00625 }
00626
00627 DB_METHOD(get_cachesize, (u_int32_t *gbytesp, u_int32_t *bytesp, int *ncachep),
00628 (db, gbytesp, bytesp, ncachep), DB_RETOK_STD)
00629 DB_METHOD(set_cachesize, (u_int32_t gbytes, u_int32_t bytes, int ncache),
00630 (db, gbytes, bytes, ncache), DB_RETOK_STD)
00631
00632 int Db::set_paniccall(void (*callback)(DbEnv *, int))
00633 {
00634 return (env_->set_paniccall(callback));
00635 }
00636
00637 __DB_STD(ostream) *Db::get_error_stream()
00638 {
00639 return env_->get_error_stream();
00640 }
00641
00642 void Db::set_error_stream(__DB_STD(ostream) *error_stream)
00643 {
00644 env_->set_error_stream(error_stream);
00645 }
00646
00647 __DB_STD(ostream) *Db::get_message_stream()
00648 {
00649 return env_->get_message_stream();
00650 }
00651
00652 void Db::set_message_stream(__DB_STD(ostream) *message_stream)
00653 {
00654 env_->set_message_stream(message_stream);
00655 }
00656
00657 DB_METHOD_QUIET(get_transactional, (), (db))