1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1997,2008 Oracle.  All rights reserved.
5 *
6 * $Id: db_cxx.in,v 12.49 2008/01/12 13:42:37 bostic Exp $
7 */
8
9#ifndef _DB_CXX_H_
10#define	_DB_CXX_H_
11//
12// C++ assumptions:
13//
14// To ensure portability to many platforms, both new and old, we make
15// few assumptions about the C++ compiler and library.  For example,
16// we do not expect STL, templates or namespaces to be available.  The
17// "newest" C++ feature used is exceptions, which are used liberally
18// to transmit error information.  Even the use of exceptions can be
19// disabled at runtime, to do so, use the DB_CXX_NO_EXCEPTIONS flags
20// with the DbEnv or Db constructor.
21//
22// C++ naming conventions:
23//
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".
33//
34// As a rule, each DbFoo object has exactly one underlying DB_FOO struct
35// (defined in db.h) associated with it.  In some 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).
43//
44
45////////////////////////////////////////////////////////////////
46////////////////////////////////////////////////////////////////
47//
48// Forward declarations
49//
50
51#include <stdarg.h>
52
53@cxx_have_stdheaders@
54#ifdef HAVE_CXX_STDHEADERS
55#include <iostream>
56#include <exception>
57#define	__DB_STD(x)	std::x
58#else
59#include <iostream.h>
60#include <exception.h>
61#define	__DB_STD(x)	x
62#endif
63
64#include "db.h"
65
66class Db;                                        // forward
67class Dbc;                                       // forward
68class DbEnv;                                     // forward
69class DbInfo;                                    // forward
70class DbLock;                                    // forward
71class DbLogc;                                    // forward
72class DbLsn;                                     // forward
73class DbMpoolFile;                               // forward
74class DbPreplist;                                // forward
75class DbSequence;                                // forward
76class Dbt;                                       // forward
77class DbTxn;                                     // forward
78
79class DbMultipleIterator;                        // forward
80class DbMultipleKeyDataIterator;                 // forward
81class DbMultipleRecnoDataIterator;               // forward
82class DbMultipleDataIterator;                    // forward
83
84class DbException;                               // forward
85class DbDeadlockException;                       // forward
86class DbLockNotGrantedException;                 // forward
87class DbMemoryException;                         // forward
88class DbRepHandleDeadException;                  // forward
89class DbRunRecoveryException;                    // forward
90
91////////////////////////////////////////////////////////////////
92////////////////////////////////////////////////////////////////
93//
94// Turn off inappropriate compiler warnings
95//
96
97#ifdef _MSC_VER
98
99// These are level 4 warnings that are explicitly disabled.
100// With Visual C++, by default you do not see above level 3 unless
101// you use /W4.  But we like to compile with the highest level
102// warnings to catch other errors.
103//
104// 4201: nameless struct/union
105//       triggered by standard include file <winnt.h>
106//
107// 4514: unreferenced inline function has been removed
108//       certain include files in MSVC define methods that are not called
109//
110#pragma warning(push)
111#pragma warning(disable: 4201 4514)
112
113#endif
114
115////////////////////////////////////////////////////////////////
116////////////////////////////////////////////////////////////////
117//
118// Mechanisms for declaring classes
119//
120
121//
122// Every class defined in this file has an _exported next to the class name.
123// This is needed for WinTel machines so that the class methods can
124// be exported or imported in a DLL as appropriate.  Users of the DLL
125// use the define DB_USE_DLL.  When the DLL is built, DB_CREATE_DLL
126// must be defined.
127//
128#if defined(_MSC_VER)
129
130#  if defined(DB_CREATE_DLL)
131#    define _exported __declspec(dllexport)      // creator of dll
132#  elif defined(DB_USE_DLL)
133#    define _exported __declspec(dllimport)      // user of dll
134#  else
135#    define _exported                            // static lib creator or user
136#  endif
137
138#else /* _MSC_VER */
139
140#  define _exported
141
142#endif /* _MSC_VER */
143
144// Some interfaces can be customized by allowing users to define
145// callback functions.  For performance and logistical reasons, some
146// callback functions must be declared in extern "C" blocks.  For others,
147// we allow you to declare the callbacks in C++ or C (or an extern "C"
148// block) as you wish.  See the set methods for the callbacks for
149// the choices.
150//
151extern "C" {
152	typedef void * (*db_malloc_fcn_type)
153		(size_t);
154	typedef void * (*db_realloc_fcn_type)
155		(void *, size_t);
156	typedef void (*db_free_fcn_type)
157		(void *);
158	typedef int (*bt_compare_fcn_type)          /*C++ version available*/
159		(DB *, const DBT *, const DBT *);
160	typedef size_t (*bt_prefix_fcn_type)        /*C++ version available*/
161		(DB *, const DBT *, const DBT *);
162	typedef int (*dup_compare_fcn_type)         /*C++ version available*/
163		(DB *, const DBT *, const DBT *);
164	typedef int (*h_compare_fcn_type)          /*C++ version available*/
165		(DB *, const DBT *, const DBT *);
166	typedef u_int32_t (*h_hash_fcn_type)        /*C++ version available*/
167		(DB *, const void *, u_int32_t);
168	typedef int (*pgin_fcn_type)
169		(DB_ENV *dbenv, db_pgno_t pgno, void *pgaddr, DBT *pgcookie);
170	typedef int (*pgout_fcn_type)
171		(DB_ENV *dbenv, db_pgno_t pgno, void *pgaddr, DBT *pgcookie);
172}
173
174//
175// Represents a database table = a set of keys with associated values.
176//
177class _exported Db
178{
179	friend class DbEnv;
180
181public:
182	Db(DbEnv*, u_int32_t);      // Create a Db object.
183	virtual ~Db();              // Calls close() if the user hasn't.
184
185	// These methods exactly match those in the C interface.
186	//
187	virtual int associate(DbTxn *txn, Db *secondary, int (*callback)
188	    (Db *, const Dbt *, const Dbt *, Dbt *), u_int32_t flags);
189	virtual int close(u_int32_t flags);
190	virtual int compact(DbTxn *txnid, Dbt *start,
191	    Dbt *stop, DB_COMPACT *c_data, u_int32_t flags, Dbt *end);
192	virtual int cursor(DbTxn *txnid, Dbc **cursorp, u_int32_t flags);
193	virtual int del(DbTxn *txnid, Dbt *key, u_int32_t flags);
194	virtual void err(int, const char *, ...);
195	virtual void errx(const char *, ...);
196	virtual int exists(DbTxn *txnid, Dbt *key, u_int32_t flags);
197	virtual int fd(int *fdp);
198	virtual int get(DbTxn *txnid, Dbt *key, Dbt *data, u_int32_t flags);
199	virtual int get_bt_minkey(u_int32_t *);
200	virtual int get_byteswapped(int *);
201	virtual int get_cachesize(u_int32_t *, u_int32_t *, int *);
202	virtual int get_dbname(const char **, const char **);
203	virtual int get_encrypt_flags(u_int32_t *);
204	virtual void get_errfile(FILE **);
205	virtual void get_errpfx(const char **);
206	virtual int get_flags(u_int32_t *);
207	virtual int get_h_ffactor(u_int32_t *);
208	virtual int get_h_nelem(u_int32_t *);
209	virtual int get_lorder(int *);
210	virtual void get_msgfile(FILE **);
211	virtual int get_multiple();
212	virtual int get_open_flags(u_int32_t *);
213	virtual int get_pagesize(u_int32_t *);
214	virtual int get_priority(DB_CACHE_PRIORITY *);
215	virtual int get_q_extentsize(u_int32_t *);
216	virtual int get_re_delim(int *);
217	virtual int get_re_len(u_int32_t *);
218	virtual int get_re_pad(int *);
219	virtual int get_re_source(const char **);
220	virtual int get_transactional();
221	virtual int get_type(DBTYPE *);
222	virtual int join(Dbc **curslist, Dbc **dbcp, u_int32_t flags);
223	virtual int key_range(DbTxn *, Dbt *, DB_KEY_RANGE *, u_int32_t);
224	virtual int open(DbTxn *txnid,
225	    const char *, const char *subname, DBTYPE, u_int32_t, int);
226	virtual int pget(DbTxn *txnid,
227	    Dbt *key, Dbt *pkey, Dbt *data, u_int32_t flags);
228	virtual int put(DbTxn *, Dbt *, Dbt *, u_int32_t);
229	virtual int remove(const char *, const char *, u_int32_t);
230	virtual int rename(const char *, const char *, const char *, u_int32_t);
231	virtual int set_alloc(
232	    db_malloc_fcn_type, db_realloc_fcn_type, db_free_fcn_type);
233	virtual void set_app_private(void *);
234	virtual int set_append_recno(int (*)(Db *, Dbt *, db_recno_t));
235	virtual int set_bt_compare(bt_compare_fcn_type); /*deprecated*/
236	virtual int set_bt_compare(int (*)(Db *, const Dbt *, const Dbt *));
237	virtual int set_bt_minkey(u_int32_t);
238	virtual int set_bt_prefix(bt_prefix_fcn_type); /*deprecated*/
239	virtual int set_bt_prefix(size_t (*)(Db *, const Dbt *, const Dbt *));
240	virtual int set_cachesize(u_int32_t, u_int32_t, int);
241	virtual int set_dup_compare(dup_compare_fcn_type); /*deprecated*/
242	virtual int set_dup_compare(int (*)(Db *, const Dbt *, const Dbt *));
243	virtual int set_encrypt(const char *, u_int32_t);
244	virtual void set_errcall(
245	    void (*)(const DbEnv *, const char *, const char *));
246	virtual void set_errfile(FILE *);
247	virtual void set_errpfx(const char *);
248	virtual int set_feedback(void (*)(Db *, int, int));
249	virtual int set_flags(u_int32_t);
250	virtual int set_h_compare(h_compare_fcn_type); /*deprecated*/
251	virtual int set_h_compare(int (*)(Db *, const Dbt *, const Dbt *));
252	virtual int set_h_ffactor(u_int32_t);
253	virtual int set_h_hash(h_hash_fcn_type); /*deprecated*/
254	virtual int set_h_hash(u_int32_t (*)(Db *, const void *, u_int32_t));
255	virtual int set_h_nelem(u_int32_t);
256	virtual int set_lorder(int);
257	virtual void set_msgcall(void (*)(const DbEnv *, const char *));
258	virtual void set_msgfile(FILE *);
259	virtual int set_pagesize(u_int32_t);
260	virtual int set_paniccall(void (*)(DbEnv *, int));
261	virtual int set_priority(DB_CACHE_PRIORITY);
262	virtual int set_q_extentsize(u_int32_t);
263	virtual int set_re_delim(int);
264	virtual int set_re_len(u_int32_t);
265	virtual int set_re_pad(int);
266	virtual int set_re_source(const char *);
267	virtual int stat(DbTxn *, void *sp, u_int32_t flags);
268	virtual int stat_print(u_int32_t flags);
269	virtual int sync(u_int32_t flags);
270	virtual int truncate(DbTxn *, u_int32_t *, u_int32_t);
271	virtual int upgrade(const char *name, u_int32_t flags);
272	virtual int verify(
273	    const char *, const char *, __DB_STD(ostream) *, u_int32_t);
274
275	// These additional methods are not in the C interface, and
276	// are only available for C++.
277	//
278	virtual void *get_app_private() const;
279	virtual __DB_STD(ostream) *get_error_stream();
280	virtual void set_error_stream(__DB_STD(ostream) *);
281	virtual __DB_STD(ostream) *get_message_stream();
282	virtual void set_message_stream(__DB_STD(ostream) *);
283
284	virtual DbEnv *get_env();
285	virtual DbMpoolFile *get_mpf();
286
287	virtual ENV *get_ENV()
288	{
289		return imp_->env;
290	}
291
292	virtual DB *get_DB()
293	{
294		return imp_;
295	}
296
297	virtual const DB *get_const_DB() const
298	{
299		return imp_;
300	}
301
302	static Db* get_Db(DB *db)
303	{
304		return (Db *)db->api_internal;
305	}
306
307	static const Db* get_const_Db(const DB *db)
308	{
309		return (const Db *)db->api_internal;
310	}
311
312private:
313	// no copying
314	Db(const Db &);
315	Db &operator = (const Db &);
316
317	void cleanup();
318	int initialize();
319	int error_policy();
320
321	// instance data
322	DB *imp_;
323	DbEnv *dbenv_;
324	DbMpoolFile *mpf_;
325	int construct_error_;
326	u_int32_t flags_;
327	u_int32_t construct_flags_;
328
329public:
330	// These are public only because they need to be called
331	// via C callback functions.  They should never be used by
332	// external users of this class.
333	//
334	int (*append_recno_callback_)(Db *, Dbt *, db_recno_t);
335	int (*associate_callback_)(Db *, const Dbt *, const Dbt *, Dbt *);
336	int (*bt_compare_callback_)(Db *, const Dbt *, const Dbt *);
337	size_t (*bt_prefix_callback_)(Db *, const Dbt *, const Dbt *);
338	int (*dup_compare_callback_)(Db *, const Dbt *, const Dbt *);
339	void (*feedback_callback_)(Db *, int, int);
340	int (*h_compare_callback_)(Db *, const Dbt *, const Dbt *);
341	u_int32_t (*h_hash_callback_)(Db *, const void *, u_int32_t);
342};
343
344//
345// Cursor
346//
347class _exported Dbc : protected DBC
348{
349	friend class Db;
350
351public:
352	int close();
353	int count(db_recno_t *countp, u_int32_t flags);
354	int del(u_int32_t flags);
355	int dup(Dbc** cursorp, u_int32_t flags);
356	int get(Dbt* key, Dbt *data, u_int32_t flags);
357	int get_priority(DB_CACHE_PRIORITY *priorityp);
358	int pget(Dbt* key, Dbt* pkey, Dbt *data, u_int32_t flags);
359	int put(Dbt* key, Dbt *data, u_int32_t flags);
360	int set_priority(DB_CACHE_PRIORITY priority);
361
362private:
363	// No data is permitted in this class (see comment at top)
364
365	// Note: use Db::cursor() to get pointers to a Dbc,
366	// and call Dbc::close() rather than delete to release them.
367	//
368	Dbc();
369	~Dbc();
370
371	// no copying
372	Dbc(const Dbc &);
373	Dbc &operator = (const Dbc &);
374};
375
376//
377// Berkeley DB environment class.  Provides functions for opening databases.
378// User of this library can use this class as a starting point for
379// developing a DB application - derive their application class from
380// this one, add application control logic.
381//
382// Note that if you use the default constructor, you must explicitly
383// call appinit() before any other db activity (e.g. opening files)
384//
385class _exported DbEnv
386{
387	friend class Db;
388	friend class DbLock;
389	friend class DbMpoolFile;
390
391public:
392	// After using this constructor, you can set any needed
393	// parameters for the environment using the set_* methods.
394	// Then call open() to finish initializing the environment
395	// and attaching it to underlying files.
396	//
397	DbEnv(u_int32_t flags);
398
399	virtual ~DbEnv();
400
401	// These methods match those in the C interface.
402	//
403	virtual int cdsgroup_begin(DbTxn **tid);
404	virtual int close(u_int32_t);
405	virtual int dbremove(DbTxn *txn, const char *name, const char *subdb,
406	    u_int32_t flags);
407	virtual int dbrename(DbTxn *txn, const char *name, const char *subdb,
408	    const char *newname, u_int32_t flags);
409	virtual void err(int, const char *, ...);
410	virtual void errx(const char *, ...);
411	virtual int failchk(u_int32_t);
412	virtual int fileid_reset(const char *, u_int32_t);
413	virtual void *get_app_private() const;
414	virtual int get_home(const char **);
415	virtual int get_open_flags(u_int32_t *);
416	virtual int open(const char *, u_int32_t, int);
417	virtual int remove(const char *, u_int32_t);
418	virtual int stat_print(u_int32_t flags);
419
420	virtual int set_alloc(db_malloc_fcn_type, db_realloc_fcn_type,
421			      db_free_fcn_type);
422	virtual void set_app_private(void *);
423	virtual int get_cachesize(u_int32_t *, u_int32_t *, int *);
424	virtual int set_cachesize(u_int32_t, u_int32_t, int);
425	virtual int get_cache_max(u_int32_t *, u_int32_t *);
426	virtual int set_cache_max(u_int32_t, u_int32_t);
427	virtual int get_data_dirs(const char ***);
428	virtual int set_data_dir(const char *);
429	virtual int get_encrypt_flags(u_int32_t *);
430	virtual int get_intermediate_dir_mode(const char **);
431	virtual int set_intermediate_dir_mode(const char *);
432	virtual int set_isalive(
433			int (*)(DbEnv *, pid_t, db_threadid_t, u_int32_t));
434	virtual int set_encrypt(const char *, u_int32_t);
435	virtual void set_errcall(
436			void (*)(const DbEnv *, const char *, const char *));
437	virtual void get_errfile(FILE **);
438	virtual void set_errfile(FILE *);
439	virtual void get_errpfx(const char **);
440	virtual void set_errpfx(const char *);
441	virtual int set_event_notify(void (*)(DbEnv *, u_int32_t, void *));
442	virtual int get_flags(u_int32_t *);
443	virtual int set_flags(u_int32_t, int);
444	virtual bool is_bigendian();
445	virtual int lsn_reset(const char *, u_int32_t);
446	virtual int set_feedback(void (*)(DbEnv *, int, int));
447	virtual int get_lg_bsize(u_int32_t *);
448	virtual int set_lg_bsize(u_int32_t);
449	virtual int get_lg_dir(const char **);
450	virtual int set_lg_dir(const char *);
451	virtual int get_lg_filemode(int *);
452	virtual int set_lg_filemode(int);
453	virtual int get_lg_max(u_int32_t *);
454	virtual int set_lg_max(u_int32_t);
455	virtual int get_lg_regionmax(u_int32_t *);
456	virtual int set_lg_regionmax(u_int32_t);
457	virtual int get_lk_conflicts(const u_int8_t **, int *);
458	virtual int set_lk_conflicts(u_int8_t *, int);
459	virtual int get_lk_detect(u_int32_t *);
460	virtual int set_lk_detect(u_int32_t);
461	virtual int get_lk_max_lockers(u_int32_t *);
462	virtual int set_lk_max_lockers(u_int32_t);
463	virtual int get_lk_max_locks(u_int32_t *);
464	virtual int set_lk_max_locks(u_int32_t);
465	virtual int get_lk_max_objects(u_int32_t *);
466	virtual int set_lk_max_objects(u_int32_t);
467	virtual int get_mp_mmapsize(size_t *);
468	virtual int set_mp_mmapsize(size_t);
469	virtual int get_mp_max_openfd(int *);
470	virtual int set_mp_max_openfd(int);
471	virtual int get_mp_max_write(int *, db_timeout_t *);
472	virtual int set_mp_max_write(int, db_timeout_t);
473	virtual void set_msgcall(void (*)(const DbEnv *, const char *));
474	virtual void get_msgfile(FILE **);
475	virtual void set_msgfile(FILE *);
476	virtual int set_paniccall(void (*)(DbEnv *, int));
477	virtual int set_rpc_server(void *, char *, long, long, u_int32_t);
478	virtual int get_shm_key(long *);
479	virtual int set_shm_key(long);
480	virtual int get_timeout(db_timeout_t *, u_int32_t);
481	virtual int set_timeout(db_timeout_t, u_int32_t);
482	virtual int get_tmp_dir(const char **);
483	virtual int set_tmp_dir(const char *);
484	virtual int get_tx_max(u_int32_t *);
485	virtual int set_tx_max(u_int32_t);
486	virtual int set_app_dispatch(int (*)(DbEnv *,
487	    Dbt *, DbLsn *, db_recops));
488	virtual int get_tx_timestamp(time_t *);
489	virtual int set_tx_timestamp(time_t *);
490	virtual int get_verbose(u_int32_t which, int *);
491	virtual int set_verbose(u_int32_t which, int);
492
493	// Version information.  A static method so it can be obtained anytime.
494	//
495	static char *version(int *major, int *minor, int *patch);
496
497	// Convert DB errors to strings
498	static char *strerror(int);
499
500	// If an error is detected and the error call function
501	// or stream is set, a message is dispatched or printed.
502	// If a prefix is set, each message is prefixed.
503	//
504	// You can use set_errcall() or set_errfile() above to control
505	// error functionality.  Alternatively, you can call
506	// set_error_stream() to force all errors to a C++ stream.
507	// It is unwise to mix these approaches.
508	//
509	virtual __DB_STD(ostream) *get_error_stream();
510	virtual void set_error_stream(__DB_STD(ostream) *);
511	virtual __DB_STD(ostream) *get_message_stream();
512	virtual void set_message_stream(__DB_STD(ostream) *);
513
514	// used internally
515	static void runtime_error(DbEnv *dbenv, const char *caller, int err,
516				  int error_policy);
517	static void runtime_error_dbt(DbEnv *dbenv, const char *caller, Dbt *dbt,
518				  int error_policy);
519	static void runtime_error_lock_get(DbEnv *dbenv, const char *caller,
520				  int err, db_lockop_t op, db_lockmode_t mode,
521				  const Dbt *obj, DbLock lock, int index,
522				  int error_policy);
523
524	// Lock functions
525	//
526	virtual int lock_detect(u_int32_t flags, u_int32_t atype, int *aborted);
527	virtual int lock_get(u_int32_t locker, u_int32_t flags, const Dbt *obj,
528		     db_lockmode_t lock_mode, DbLock *lock);
529	virtual int lock_id(u_int32_t *idp);
530	virtual int lock_id_free(u_int32_t id);
531	virtual int lock_put(DbLock *lock);
532	virtual int lock_stat(DB_LOCK_STAT **statp, u_int32_t flags);
533	virtual int lock_stat_print(u_int32_t flags);
534	virtual int lock_vec(u_int32_t locker, u_int32_t flags,
535		     DB_LOCKREQ list[], int nlist, DB_LOCKREQ **elistp);
536
537	// Log functions
538	//
539	virtual int log_archive(char **list[], u_int32_t flags);
540	static int log_compare(const DbLsn *lsn0, const DbLsn *lsn1);
541	virtual int log_cursor(DbLogc **cursorp, u_int32_t flags);
542	virtual int log_file(DbLsn *lsn, char *namep, size_t len);
543	virtual int log_flush(const DbLsn *lsn);
544	virtual int log_get_config(u_int32_t, int *);
545	virtual int log_put(DbLsn *lsn, const Dbt *data, u_int32_t flags);
546	virtual int log_printf(DbTxn *, const char *, ...);
547	virtual int log_set_config(u_int32_t, int);
548	virtual int log_stat(DB_LOG_STAT **spp, u_int32_t flags);
549	virtual int log_stat_print(u_int32_t flags);
550
551	// Mpool functions
552	//
553	virtual int memp_fcreate(DbMpoolFile **dbmfp, u_int32_t flags);
554	virtual int memp_register(int ftype,
555			  pgin_fcn_type pgin_fcn,
556			  pgout_fcn_type pgout_fcn);
557	virtual int memp_stat(DB_MPOOL_STAT
558		      **gsp, DB_MPOOL_FSTAT ***fsp, u_int32_t flags);
559	virtual int memp_stat_print(u_int32_t flags);
560	virtual int memp_sync(DbLsn *lsn);
561	virtual int memp_trickle(int pct, int *nwrotep);
562
563	// Mpool functions
564	//
565	virtual int mutex_alloc(u_int32_t, db_mutex_t *);
566	virtual int mutex_free(db_mutex_t);
567	virtual int mutex_get_align(u_int32_t *);
568	virtual int mutex_get_increment(u_int32_t *);
569	virtual int mutex_get_max(u_int32_t *);
570	virtual int mutex_get_tas_spins(u_int32_t *);
571	virtual int mutex_lock(db_mutex_t);
572	virtual int mutex_set_align(u_int32_t);
573	virtual int mutex_set_increment(u_int32_t);
574	virtual int mutex_set_max(u_int32_t);
575	virtual int mutex_set_tas_spins(u_int32_t);
576	virtual int mutex_stat(DB_MUTEX_STAT **, u_int32_t);
577	virtual int mutex_stat_print(u_int32_t);
578	virtual int mutex_unlock(db_mutex_t);
579
580	// Transaction functions
581	//
582	virtual int txn_begin(DbTxn *pid, DbTxn **tid, u_int32_t flags);
583	virtual int txn_checkpoint(u_int32_t kbyte, u_int32_t min,
584			u_int32_t flags);
585	virtual int txn_recover(DbPreplist *preplist, long count,
586			long *retp, u_int32_t flags);
587	virtual int txn_stat(DB_TXN_STAT **statp, u_int32_t flags);
588	virtual int txn_stat_print(u_int32_t flags);
589
590	// Replication functions
591	//
592	virtual int rep_elect(u_int32_t, u_int32_t, u_int32_t);
593	virtual int rep_flush();
594	virtual int rep_process_message(Dbt *, Dbt *, int, DbLsn *);
595	virtual int rep_start(Dbt *, u_int32_t);
596	virtual int rep_stat(DB_REP_STAT **statp, u_int32_t flags);
597	virtual int rep_stat_print(u_int32_t flags);
598	virtual int rep_get_clockskew(u_int32_t *, u_int32_t *);
599	virtual int rep_set_clockskew(u_int32_t, u_int32_t);
600	virtual int rep_get_limit(u_int32_t *, u_int32_t *);
601	virtual int rep_set_limit(u_int32_t, u_int32_t);
602	virtual int rep_set_transport(int, int (*)(DbEnv *,
603	    const Dbt *, const Dbt *, const DbLsn *, int, u_int32_t));
604	virtual int rep_set_request(u_int32_t, u_int32_t);
605	virtual int rep_get_request(u_int32_t *, u_int32_t *);
606	virtual int get_thread_count(u_int32_t *);
607	virtual int set_thread_count(u_int32_t);
608	virtual int set_thread_id(void (*)(DbEnv *, pid_t *, db_threadid_t *));
609	virtual int set_thread_id_string(char *(*)(DbEnv *,
610	    pid_t, db_threadid_t, char *));
611	virtual int rep_set_config(u_int32_t, int);
612	virtual int rep_get_config(u_int32_t, int *);
613	virtual int rep_sync(u_int32_t flags);
614
615	// Advanced replication functions
616	//
617	virtual int rep_get_nsites(u_int32_t *n);
618	virtual int rep_set_nsites(u_int32_t n);
619	virtual int rep_get_priority(u_int32_t *priorityp);
620	virtual int rep_set_priority(u_int32_t priority);
621	virtual int rep_get_timeout(int which, db_timeout_t *timeout);
622	virtual int rep_set_timeout(int which, db_timeout_t timeout);
623	virtual int repmgr_add_remote_site(const char * host, u_int16_t port,
624	    int *eidp, u_int32_t flags);
625	virtual int repmgr_get_ack_policy(int *policy);
626	virtual int repmgr_set_ack_policy(int policy);
627	virtual int repmgr_set_local_site(const char * host, u_int16_t port,
628	    u_int32_t flags);
629	virtual int repmgr_site_list(u_int *countp, DB_REPMGR_SITE **listp);
630	virtual int repmgr_start(int nthreads, u_int32_t flags);
631	virtual int repmgr_stat(DB_REPMGR_STAT **statp, u_int32_t flags);
632	virtual int repmgr_stat_print(u_int32_t flags);
633
634	// Conversion functions
635	//
636	virtual ENV *get_ENV()
637	{
638		return imp_->env;
639	}
640
641	virtual DB_ENV *get_DB_ENV()
642	{
643		return imp_;
644	}
645
646	virtual const DB_ENV *get_const_DB_ENV() const
647	{
648		return imp_;
649	}
650
651	static DbEnv* get_DbEnv(DB_ENV *dbenv)
652	{
653		return dbenv ? (DbEnv *)dbenv->api1_internal : 0;
654	}
655
656	static const DbEnv* get_const_DbEnv(const DB_ENV *dbenv)
657	{
658		return dbenv ? (const DbEnv *)dbenv->api1_internal : 0;
659	}
660
661	// For internal use only.
662	static DbEnv* wrap_DB_ENV(DB_ENV *dbenv);
663
664	// These are public only because they need to be called
665	// via C functions.  They should never be called by users
666	// of this class.
667	//
668	static int _app_dispatch_intercept(DB_ENV *dbenv, DBT *dbt, DB_LSN *lsn,
669				       db_recops op);
670	static void _paniccall_intercept(DB_ENV *dbenv, int errval);
671	static void _feedback_intercept(DB_ENV *dbenv, int opcode, int pct);
672	static void  _event_func_intercept(DB_ENV *dbenv, u_int32_t, void *);
673	static int _isalive_intercept(DB_ENV *dbenv, pid_t pid,
674	    db_threadid_t thrid, u_int32_t flags);
675	static int _rep_send_intercept(DB_ENV *dbenv, const DBT *cntrl,
676	    const DBT *data, const DB_LSN *lsn, int id, u_int32_t flags);
677	static void _stream_error_function(const DB_ENV *dbenv,
678	    const char *prefix, const char *message);
679	static void _stream_message_function(const DB_ENV *dbenv,
680	    const char *message);
681	static void _thread_id_intercept(DB_ENV *dbenv, pid_t *pidp,
682	    db_threadid_t *thridp);
683	static char *_thread_id_string_intercept(DB_ENV *dbenv, pid_t pid,
684	    db_threadid_t thrid, char *buf);
685
686private:
687	void cleanup();
688	int initialize(DB_ENV *dbenv);
689	int error_policy();
690
691	// For internal use only.
692	DbEnv(DB_ENV *, u_int32_t flags);
693
694	// no copying
695	DbEnv(const DbEnv &);
696	void operator = (const DbEnv &);
697
698	// instance data
699	DB_ENV *imp_;
700	int construct_error_;
701	u_int32_t construct_flags_;
702	__DB_STD(ostream) *error_stream_;
703	__DB_STD(ostream) *message_stream_;
704
705	int (*app_dispatch_callback_)(DbEnv *, Dbt *, DbLsn *, db_recops);
706	int (*isalive_callback_)(DbEnv *, pid_t, db_threadid_t, u_int32_t);
707	void (*error_callback_)(const DbEnv *, const char *, const char *);
708	void (*feedback_callback_)(DbEnv *, int, int);
709	void (*message_callback_)(const DbEnv *, const char *);
710	void (*paniccall_callback_)(DbEnv *, int);
711	void (*event_func_callback_)(DbEnv *, u_int32_t, void *);
712	int (*rep_send_callback_)(DbEnv *, const Dbt *, const Dbt *,
713	    const DbLsn *, int, u_int32_t);
714	void (*thread_id_callback_)(DbEnv *, pid_t *, db_threadid_t *);
715	char *(*thread_id_string_callback_)(DbEnv *, pid_t, db_threadid_t,
716	    char *);
717};
718
719//
720// Lock
721//
722class _exported DbLock
723{
724	friend class DbEnv;
725
726public:
727	DbLock();
728	DbLock(const DbLock &);
729	DbLock &operator = (const DbLock &);
730
731protected:
732	// We can add data to this class if needed
733	// since its contained class is not allocated by db.
734	// (see comment at top)
735
736	DbLock(DB_LOCK);
737	DB_LOCK lock_;
738};
739
740//
741// Log cursor
742//
743class _exported DbLogc : protected DB_LOGC
744{
745	friend class DbEnv;
746
747public:
748	int close(u_int32_t _flags);
749	int get(DbLsn *lsn, Dbt *data, u_int32_t _flags);
750	int version(u_int32_t *versionp, u_int32_t _flags);
751
752private:
753	// No data is permitted in this class (see comment at top)
754
755	// Note: use Db::cursor() to get pointers to a Dbc,
756	// and call Dbc::close() rather than delete to release them.
757	//
758	DbLogc();
759	~DbLogc();
760
761	// no copying
762	DbLogc(const Dbc &);
763	DbLogc &operator = (const Dbc &);
764};
765
766//
767// Log sequence number
768//
769class _exported DbLsn : public DB_LSN
770{
771	friend class DbEnv;          // friendship needed to cast to base class
772	friend class DbLogc;         // friendship needed to cast to base class
773};
774
775//
776// Memory pool file
777//
778class _exported DbMpoolFile
779{
780	friend class DbEnv;
781	friend class Db;
782
783public:
784	int close(u_int32_t flags);
785	int get(db_pgno_t *pgnoaddr, DbTxn *txn, u_int32_t flags, void *pagep);
786	int get_clear_len(u_int32_t *len);
787	int get_fileid(u_int8_t *fileid);
788	int get_flags(u_int32_t *flagsp);
789	int get_ftype(int *ftype);
790	int get_last_pgno(db_pgno_t *pgnop);
791	int get_lsn_offset(int32_t *offsetp);
792	int get_maxsize(u_int32_t *gbytes, u_int32_t *bytes);
793	int get_pgcookie(DBT *dbt);
794	int get_priority(DB_CACHE_PRIORITY *priorityp);
795	int get_transactional(void);
796	int open(const char *file, u_int32_t flags, int mode, size_t pagesize);
797	int put(void *pgaddr, DB_CACHE_PRIORITY priority, u_int32_t flags);
798	int set_clear_len(u_int32_t len);
799	int set_fileid(u_int8_t *fileid);
800	int set_flags(u_int32_t flags, int onoff);
801	int set_ftype(int ftype);
802	int set_lsn_offset(int32_t offset);
803	int set_maxsize(u_int32_t gbytes, u_int32_t bytes);
804	int set_pgcookie(DBT *dbt);
805	int set_priority(DB_CACHE_PRIORITY priority);
806	int sync();
807
808	virtual DB_MPOOLFILE *get_DB_MPOOLFILE()
809	{
810		return imp_;
811	}
812
813	virtual const DB_MPOOLFILE *get_const_DB_MPOOLFILE() const
814	{
815		return imp_;
816	}
817
818private:
819	DB_MPOOLFILE *imp_;
820
821	// We can add data to this class if needed
822	// since it is implemented via a pointer.
823	// (see comment at top)
824
825	// Note: use DbEnv::memp_fcreate() to get pointers to a DbMpoolFile,
826	// and call DbMpoolFile::close() rather than delete to release them.
827	//
828	DbMpoolFile();
829
830	// Shut g++ up.
831protected:
832	virtual ~DbMpoolFile();
833
834private:
835	// no copying
836	DbMpoolFile(const DbMpoolFile &);
837	void operator = (const DbMpoolFile &);
838};
839
840//
841// This is filled in and returned by the DbEnv::txn_recover() method.
842//
843class _exported DbPreplist
844{
845public:
846	DbTxn *txn;
847	u_int8_t gid[DB_XIDDATASIZE];
848};
849
850//
851// A sequence record in a database
852//
853class _exported DbSequence
854{
855public:
856	DbSequence(Db *db, u_int32_t flags);
857	virtual ~DbSequence();
858
859	int open(DbTxn *txnid, Dbt *key, u_int32_t flags);
860	int initial_value(db_seq_t value);
861	int close(u_int32_t flags);
862	int remove(DbTxn *txnid, u_int32_t flags);
863	int stat(DB_SEQUENCE_STAT **sp, u_int32_t flags);
864	int stat_print(u_int32_t flags);
865
866	int get(DbTxn *txnid, int32_t delta, db_seq_t *retp, u_int32_t flags);
867	int get_cachesize(int32_t *sizep);
868	int set_cachesize(int32_t size);
869	int get_flags(u_int32_t *flagsp);
870	int set_flags(u_int32_t flags);
871	int get_range(db_seq_t *minp, db_seq_t *maxp);
872	int set_range(db_seq_t min, db_seq_t max);
873
874	Db *get_db();
875	Dbt *get_key();
876
877	virtual DB_SEQUENCE *get_DB_SEQUENCE()
878	{
879		return imp_;
880	}
881
882	virtual const DB_SEQUENCE *get_const_DB_SEQUENCE() const
883	{
884		return imp_;
885	}
886
887	static DbSequence* get_DbSequence(DB_SEQUENCE *seq)
888	{
889		return (DbSequence *)seq->api_internal;
890	}
891
892	static const DbSequence* get_const_DbSequence(const DB_SEQUENCE *seq)
893	{
894		return (const DbSequence *)seq->api_internal;
895	}
896
897	// For internal use only.
898	static DbSequence* wrap_DB_SEQUENCE(DB_SEQUENCE *seq);
899
900private:
901	DbSequence(DB_SEQUENCE *seq);
902	// no copying
903	DbSequence(const DbSequence &);
904	DbSequence &operator = (const DbSequence &);
905
906	DB_SEQUENCE *imp_;
907	DBT key_;
908};
909
910//
911// Transaction
912//
913class _exported DbTxn
914{
915	friend class DbEnv;
916
917public:
918	int abort();
919	int commit(u_int32_t flags);
920	int discard(u_int32_t flags);
921	u_int32_t id();
922	int get_name(const char **namep);
923	int prepare(u_int8_t *gid);
924	int set_name(const char *name);
925	int set_timeout(db_timeout_t timeout, u_int32_t flags);
926
927	virtual DB_TXN *get_DB_TXN()
928	{
929		return imp_;
930	}
931
932	virtual const DB_TXN *get_const_DB_TXN() const
933	{
934		return imp_;
935	}
936
937	static DbTxn* get_DbTxn(DB_TXN *txn)
938	{
939		return (DbTxn *)txn->api_internal;
940	}
941
942	static const DbTxn* get_const_DbTxn(const DB_TXN *txn)
943	{
944		return (const DbTxn *)txn->api_internal;
945	}
946
947	// For internal use only.
948	static DbTxn* wrap_DB_TXN(DB_TXN *txn);
949
950private:
951	DB_TXN *imp_;
952
953	// We can add data to this class if needed
954	// since it is implemented via a pointer.
955	// (see comment at top)
956
957	// Note: use DbEnv::txn_begin() to get pointers to a DbTxn,
958	// and call DbTxn::abort() or DbTxn::commit rather than
959	// delete to release them.
960	//
961	DbTxn();
962	// For internal use only.
963	DbTxn(DB_TXN *txn);
964	virtual ~DbTxn();
965
966	// no copying
967	DbTxn(const DbTxn &);
968	void operator = (const DbTxn &);
969};
970
971//
972// A chunk of data, maybe a key or value.
973//
974class _exported Dbt : private DBT
975{
976	friend class Db;
977	friend class Dbc;
978	friend class DbEnv;
979	friend class DbLogc;
980	friend class DbSequence;
981
982public:
983	// key/data
984	void *get_data() const                 { return data; }
985	void set_data(void *value)             { data = value; }
986
987	// key/data length
988	u_int32_t get_size() const             { return size; }
989	void set_size(u_int32_t value)         { size = value; }
990
991	// RO: length of user buffer.
992	u_int32_t get_ulen() const             { return ulen; }
993	void set_ulen(u_int32_t value)         { ulen = value; }
994
995	// RO: get/put record length.
996	u_int32_t get_dlen() const             { return dlen; }
997	void set_dlen(u_int32_t value)         { dlen = value; }
998
999	// RO: get/put record offset.
1000	u_int32_t get_doff() const             { return doff; }
1001	void set_doff(u_int32_t value)         { doff = value; }
1002
1003	// flags
1004	u_int32_t get_flags() const            { return flags; }
1005	void set_flags(u_int32_t value)        { flags = value; }
1006
1007	// Conversion functions
1008	DBT *get_DBT()                         { return (DBT *)this; }
1009	const DBT *get_const_DBT() const       { return (const DBT *)this; }
1010
1011	static Dbt* get_Dbt(DBT *dbt)          { return (Dbt *)dbt; }
1012	static const Dbt* get_const_Dbt(const DBT *dbt)
1013					       { return (const Dbt *)dbt; }
1014
1015	Dbt(void *data, u_int32_t size);
1016	Dbt();
1017	~Dbt();
1018	Dbt(const Dbt &);
1019	Dbt &operator = (const Dbt &);
1020
1021private:
1022	// Note: no extra data appears in this class (other than
1023	// inherited from DBT) since we need DBT and Dbt objects
1024	// to have interchangable pointers.
1025	//
1026	// When subclassing this class, remember that callback
1027	// methods like bt_compare, bt_prefix, dup_compare may
1028	// internally manufacture DBT objects (which later are
1029	// cast to Dbt), so such callbacks might receive objects
1030	// not of your subclassed type.
1031};
1032
1033////////////////////////////////////////////////////////////////
1034////////////////////////////////////////////////////////////////
1035//
1036// multiple key/data/reco iterator classes
1037//
1038
1039// DbMultipleIterator is a shared private base class for the three types
1040// of bulk-return Iterator;  it should never be instantiated directly,
1041// but it handles the functionality shared by its subclasses.
1042class _exported DbMultipleIterator
1043{
1044public:
1045	DbMultipleIterator(const Dbt &dbt);
1046protected:
1047	u_int8_t *data_;
1048	u_int32_t *p_;
1049};
1050
1051class _exported DbMultipleKeyDataIterator : private DbMultipleIterator
1052{
1053public:
1054	DbMultipleKeyDataIterator(const Dbt &dbt) : DbMultipleIterator(dbt) {}
1055	bool next(Dbt &key, Dbt &data);
1056};
1057
1058class _exported DbMultipleRecnoDataIterator : private DbMultipleIterator
1059{
1060public:
1061	DbMultipleRecnoDataIterator(const Dbt &dbt) : DbMultipleIterator(dbt) {}
1062	bool next(db_recno_t &recno, Dbt &data);
1063};
1064
1065class _exported DbMultipleDataIterator : private DbMultipleIterator
1066{
1067public:
1068	DbMultipleDataIterator(const Dbt &dbt) : DbMultipleIterator(dbt) {}
1069	bool next(Dbt &data);
1070};
1071
1072////////////////////////////////////////////////////////////////
1073////////////////////////////////////////////////////////////////
1074//
1075// Exception classes
1076//
1077
1078// Almost any error in the DB library throws a DbException.
1079// Every exception should be considered an abnormality
1080// (e.g. bug, misuse of DB, file system error).
1081//
1082class _exported DbException : public __DB_STD(exception)
1083{
1084public:
1085	virtual ~DbException() throw();
1086	DbException(int err);
1087	DbException(const char *description);
1088	DbException(const char *description, int err);
1089	DbException(const char *prefix, const char *description, int err);
1090	int get_errno() const;
1091	virtual const char *what() const throw();
1092	DbEnv *get_env() const;
1093	void set_env(DbEnv *dbenv);
1094
1095	DbException(const DbException &);
1096	DbException &operator = (const DbException &);
1097
1098private:
1099	void describe(const char *prefix, const char *description);
1100
1101	char *what_;
1102	int err_;                   // errno
1103	DbEnv *dbenv_;
1104};
1105
1106//
1107// A specific sort of exception that occurs when
1108// an operation is aborted to resolve a deadlock.
1109//
1110class _exported DbDeadlockException : public DbException
1111{
1112public:
1113	virtual ~DbDeadlockException() throw();
1114	DbDeadlockException(const char *description);
1115
1116	DbDeadlockException(const DbDeadlockException &);
1117	DbDeadlockException &operator = (const DbDeadlockException &);
1118};
1119
1120//
1121// A specific sort of exception that occurs when
1122// a lock is not granted, e.g. by lock_get or lock_vec.
1123// Note that the Dbt is only live as long as the Dbt used
1124// in the offending call.
1125//
1126class _exported DbLockNotGrantedException : public DbException
1127{
1128public:
1129	virtual ~DbLockNotGrantedException() throw();
1130	DbLockNotGrantedException(const char *prefix, db_lockop_t op,
1131	    db_lockmode_t mode, const Dbt *obj, const DbLock lock, int index);
1132	DbLockNotGrantedException(const char *description);
1133
1134	DbLockNotGrantedException(const DbLockNotGrantedException &);
1135	DbLockNotGrantedException &operator =
1136	    (const DbLockNotGrantedException &);
1137
1138	db_lockop_t get_op() const;
1139	db_lockmode_t get_mode() const;
1140	const Dbt* get_obj() const;
1141	DbLock *get_lock() const;
1142	int get_index() const;
1143
1144private:
1145	db_lockop_t op_;
1146	db_lockmode_t mode_;
1147	const Dbt *obj_;
1148	DbLock *lock_;
1149	int index_;
1150};
1151
1152//
1153// A specific sort of exception that occurs when
1154// user declared memory is insufficient in a Dbt.
1155//
1156class _exported DbMemoryException : public DbException
1157{
1158public:
1159	virtual ~DbMemoryException() throw();
1160	DbMemoryException(Dbt *dbt);
1161	DbMemoryException(const char *prefix, Dbt *dbt);
1162
1163	DbMemoryException(const DbMemoryException &);
1164	DbMemoryException &operator = (const DbMemoryException &);
1165
1166	Dbt *get_dbt() const;
1167private:
1168	Dbt *dbt_;
1169};
1170
1171//
1172// A specific sort of exception that occurs when a change of replication
1173// master requires that all handles be re-opened.
1174//
1175class _exported DbRepHandleDeadException : public DbException
1176{
1177public:
1178	virtual ~DbRepHandleDeadException() throw();
1179	DbRepHandleDeadException(const char *description);
1180
1181	DbRepHandleDeadException(const DbRepHandleDeadException &);
1182	DbRepHandleDeadException &operator = (const DbRepHandleDeadException &);
1183};
1184
1185//
1186// A specific sort of exception that occurs when
1187// recovery is required before continuing DB activity.
1188//
1189class _exported DbRunRecoveryException : public DbException
1190{
1191public:
1192	virtual ~DbRunRecoveryException() throw();
1193	DbRunRecoveryException(const char *description);
1194
1195	DbRunRecoveryException(const DbRunRecoveryException &);
1196	DbRunRecoveryException &operator = (const DbRunRecoveryException &);
1197};
1198
1199//
1200// A specific sort of exception that occurs when
1201
1202////////////////////////////////////////////////////////////////
1203////////////////////////////////////////////////////////////////
1204//
1205// Restore default compiler warnings
1206//
1207#ifdef _MSC_VER
1208#pragma warning(pop)
1209#endif
1210
1211#endif /* !_DB_CXX_H_ */
1212