2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1997, 1998
5 * Sleepycat Software. All rights reserved.
7 * @(#)db_cxx.h 10.17 (Sleepycat) 5/2/98
15 // To ensure portability to many platforms, both new and old, we make
16 // few assumptions about the C++ compiler and library. For example,
17 // we do not expect STL, templates or namespaces to be available. The
18 // "newest" C++ feature used is exceptions, which are used liberally
19 // to transmit error information. Even the use of exceptions can be
20 // disabled at runtime, see setErrorModel().
22 // C++ naming conventions:
24 // - All top level class names start with Db.
25 // - All class members start with lower case letter.
26 // - All private data members are suffixed with underscore.
27 // - Use underscores to divide names into multiple words.
28 // - Simple data accessors are named with get_ or set_ prefix.
29 // - All method names are taken from names of functions in the C
30 // layer of db (usually by dropping a prefix like "db_").
31 // These methods have the same argument types and order,
32 // other than dropping the explicit arg that acts as "this".
34 // As a rule, each DbFoo object has exactly one underlying DB_FOO struct
35 // (defined in db.h) associated with it. In many cases, we inherit directly
36 // from the DB_FOO structure to make this relationship explicit. Often,
37 // the underlying C layer allocates and deallocates these structures, so
38 // there is no easy way to add any data to the DbFoo class. When you see
39 // a comment about whether data is permitted to be added, this is what
40 // is going on. Of course, if we need to add data to such C++ classes
41 // in the future, we will arrange to have an indirect pointer to the
42 // DB_FOO struct (as some of the classes already have).
46 ////////////////////////////////////////////////////////////////
47 ////////////////////////////////////////////////////////////////
49 // Forward declarations
56 class DbEnv
; // forward
57 class DbException
; // forward
58 class DbInfo
; // forward
59 class DbLock
; // forward
60 class DbLockTab
; // forward
61 class DbLog
; // forward
62 class DbLsn
; // forward
63 class DbMpool
; // forward
64 class DbMpoolFile
; // forward
66 class DbTxn
; // forward
67 class DbTxnMgr
; // forward
69 ////////////////////////////////////////////////////////////////
70 ////////////////////////////////////////////////////////////////
72 // Mechanisms for declaring classes
76 // Every class defined in this file has an _exported next to the class name.
77 // This is needed for WinTel machines so that the class methods can
78 // be exported or imported in a DLL as appropriate. Users of the DLL
79 // use the define DB_USE_DLL. When the DLL is built, DB_CREATE_DLL
84 # if defined(DB_CREATE_DLL)
85 # define _exported __declspec(dllexport) // creator of dll
86 # elif defined(DB_USE_DLL)
87 # define _exported __declspec(dllimport) // user of dll
89 # define _exported // static lib creator or user
98 // DEFINE_DB_CLASS defines an imp_ data member and imp() accessor.
99 // The underlying type is a pointer to an opaque *Imp class, that
100 // gets converted to the correct implementation class by the implementation.
102 // Since these defines use "private/public" labels, and leave the access
103 // being "private", we always use these by convention before any data
104 // members in the private section of a class. Keeping them in the
105 // private section also emphasizes that they are off limits to user code.
107 #define DEFINE_DB_CLASS(name) \
108 public: class name##Imp* imp() { return imp_; } \
109 public: const class name##Imp* imp() const { return imp_; } \
110 private: class name##Imp* imp_
113 ////////////////////////////////////////////////////////////////
114 ////////////////////////////////////////////////////////////////
116 // Turn off inappropriate compiler warnings
121 // These are level 4 warnings that are explicitly disabled.
122 // With Visual C++, by default you do not see above level 3 unless
123 // you use /W4. But we like to compile with the highest level
124 // warnings to catch other errors.
126 // 4201: nameless struct/union
127 // triggered by standard include file <winnt.h>
129 // 4514: unreferenced inline function has been removed
130 // certain include files in MSVC define methods that are not called
132 #pragma warning(disable: 4201 4514)
136 ////////////////////////////////////////////////////////////////
137 ////////////////////////////////////////////////////////////////
142 // Almost any error in the DB library throws a DbException.
143 // Every exception should be considered an abnormality
144 // (e.g. bug, misuse of DB, file system error).
146 // NOTE: We would like to inherit from class exception and
147 // let it handle what(), but there are
148 // MSVC++ problems when <exception> is included.
150 class _exported DbException
153 virtual ~DbException();
154 DbException(int err
);
155 DbException(const char *description
);
156 DbException(const char *prefix
, int err
);
157 DbException(const char *prefix1
, const char *prefix2
, int err
);
158 const int get_errno();
159 virtual const char *what() const;
161 DbException(const DbException
&);
162 DbException
&operator = (const DbException
&);
170 ////////////////////////////////////////////////////////////////
171 ////////////////////////////////////////////////////////////////
176 class _exported DbLock
185 void set_lock_id(u_int
);
187 int put(DbLockTab
*locktab
);
189 DbLock(const DbLock
&);
190 DbLock
&operator = (const DbLock
&);
193 // We can add data to this class if needed
194 // since its contained class is not allocated by db.
195 // (see comment at top)
200 class _exported DbLockTab
205 int detect(u_int32_t flags
, int atype
);
206 int get(u_int32_t locker
, u_int32_t flags
, const Dbt
*obj
,
207 db_lockmode_t lock_mode
, DbLock
*lock
);
208 int id(u_int32_t
*idp
);
209 int vec(u_int32_t locker
, u_int32_t flags
, DB_LOCKREQ list
[],
210 int nlist
, DB_LOCKREQ
**elistp
);
212 // Create or remove new locktab files
214 static int open(const char *dir
, u_int32_t flags
, int mode
,
215 DbEnv
* dbenv
, DbLockTab
**regionp
);
216 static int unlink(const char *dir
, int force
, DbEnv
* dbenv
);
219 // We can add data to this class if needed
220 // since it is implemented via a pointer.
221 // (see comment at top)
223 // copying not allowed
225 DbLockTab(const DbLockTab
&);
226 DbLockTab
&operator = (const DbLockTab
&);
228 // Note: use DbLockTab::open() or DbEnv::get_lk_info()
229 // to get pointers to a DbLockTab,
230 // and call DbLockTab::close() rather than delete to release them.
235 DEFINE_DB_CLASS(DbLockTab
);
239 ////////////////////////////////////////////////////////////////
240 ////////////////////////////////////////////////////////////////
245 class _exported DbLsn
: protected DB_LSN
247 friend DbLog
; // friendship needed to cast to base class
251 class _exported DbLog
255 int archive(char **list
[], u_int32_t flags
, void *(*db_malloc
)(size_t));
257 static int compare(const DbLsn
*lsn0
, const DbLsn
*lsn1
);
258 int file(DbLsn
*lsn
, char *namep
, int len
);
259 int flush(const DbLsn
*lsn
);
260 int get(DbLsn
*lsn
, Dbt
*data
, u_int32_t flags
);
261 int put(DbLsn
*lsn
, const Dbt
*data
, u_int32_t flags
);
263 // Normally these would be called register and unregister to
264 // parallel the C interface, but "register" is a reserved word.
266 int db_register(Db
*dbp
, const char *name
, DBTYPE type
, u_int32_t
*fidp
);
267 int db_unregister(u_int32_t fid
);
269 // Create or remove new log files
271 static int open(const char *dir
, u_int32_t flags
, int mode
,
272 DbEnv
* dbenv
, DbLog
**regionp
);
273 static int unlink(const char *dir
, int force
, DbEnv
* dbenv
);
276 // We can add data to this class if needed
277 // since it is implemented via a pointer.
278 // (see comment at top)
280 // Note: use DbLog::open() or DbEnv::get_lg_info()
281 // to get pointers to a DbLog,
282 // and call DbLog::close() rather than delete to release them.
288 DbLog(const DbLog
&);
289 operator = (const DbLog
&);
291 DEFINE_DB_CLASS(DbLog
);
295 ////////////////////////////////////////////////////////////////
296 ////////////////////////////////////////////////////////////////
298 // Memory pool classes
301 class _exported DbMpoolFile
306 int get(db_pgno_t
*pgnoaddr
, u_int32_t flags
, void *pagep
);
307 int put(void *pgaddr
, u_int32_t flags
);
308 int set(void *pgaddr
, u_int32_t flags
);
311 static int open(DbMpool
*mp
, const char *file
,
312 u_int32_t flags
, int mode
, size_t pagesize
,
313 DB_MPOOL_FINFO
*finfop
, DbMpoolFile
**mpf
);
316 // We can add data to this class if needed
317 // since it is implemented via a pointer.
318 // (see comment at top)
320 // Note: use DbMpoolFile::open()
321 // to get pointers to a DbMpoolFile,
322 // and call DbMpoolFile::close() rather than delete to release them.
332 DbMpoolFile(const DbMpoolFile
&);
333 operator = (const DbMpoolFile
&);
335 DEFINE_DB_CLASS(DbMpoolFile
);
338 class _exported DbMpool
344 // access to low level interface
345 // Normally this would be called register to parallel
346 // the C interface, but "register" is a reserved word.
348 int db_register(int ftype
,
349 int (*pgin
)(db_pgno_t pgno
, void *pgaddr
, DBT
*pgcookie
),
350 int (*pgout
)(db_pgno_t pgno
, void *pgaddr
, DBT
*pgcookie
));
352 int stat(DB_MPOOL_STAT
**gsp
, DB_MPOOL_FSTAT
***fsp
,
353 void *(*db_malloc
)(size_t));
354 int sync(DbLsn
*lsn
);
355 int trickle(int pct
, int *nwrotep
);
357 // Create or remove new mpool files
359 static int open(const char *dir
, u_int32_t flags
, int mode
,
360 DbEnv
* dbenv
, DbMpool
**regionp
);
361 static int unlink(const char *dir
, int force
, DbEnv
* dbenv
);
364 // We can add data to this class if needed
365 // since it is implemented via a pointer.
366 // (see comment at top)
368 // Note: use DbMpool::open() or DbEnv::get_mp_info()
369 // to get pointers to a DbMpool,
370 // and call DbMpool::close() rather than delete to release them.
376 DbMpool(const DbMpool
&);
377 DbMpool
&operator = (const DbMpool
&);
379 DEFINE_DB_CLASS(DbMpool
);
383 ////////////////////////////////////////////////////////////////
384 ////////////////////////////////////////////////////////////////
386 // Transaction classes
389 class _exported DbTxnMgr
393 int begin(DbTxn
*pid
, DbTxn
**tid
);
394 int checkpoint(u_int32_t kbyte
, u_int32_t min
) const;
396 int stat(DB_TXN_STAT
**statp
, void *(*db_malloc
)(size_t));
398 // Create or remove new txnmgr files
400 static int open(const char *dir
, u_int32_t flags
, int mode
,
401 DbEnv
* dbenv
, DbTxnMgr
**regionp
);
402 static int unlink(const char *dir
, int force
, DbEnv
* dbenv
);
405 // We can add data to this class if needed
406 // since it is implemented via a pointer.
407 // (see comment at top)
409 // Note: use DbTxnMgr::open() or DbEnv::get_tx_info()
410 // to get pointers to a DbTxnMgr,
411 // and call DbTxnMgr::close() rather than delete to release them.
417 DbTxnMgr(const DbTxnMgr
&);
418 operator = (const DbTxnMgr
&);
420 DEFINE_DB_CLASS(DbTxnMgr
);
423 class _exported DbTxn
433 // We can add data to this class if needed
434 // since it is implemented via a pointer.
435 // (see comment at top)
437 // Note: use DbTxnMgr::begin() to get pointers to a DbTxn,
438 // and call DbTxn::abort() or DbTxn::commit rather than
439 // delete to release them.
445 DbTxn(const DbTxn
&);
446 operator = (const DbTxn
&);
448 DEFINE_DB_CLASS(DbTxn
);
452 ////////////////////////////////////////////////////////////////
453 ////////////////////////////////////////////////////////////////
455 // Application classes
459 // A set of application options - define how this application uses
462 class _exported DbInfo
: protected DB_INFO
472 int get_lorder() const;
473 void set_lorder(int);
475 // Underlying cache size.
476 size_t get_cachesize() const;
477 void set_cachesize(size_t);
479 // Underlying page size.
480 size_t get_pagesize() const;
481 void set_pagesize(size_t);
483 // Local heap allocation.
484 typedef void *(*db_malloc_fcn
)(size_t);
485 db_malloc_fcn
get_malloc() const;
486 void set_malloc(db_malloc_fcn
);
488 ////////////////////////////////////////////////////////////////
489 // Btree access method.
491 // Maximum keys per page.
492 int get_bt_maxkey() const;
493 void set_bt_maxkey(int);
495 // Minimum keys per page.
496 int get_bt_minkey() const;
497 void set_bt_minkey(int);
499 // Comparison function.
500 typedef int (*bt_compare_fcn
)(const DBT
*, const DBT
*);
501 bt_compare_fcn
get_bt_compare() const;
502 void set_bt_compare(bt_compare_fcn
);
505 typedef size_t (*bt_prefix_fcn
)(const DBT
*, const DBT
*);
506 bt_prefix_fcn
get_bt_prefix() const;
507 void set_bt_prefix(bt_prefix_fcn
);
509 ////////////////////////////////////////////////////////////////
510 // Hash access method.
513 u_int32_t
get_h_ffactor() const;
514 void set_h_ffactor(u_int32_t
);
516 // Number of elements.
517 u_int32_t
get_h_nelem() const;
518 void set_h_nelem(u_int32_t
);
521 typedef u_int32_t (*h_hash_fcn
)(const void *, u_int32_t
);
522 h_hash_fcn
get_h_hash() const;
523 void set_h_hash(h_hash_fcn
);
525 ////////////////////////////////////////////////////////////////
526 // Recno access method.
528 // Fixed-length padding byte.
529 int get_re_pad() const;
530 void set_re_pad(int);
532 // Variable-length delimiting byte.
533 int get_re_delim() const;
534 void set_re_delim(int);
536 // Length for fixed-length records.
537 u_int32_t
get_re_len() const;
538 void set_re_len(u_int32_t
);
541 char *get_re_source() const;
542 void set_re_source(char *);
544 // Note: some flags are set as side effects of calling
545 // above "set" methods.
547 u_int32_t
get_flags() const;
548 void set_flags(u_int32_t
);
551 // (deep) copying of this object is allowed.
553 DbInfo(const DbInfo
&);
554 DbInfo
&operator = (const DbInfo
&);
557 // We can add data to this class if needed
558 // since parent class is not allocated by db.
559 // (see comment at top)
563 // Base application class. Provides functions for opening a database.
564 // User of this library can use this class as a starting point for
565 // developing a DB application - derive their application class from
566 // this one, add application control logic.
568 // Note that if you use the default constructor, you must explicitly
569 // call appinit() before any other db activity (e.g. opening files)
571 class _exported DbEnv
: protected DB_ENV
583 // This constructor can be used to immediately initialize the
584 // application with these arguments. Do not use it if you
585 // need to set other parameters via the access methods.
587 DbEnv(const char *homeDir
, char *const *db_config
, u_int32_t flags
);
589 // Use this constructor if you wish to *delay* the initialization
590 // of the db library. This is useful if you need to set
591 // any particular parameters via the access methods below.
592 // Then call appinit() to complete the initialization.
596 // Used in conjunction with the default constructor to
597 // complete the initialization of the db library.
599 int appinit(const char *homeDir
, char *const *db_config
, u_int32_t flags
);
601 // Called automatically when DbEnv is destroyed, or can be
602 // called at any time to shut down Db.
606 ////////////////////////////////////////////////////////////////
607 // simple get/set access methods
609 // If you are calling set_ methods, you need to
610 // use the default constructor along with appinit().
613 int get_lorder() const;
614 void set_lorder(int);
616 // Error message callback.
617 typedef void (*db_errcall_fcn
)(const char *, char *);
618 db_errcall_fcn
get_errcall() const;
619 void set_errcall(db_errcall_fcn
);
621 // Error message file stream.
622 FILE *get_errfile() const;
623 void set_errfile(FILE *);
625 // Error message prefix.
626 const char *get_errpfx() const;
627 void set_errpfx(const char *);
629 // Generate debugging messages.
630 int get_verbose() const;
631 void set_verbose(int);
633 ////////////////////////////////////////////////////////////////
637 char *get_home() const;
638 void set_home(char *);
640 // Database log file directory.
641 char *get_log_dir() const;
642 void set_log_dir(char *);
644 // Database tmp file directory.
645 char *get_tmp_dir() const;
646 void set_tmp_dir(char *);
648 // Database data file directories.
649 char **get_data_dir() const;
650 void set_data_dir(char **);
652 // Database data file slots.
653 int get_data_cnt() const;
654 void set_data_cnt(int);
656 // Next Database data file slot.
657 int get_data_next() const;
658 void set_data_next(int);
661 ////////////////////////////////////////////////////////////////
664 // Return from lock_open().
665 DbLockTab
*get_lk_info() const;
667 // Two dimensional conflict matrix.
668 u_int8_t
*get_lk_conflicts() const;
669 void set_lk_conflicts(u_int8_t
*);
671 // Number of lock modes in table.
672 int get_lk_modes() const;
673 void set_lk_modes(int);
675 // Maximum number of locks.
676 u_int32_t
get_lk_max() const;
677 void set_lk_max(u_int32_t
);
679 // Deadlock detect on every conflict.
680 u_int32_t
get_lk_detect() const;
681 void set_lk_detect(u_int32_t
);
684 ////////////////////////////////////////////////////////////////
687 // Return from log_open().
688 DbLog
*get_lg_info() const;
690 // Maximum file size.
691 u_int32_t
get_lg_max() const;
692 void set_lg_max(u_int32_t
);
695 ////////////////////////////////////////////////////////////////
698 // Return from memp_open().
699 DbMpool
*get_mp_info() const;
701 // Maximum file size for mmap.
702 size_t get_mp_mmapsize() const;
703 void set_mp_mmapsize(size_t);
705 // Bytes in the mpool cache.
706 size_t get_mp_size() const;
707 void set_mp_size(size_t);
710 ////////////////////////////////////////////////////////////////
713 // Return from txn_open().
714 DbTxnMgr
*get_tx_info() const;
716 // Maximum number of transactions.
717 u_int32_t
get_tx_max() const;
718 void set_tx_max(u_int32_t
);
720 // Dispatch function for recovery.
721 typedef int (*tx_recover_fcn
)(DB_LOG
*, DBT
*, DB_LSN
*, int, void *);
722 tx_recover_fcn
get_tx_recover() const;
723 void set_tx_recover(tx_recover_fcn
);
726 u_int32_t
get_flags() const;
727 void set_flags(u_int32_t
);
729 ////////////////////////////////////////////////////////////////
730 // The default error model is to throw an exception whenever
731 // an error occurs. This generally allows for cleaner logic
732 // for transaction processing, as a try block can surround a
733 // single transaction. Alternatively, since almost every method
734 // returns an error code (errno), the error model can be set to
735 // not throw exceptions, and instead return the appropriate code.
737 enum ErrorModel
{ Exception
, ErrorReturn
};
738 void set_error_model(ErrorModel
);
739 ErrorModel
get_error_model() const;
741 // If an error is detected and the error call function
742 // or stream is set, a message is dispatched or printed.
743 // If a prefix is set, each message is prefixed.
745 // You can use set_errcall() or set_errfile() above to control
746 // error functionality using a C model. Alternatively, you can
747 // call set_error_stream() to force all errors to a C++ stream.
748 // It is unwise to mix these approaches.
750 class ostream
* get_error_stream() const;
751 void set_error_stream(class ostream
*);
754 static int runtime_error(const char *caller
, int err
, int in_destructor
= 0);
757 // We can add data to this class if needed
758 // since parent class is not allocated by db.
759 // (see comment at top)
762 DbEnv(const DbEnv
&);
763 operator = (const DbEnv
&);
765 ErrorModel error_model_
;
766 static void stream_error_function(const char *, char *);
767 static ostream
*error_stream_
;
770 ////////////////////////////////////////////////////////////////
771 ////////////////////////////////////////////////////////////////
773 // Table access classes
777 // Represents a database table = a set of keys with associated values.
784 int close(u_int32_t flags
);
785 int cursor(DbTxn
*txnid
, Dbc
**cursorp
);
786 int del(DbTxn
*txnid
, Dbt
*key
, u_int32_t flags
);
788 int get(DbTxn
*txnid
, Dbt
*key
, Dbt
*data
, u_int32_t flags
);
789 int put(DbTxn
*txnid
, Dbt
*key
, Dbt
*data
, u_int32_t flags
);
790 int stat(void *sp
, void *(*db_malloc
)(size_t), u_int32_t flags
);
791 int sync(u_int32_t flags
);
793 DBTYPE
get_type() const;
795 static int open(const char *fname
, DBTYPE type
, u_int32_t flags
,
796 int mode
, DbEnv
*dbenv
, DbInfo
*info
, Db
**dbpp
);
799 // We can add data to this class if needed
800 // since it is implemented via a pointer.
801 // (see comment at top)
803 // Note: use Db::open() to get initialize pointers to a Db,
804 // and call Db::close() rather than delete to release them.
810 Db
&operator = (const Db
&);
816 // A chunk of data, maybe a key or value.
818 class _exported Dbt
: private DBT
829 void *get_data() const;
830 void set_data(void *);
833 u_int32_t
get_size() const;
834 void set_size(u_int32_t
);
836 // RO: length of user buffer.
837 u_int32_t
get_ulen() const;
838 void set_ulen(u_int32_t
);
840 // RO: get/put record length.
841 u_int32_t
get_dlen() const;
842 void set_dlen(u_int32_t
);
844 // RO: get/put record offset.
845 u_int32_t
get_doff() const;
846 void set_doff(u_int32_t
);
849 u_int32_t
get_flags() const;
850 void set_flags(u_int32_t
);
852 Dbt(void *data
, size_t size
);
856 Dbt
&operator = (const Dbt
&);
859 // We can add data to this class if needed
860 // since parent class is not allocated by db.
861 // (see comment at top)
864 class _exported Dbc
: protected DBC
870 int del(u_int32_t flags
);
871 int get(Dbt
* key
, Dbt
*data
, u_int32_t flags
);
872 int put(Dbt
* key
, Dbt
*data
, u_int32_t flags
);
875 // No data is permitted in this class (see comment at top)
877 // Note: use Db::cursor() to get pointers to a Dbc,
878 // and call Dbc::close() rather than delete to release them.
885 Dbc
&operator = (const Dbc
&);
887 #endif /* !_DB_CXX_H_ */