blob: 70c7bf887c6430f8855bbccd3bccab90c3643ab1 [file] [log] [blame]
Gregory P. Smith39250532007-10-09 06:02:21 +00001/*----------------------------------------------------------------------
2 Copyright (c) 1999-2001, Digital Creations, Fredericksburg, VA, USA
3 and Andrew Kuchling. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8
9 o Redistributions of source code must retain the above copyright
10 notice, this list of conditions, and the disclaimer that follows.
11
12 o Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions, and the following disclaimer in
14 the documentation and/or other materials provided with the
15 distribution.
16
17 o Neither the name of Digital Creations nor the names of its
18 contributors may be used to endorse or promote products derived
19 from this software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
22 IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL
25 CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31 USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32 DAMAGE.
33------------------------------------------------------------------------*/
34
35
36/*
37 * Handwritten code to wrap version 3.x of the Berkeley DB library,
38 * written to replace a SWIG-generated file. It has since been updated
Jesus Ceaef9764f2008-05-13 18:45:46 +000039 * to compile with Berkeley DB versions 3.2 through 4.2.
Gregory P. Smith39250532007-10-09 06:02:21 +000040 *
41 * This module was started by Andrew Kuchling to remove the dependency
42 * on SWIG in a package by Gregory P. Smith who based his work on a
43 * similar package by Robin Dunn <robin@alldunn.com> which wrapped
44 * Berkeley DB 2.7.x.
45 *
46 * Development of this module then returned full circle back to Robin Dunn
47 * who worked on behalf of Digital Creations to complete the wrapping of
48 * the DB 3.x API and to build a solid unit test suite. Robin has
49 * since gone onto other projects (wxPython).
50 *
51 * Gregory P. Smith <greg@krypto.org> is once again the maintainer.
52 *
53 * Use the pybsddb-users@lists.sf.net mailing list for all questions.
54 * Things can change faster than the header of this file is updated. This
55 * file is shared with the PyBSDDB project at SourceForge:
56 *
57 * http://pybsddb.sf.net
58 *
59 * This file should remain backward compatible with Python 2.1, but see PEP
60 * 291 for the most current backward compatibility requirements:
61 *
62 * http://www.python.org/peps/pep-0291.html
63 *
64 * This module contains 6 types:
65 *
66 * DB (Database)
67 * DBCursor (Database Cursor)
68 * DBEnv (database environment)
69 * DBTxn (An explicit database transaction)
70 * DBLock (A lock handle)
71 * DBSequence (Sequence)
72 *
Jesus Cea6557aac2010-03-22 14:22:26 +000073 * New datatypes:
74 *
75 * DBLogCursor (Log Cursor)
76 *
Gregory P. Smith39250532007-10-09 06:02:21 +000077 */
78
79/* --------------------------------------------------------------------- */
80
81/*
82 * Portions of this module, associated unit tests and build scripts are the
83 * result of a contract with The Written Word (http://thewrittenword.com/)
84 * Many thanks go out to them for causing me to raise the bar on quality and
85 * functionality, resulting in a better bsddb3 package for all of us to use.
86 *
87 * --Robin
88 */
89
90/* --------------------------------------------------------------------- */
91
92/*
93 * Work to split it up into a separate header and to add a C API was
94 * contributed by Duncan Grisby <duncan@tideway.com>. See here:
95 * http://sourceforge.net/tracker/index.php?func=detail&aid=1551895&group_id=13900&atid=313900
96 */
97
98/* --------------------------------------------------------------------- */
99
100#ifndef _BSDDB_H_
101#define _BSDDB_H_
102
103#include <db.h>
104
105
106/* 40 = 4.0, 33 = 3.3; this will break if the minor revision is > 9 */
107#define DBVER (DB_VERSION_MAJOR * 10 + DB_VERSION_MINOR)
108#if DB_VERSION_MINOR > 9
109#error "eek! DBVER can't handle minor versions > 9"
110#endif
111
Jesus Cea6557aac2010-03-22 14:22:26 +0000112#define PY_BSDDB_VERSION "4.8.4"
Gregory P. Smith39250532007-10-09 06:02:21 +0000113
114/* Python object definitions */
115
116struct behaviourFlags {
117 /* What is the default behaviour when DB->get or DBCursor->get returns a
118 DB_NOTFOUND || DB_KEYEMPTY error? Return None or raise an exception? */
119 unsigned int getReturnsNone : 1;
120 /* What is the default behaviour for DBCursor.set* methods when DBCursor->get
121 * returns a DB_NOTFOUND || DB_KEYEMPTY error? Return None or raise? */
122 unsigned int cursorSetReturnsNone : 1;
123};
124
125
Jesus Ceaef9764f2008-05-13 18:45:46 +0000126
127struct DBObject; /* Forward declaration */
128struct DBCursorObject; /* Forward declaration */
Jesus Cea6557aac2010-03-22 14:22:26 +0000129struct DBLogCursorObject; /* Forward declaration */
Jesus Ceaef9764f2008-05-13 18:45:46 +0000130struct DBTxnObject; /* Forward declaration */
131struct DBSequenceObject; /* Forward declaration */
132
Gregory P. Smith39250532007-10-09 06:02:21 +0000133typedef struct {
134 PyObject_HEAD
135 DB_ENV* db_env;
136 u_int32_t flags; /* saved flags from open() */
137 int closed;
138 struct behaviourFlags moduleFlags;
Jesus Ceaef9764f2008-05-13 18:45:46 +0000139 PyObject* event_notifyCallback;
Jesus Ceaef9764f2008-05-13 18:45:46 +0000140 struct DBObject *children_dbs;
141 struct DBTxnObject *children_txns;
Jesus Cea6557aac2010-03-22 14:22:26 +0000142 struct DBLogCursorObject *children_logcursors;
Jesus Cea4907d272008-08-31 14:00:51 +0000143 PyObject *private_obj;
Jesus Ceac5a11fa2008-07-23 11:38:42 +0000144 PyObject *rep_transport;
Gregory P. Smith39250532007-10-09 06:02:21 +0000145 PyObject *in_weakreflist; /* List of weak references */
146} DBEnvObject;
147
Jesus Ceaef9764f2008-05-13 18:45:46 +0000148typedef struct DBObject {
Gregory P. Smith39250532007-10-09 06:02:21 +0000149 PyObject_HEAD
150 DB* db;
151 DBEnvObject* myenvobj; /* PyObject containing the DB_ENV */
152 u_int32_t flags; /* saved flags from open() */
153 u_int32_t setflags; /* saved flags from set_flags() */
Gregory P. Smith39250532007-10-09 06:02:21 +0000154 struct behaviourFlags moduleFlags;
Jesus Ceaef9764f2008-05-13 18:45:46 +0000155 struct DBTxnObject *txn;
156 struct DBCursorObject *children_cursors;
157#if (DBVER >=43)
158 struct DBSequenceObject *children_sequences;
159#endif
160 struct DBObject **sibling_prev_p;
161 struct DBObject *sibling_next;
162 struct DBObject **sibling_prev_p_txn;
163 struct DBObject *sibling_next_txn;
Gregory P. Smith39250532007-10-09 06:02:21 +0000164 PyObject* associateCallback;
165 PyObject* btCompareCallback;
166 int primaryDBType;
Jesus Cea4907d272008-08-31 14:00:51 +0000167 PyObject *private_obj;
Gregory P. Smith39250532007-10-09 06:02:21 +0000168 PyObject *in_weakreflist; /* List of weak references */
169} DBObject;
170
171
Jesus Ceaef9764f2008-05-13 18:45:46 +0000172typedef struct DBCursorObject {
Gregory P. Smith39250532007-10-09 06:02:21 +0000173 PyObject_HEAD
174 DBC* dbc;
Jesus Ceaef9764f2008-05-13 18:45:46 +0000175 struct DBCursorObject **sibling_prev_p;
176 struct DBCursorObject *sibling_next;
177 struct DBCursorObject **sibling_prev_p_txn;
178 struct DBCursorObject *sibling_next_txn;
Gregory P. Smith39250532007-10-09 06:02:21 +0000179 DBObject* mydb;
Jesus Ceaef9764f2008-05-13 18:45:46 +0000180 struct DBTxnObject *txn;
Gregory P. Smith39250532007-10-09 06:02:21 +0000181 PyObject *in_weakreflist; /* List of weak references */
182} DBCursorObject;
183
184
Jesus Ceaef9764f2008-05-13 18:45:46 +0000185typedef struct DBTxnObject {
Gregory P. Smith39250532007-10-09 06:02:21 +0000186 PyObject_HEAD
187 DB_TXN* txn;
Jesus Ceaef9764f2008-05-13 18:45:46 +0000188 DBEnvObject* env;
189 int flag_prepare;
190 struct DBTxnObject *parent_txn;
191 struct DBTxnObject **sibling_prev_p;
192 struct DBTxnObject *sibling_next;
193 struct DBTxnObject *children_txns;
194 struct DBObject *children_dbs;
195 struct DBSequenceObject *children_sequences;
196 struct DBCursorObject *children_cursors;
Gregory P. Smith39250532007-10-09 06:02:21 +0000197 PyObject *in_weakreflist; /* List of weak references */
198} DBTxnObject;
199
200
Jesus Cea6557aac2010-03-22 14:22:26 +0000201typedef struct DBLogCursorObject {
202 PyObject_HEAD
203 DB_LOGC* logc;
204 DBEnvObject* env;
205 struct DBLogCursorObject **sibling_prev_p;
206 struct DBLogCursorObject *sibling_next;
207 PyObject *in_weakreflist; /* List of weak references */
208} DBLogCursorObject;
209
210
Gregory P. Smith39250532007-10-09 06:02:21 +0000211typedef struct {
212 PyObject_HEAD
213 DB_LOCK lock;
Jesus Cea6557aac2010-03-22 14:22:26 +0000214 int lock_initialized; /* Signal if we actually have a lock */
Gregory P. Smith39250532007-10-09 06:02:21 +0000215 PyObject *in_weakreflist; /* List of weak references */
216} DBLockObject;
217
218
219#if (DBVER >= 43)
Jesus Ceaef9764f2008-05-13 18:45:46 +0000220typedef struct DBSequenceObject {
Gregory P. Smith39250532007-10-09 06:02:21 +0000221 PyObject_HEAD
222 DB_SEQUENCE* sequence;
223 DBObject* mydb;
Jesus Ceaef9764f2008-05-13 18:45:46 +0000224 struct DBTxnObject *txn;
225 struct DBSequenceObject **sibling_prev_p;
226 struct DBSequenceObject *sibling_next;
227 struct DBSequenceObject **sibling_prev_p_txn;
228 struct DBSequenceObject *sibling_next_txn;
Gregory P. Smith39250532007-10-09 06:02:21 +0000229 PyObject *in_weakreflist; /* List of weak references */
230} DBSequenceObject;
Gregory P. Smith39250532007-10-09 06:02:21 +0000231#endif
232
233
234/* API structure for use by C code */
235
236/* To access the structure from an external module, use code like the
237 following (error checking missed out for clarity):
238
Jesus Cea6557aac2010-03-22 14:22:26 +0000239 // If you are using Python before 3.2:
Gregory P. Smith39250532007-10-09 06:02:21 +0000240 BSDDB_api* bsddb_api;
241 PyObject* mod;
242 PyObject* cobj;
243
244 mod = PyImport_ImportModule("bsddb._bsddb");
245 // Use "bsddb3._pybsddb" if you're using the standalone pybsddb add-on.
246 cobj = PyObject_GetAttrString(mod, "api");
247 api = (BSDDB_api*)PyCObject_AsVoidPtr(cobj);
248 Py_DECREF(cobj);
249 Py_DECREF(mod);
250
Jesus Cea6557aac2010-03-22 14:22:26 +0000251
252 // If you are using Python 3.2 or up:
253 BSDDB_api* bsddb_api;
254
255 // Use "bsddb3._pybsddb.api" if you're using
256 // the standalone pybsddb add-on.
257 bsddb_api = (void **)PyCapsule_Import("bsddb._bsddb.api", 1);
258
259
Gregory P. Smith39250532007-10-09 06:02:21 +0000260 The structure's members must not be changed.
261*/
262
263typedef struct {
264 /* Type objects */
265 PyTypeObject* db_type;
266 PyTypeObject* dbcursor_type;
Jesus Cea6557aac2010-03-22 14:22:26 +0000267 PyTypeObject* dblogcursor_type;
Gregory P. Smith39250532007-10-09 06:02:21 +0000268 PyTypeObject* dbenv_type;
269 PyTypeObject* dbtxn_type;
270 PyTypeObject* dblock_type;
271#if (DBVER >= 43)
272 PyTypeObject* dbsequence_type;
273#endif
274
275 /* Functions */
276 int (*makeDBError)(int err);
Gregory P. Smith39250532007-10-09 06:02:21 +0000277} BSDDB_api;
278
279
280#ifndef COMPILING_BSDDB_C
281
282/* If not inside _bsddb.c, define type check macros that use the api
283 structure. The calling code must have a value named bsddb_api
284 pointing to the api structure.
285*/
286
287#define DBObject_Check(v) ((v)->ob_type == bsddb_api->db_type)
288#define DBCursorObject_Check(v) ((v)->ob_type == bsddb_api->dbcursor_type)
289#define DBEnvObject_Check(v) ((v)->ob_type == bsddb_api->dbenv_type)
290#define DBTxnObject_Check(v) ((v)->ob_type == bsddb_api->dbtxn_type)
291#define DBLockObject_Check(v) ((v)->ob_type == bsddb_api->dblock_type)
292#if (DBVER >= 43)
293#define DBSequenceObject_Check(v) ((v)->ob_type == bsddb_api->dbsequence_type)
294#endif
295
Christian Heimes2518b252007-12-14 03:02:34 +0000296#endif /* COMPILING_BSDDB_C */
Gregory P. Smith39250532007-10-09 06:02:21 +0000297
298
Christian Heimes2518b252007-12-14 03:02:34 +0000299#endif /* _BSDDB_H_ */