You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
6.0 KiB

  1. #ifndef _DB_CXX_H_
  2. #define _DB_CXX_H_
  3. #include <db.h>
  4. #include <iostream>
  5. #include <exception>
  6. #include <string.h>
  7. #ident "Copyright (c) 2007, 2008 Tokutek Inc. All rights reserved."
  8. class Dbt;
  9. class DbEnv;
  10. class DbTxn;
  11. class Dbc;
  12. class DbException;
  13. class DbException : public std::exception {
  14. friend class DbEnv;
  15. public:
  16. ~DbException() throw();
  17. DbException(int err);
  18. int get_errno() const;
  19. const char *what() const throw();
  20. DbEnv *get_env() const;
  21. void set_env(DbEnv *);
  22. DbException(const DbException &);
  23. DbException &operator = (const DbException &);
  24. private:
  25. char *the_what;
  26. int the_err;
  27. DbEnv *the_env;
  28. void FillTheWhat(void);
  29. };
  30. class DbDeadlockException : public DbException {
  31. public:
  32. DbDeadlockException(DbEnv*);
  33. };
  34. class DbLockNotGrantedException {
  35. };
  36. class DbMemoryException {
  37. };
  38. class DbRunRecoveryException {
  39. };
  40. // DBT and Dbt objects are the same pointers. So watch out if you use Dbt to make other classes (e.g., with subclassing).
  41. class Dbt : private DBT {
  42. friend class Dbc;
  43. public:
  44. void * get_data(void) const { return data; }
  45. void set_data(void *p) { data = p; }
  46. u_int32_t get_size(void) const { return size; }
  47. void set_size(u_int32_t p) { size = p; }
  48. u_int32_t get_flags() const { return flags; }
  49. void set_flags(u_int32_t f) { flags = f; }
  50. u_int32_t get_ulen() const { return ulen; }
  51. void set_ulen(u_int32_t p) { ulen = p; }
  52. DBT *get_DBT(void) { return (DBT*)this; }
  53. const DBT *get_const_DBT(void) const { return (const DBT*)this; }
  54. static Dbt* get_Dbt(DBT *dbt) { return (Dbt *)dbt; }
  55. static const Dbt* get_const_Dbt(const DBT *dbt) { return (const Dbt *)dbt; }
  56. Dbt(void */*data*/, u_int32_t /*size*/);
  57. Dbt(void);
  58. ~Dbt();
  59. private:
  60. // Nothing here.
  61. };
  62. extern "C" {
  63. typedef int (*bt_compare_fcn_type)(DB *db, const DBT *dbt1, const DBT *dbt2);
  64. typedef int (*dup_compare_fcn_type)(DB *db, const DBT *dbt1, const DBT *dbt2);
  65. };
  66. class Db {
  67. public:
  68. /* Functions to make C++ work, defined in the BDB C++ API documents */
  69. Db(DbEnv *dbenv, u_int32_t flags);
  70. ~Db();
  71. DB *get_DB(void) {
  72. return the_db;
  73. }
  74. const DB *get_const_DB() const {
  75. return the_db;
  76. }
  77. static Db *get_Db(DB *db) {
  78. return (Db*)db->api_internal;
  79. }
  80. static const Db *get_const_Db(const DB *db) {
  81. return (Db*)db->api_internal;
  82. }
  83. /* C++ analogues of the C functions. */
  84. int open(DbTxn */*txn*/, const char */*name*/, const char */*subname*/, DBTYPE, u_int32_t/*flags*/, int/*mode*/);
  85. int close(u_int32_t /*flags*/);
  86. int cursor(DbTxn */*txn*/, Dbc **/*cursorp*/, u_int32_t /*flags*/);
  87. int del(DbTxn */*txn*/, Dbt */*key*/, u_int32_t /*flags*/);
  88. int get(DbTxn */*txn*/, Dbt */*key*/, Dbt */*data*/, u_int32_t /*flags*/);
  89. int pget(DbTxn *, Dbt *, Dbt *, Dbt *, u_int32_t);
  90. int put(DbTxn *, Dbt *, Dbt *, u_int32_t);
  91. int get_flags(u_int32_t *);
  92. int set_flags(u_int32_t);
  93. int set_pagesize(u_int32_t);
  94. int remove(const char *file, const char *database, u_int32_t flags);
  95. int set_bt_compare(bt_compare_fcn_type bt_compare_fcn);
  96. int set_bt_compare(int (*)(Db *, const Dbt *, const Dbt *));
  97. int set_dup_compare(dup_compare_fcn_type dup_compare_fcn);
  98. int set_dup_compare(int (*)(Db *, const Dbt *, const Dbt *));
  99. int associate(DbTxn *, Db *, int (*)(Db *, const Dbt *, const Dbt *, Dbt *), u_int32_t);
  100. int fd(int *);
  101. void set_errpfx(const char *errpfx);
  102. void set_error_stream(std::ostream *);
  103. /* the cxx callbacks must be public so they can be called by the c callback. But it's really private. */
  104. int (*associate_callback_cxx)(Db *, const Dbt *, const Dbt *, Dbt*);
  105. int (*bt_compare_callback_cxx)(Db *, const Dbt *, const Dbt *);
  106. int (*dup_compare_callback_cxx)(Db *, const Dbt *, const Dbt *);
  107. private:
  108. DB *the_db;
  109. DbEnv *the_Env;
  110. int is_private_env;
  111. };
  112. class DbEnv {
  113. friend class Db;
  114. friend class Dbc;
  115. friend class DbTxn;
  116. public:
  117. DbEnv(u_int32_t flags);
  118. ~DbEnv(void);
  119. DB_ENV *get_DB_ENV(void) {
  120. if (this==0) return 0;
  121. return the_env;
  122. }
  123. /* C++ analogues of the C functions. */
  124. int close(u_int32_t);
  125. int open(const char *, u_int32_t, int);
  126. int set_cachesize(u_int32_t, u_int32_t, int);
  127. int set_flags(u_int32_t, int);
  128. int txn_begin(DbTxn *, DbTxn **, u_int32_t);
  129. int set_data_dir(const char *dir);
  130. void set_errpfx(const char *errpfx);
  131. void err(int error, const char *fmt, ...)
  132. __attribute__((__format__(__printf__, 3, 4)));
  133. void set_errfile(FILE *errfile);
  134. void set_errcall(void (*)(const DbEnv *, const char *, const char *));
  135. void set_error_stream(std::ostream *);
  136. int get_flags(u_int32_t *flagsp);
  137. // locking
  138. #if DB_VERSION_MAJOR<4 || (DB_VERSION_MAJOR==4 && DB_VERSION_MINOR<=4)
  139. // set_lk_max is only defined for versions up to 4.4
  140. int set_lk_max(u_int32_t);
  141. #endif
  142. int set_lk_max_locks(u_int32_t);
  143. int get_lk_max_locks(u_int32_t *);
  144. // somewhat_private:
  145. int do_no_exceptions; // This should be private!!!
  146. void (*errcall)(const DbEnv *, const char *, const char *);
  147. std::ostream *_error_stream;
  148. private:
  149. DB_ENV *the_env;
  150. DbEnv(DB_ENV *, u_int32_t /*flags*/);
  151. int maybe_throw_error(int /*err*/) throw (DbException);
  152. static int maybe_throw_error(int, DbEnv*, int /*no_exceptions*/) throw (DbException);
  153. };
  154. class DbTxn {
  155. public:
  156. int commit (u_int32_t /*flags*/);
  157. int abort ();
  158. virtual ~DbTxn();
  159. DB_TXN *get_DB_TXN()
  160. {
  161. if (this==0) return 0;
  162. return the_txn;
  163. }
  164. DbTxn(DB_TXN*);
  165. private:
  166. DB_TXN *the_txn;
  167. };
  168. class Dbc : protected DBC {
  169. public:
  170. int close(void);
  171. int get(Dbt *, Dbt *, u_int32_t);
  172. int pget(Dbt *, Dbt *, Dbt *, u_int32_t);
  173. int del(u_int32_t);
  174. int count(db_recno_t *, u_int32_t);
  175. int put(Dbt *, Dbt *, u_int32_t);
  176. private:
  177. Dbc(); // User may not call it.
  178. ~Dbc(); // User may not delete it.
  179. };
  180. #endif