Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1 | /*---------------------------------------------------------------------- |
| 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, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 38 | * written to replace a SWIG-generated file. It has since been updated |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 39 | * to compile with Berkeley DB versions 3.2 through 4.2. |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 40 | * |
| 41 | * This module was started by Andrew Kuchling to remove the dependency |
Gregory P. Smith | f805785 | 2007-09-09 20:25:00 +0000 | [diff] [blame] | 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. |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 45 | * |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 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 | * |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 51 | * Gregory P. Smith <greg@krypto.org> was once again the maintainer. |
| 52 | * |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 53 | * Since January 2008, new maintainer is Jesus Cea <jcea@jcea.es>. |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 54 | * Jesus Cea licenses this code to PSF under a Contributor Agreement. |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 55 | * |
| 56 | * Use the pybsddb-users@lists.sf.net mailing list for all questions. |
Barry Warsaw | c74e4a5 | 2003-04-24 14:28:08 +0000 | [diff] [blame] | 57 | * Things can change faster than the header of this file is updated. This |
| 58 | * file is shared with the PyBSDDB project at SourceForge: |
| 59 | * |
| 60 | * http://pybsddb.sf.net |
| 61 | * |
| 62 | * This file should remain backward compatible with Python 2.1, but see PEP |
| 63 | * 291 for the most current backward compatibility requirements: |
| 64 | * |
| 65 | * http://www.python.org/peps/pep-0291.html |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 66 | * |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 67 | * This module contains 6 types: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 68 | * |
| 69 | * DB (Database) |
| 70 | * DBCursor (Database Cursor) |
| 71 | * DBEnv (database environment) |
| 72 | * DBTxn (An explicit database transaction) |
| 73 | * DBLock (A lock handle) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 74 | * DBSequence (Sequence) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 75 | * |
| 76 | */ |
| 77 | |
| 78 | /* --------------------------------------------------------------------- */ |
| 79 | |
| 80 | /* |
| 81 | * Portions of this module, associated unit tests and build scripts are the |
| 82 | * result of a contract with The Written Word (http://thewrittenword.com/) |
| 83 | * Many thanks go out to them for causing me to raise the bar on quality and |
| 84 | * functionality, resulting in a better bsddb3 package for all of us to use. |
| 85 | * |
| 86 | * --Robin |
| 87 | */ |
| 88 | |
| 89 | /* --------------------------------------------------------------------- */ |
| 90 | |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 91 | #include <stddef.h> /* for offsetof() */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 92 | #include <Python.h> |
Gregory P. Smith | 3925053 | 2007-10-09 06:02:21 +0000 | [diff] [blame] | 93 | |
| 94 | #define COMPILING_BSDDB_C |
| 95 | #include "bsddb.h" |
| 96 | #undef COMPILING_BSDDB_C |
| 97 | |
| 98 | static char *rcs_id = "$Id$"; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 99 | |
| 100 | /* --------------------------------------------------------------------- */ |
| 101 | /* Various macro definitions */ |
| 102 | |
Gregory P. Smith | 7f5b6f4 | 2006-04-08 07:10:51 +0000 | [diff] [blame] | 103 | #if (PY_VERSION_HEX < 0x02050000) |
Neal Norwitz | 09a29fa | 2006-06-12 02:05:55 +0000 | [diff] [blame] | 104 | typedef int Py_ssize_t; |
Gregory P. Smith | 7f5b6f4 | 2006-04-08 07:10:51 +0000 | [diff] [blame] | 105 | #endif |
| 106 | |
Gregory P. Smith | 572226c | 2008-05-26 19:03:35 +0000 | [diff] [blame] | 107 | #if (PY_VERSION_HEX < 0x02060000) /* really: before python trunk r63675 */ |
| 108 | /* This code now uses PyBytes* API function names instead of PyString*. |
| 109 | * These #defines map to their equivalent on earlier python versions. */ |
| 110 | #define PyBytes_FromStringAndSize PyString_FromStringAndSize |
| 111 | #define PyBytes_FromString PyString_FromString |
| 112 | #define PyBytes_AsStringAndSize PyString_AsStringAndSize |
| 113 | #define PyBytes_Check PyString_Check |
| 114 | #define PyBytes_GET_SIZE PyString_GET_SIZE |
| 115 | #define PyBytes_AS_STRING PyString_AS_STRING |
| 116 | #endif |
| 117 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 118 | #if (PY_VERSION_HEX >= 0x03000000) |
| 119 | #define NUMBER_Check PyLong_Check |
| 120 | #define NUMBER_AsLong PyLong_AsLong |
| 121 | #define NUMBER_FromLong PyLong_FromLong |
| 122 | #else |
| 123 | #define NUMBER_Check PyInt_Check |
| 124 | #define NUMBER_AsLong PyInt_AsLong |
| 125 | #define NUMBER_FromLong PyInt_FromLong |
| 126 | #endif |
| 127 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 128 | #ifdef WITH_THREAD |
| 129 | |
| 130 | /* These are for when calling Python --> C */ |
| 131 | #define MYDB_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS; |
| 132 | #define MYDB_END_ALLOW_THREADS Py_END_ALLOW_THREADS; |
| 133 | |
Mark Hammond | a69d409 | 2003-04-22 23:13:27 +0000 | [diff] [blame] | 134 | /* For 2.3, use the PyGILState_ calls */ |
| 135 | #if (PY_VERSION_HEX >= 0x02030000) |
| 136 | #define MYDB_USE_GILSTATE |
| 137 | #endif |
| 138 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 139 | /* and these are for calling C --> Python */ |
Mark Hammond | a69d409 | 2003-04-22 23:13:27 +0000 | [diff] [blame] | 140 | #if defined(MYDB_USE_GILSTATE) |
| 141 | #define MYDB_BEGIN_BLOCK_THREADS \ |
| 142 | PyGILState_STATE __savestate = PyGILState_Ensure(); |
| 143 | #define MYDB_END_BLOCK_THREADS \ |
| 144 | PyGILState_Release(__savestate); |
| 145 | #else /* MYDB_USE_GILSTATE */ |
| 146 | /* Pre GILState API - do it the long old way */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 147 | static PyInterpreterState* _db_interpreterState = NULL; |
| 148 | #define MYDB_BEGIN_BLOCK_THREADS { \ |
| 149 | PyThreadState* prevState; \ |
| 150 | PyThreadState* newState; \ |
| 151 | PyEval_AcquireLock(); \ |
| 152 | newState = PyThreadState_New(_db_interpreterState); \ |
| 153 | prevState = PyThreadState_Swap(newState); |
| 154 | |
| 155 | #define MYDB_END_BLOCK_THREADS \ |
| 156 | newState = PyThreadState_Swap(prevState); \ |
| 157 | PyThreadState_Clear(newState); \ |
| 158 | PyEval_ReleaseLock(); \ |
| 159 | PyThreadState_Delete(newState); \ |
| 160 | } |
Mark Hammond | a69d409 | 2003-04-22 23:13:27 +0000 | [diff] [blame] | 161 | #endif /* MYDB_USE_GILSTATE */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 162 | |
| 163 | #else |
Mark Hammond | a69d409 | 2003-04-22 23:13:27 +0000 | [diff] [blame] | 164 | /* Compiled without threads - avoid all this cruft */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 165 | #define MYDB_BEGIN_ALLOW_THREADS |
| 166 | #define MYDB_END_ALLOW_THREADS |
| 167 | #define MYDB_BEGIN_BLOCK_THREADS |
| 168 | #define MYDB_END_BLOCK_THREADS |
| 169 | |
| 170 | #endif |
| 171 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 172 | /* Should DB_INCOMPLETE be turned into a warning or an exception? */ |
| 173 | #define INCOMPLETE_IS_WARNING 1 |
| 174 | |
| 175 | /* --------------------------------------------------------------------- */ |
| 176 | /* Exceptions */ |
| 177 | |
| 178 | static PyObject* DBError; /* Base class, all others derive from this */ |
Gregory P. Smith | e276717 | 2003-11-02 08:06:29 +0000 | [diff] [blame] | 179 | static PyObject* DBCursorClosedError; /* raised when trying to use a closed cursor object */ |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 180 | static PyObject* DBKeyEmptyError; /* DB_KEYEMPTY: also derives from KeyError */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 181 | static PyObject* DBKeyExistError; /* DB_KEYEXIST */ |
| 182 | static PyObject* DBLockDeadlockError; /* DB_LOCK_DEADLOCK */ |
| 183 | static PyObject* DBLockNotGrantedError; /* DB_LOCK_NOTGRANTED */ |
| 184 | static PyObject* DBNotFoundError; /* DB_NOTFOUND: also derives from KeyError */ |
| 185 | static PyObject* DBOldVersionError; /* DB_OLD_VERSION */ |
| 186 | static PyObject* DBRunRecoveryError; /* DB_RUNRECOVERY */ |
| 187 | static PyObject* DBVerifyBadError; /* DB_VERIFY_BAD */ |
| 188 | static PyObject* DBNoServerError; /* DB_NOSERVER */ |
| 189 | static PyObject* DBNoServerHomeError; /* DB_NOSERVER_HOME */ |
| 190 | static PyObject* DBNoServerIDError; /* DB_NOSERVER_ID */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 191 | static PyObject* DBPageNotFoundError; /* DB_PAGE_NOTFOUND */ |
| 192 | static PyObject* DBSecondaryBadError; /* DB_SECONDARY_BAD */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 193 | |
| 194 | #if !INCOMPLETE_IS_WARNING |
| 195 | static PyObject* DBIncompleteError; /* DB_INCOMPLETE */ |
| 196 | #endif |
| 197 | |
| 198 | static PyObject* DBInvalidArgError; /* EINVAL */ |
| 199 | static PyObject* DBAccessError; /* EACCES */ |
| 200 | static PyObject* DBNoSpaceError; /* ENOSPC */ |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 201 | static PyObject* DBNoMemoryError; /* DB_BUFFER_SMALL (ENOMEM when < 4.3) */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 202 | static PyObject* DBAgainError; /* EAGAIN */ |
| 203 | static PyObject* DBBusyError; /* EBUSY */ |
| 204 | static PyObject* DBFileExistsError; /* EEXIST */ |
| 205 | static PyObject* DBNoSuchFileError; /* ENOENT */ |
| 206 | static PyObject* DBPermissionsError; /* EPERM */ |
| 207 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 208 | #if (DBVER >= 42) |
| 209 | static PyObject* DBRepHandleDeadError; /* DB_REP_HANDLE_DEAD */ |
| 210 | #endif |
| 211 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 212 | static PyObject* DBRepUnavailError; /* DB_REP_UNAVAIL */ |
| 213 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 214 | #if (DBVER < 43) |
| 215 | #define DB_BUFFER_SMALL ENOMEM |
| 216 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 217 | |
| 218 | |
| 219 | /* --------------------------------------------------------------------- */ |
| 220 | /* Structure definitions */ |
| 221 | |
Gregory P. Smith | 3925053 | 2007-10-09 06:02:21 +0000 | [diff] [blame] | 222 | #if PYTHON_API_VERSION < 1010 |
| 223 | #error "Python 2.1 or later required" |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 224 | #endif |
| 225 | |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 226 | |
Gregory P. Smith | 3925053 | 2007-10-09 06:02:21 +0000 | [diff] [blame] | 227 | /* Defaults for moduleFlags in DBEnvObject and DBObject. */ |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 228 | #define DEFAULT_GET_RETURNS_NONE 1 |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 229 | #define DEFAULT_CURSOR_SET_RETURNS_NONE 1 /* 0 in pybsddb < 4.2, python < 2.4 */ |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 230 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 231 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 232 | /* See comment in Python 2.6 "object.h" */ |
| 233 | #ifndef staticforward |
| 234 | #define staticforward static |
| 235 | #endif |
| 236 | #ifndef statichere |
| 237 | #define statichere static |
| 238 | #endif |
| 239 | |
| 240 | staticforward PyTypeObject DB_Type, DBCursor_Type, DBEnv_Type, DBTxn_Type, |
| 241 | DBLock_Type; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 242 | #if (DBVER >= 43) |
| 243 | staticforward PyTypeObject DBSequence_Type; |
| 244 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 245 | |
Martin v. Löwis | 83c9201 | 2008-04-24 13:17:24 +0000 | [diff] [blame] | 246 | #ifndef Py_TYPE |
Gregory P. Smith | fc00669 | 2007-11-05 09:06:28 +0000 | [diff] [blame] | 247 | /* for compatibility with Python 2.5 and earlier */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 248 | #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) |
Gregory P. Smith | fc00669 | 2007-11-05 09:06:28 +0000 | [diff] [blame] | 249 | #endif |
| 250 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 251 | #define DBObject_Check(v) (Py_TYPE(v) == &DB_Type) |
| 252 | #define DBCursorObject_Check(v) (Py_TYPE(v) == &DBCursor_Type) |
| 253 | #define DBEnvObject_Check(v) (Py_TYPE(v) == &DBEnv_Type) |
| 254 | #define DBTxnObject_Check(v) (Py_TYPE(v) == &DBTxn_Type) |
| 255 | #define DBLockObject_Check(v) (Py_TYPE(v) == &DBLock_Type) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 256 | #if (DBVER >= 43) |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 257 | #define DBSequenceObject_Check(v) (Py_TYPE(v) == &DBSequence_Type) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 258 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 259 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 260 | #if (DBVER < 46) |
| 261 | #define _DBC_close(dbc) dbc->c_close(dbc) |
| 262 | #define _DBC_count(dbc,a,b) dbc->c_count(dbc,a,b) |
| 263 | #define _DBC_del(dbc,a) dbc->c_del(dbc,a) |
| 264 | #define _DBC_dup(dbc,a,b) dbc->c_dup(dbc,a,b) |
| 265 | #define _DBC_get(dbc,a,b,c) dbc->c_get(dbc,a,b,c) |
| 266 | #define _DBC_pget(dbc,a,b,c,d) dbc->c_pget(dbc,a,b,c,d) |
| 267 | #define _DBC_put(dbc,a,b,c) dbc->c_put(dbc,a,b,c) |
| 268 | #else |
| 269 | #define _DBC_close(dbc) dbc->close(dbc) |
| 270 | #define _DBC_count(dbc,a,b) dbc->count(dbc,a,b) |
| 271 | #define _DBC_del(dbc,a) dbc->del(dbc,a) |
| 272 | #define _DBC_dup(dbc,a,b) dbc->dup(dbc,a,b) |
| 273 | #define _DBC_get(dbc,a,b,c) dbc->get(dbc,a,b,c) |
| 274 | #define _DBC_pget(dbc,a,b,c,d) dbc->pget(dbc,a,b,c,d) |
| 275 | #define _DBC_put(dbc,a,b,c) dbc->put(dbc,a,b,c) |
| 276 | #endif |
| 277 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 278 | |
| 279 | /* --------------------------------------------------------------------- */ |
| 280 | /* Utility macros and functions */ |
| 281 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 282 | #define INSERT_IN_DOUBLE_LINKED_LIST(backlink,object) \ |
| 283 | { \ |
| 284 | object->sibling_next=backlink; \ |
| 285 | object->sibling_prev_p=&(backlink); \ |
| 286 | backlink=object; \ |
| 287 | if (object->sibling_next) { \ |
| 288 | object->sibling_next->sibling_prev_p=&(object->sibling_next); \ |
| 289 | } \ |
| 290 | } |
| 291 | |
| 292 | #define EXTRACT_FROM_DOUBLE_LINKED_LIST(object) \ |
| 293 | { \ |
| 294 | if (object->sibling_next) { \ |
| 295 | object->sibling_next->sibling_prev_p=object->sibling_prev_p; \ |
| 296 | } \ |
| 297 | *(object->sibling_prev_p)=object->sibling_next; \ |
| 298 | } |
| 299 | |
| 300 | #define EXTRACT_FROM_DOUBLE_LINKED_LIST_MAYBE_NULL(object) \ |
| 301 | { \ |
| 302 | if (object->sibling_next) { \ |
| 303 | object->sibling_next->sibling_prev_p=object->sibling_prev_p; \ |
| 304 | } \ |
| 305 | if (object->sibling_prev_p) { \ |
| 306 | *(object->sibling_prev_p)=object->sibling_next; \ |
| 307 | } \ |
| 308 | } |
| 309 | |
| 310 | #define INSERT_IN_DOUBLE_LINKED_LIST_TXN(backlink,object) \ |
| 311 | { \ |
| 312 | object->sibling_next_txn=backlink; \ |
| 313 | object->sibling_prev_p_txn=&(backlink); \ |
| 314 | backlink=object; \ |
| 315 | if (object->sibling_next_txn) { \ |
| 316 | object->sibling_next_txn->sibling_prev_p_txn= \ |
| 317 | &(object->sibling_next_txn); \ |
| 318 | } \ |
| 319 | } |
| 320 | |
| 321 | #define EXTRACT_FROM_DOUBLE_LINKED_LIST_TXN(object) \ |
| 322 | { \ |
| 323 | if (object->sibling_next_txn) { \ |
| 324 | object->sibling_next_txn->sibling_prev_p_txn= \ |
| 325 | object->sibling_prev_p_txn; \ |
| 326 | } \ |
| 327 | *(object->sibling_prev_p_txn)=object->sibling_next_txn; \ |
| 328 | } |
| 329 | |
| 330 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 331 | #define RETURN_IF_ERR() \ |
| 332 | if (makeDBError(err)) { \ |
| 333 | return NULL; \ |
| 334 | } |
| 335 | |
| 336 | #define RETURN_NONE() Py_INCREF(Py_None); return Py_None; |
| 337 | |
Gregory P. Smith | e276717 | 2003-11-02 08:06:29 +0000 | [diff] [blame] | 338 | #define _CHECK_OBJECT_NOT_CLOSED(nonNull, pyErrObj, name) \ |
| 339 | if ((nonNull) == NULL) { \ |
| 340 | PyObject *errTuple = NULL; \ |
| 341 | errTuple = Py_BuildValue("(is)", 0, #name " object has been closed"); \ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 342 | if (errTuple) { \ |
| 343 | PyErr_SetObject((pyErrObj), errTuple); \ |
| 344 | Py_DECREF(errTuple); \ |
| 345 | } \ |
Gregory P. Smith | e276717 | 2003-11-02 08:06:29 +0000 | [diff] [blame] | 346 | return NULL; \ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Gregory P. Smith | e276717 | 2003-11-02 08:06:29 +0000 | [diff] [blame] | 349 | #define CHECK_DB_NOT_CLOSED(dbobj) \ |
| 350 | _CHECK_OBJECT_NOT_CLOSED(dbobj->db, DBError, DB) |
| 351 | |
| 352 | #define CHECK_ENV_NOT_CLOSED(env) \ |
| 353 | _CHECK_OBJECT_NOT_CLOSED(env->db_env, DBError, DBEnv) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 354 | |
| 355 | #define CHECK_CURSOR_NOT_CLOSED(curs) \ |
Gregory P. Smith | e276717 | 2003-11-02 08:06:29 +0000 | [diff] [blame] | 356 | _CHECK_OBJECT_NOT_CLOSED(curs->dbc, DBCursorClosedError, DBCursor) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 357 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 358 | #if (DBVER >= 43) |
| 359 | #define CHECK_SEQUENCE_NOT_CLOSED(curs) \ |
| 360 | _CHECK_OBJECT_NOT_CLOSED(curs->sequence, DBError, DBSequence) |
| 361 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 362 | |
| 363 | #define CHECK_DBFLAG(mydb, flag) (((mydb)->flags & (flag)) || \ |
| 364 | (((mydb)->myenvobj != NULL) && ((mydb)->myenvobj->flags & (flag)))) |
| 365 | |
| 366 | #define CLEAR_DBT(dbt) (memset(&(dbt), 0, sizeof(dbt))) |
| 367 | |
| 368 | #define FREE_DBT(dbt) if ((dbt.flags & (DB_DBT_MALLOC|DB_DBT_REALLOC)) && \ |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 369 | dbt.data != NULL) { free(dbt.data); dbt.data = NULL; } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 370 | |
| 371 | |
| 372 | static int makeDBError(int err); |
| 373 | |
| 374 | |
| 375 | /* Return the access method type of the DBObject */ |
| 376 | static int _DB_get_type(DBObject* self) |
| 377 | { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 378 | DBTYPE type; |
| 379 | int err; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 380 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 381 | err = self->db->get_type(self->db, &type); |
| 382 | if (makeDBError(err)) { |
| 383 | return -1; |
| 384 | } |
| 385 | return type; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | |
| 389 | /* Create a DBT structure (containing key and data values) from Python |
| 390 | strings. Returns 1 on success, 0 on an error. */ |
| 391 | static int make_dbt(PyObject* obj, DBT* dbt) |
| 392 | { |
| 393 | CLEAR_DBT(*dbt); |
| 394 | if (obj == Py_None) { |
| 395 | /* no need to do anything, the structure has already been zeroed */ |
| 396 | } |
| 397 | else if (!PyArg_Parse(obj, "s#", &dbt->data, &dbt->size)) { |
| 398 | PyErr_SetString(PyExc_TypeError, |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 399 | #if (PY_VERSION_HEX < 0x03000000) |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 400 | "Data values must be of type string or None."); |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 401 | #else |
| 402 | "Data values must be of type bytes or None."); |
| 403 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 404 | return 0; |
| 405 | } |
| 406 | return 1; |
| 407 | } |
| 408 | |
| 409 | |
| 410 | /* Recno and Queue DBs can have integer keys. This function figures out |
| 411 | what's been given, verifies that it's allowed, and then makes the DBT. |
| 412 | |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 413 | Caller MUST call FREE_DBT(key) when done. */ |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 414 | static int |
| 415 | make_key_dbt(DBObject* self, PyObject* keyobj, DBT* key, int* pflags) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 416 | { |
| 417 | db_recno_t recno; |
| 418 | int type; |
| 419 | |
| 420 | CLEAR_DBT(*key); |
Gustavo Niemeyer | f073b75 | 2004-01-20 15:24:29 +0000 | [diff] [blame] | 421 | if (keyobj == Py_None) { |
Gustavo Niemeyer | 024f2de | 2004-01-20 15:14:55 +0000 | [diff] [blame] | 422 | type = _DB_get_type(self); |
Gustavo Niemeyer | 8974f72 | 2004-01-20 15:20:03 +0000 | [diff] [blame] | 423 | if (type == -1) |
| 424 | return 0; |
Gustavo Niemeyer | 024f2de | 2004-01-20 15:14:55 +0000 | [diff] [blame] | 425 | if (type == DB_RECNO || type == DB_QUEUE) { |
| 426 | PyErr_SetString( |
| 427 | PyExc_TypeError, |
| 428 | "None keys not allowed for Recno and Queue DB's"); |
| 429 | return 0; |
| 430 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 431 | /* no need to do anything, the structure has already been zeroed */ |
| 432 | } |
| 433 | |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 434 | else if (PyBytes_Check(keyobj)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 435 | /* verify access method type */ |
| 436 | type = _DB_get_type(self); |
| 437 | if (type == -1) |
| 438 | return 0; |
| 439 | if (type == DB_RECNO || type == DB_QUEUE) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 440 | PyErr_SetString( |
| 441 | PyExc_TypeError, |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 442 | #if (PY_VERSION_HEX < 0x03000000) |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 443 | "String keys not allowed for Recno and Queue DB's"); |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 444 | #else |
| 445 | "Bytes keys not allowed for Recno and Queue DB's"); |
| 446 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 447 | return 0; |
| 448 | } |
| 449 | |
Gregory P. Smith | 10bed54 | 2007-10-09 06:50:43 +0000 | [diff] [blame] | 450 | /* |
| 451 | * NOTE(gps): I don't like doing a data copy here, it seems |
| 452 | * wasteful. But without a clean way to tell FREE_DBT if it |
| 453 | * should free key->data or not we have to. Other places in |
| 454 | * the code check for DB_THREAD and forceably set DBT_MALLOC |
| 455 | * when we otherwise would leave flags 0 to indicate that. |
| 456 | */ |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 457 | key->data = malloc(PyBytes_GET_SIZE(keyobj)); |
Gregory P. Smith | 10bed54 | 2007-10-09 06:50:43 +0000 | [diff] [blame] | 458 | if (key->data == NULL) { |
| 459 | PyErr_SetString(PyExc_MemoryError, "Key memory allocation failed"); |
| 460 | return 0; |
| 461 | } |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 462 | memcpy(key->data, PyBytes_AS_STRING(keyobj), |
| 463 | PyBytes_GET_SIZE(keyobj)); |
Gregory P. Smith | 10bed54 | 2007-10-09 06:50:43 +0000 | [diff] [blame] | 464 | key->flags = DB_DBT_REALLOC; |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 465 | key->size = PyBytes_GET_SIZE(keyobj); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 468 | else if (NUMBER_Check(keyobj)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 469 | /* verify access method type */ |
| 470 | type = _DB_get_type(self); |
| 471 | if (type == -1) |
| 472 | return 0; |
| 473 | if (type == DB_BTREE && pflags != NULL) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 474 | /* if BTREE then an Integer key is allowed with the |
| 475 | * DB_SET_RECNO flag */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 476 | *pflags |= DB_SET_RECNO; |
| 477 | } |
| 478 | else if (type != DB_RECNO && type != DB_QUEUE) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 479 | PyErr_SetString( |
| 480 | PyExc_TypeError, |
| 481 | "Integer keys only allowed for Recno and Queue DB's"); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 482 | return 0; |
| 483 | } |
| 484 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 485 | /* Make a key out of the requested recno, use allocated space so DB |
| 486 | * will be able to realloc room for the real key if needed. */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 487 | recno = NUMBER_AsLong(keyobj); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 488 | key->data = malloc(sizeof(db_recno_t)); |
| 489 | if (key->data == NULL) { |
| 490 | PyErr_SetString(PyExc_MemoryError, "Key memory allocation failed"); |
| 491 | return 0; |
| 492 | } |
| 493 | key->ulen = key->size = sizeof(db_recno_t); |
| 494 | memcpy(key->data, &recno, sizeof(db_recno_t)); |
| 495 | key->flags = DB_DBT_REALLOC; |
| 496 | } |
| 497 | else { |
| 498 | PyErr_Format(PyExc_TypeError, |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 499 | #if (PY_VERSION_HEX < 0x03000000) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 500 | "String or Integer object expected for key, %s found", |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 501 | #else |
| 502 | "Bytes or Integer object expected for key, %s found", |
| 503 | #endif |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 504 | Py_TYPE(keyobj)->tp_name); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | return 1; |
| 509 | } |
| 510 | |
| 511 | |
| 512 | /* Add partial record access to an existing DBT data struct. |
| 513 | If dlen and doff are set, then the DB_DBT_PARTIAL flag will be set |
| 514 | and the data storage/retrieval will be done using dlen and doff. */ |
| 515 | static int add_partial_dbt(DBT* d, int dlen, int doff) { |
| 516 | /* if neither were set we do nothing (-1 is the default value) */ |
| 517 | if ((dlen == -1) && (doff == -1)) { |
| 518 | return 1; |
| 519 | } |
| 520 | |
| 521 | if ((dlen < 0) || (doff < 0)) { |
| 522 | PyErr_SetString(PyExc_TypeError, "dlen and doff must both be >= 0"); |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | d->flags = d->flags | DB_DBT_PARTIAL; |
| 527 | d->dlen = (unsigned int) dlen; |
| 528 | d->doff = (unsigned int) doff; |
| 529 | return 1; |
| 530 | } |
| 531 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 532 | /* a safe strcpy() without the zeroing behaviour and semantics of strncpy. */ |
| 533 | /* TODO: make this use the native libc strlcpy() when available (BSD) */ |
| 534 | unsigned int our_strlcpy(char* dest, const char* src, unsigned int n) |
| 535 | { |
| 536 | unsigned int srclen, copylen; |
| 537 | |
| 538 | srclen = strlen(src); |
| 539 | if (n <= 0) |
| 540 | return srclen; |
| 541 | copylen = (srclen > n-1) ? n-1 : srclen; |
| 542 | /* populate dest[0] thru dest[copylen-1] */ |
| 543 | memcpy(dest, src, copylen); |
| 544 | /* guarantee null termination */ |
| 545 | dest[copylen] = 0; |
| 546 | |
| 547 | return srclen; |
| 548 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 549 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 550 | /* Callback used to save away more information about errors from the DB |
| 551 | * library. */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 552 | static char _db_errmsg[1024]; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 553 | #if (DBVER <= 42) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 554 | static void _db_errorCallback(const char* prefix, char* msg) |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 555 | #else |
| 556 | static void _db_errorCallback(const DB_ENV *db_env, |
| 557 | const char* prefix, const char* msg) |
| 558 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 559 | { |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 560 | our_strlcpy(_db_errmsg, msg, sizeof(_db_errmsg)); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 564 | /* |
| 565 | ** We need these functions because some results |
| 566 | ** are undefined if pointer is NULL. Some other |
| 567 | ** give None instead of "". |
| 568 | ** |
| 569 | ** This functions are static and will be |
| 570 | ** -I hope- inlined. |
| 571 | */ |
| 572 | static const char *DummyString = "This string is a simple placeholder"; |
| 573 | static PyObject *Build_PyString(const char *p,int s) |
| 574 | { |
| 575 | if (!p) { |
| 576 | p=DummyString; |
| 577 | assert(s==0); |
| 578 | } |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 579 | return PyBytes_FromStringAndSize(p,s); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | static PyObject *BuildValue_S(const void *p,int s) |
| 583 | { |
| 584 | if (!p) { |
| 585 | p=DummyString; |
| 586 | assert(s==0); |
| 587 | } |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 588 | return PyBytes_FromStringAndSize(p, s); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | static PyObject *BuildValue_SS(const void *p1,int s1,const void *p2,int s2) |
| 592 | { |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 593 | PyObject *a, *b, *r; |
| 594 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 595 | if (!p1) { |
| 596 | p1=DummyString; |
| 597 | assert(s1==0); |
| 598 | } |
| 599 | if (!p2) { |
| 600 | p2=DummyString; |
| 601 | assert(s2==0); |
| 602 | } |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 603 | |
| 604 | if (!(a = PyBytes_FromStringAndSize(p1, s1))) { |
| 605 | return NULL; |
| 606 | } |
| 607 | if (!(b = PyBytes_FromStringAndSize(p2, s2))) { |
| 608 | Py_DECREF(a); |
| 609 | return NULL; |
| 610 | } |
| 611 | |
| 612 | #if (PY_VERSION_HEX >= 0x02040000) |
| 613 | r = PyTuple_Pack(2, a, b) ; |
| 614 | #else |
| 615 | r = Py_BuildValue("OO", a, b); |
| 616 | #endif |
| 617 | Py_DECREF(a); |
| 618 | Py_DECREF(b); |
| 619 | return r; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | static PyObject *BuildValue_IS(int i,const void *p,int s) |
| 623 | { |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 624 | PyObject *a, *r; |
| 625 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 626 | if (!p) { |
| 627 | p=DummyString; |
| 628 | assert(s==0); |
| 629 | } |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 630 | |
| 631 | if (!(a = PyBytes_FromStringAndSize(p, s))) { |
| 632 | return NULL; |
| 633 | } |
| 634 | |
| 635 | r = Py_BuildValue("iO", i, a); |
| 636 | Py_DECREF(a); |
| 637 | return r; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 640 | static PyObject *BuildValue_LS(long l,const void *p,int s) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 641 | { |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 642 | PyObject *a, *r; |
| 643 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 644 | if (!p) { |
| 645 | p=DummyString; |
| 646 | assert(s==0); |
| 647 | } |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 648 | |
| 649 | if (!(a = PyBytes_FromStringAndSize(p, s))) { |
| 650 | return NULL; |
| 651 | } |
| 652 | |
| 653 | r = Py_BuildValue("lO", l, a); |
| 654 | Py_DECREF(a); |
| 655 | return r; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | |
| 659 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 660 | /* make a nice exception object to raise for errors. */ |
| 661 | static int makeDBError(int err) |
| 662 | { |
| 663 | char errTxt[2048]; /* really big, just in case... */ |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 664 | PyObject *errObj = NULL; |
| 665 | PyObject *errTuple = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 666 | int exceptionRaised = 0; |
Neal Norwitz | dce937f | 2006-07-23 08:01:43 +0000 | [diff] [blame] | 667 | unsigned int bytes_left; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 668 | |
| 669 | switch (err) { |
| 670 | case 0: /* successful, no error */ break; |
| 671 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 672 | #if (DBVER < 41) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 673 | case DB_INCOMPLETE: |
| 674 | #if INCOMPLETE_IS_WARNING |
Neal Norwitz | dce937f | 2006-07-23 08:01:43 +0000 | [diff] [blame] | 675 | bytes_left = our_strlcpy(errTxt, db_strerror(err), sizeof(errTxt)); |
| 676 | /* Ensure that bytes_left never goes negative */ |
| 677 | if (_db_errmsg[0] && bytes_left < (sizeof(errTxt) - 4)) { |
| 678 | bytes_left = sizeof(errTxt) - bytes_left - 4 - 1; |
| 679 | assert(bytes_left >= 0); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 680 | strcat(errTxt, " -- "); |
Neal Norwitz | dce937f | 2006-07-23 08:01:43 +0000 | [diff] [blame] | 681 | strncat(errTxt, _db_errmsg, bytes_left); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 682 | } |
Neal Norwitz | dce937f | 2006-07-23 08:01:43 +0000 | [diff] [blame] | 683 | _db_errmsg[0] = 0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 684 | exceptionRaised = PyErr_Warn(PyExc_RuntimeWarning, errTxt); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 685 | |
| 686 | #else /* do an exception instead */ |
| 687 | errObj = DBIncompleteError; |
| 688 | #endif |
| 689 | break; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 690 | #endif /* DBVER < 41 */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 691 | |
| 692 | case DB_KEYEMPTY: errObj = DBKeyEmptyError; break; |
| 693 | case DB_KEYEXIST: errObj = DBKeyExistError; break; |
| 694 | case DB_LOCK_DEADLOCK: errObj = DBLockDeadlockError; break; |
| 695 | case DB_LOCK_NOTGRANTED: errObj = DBLockNotGrantedError; break; |
| 696 | case DB_NOTFOUND: errObj = DBNotFoundError; break; |
| 697 | case DB_OLD_VERSION: errObj = DBOldVersionError; break; |
| 698 | case DB_RUNRECOVERY: errObj = DBRunRecoveryError; break; |
| 699 | case DB_VERIFY_BAD: errObj = DBVerifyBadError; break; |
| 700 | case DB_NOSERVER: errObj = DBNoServerError; break; |
| 701 | case DB_NOSERVER_HOME: errObj = DBNoServerHomeError; break; |
| 702 | case DB_NOSERVER_ID: errObj = DBNoServerIDError; break; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 703 | case DB_PAGE_NOTFOUND: errObj = DBPageNotFoundError; break; |
| 704 | case DB_SECONDARY_BAD: errObj = DBSecondaryBadError; break; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 705 | case DB_BUFFER_SMALL: errObj = DBNoMemoryError; break; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 706 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 707 | #if (DBVER >= 43) |
| 708 | /* ENOMEM and DB_BUFFER_SMALL were one and the same until 4.3 */ |
| 709 | case ENOMEM: errObj = PyExc_MemoryError; break; |
| 710 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 711 | case EINVAL: errObj = DBInvalidArgError; break; |
| 712 | case EACCES: errObj = DBAccessError; break; |
| 713 | case ENOSPC: errObj = DBNoSpaceError; break; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 714 | case EAGAIN: errObj = DBAgainError; break; |
| 715 | case EBUSY : errObj = DBBusyError; break; |
| 716 | case EEXIST: errObj = DBFileExistsError; break; |
| 717 | case ENOENT: errObj = DBNoSuchFileError; break; |
| 718 | case EPERM : errObj = DBPermissionsError; break; |
| 719 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 720 | #if (DBVER >= 42) |
| 721 | case DB_REP_HANDLE_DEAD : errObj = DBRepHandleDeadError; break; |
| 722 | #endif |
| 723 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 724 | case DB_REP_UNAVAIL : errObj = DBRepUnavailError; break; |
| 725 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 726 | default: errObj = DBError; break; |
| 727 | } |
| 728 | |
| 729 | if (errObj != NULL) { |
Neal Norwitz | dce937f | 2006-07-23 08:01:43 +0000 | [diff] [blame] | 730 | bytes_left = our_strlcpy(errTxt, db_strerror(err), sizeof(errTxt)); |
| 731 | /* Ensure that bytes_left never goes negative */ |
| 732 | if (_db_errmsg[0] && bytes_left < (sizeof(errTxt) - 4)) { |
| 733 | bytes_left = sizeof(errTxt) - bytes_left - 4 - 1; |
| 734 | assert(bytes_left >= 0); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 735 | strcat(errTxt, " -- "); |
Neal Norwitz | dce937f | 2006-07-23 08:01:43 +0000 | [diff] [blame] | 736 | strncat(errTxt, _db_errmsg, bytes_left); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 737 | } |
Neal Norwitz | dce937f | 2006-07-23 08:01:43 +0000 | [diff] [blame] | 738 | _db_errmsg[0] = 0; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 739 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 740 | errTuple = Py_BuildValue("(is)", err, errTxt); |
| 741 | if (errTuple == NULL) { |
| 742 | Py_DECREF(errObj); |
| 743 | return !0; |
| 744 | } |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 745 | PyErr_SetObject(errObj, errTuple); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 746 | Py_DECREF(errTuple); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | return ((errObj != NULL) || exceptionRaised); |
| 750 | } |
| 751 | |
| 752 | |
| 753 | |
| 754 | /* set a type exception */ |
| 755 | static void makeTypeError(char* expected, PyObject* found) |
| 756 | { |
| 757 | PyErr_Format(PyExc_TypeError, "Expected %s argument, %s found.", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 758 | expected, Py_TYPE(found)->tp_name); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | |
| 762 | /* verify that an obj is either None or a DBTxn, and set the txn pointer */ |
| 763 | static int checkTxnObj(PyObject* txnobj, DB_TXN** txn) |
| 764 | { |
| 765 | if (txnobj == Py_None || txnobj == NULL) { |
| 766 | *txn = NULL; |
| 767 | return 1; |
| 768 | } |
| 769 | if (DBTxnObject_Check(txnobj)) { |
| 770 | *txn = ((DBTxnObject*)txnobj)->txn; |
| 771 | return 1; |
| 772 | } |
| 773 | else |
| 774 | makeTypeError("DBTxn", txnobj); |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | |
| 779 | /* Delete a key from a database |
| 780 | Returns 0 on success, -1 on an error. */ |
| 781 | static int _DB_delete(DBObject* self, DB_TXN *txn, DBT *key, int flags) |
| 782 | { |
| 783 | int err; |
| 784 | |
| 785 | MYDB_BEGIN_ALLOW_THREADS; |
| 786 | err = self->db->del(self->db, txn, key, 0); |
| 787 | MYDB_END_ALLOW_THREADS; |
| 788 | if (makeDBError(err)) { |
| 789 | return -1; |
| 790 | } |
| 791 | self->haveStat = 0; |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | |
| 796 | /* Store a key into a database |
| 797 | Returns 0 on success, -1 on an error. */ |
| 798 | static int _DB_put(DBObject* self, DB_TXN *txn, DBT *key, DBT *data, int flags) |
| 799 | { |
| 800 | int err; |
| 801 | |
| 802 | MYDB_BEGIN_ALLOW_THREADS; |
| 803 | err = self->db->put(self->db, txn, key, data, flags); |
| 804 | MYDB_END_ALLOW_THREADS; |
| 805 | if (makeDBError(err)) { |
| 806 | return -1; |
| 807 | } |
| 808 | self->haveStat = 0; |
| 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | /* Get a key/data pair from a cursor */ |
| 813 | static PyObject* _DBCursor_get(DBCursorObject* self, int extra_flags, |
| 814 | PyObject *args, PyObject *kwargs, char *format) |
| 815 | { |
| 816 | int err; |
| 817 | PyObject* retval = NULL; |
| 818 | DBT key, data; |
| 819 | int dlen = -1; |
| 820 | int doff = -1; |
| 821 | int flags = 0; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 822 | static char* kwnames[] = { "flags", "dlen", "doff", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 823 | |
| 824 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, kwnames, |
| 825 | &flags, &dlen, &doff)) |
| 826 | return NULL; |
| 827 | |
| 828 | CHECK_CURSOR_NOT_CLOSED(self); |
| 829 | |
| 830 | flags |= extra_flags; |
| 831 | CLEAR_DBT(key); |
| 832 | CLEAR_DBT(data); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 833 | if (!add_partial_dbt(&data, dlen, doff)) |
| 834 | return NULL; |
| 835 | |
| 836 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 837 | err = _DBC_get(self->dbc, &key, &data, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 838 | MYDB_END_ALLOW_THREADS; |
| 839 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 840 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 841 | && self->mydb->moduleFlags.getReturnsNone) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 842 | Py_INCREF(Py_None); |
| 843 | retval = Py_None; |
| 844 | } |
| 845 | else if (makeDBError(err)) { |
| 846 | retval = NULL; |
| 847 | } |
| 848 | else { /* otherwise, success! */ |
| 849 | |
| 850 | /* if Recno or Queue, return the key as an Int */ |
| 851 | switch (_DB_get_type(self->mydb)) { |
| 852 | case -1: |
| 853 | retval = NULL; |
| 854 | break; |
| 855 | |
| 856 | case DB_RECNO: |
| 857 | case DB_QUEUE: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 858 | retval = BuildValue_IS(*((db_recno_t*)key.data), data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 859 | break; |
| 860 | case DB_HASH: |
| 861 | case DB_BTREE: |
| 862 | default: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 863 | retval = BuildValue_SS(key.data, key.size, data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 864 | break; |
| 865 | } |
| 866 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 867 | return retval; |
| 868 | } |
| 869 | |
| 870 | |
| 871 | /* add an integer to a dictionary using the given name as a key */ |
| 872 | static void _addIntToDict(PyObject* dict, char *name, int value) |
| 873 | { |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 874 | PyObject* v = NUMBER_FromLong((long) value); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 875 | if (!v || PyDict_SetItemString(dict, name, v)) |
| 876 | PyErr_Clear(); |
| 877 | |
| 878 | Py_XDECREF(v); |
| 879 | } |
Kristján Valur Jónsson | bd77c03 | 2007-04-26 15:24:54 +0000 | [diff] [blame] | 880 | |
| 881 | /* The same, when the value is a time_t */ |
| 882 | static void _addTimeTToDict(PyObject* dict, char *name, time_t value) |
| 883 | { |
| 884 | PyObject* v; |
| 885 | /* if the value fits in regular int, use that. */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 886 | #ifdef PY_LONG_LONG |
Kristján Valur Jónsson | bd77c03 | 2007-04-26 15:24:54 +0000 | [diff] [blame] | 887 | if (sizeof(time_t) > sizeof(long)) |
| 888 | v = PyLong_FromLongLong((PY_LONG_LONG) value); |
| 889 | else |
| 890 | #endif |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 891 | v = NUMBER_FromLong((long) value); |
Kristján Valur Jónsson | bd77c03 | 2007-04-26 15:24:54 +0000 | [diff] [blame] | 892 | if (!v || PyDict_SetItemString(dict, name, v)) |
| 893 | PyErr_Clear(); |
| 894 | |
| 895 | Py_XDECREF(v); |
| 896 | } |
| 897 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 898 | #if (DBVER >= 43) |
| 899 | /* add an db_seq_t to a dictionary using the given name as a key */ |
| 900 | static void _addDb_seq_tToDict(PyObject* dict, char *name, db_seq_t value) |
| 901 | { |
| 902 | PyObject* v = PyLong_FromLongLong(value); |
| 903 | if (!v || PyDict_SetItemString(dict, name, v)) |
| 904 | PyErr_Clear(); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 905 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 906 | Py_XDECREF(v); |
| 907 | } |
| 908 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 909 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 910 | static void _addDB_lsnToDict(PyObject* dict, char *name, DB_LSN value) |
| 911 | { |
| 912 | PyObject *v = Py_BuildValue("(ll)",value.file,value.offset); |
| 913 | if (!v || PyDict_SetItemString(dict, name, v)) |
| 914 | PyErr_Clear(); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 915 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 916 | Py_XDECREF(v); |
| 917 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 918 | |
| 919 | /* --------------------------------------------------------------------- */ |
| 920 | /* Allocators and deallocators */ |
| 921 | |
| 922 | static DBObject* |
| 923 | newDBObject(DBEnvObject* arg, int flags) |
| 924 | { |
| 925 | DBObject* self; |
| 926 | DB_ENV* db_env = NULL; |
| 927 | int err; |
| 928 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 929 | self = PyObject_New(DBObject, &DB_Type); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 930 | if (self == NULL) |
| 931 | return NULL; |
| 932 | |
| 933 | self->haveStat = 0; |
| 934 | self->flags = 0; |
| 935 | self->setflags = 0; |
| 936 | self->myenvobj = NULL; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 937 | self->db = NULL; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 938 | self->children_cursors = NULL; |
| 939 | #if (DBVER >=43) |
| 940 | self->children_sequences = NULL; |
| 941 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 942 | self->associateCallback = NULL; |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 943 | self->btCompareCallback = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 944 | self->primaryDBType = 0; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 945 | Py_INCREF(Py_None); |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 946 | self->private_obj = Py_None; |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 947 | self->in_weakreflist = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 948 | |
| 949 | /* keep a reference to our python DBEnv object */ |
| 950 | if (arg) { |
| 951 | Py_INCREF(arg); |
| 952 | self->myenvobj = arg; |
| 953 | db_env = arg->db_env; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 954 | INSERT_IN_DOUBLE_LINKED_LIST(self->myenvobj->children_dbs,self); |
| 955 | } else { |
| 956 | self->sibling_prev_p=NULL; |
| 957 | self->sibling_next=NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 958 | } |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 959 | self->txn=NULL; |
| 960 | self->sibling_prev_p_txn=NULL; |
| 961 | self->sibling_next_txn=NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 962 | |
| 963 | if (self->myenvobj) |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 964 | self->moduleFlags = self->myenvobj->moduleFlags; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 965 | else |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 966 | self->moduleFlags.getReturnsNone = DEFAULT_GET_RETURNS_NONE; |
| 967 | self->moduleFlags.cursorSetReturnsNone = DEFAULT_CURSOR_SET_RETURNS_NONE; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 968 | |
| 969 | MYDB_BEGIN_ALLOW_THREADS; |
| 970 | err = db_create(&self->db, db_env, flags); |
Neal Norwitz | dce937f | 2006-07-23 08:01:43 +0000 | [diff] [blame] | 971 | if (self->db != NULL) { |
| 972 | self->db->set_errcall(self->db, _db_errorCallback); |
Neal Norwitz | dce937f | 2006-07-23 08:01:43 +0000 | [diff] [blame] | 973 | self->db->app_private = (void*)self; |
Neal Norwitz | dce937f | 2006-07-23 08:01:43 +0000 | [diff] [blame] | 974 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 975 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 976 | /* TODO add a weakref(self) to the self->myenvobj->open_child_weakrefs |
| 977 | * list so that a DBEnv can refuse to close without aborting any open |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 978 | * DBTxns and closing any open DBs first. */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 979 | if (makeDBError(err)) { |
| 980 | if (self->myenvobj) { |
| 981 | Py_DECREF(self->myenvobj); |
| 982 | self->myenvobj = NULL; |
| 983 | } |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 984 | Py_DECREF(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 985 | self = NULL; |
| 986 | } |
| 987 | return self; |
| 988 | } |
| 989 | |
| 990 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 991 | /* Forward declaration */ |
| 992 | static PyObject *DB_close_internal(DBObject* self, int flags); |
| 993 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 994 | static void |
| 995 | DB_dealloc(DBObject* self) |
| 996 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 997 | PyObject *dummy; |
| 998 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 999 | if (self->db != NULL) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1000 | dummy=DB_close_internal(self,0); |
| 1001 | Py_XDECREF(dummy); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1002 | } |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1003 | if (self->in_weakreflist != NULL) { |
| 1004 | PyObject_ClearWeakRefs((PyObject *) self); |
| 1005 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1006 | if (self->myenvobj) { |
| 1007 | Py_DECREF(self->myenvobj); |
| 1008 | self->myenvobj = NULL; |
| 1009 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1010 | if (self->associateCallback != NULL) { |
| 1011 | Py_DECREF(self->associateCallback); |
| 1012 | self->associateCallback = NULL; |
| 1013 | } |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 1014 | if (self->btCompareCallback != NULL) { |
| 1015 | Py_DECREF(self->btCompareCallback); |
| 1016 | self->btCompareCallback = NULL; |
| 1017 | } |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 1018 | Py_DECREF(self->private_obj); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1019 | PyObject_Del(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1022 | static DBCursorObject* |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1023 | newDBCursorObject(DBC* dbc, DBTxnObject *txn, DBObject* db) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1024 | { |
Neal Norwitz | b4a5581 | 2004-07-09 23:30:57 +0000 | [diff] [blame] | 1025 | DBCursorObject* self = PyObject_New(DBCursorObject, &DBCursor_Type); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1026 | if (self == NULL) |
| 1027 | return NULL; |
| 1028 | |
| 1029 | self->dbc = dbc; |
| 1030 | self->mydb = db; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1031 | |
| 1032 | INSERT_IN_DOUBLE_LINKED_LIST(self->mydb->children_cursors,self); |
| 1033 | if (txn && ((PyObject *)txn!=Py_None)) { |
| 1034 | INSERT_IN_DOUBLE_LINKED_LIST_TXN(txn->children_cursors,self); |
| 1035 | self->txn=txn; |
| 1036 | } else { |
| 1037 | self->txn=NULL; |
| 1038 | } |
| 1039 | |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 1040 | self->in_weakreflist = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1041 | Py_INCREF(self->mydb); |
| 1042 | return self; |
| 1043 | } |
| 1044 | |
| 1045 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1046 | /* Forward declaration */ |
| 1047 | static PyObject *DBC_close_internal(DBCursorObject* self); |
| 1048 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1049 | static void |
| 1050 | DBCursor_dealloc(DBCursorObject* self) |
| 1051 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1052 | PyObject *dummy; |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 1053 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1054 | if (self->dbc != NULL) { |
| 1055 | dummy=DBC_close_internal(self); |
| 1056 | Py_XDECREF(dummy); |
| 1057 | } |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 1058 | if (self->in_weakreflist != NULL) { |
| 1059 | PyObject_ClearWeakRefs((PyObject *) self); |
| 1060 | } |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1061 | Py_DECREF(self->mydb); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1062 | PyObject_Del(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | |
| 1066 | static DBEnvObject* |
| 1067 | newDBEnvObject(int flags) |
| 1068 | { |
| 1069 | int err; |
Neal Norwitz | b4a5581 | 2004-07-09 23:30:57 +0000 | [diff] [blame] | 1070 | DBEnvObject* self = PyObject_New(DBEnvObject, &DBEnv_Type); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1071 | if (self == NULL) |
| 1072 | return NULL; |
| 1073 | |
| 1074 | self->closed = 1; |
| 1075 | self->flags = flags; |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 1076 | self->moduleFlags.getReturnsNone = DEFAULT_GET_RETURNS_NONE; |
| 1077 | self->moduleFlags.cursorSetReturnsNone = DEFAULT_CURSOR_SET_RETURNS_NONE; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1078 | self->children_dbs = NULL; |
| 1079 | self->children_txns = NULL; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1080 | Py_INCREF(Py_None); |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 1081 | self->private_obj = Py_None; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1082 | Py_INCREF(Py_None); |
| 1083 | self->rep_transport = Py_None; |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1084 | self->in_weakreflist = NULL; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1085 | self->event_notifyCallback = NULL; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1086 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1087 | MYDB_BEGIN_ALLOW_THREADS; |
| 1088 | err = db_env_create(&self->db_env, flags); |
| 1089 | MYDB_END_ALLOW_THREADS; |
| 1090 | if (makeDBError(err)) { |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1091 | Py_DECREF(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1092 | self = NULL; |
| 1093 | } |
| 1094 | else { |
| 1095 | self->db_env->set_errcall(self->db_env, _db_errorCallback); |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 1096 | self->db_env->app_private = self; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1097 | } |
| 1098 | return self; |
| 1099 | } |
| 1100 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1101 | /* Forward declaration */ |
| 1102 | static PyObject *DBEnv_close_internal(DBEnvObject* self, int flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1103 | |
| 1104 | static void |
| 1105 | DBEnv_dealloc(DBEnvObject* self) |
| 1106 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1107 | PyObject *dummy; |
| 1108 | |
Jesus Cea | ac25fab | 2008-09-03 17:50:32 +0000 | [diff] [blame] | 1109 | if (self->db_env) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1110 | dummy=DBEnv_close_internal(self,0); |
| 1111 | Py_XDECREF(dummy); |
| 1112 | } |
| 1113 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1114 | Py_XDECREF(self->event_notifyCallback); |
| 1115 | self->event_notifyCallback = NULL; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1116 | |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1117 | if (self->in_weakreflist != NULL) { |
| 1118 | PyObject_ClearWeakRefs((PyObject *) self); |
| 1119 | } |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 1120 | Py_DECREF(self->private_obj); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1121 | Py_DECREF(self->rep_transport); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1122 | PyObject_Del(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | |
| 1126 | static DBTxnObject* |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1127 | newDBTxnObject(DBEnvObject* myenv, DBTxnObject *parent, DB_TXN *txn, int flags) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1128 | { |
| 1129 | int err; |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1130 | DB_TXN *parent_txn = NULL; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1131 | |
Neal Norwitz | b4a5581 | 2004-07-09 23:30:57 +0000 | [diff] [blame] | 1132 | DBTxnObject* self = PyObject_New(DBTxnObject, &DBTxn_Type); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1133 | if (self == NULL) |
| 1134 | return NULL; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1135 | |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1136 | self->in_weakreflist = NULL; |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1137 | self->children_txns = NULL; |
| 1138 | self->children_dbs = NULL; |
| 1139 | self->children_cursors = NULL; |
| 1140 | self->children_sequences = NULL; |
| 1141 | self->flag_prepare = 0; |
| 1142 | self->parent_txn = NULL; |
| 1143 | self->env = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1144 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1145 | if (parent && ((PyObject *)parent!=Py_None)) { |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1146 | parent_txn = parent->txn; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1147 | } |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1148 | |
| 1149 | if (txn) { |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1150 | self->txn = txn; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1151 | } else { |
| 1152 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1153 | err = myenv->db_env->txn_begin(myenv->db_env, parent_txn, &(self->txn), flags); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1154 | MYDB_END_ALLOW_THREADS; |
| 1155 | |
| 1156 | if (makeDBError(err)) { |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1157 | Py_DECREF(self); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1158 | return NULL; |
| 1159 | } |
| 1160 | } |
| 1161 | |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1162 | /* Can't use 'parent' because could be 'parent==Py_None' */ |
| 1163 | if (parent_txn) { |
| 1164 | self->parent_txn = parent; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1165 | Py_INCREF(parent); |
| 1166 | self->env = NULL; |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1167 | INSERT_IN_DOUBLE_LINKED_LIST(parent->children_txns, self); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1168 | } else { |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1169 | self->parent_txn = NULL; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1170 | Py_INCREF(myenv); |
| 1171 | self->env = myenv; |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1172 | INSERT_IN_DOUBLE_LINKED_LIST(myenv->children_txns, self); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1175 | return self; |
| 1176 | } |
| 1177 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1178 | /* Forward declaration */ |
| 1179 | static PyObject * |
| 1180 | DBTxn_abort_discard_internal(DBTxnObject* self, int discard); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1181 | |
| 1182 | static void |
| 1183 | DBTxn_dealloc(DBTxnObject* self) |
| 1184 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1185 | PyObject *dummy; |
| 1186 | |
| 1187 | if (self->txn) { |
| 1188 | int flag_prepare = self->flag_prepare; |
| 1189 | dummy=DBTxn_abort_discard_internal(self,0); |
| 1190 | Py_XDECREF(dummy); |
| 1191 | if (!flag_prepare) { |
| 1192 | PyErr_Warn(PyExc_RuntimeWarning, |
| 1193 | "DBTxn aborted in destructor. No prior commit() or abort()."); |
| 1194 | } |
| 1195 | } |
| 1196 | |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1197 | if (self->in_weakreflist != NULL) { |
| 1198 | PyObject_ClearWeakRefs((PyObject *) self); |
| 1199 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1200 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1201 | if (self->env) { |
| 1202 | Py_DECREF(self->env); |
| 1203 | } else { |
| 1204 | Py_DECREF(self->parent_txn); |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1205 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1206 | PyObject_Del(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | |
| 1210 | static DBLockObject* |
| 1211 | newDBLockObject(DBEnvObject* myenv, u_int32_t locker, DBT* obj, |
| 1212 | db_lockmode_t lock_mode, int flags) |
| 1213 | { |
| 1214 | int err; |
Neal Norwitz | b4a5581 | 2004-07-09 23:30:57 +0000 | [diff] [blame] | 1215 | DBLockObject* self = PyObject_New(DBLockObject, &DBLock_Type); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1216 | if (self == NULL) |
| 1217 | return NULL; |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1218 | self->in_weakreflist = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1219 | |
| 1220 | MYDB_BEGIN_ALLOW_THREADS; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1221 | err = myenv->db_env->lock_get(myenv->db_env, locker, flags, obj, lock_mode, |
| 1222 | &self->lock); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1223 | MYDB_END_ALLOW_THREADS; |
| 1224 | if (makeDBError(err)) { |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1225 | Py_DECREF(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1226 | self = NULL; |
| 1227 | } |
| 1228 | |
| 1229 | return self; |
| 1230 | } |
| 1231 | |
| 1232 | |
| 1233 | static void |
| 1234 | DBLock_dealloc(DBLockObject* self) |
| 1235 | { |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1236 | if (self->in_weakreflist != NULL) { |
| 1237 | PyObject_ClearWeakRefs((PyObject *) self); |
| 1238 | } |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1239 | /* TODO: is this lock held? should we release it? */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1240 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1241 | PyObject_Del(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 1245 | #if (DBVER >= 43) |
| 1246 | static DBSequenceObject* |
| 1247 | newDBSequenceObject(DBObject* mydb, int flags) |
| 1248 | { |
| 1249 | int err; |
| 1250 | DBSequenceObject* self = PyObject_New(DBSequenceObject, &DBSequence_Type); |
| 1251 | if (self == NULL) |
| 1252 | return NULL; |
| 1253 | Py_INCREF(mydb); |
| 1254 | self->mydb = mydb; |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 1255 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1256 | INSERT_IN_DOUBLE_LINKED_LIST(self->mydb->children_sequences,self); |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1257 | self->txn = NULL; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1258 | |
| 1259 | self->in_weakreflist = NULL; |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 1260 | |
| 1261 | MYDB_BEGIN_ALLOW_THREADS; |
| 1262 | err = db_sequence_create(&self->sequence, self->mydb->db, flags); |
| 1263 | MYDB_END_ALLOW_THREADS; |
| 1264 | if (makeDBError(err)) { |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1265 | Py_DECREF(self); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 1266 | self = NULL; |
| 1267 | } |
| 1268 | |
| 1269 | return self; |
| 1270 | } |
| 1271 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1272 | /* Forward declaration */ |
| 1273 | static PyObject |
| 1274 | *DBSequence_close_internal(DBSequenceObject* self, int flags, int do_not_close); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 1275 | |
| 1276 | static void |
| 1277 | DBSequence_dealloc(DBSequenceObject* self) |
| 1278 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1279 | PyObject *dummy; |
| 1280 | |
| 1281 | if (self->sequence != NULL) { |
| 1282 | dummy=DBSequence_close_internal(self,0,0); |
| 1283 | Py_XDECREF(dummy); |
| 1284 | } |
| 1285 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 1286 | if (self->in_weakreflist != NULL) { |
| 1287 | PyObject_ClearWeakRefs((PyObject *) self); |
| 1288 | } |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 1289 | |
| 1290 | Py_DECREF(self->mydb); |
| 1291 | PyObject_Del(self); |
| 1292 | } |
| 1293 | #endif |
| 1294 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1295 | /* --------------------------------------------------------------------- */ |
| 1296 | /* DB methods */ |
| 1297 | |
| 1298 | static PyObject* |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 1299 | DB_append(DBObject* self, PyObject* args, PyObject* kwargs) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1300 | { |
| 1301 | PyObject* txnobj = NULL; |
| 1302 | PyObject* dataobj; |
| 1303 | db_recno_t recno; |
| 1304 | DBT key, data; |
| 1305 | DB_TXN *txn = NULL; |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 1306 | static char* kwnames[] = { "data", "txn", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1307 | |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 1308 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:append", kwnames, |
| 1309 | &dataobj, &txnobj)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1310 | return NULL; |
| 1311 | |
| 1312 | CHECK_DB_NOT_CLOSED(self); |
| 1313 | |
| 1314 | /* make a dummy key out of a recno */ |
| 1315 | recno = 0; |
| 1316 | CLEAR_DBT(key); |
| 1317 | key.data = &recno; |
| 1318 | key.size = sizeof(recno); |
| 1319 | key.ulen = key.size; |
| 1320 | key.flags = DB_DBT_USERMEM; |
| 1321 | |
| 1322 | if (!make_dbt(dataobj, &data)) return NULL; |
| 1323 | if (!checkTxnObj(txnobj, &txn)) return NULL; |
| 1324 | |
| 1325 | if (-1 == _DB_put(self, txn, &key, &data, DB_APPEND)) |
| 1326 | return NULL; |
| 1327 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1328 | return NUMBER_FromLong(recno); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1332 | static int |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1333 | _db_associateCallback(DB* db, const DBT* priKey, const DBT* priData, |
| 1334 | DBT* secKey) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1335 | { |
| 1336 | int retval = DB_DONOTINDEX; |
| 1337 | DBObject* secondaryDB = (DBObject*)db->app_private; |
| 1338 | PyObject* callback = secondaryDB->associateCallback; |
| 1339 | int type = secondaryDB->primaryDBType; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1340 | PyObject* args; |
Thomas Wouters | 89ba381 | 2006-03-07 14:14:51 +0000 | [diff] [blame] | 1341 | PyObject* result = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1342 | |
| 1343 | |
| 1344 | if (callback != NULL) { |
| 1345 | MYDB_BEGIN_BLOCK_THREADS; |
| 1346 | |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 1347 | if (type == DB_RECNO || type == DB_QUEUE) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1348 | args = BuildValue_LS(*((db_recno_t*)priKey->data), priData->data, priData->size); |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 1349 | else |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1350 | args = BuildValue_SS(priKey->data, priKey->size, priData->data, priData->size); |
Thomas Wouters | 098f694 | 2006-03-07 14:13:17 +0000 | [diff] [blame] | 1351 | if (args != NULL) { |
Thomas Wouters | 098f694 | 2006-03-07 14:13:17 +0000 | [diff] [blame] | 1352 | result = PyEval_CallObject(callback, args); |
| 1353 | } |
| 1354 | if (args == NULL || result == NULL) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1355 | PyErr_Print(); |
| 1356 | } |
| 1357 | else if (result == Py_None) { |
| 1358 | retval = DB_DONOTINDEX; |
| 1359 | } |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1360 | else if (NUMBER_Check(result)) { |
| 1361 | retval = NUMBER_AsLong(result); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1362 | } |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 1363 | else if (PyBytes_Check(result)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1364 | char* data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1365 | Py_ssize_t size; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1366 | |
| 1367 | CLEAR_DBT(*secKey); |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 1368 | PyBytes_AsStringAndSize(result, &data, &size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1369 | secKey->flags = DB_DBT_APPMALLOC; /* DB will free */ |
| 1370 | secKey->data = malloc(size); /* TODO, check this */ |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1371 | if (secKey->data) { |
| 1372 | memcpy(secKey->data, data, size); |
| 1373 | secKey->size = size; |
| 1374 | retval = 0; |
| 1375 | } |
| 1376 | else { |
| 1377 | PyErr_SetString(PyExc_MemoryError, |
| 1378 | "malloc failed in _db_associateCallback"); |
| 1379 | PyErr_Print(); |
| 1380 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1381 | } |
| 1382 | else { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1383 | PyErr_SetString( |
| 1384 | PyExc_TypeError, |
| 1385 | "DB associate callback should return DB_DONOTINDEX or string."); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1386 | PyErr_Print(); |
| 1387 | } |
| 1388 | |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 1389 | Py_XDECREF(args); |
| 1390 | Py_XDECREF(result); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1391 | |
| 1392 | MYDB_END_BLOCK_THREADS; |
| 1393 | } |
| 1394 | return retval; |
| 1395 | } |
| 1396 | |
| 1397 | |
| 1398 | static PyObject* |
| 1399 | DB_associate(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1400 | { |
| 1401 | int err, flags=0; |
| 1402 | DBObject* secondaryDB; |
| 1403 | PyObject* callback; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1404 | #if (DBVER >= 41) |
| 1405 | PyObject *txnobj = NULL; |
| 1406 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1407 | static char* kwnames[] = {"secondaryDB", "callback", "flags", "txn", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1408 | NULL}; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1409 | #else |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1410 | static char* kwnames[] = {"secondaryDB", "callback", "flags", NULL}; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1411 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1412 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1413 | #if (DBVER >= 41) |
| 1414 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iO:associate", kwnames, |
| 1415 | &secondaryDB, &callback, &flags, |
| 1416 | &txnobj)) { |
| 1417 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1418 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:associate", kwnames, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1419 | &secondaryDB, &callback, &flags)) { |
| 1420 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1421 | return NULL; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | #if (DBVER >= 41) |
| 1425 | if (!checkTxnObj(txnobj, &txn)) return NULL; |
| 1426 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1427 | |
| 1428 | CHECK_DB_NOT_CLOSED(self); |
| 1429 | if (!DBObject_Check(secondaryDB)) { |
| 1430 | makeTypeError("DB", (PyObject*)secondaryDB); |
| 1431 | return NULL; |
| 1432 | } |
Gregory P. Smith | 91116b6 | 2005-06-06 10:28:06 +0000 | [diff] [blame] | 1433 | CHECK_DB_NOT_CLOSED(secondaryDB); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1434 | if (callback == Py_None) { |
| 1435 | callback = NULL; |
| 1436 | } |
| 1437 | else if (!PyCallable_Check(callback)) { |
| 1438 | makeTypeError("Callable", callback); |
| 1439 | return NULL; |
| 1440 | } |
| 1441 | |
| 1442 | /* Save a reference to the callback in the secondary DB. */ |
Gregory P. Smith | 692ca9a | 2005-06-06 09:55:06 +0000 | [diff] [blame] | 1443 | Py_XDECREF(secondaryDB->associateCallback); |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 1444 | Py_XINCREF(callback); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1445 | secondaryDB->associateCallback = callback; |
| 1446 | secondaryDB->primaryDBType = _DB_get_type(self); |
| 1447 | |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 1448 | /* PyEval_InitThreads is called here due to a quirk in python 1.5 |
| 1449 | * - 2.2.1 (at least) according to Russell Williamson <merel@wt.net>: |
| 1450 | * The global interepreter lock is not initialized until the first |
| 1451 | * thread is created using thread.start_new_thread() or fork() is |
| 1452 | * called. that would cause the ALLOW_THREADS here to segfault due |
| 1453 | * to a null pointer reference if no threads or child processes |
| 1454 | * have been created. This works around that and is a no-op if |
| 1455 | * threads have already been initialized. |
| 1456 | * (see pybsddb-users mailing list post on 2002-08-07) |
| 1457 | */ |
Gregory P. Smith | aa71f5f | 2003-01-17 07:56:16 +0000 | [diff] [blame] | 1458 | #ifdef WITH_THREAD |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 1459 | PyEval_InitThreads(); |
Gregory P. Smith | aa71f5f | 2003-01-17 07:56:16 +0000 | [diff] [blame] | 1460 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1461 | MYDB_BEGIN_ALLOW_THREADS; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1462 | #if (DBVER >= 41) |
| 1463 | err = self->db->associate(self->db, |
| 1464 | txn, |
| 1465 | secondaryDB->db, |
| 1466 | _db_associateCallback, |
| 1467 | flags); |
| 1468 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1469 | err = self->db->associate(self->db, |
| 1470 | secondaryDB->db, |
| 1471 | _db_associateCallback, |
| 1472 | flags); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1473 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1474 | MYDB_END_ALLOW_THREADS; |
| 1475 | |
| 1476 | if (err) { |
Gregory P. Smith | 692ca9a | 2005-06-06 09:55:06 +0000 | [diff] [blame] | 1477 | Py_XDECREF(secondaryDB->associateCallback); |
| 1478 | secondaryDB->associateCallback = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1479 | secondaryDB->primaryDBType = 0; |
| 1480 | } |
| 1481 | |
| 1482 | RETURN_IF_ERR(); |
| 1483 | RETURN_NONE(); |
| 1484 | } |
| 1485 | |
| 1486 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1487 | static PyObject* |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1488 | DB_close_internal(DBObject* self, int flags) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1489 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1490 | PyObject *dummy; |
| 1491 | int err; |
| 1492 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1493 | if (self->db != NULL) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1494 | /* Can be NULL if db is not in an environment */ |
| 1495 | EXTRACT_FROM_DOUBLE_LINKED_LIST_MAYBE_NULL(self); |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 1496 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1497 | if (self->txn) { |
| 1498 | EXTRACT_FROM_DOUBLE_LINKED_LIST_TXN(self); |
| 1499 | self->txn=NULL; |
| 1500 | } |
| 1501 | |
| 1502 | while(self->children_cursors) { |
| 1503 | dummy=DBC_close_internal(self->children_cursors); |
| 1504 | Py_XDECREF(dummy); |
| 1505 | } |
| 1506 | |
| 1507 | #if (DBVER >= 43) |
| 1508 | while(self->children_sequences) { |
| 1509 | dummy=DBSequence_close_internal(self->children_sequences,0,0); |
| 1510 | Py_XDECREF(dummy); |
| 1511 | } |
| 1512 | #endif |
| 1513 | |
| 1514 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1515 | err = self->db->close(self->db, flags); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1516 | MYDB_END_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1517 | self->db = NULL; |
| 1518 | RETURN_IF_ERR(); |
| 1519 | } |
| 1520 | RETURN_NONE(); |
| 1521 | } |
| 1522 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1523 | static PyObject* |
| 1524 | DB_close(DBObject* self, PyObject* args) |
| 1525 | { |
| 1526 | int flags=0; |
| 1527 | if (!PyArg_ParseTuple(args,"|i:close", &flags)) |
| 1528 | return NULL; |
| 1529 | return DB_close_internal(self,flags); |
| 1530 | } |
| 1531 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1532 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1533 | static PyObject* |
| 1534 | _DB_consume(DBObject* self, PyObject* args, PyObject* kwargs, int consume_flag) |
| 1535 | { |
| 1536 | int err, flags=0, type; |
| 1537 | PyObject* txnobj = NULL; |
| 1538 | PyObject* retval = NULL; |
| 1539 | DBT key, data; |
| 1540 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1541 | static char* kwnames[] = { "txn", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1542 | |
| 1543 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:consume", kwnames, |
| 1544 | &txnobj, &flags)) |
| 1545 | return NULL; |
| 1546 | |
| 1547 | CHECK_DB_NOT_CLOSED(self); |
| 1548 | type = _DB_get_type(self); |
| 1549 | if (type == -1) |
| 1550 | return NULL; |
| 1551 | if (type != DB_QUEUE) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1552 | PyErr_SetString(PyExc_TypeError, |
| 1553 | "Consume methods only allowed for Queue DB's"); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1554 | return NULL; |
| 1555 | } |
| 1556 | if (!checkTxnObj(txnobj, &txn)) |
| 1557 | return NULL; |
| 1558 | |
| 1559 | CLEAR_DBT(key); |
| 1560 | CLEAR_DBT(data); |
| 1561 | if (CHECK_DBFLAG(self, DB_THREAD)) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1562 | /* Tell Berkeley DB to malloc the return value (thread safe) */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1563 | data.flags = DB_DBT_MALLOC; |
| 1564 | key.flags = DB_DBT_MALLOC; |
| 1565 | } |
| 1566 | |
| 1567 | MYDB_BEGIN_ALLOW_THREADS; |
| 1568 | err = self->db->get(self->db, txn, &key, &data, flags|consume_flag); |
| 1569 | MYDB_END_ALLOW_THREADS; |
| 1570 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 1571 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 1572 | && self->moduleFlags.getReturnsNone) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1573 | err = 0; |
| 1574 | Py_INCREF(Py_None); |
| 1575 | retval = Py_None; |
| 1576 | } |
| 1577 | else if (!err) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1578 | retval = BuildValue_SS(key.data, key.size, data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1579 | FREE_DBT(key); |
| 1580 | FREE_DBT(data); |
| 1581 | } |
| 1582 | |
| 1583 | RETURN_IF_ERR(); |
| 1584 | return retval; |
| 1585 | } |
| 1586 | |
| 1587 | static PyObject* |
| 1588 | DB_consume(DBObject* self, PyObject* args, PyObject* kwargs, int consume_flag) |
| 1589 | { |
| 1590 | return _DB_consume(self, args, kwargs, DB_CONSUME); |
| 1591 | } |
| 1592 | |
| 1593 | static PyObject* |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1594 | DB_consume_wait(DBObject* self, PyObject* args, PyObject* kwargs, |
| 1595 | int consume_flag) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1596 | { |
| 1597 | return _DB_consume(self, args, kwargs, DB_CONSUME_WAIT); |
| 1598 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1599 | |
| 1600 | |
| 1601 | static PyObject* |
| 1602 | DB_cursor(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1603 | { |
| 1604 | int err, flags=0; |
| 1605 | DBC* dbc; |
| 1606 | PyObject* txnobj = NULL; |
| 1607 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1608 | static char* kwnames[] = { "txn", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1609 | |
| 1610 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:cursor", kwnames, |
| 1611 | &txnobj, &flags)) |
| 1612 | return NULL; |
| 1613 | CHECK_DB_NOT_CLOSED(self); |
| 1614 | if (!checkTxnObj(txnobj, &txn)) |
| 1615 | return NULL; |
| 1616 | |
| 1617 | MYDB_BEGIN_ALLOW_THREADS; |
| 1618 | err = self->db->cursor(self->db, txn, &dbc, flags); |
| 1619 | MYDB_END_ALLOW_THREADS; |
| 1620 | RETURN_IF_ERR(); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1621 | return (PyObject*) newDBCursorObject(dbc, (DBTxnObject *)txnobj, self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | |
| 1625 | static PyObject* |
| 1626 | DB_delete(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1627 | { |
| 1628 | PyObject* txnobj = NULL; |
| 1629 | int flags = 0; |
| 1630 | PyObject* keyobj; |
| 1631 | DBT key; |
| 1632 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1633 | static char* kwnames[] = { "key", "txn", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1634 | |
| 1635 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Oi:delete", kwnames, |
| 1636 | &keyobj, &txnobj, &flags)) |
| 1637 | return NULL; |
| 1638 | CHECK_DB_NOT_CLOSED(self); |
| 1639 | if (!make_key_dbt(self, keyobj, &key, NULL)) |
| 1640 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1641 | if (!checkTxnObj(txnobj, &txn)) { |
| 1642 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1643 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1644 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1645 | |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1646 | if (-1 == _DB_delete(self, txn, &key, 0)) { |
| 1647 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1648 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1649 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1650 | |
| 1651 | FREE_DBT(key); |
| 1652 | RETURN_NONE(); |
| 1653 | } |
| 1654 | |
| 1655 | |
| 1656 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1657 | DB_fd(DBObject* self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1658 | { |
| 1659 | int err, the_fd; |
| 1660 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1661 | CHECK_DB_NOT_CLOSED(self); |
| 1662 | |
| 1663 | MYDB_BEGIN_ALLOW_THREADS; |
| 1664 | err = self->db->fd(self->db, &the_fd); |
| 1665 | MYDB_END_ALLOW_THREADS; |
| 1666 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1667 | return NUMBER_FromLong(the_fd); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | |
| 1671 | static PyObject* |
| 1672 | DB_get(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1673 | { |
| 1674 | int err, flags=0; |
| 1675 | PyObject* txnobj = NULL; |
| 1676 | PyObject* keyobj; |
| 1677 | PyObject* dfltobj = NULL; |
| 1678 | PyObject* retval = NULL; |
| 1679 | int dlen = -1; |
| 1680 | int doff = -1; |
| 1681 | DBT key, data; |
| 1682 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1683 | static char* kwnames[] = {"key", "default", "txn", "flags", "dlen", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1684 | "doff", NULL}; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1685 | |
| 1686 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOiii:get", kwnames, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1687 | &keyobj, &dfltobj, &txnobj, &flags, &dlen, |
| 1688 | &doff)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1689 | return NULL; |
| 1690 | |
| 1691 | CHECK_DB_NOT_CLOSED(self); |
| 1692 | if (!make_key_dbt(self, keyobj, &key, &flags)) |
| 1693 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1694 | if (!checkTxnObj(txnobj, &txn)) { |
| 1695 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1696 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1697 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1698 | |
| 1699 | CLEAR_DBT(data); |
| 1700 | if (CHECK_DBFLAG(self, DB_THREAD)) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1701 | /* Tell Berkeley DB to malloc the return value (thread safe) */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1702 | data.flags = DB_DBT_MALLOC; |
| 1703 | } |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1704 | if (!add_partial_dbt(&data, dlen, doff)) { |
| 1705 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1706 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1707 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1708 | |
| 1709 | MYDB_BEGIN_ALLOW_THREADS; |
| 1710 | err = self->db->get(self->db, txn, &key, &data, flags); |
| 1711 | MYDB_END_ALLOW_THREADS; |
| 1712 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 1713 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) && (dfltobj != NULL)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1714 | err = 0; |
| 1715 | Py_INCREF(dfltobj); |
| 1716 | retval = dfltobj; |
| 1717 | } |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 1718 | else if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 1719 | && self->moduleFlags.getReturnsNone) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1720 | err = 0; |
| 1721 | Py_INCREF(Py_None); |
| 1722 | retval = Py_None; |
| 1723 | } |
| 1724 | else if (!err) { |
| 1725 | if (flags & DB_SET_RECNO) /* return both key and data */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1726 | retval = BuildValue_SS(key.data, key.size, data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1727 | else /* return just the data */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1728 | retval = Build_PyString(data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1729 | FREE_DBT(data); |
| 1730 | } |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1731 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1732 | |
| 1733 | RETURN_IF_ERR(); |
| 1734 | return retval; |
| 1735 | } |
| 1736 | |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1737 | static PyObject* |
| 1738 | DB_pget(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1739 | { |
| 1740 | int err, flags=0; |
| 1741 | PyObject* txnobj = NULL; |
| 1742 | PyObject* keyobj; |
| 1743 | PyObject* dfltobj = NULL; |
| 1744 | PyObject* retval = NULL; |
| 1745 | int dlen = -1; |
| 1746 | int doff = -1; |
| 1747 | DBT key, pkey, data; |
| 1748 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1749 | static char* kwnames[] = {"key", "default", "txn", "flags", "dlen", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1750 | "doff", NULL}; |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1751 | |
| 1752 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOiii:pget", kwnames, |
| 1753 | &keyobj, &dfltobj, &txnobj, &flags, &dlen, |
| 1754 | &doff)) |
| 1755 | return NULL; |
| 1756 | |
| 1757 | CHECK_DB_NOT_CLOSED(self); |
| 1758 | if (!make_key_dbt(self, keyobj, &key, &flags)) |
| 1759 | return NULL; |
| 1760 | if (!checkTxnObj(txnobj, &txn)) { |
| 1761 | FREE_DBT(key); |
| 1762 | return NULL; |
| 1763 | } |
| 1764 | |
| 1765 | CLEAR_DBT(data); |
| 1766 | if (CHECK_DBFLAG(self, DB_THREAD)) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1767 | /* Tell Berkeley DB to malloc the return value (thread safe) */ |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1768 | data.flags = DB_DBT_MALLOC; |
| 1769 | } |
| 1770 | if (!add_partial_dbt(&data, dlen, doff)) { |
| 1771 | FREE_DBT(key); |
| 1772 | return NULL; |
| 1773 | } |
| 1774 | |
| 1775 | CLEAR_DBT(pkey); |
| 1776 | pkey.flags = DB_DBT_MALLOC; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1777 | |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1778 | MYDB_BEGIN_ALLOW_THREADS; |
| 1779 | err = self->db->pget(self->db, txn, &key, &pkey, &data, flags); |
| 1780 | MYDB_END_ALLOW_THREADS; |
| 1781 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 1782 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) && (dfltobj != NULL)) { |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1783 | err = 0; |
| 1784 | Py_INCREF(dfltobj); |
| 1785 | retval = dfltobj; |
| 1786 | } |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 1787 | else if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 1788 | && self->moduleFlags.getReturnsNone) { |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1789 | err = 0; |
| 1790 | Py_INCREF(Py_None); |
| 1791 | retval = Py_None; |
| 1792 | } |
| 1793 | else if (!err) { |
| 1794 | PyObject *pkeyObj; |
| 1795 | PyObject *dataObj; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1796 | dataObj = Build_PyString(data.data, data.size); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1797 | |
| 1798 | if (self->primaryDBType == DB_RECNO || |
| 1799 | self->primaryDBType == DB_QUEUE) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1800 | pkeyObj = NUMBER_FromLong(*(int *)pkey.data); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1801 | else |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1802 | pkeyObj = Build_PyString(pkey.data, pkey.size); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1803 | |
| 1804 | if (flags & DB_SET_RECNO) /* return key , pkey and data */ |
| 1805 | { |
| 1806 | PyObject *keyObj; |
| 1807 | int type = _DB_get_type(self); |
| 1808 | if (type == DB_RECNO || type == DB_QUEUE) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1809 | keyObj = NUMBER_FromLong(*(int *)key.data); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1810 | else |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1811 | keyObj = Build_PyString(key.data, key.size); |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 1812 | #if (PY_VERSION_HEX >= 0x02040000) |
Gregory P. Smith | 4e414d8 | 2006-01-24 19:55:02 +0000 | [diff] [blame] | 1813 | retval = PyTuple_Pack(3, keyObj, pkeyObj, dataObj); |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 1814 | #else |
| 1815 | retval = Py_BuildValue("OOO", keyObj, pkeyObj, dataObj); |
| 1816 | #endif |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 1817 | Py_DECREF(keyObj); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1818 | } |
| 1819 | else /* return just the pkey and data */ |
| 1820 | { |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 1821 | #if (PY_VERSION_HEX >= 0x02040000) |
Gregory P. Smith | 4e414d8 | 2006-01-24 19:55:02 +0000 | [diff] [blame] | 1822 | retval = PyTuple_Pack(2, pkeyObj, dataObj); |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 1823 | #else |
| 1824 | retval = Py_BuildValue("OO", pkeyObj, dataObj); |
| 1825 | #endif |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1826 | } |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 1827 | Py_DECREF(dataObj); |
| 1828 | Py_DECREF(pkeyObj); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1829 | FREE_DBT(pkey); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1830 | FREE_DBT(data); |
| 1831 | } |
| 1832 | FREE_DBT(key); |
| 1833 | |
| 1834 | RETURN_IF_ERR(); |
| 1835 | return retval; |
| 1836 | } |
| 1837 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1838 | |
| 1839 | /* Return size of entry */ |
| 1840 | static PyObject* |
| 1841 | DB_get_size(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1842 | { |
| 1843 | int err, flags=0; |
| 1844 | PyObject* txnobj = NULL; |
| 1845 | PyObject* keyobj; |
| 1846 | PyObject* retval = NULL; |
| 1847 | DBT key, data; |
| 1848 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1849 | static char* kwnames[] = { "key", "txn", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1850 | |
| 1851 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:get_size", kwnames, |
| 1852 | &keyobj, &txnobj)) |
| 1853 | return NULL; |
| 1854 | CHECK_DB_NOT_CLOSED(self); |
| 1855 | if (!make_key_dbt(self, keyobj, &key, &flags)) |
| 1856 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1857 | if (!checkTxnObj(txnobj, &txn)) { |
| 1858 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1859 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1860 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1861 | CLEAR_DBT(data); |
| 1862 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 1863 | /* We don't allocate any memory, forcing a DB_BUFFER_SMALL error and |
| 1864 | thus getting the record size. */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1865 | data.flags = DB_DBT_USERMEM; |
| 1866 | data.ulen = 0; |
| 1867 | MYDB_BEGIN_ALLOW_THREADS; |
| 1868 | err = self->db->get(self->db, txn, &key, &data, flags); |
| 1869 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 1870 | if (err == DB_BUFFER_SMALL) { |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1871 | retval = NUMBER_FromLong((long)data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1872 | err = 0; |
| 1873 | } |
| 1874 | |
| 1875 | FREE_DBT(key); |
| 1876 | FREE_DBT(data); |
| 1877 | RETURN_IF_ERR(); |
| 1878 | return retval; |
| 1879 | } |
| 1880 | |
| 1881 | |
| 1882 | static PyObject* |
| 1883 | DB_get_both(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1884 | { |
| 1885 | int err, flags=0; |
| 1886 | PyObject* txnobj = NULL; |
| 1887 | PyObject* keyobj; |
| 1888 | PyObject* dataobj; |
| 1889 | PyObject* retval = NULL; |
| 1890 | DBT key, data; |
Neal Norwitz | 994ebed | 2007-06-03 20:32:50 +0000 | [diff] [blame] | 1891 | void *orig_data; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1892 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1893 | static char* kwnames[] = { "key", "data", "txn", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1894 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1895 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|Oi:get_both", kwnames, |
| 1896 | &keyobj, &dataobj, &txnobj, &flags)) |
| 1897 | return NULL; |
| 1898 | |
| 1899 | CHECK_DB_NOT_CLOSED(self); |
| 1900 | if (!make_key_dbt(self, keyobj, &key, NULL)) |
| 1901 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1902 | if ( !make_dbt(dataobj, &data) || |
| 1903 | !checkTxnObj(txnobj, &txn) ) |
| 1904 | { |
| 1905 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1906 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1907 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1908 | |
| 1909 | flags |= DB_GET_BOTH; |
Neal Norwitz | 994ebed | 2007-06-03 20:32:50 +0000 | [diff] [blame] | 1910 | orig_data = data.data; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1911 | |
| 1912 | if (CHECK_DBFLAG(self, DB_THREAD)) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1913 | /* Tell Berkeley DB to malloc the return value (thread safe) */ |
Neal Norwitz | 994ebed | 2007-06-03 20:32:50 +0000 | [diff] [blame] | 1914 | /* XXX(nnorwitz): At least 4.4.20 and 4.5.20 require this flag. */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1915 | data.flags = DB_DBT_MALLOC; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | MYDB_BEGIN_ALLOW_THREADS; |
| 1919 | err = self->db->get(self->db, txn, &key, &data, flags); |
| 1920 | MYDB_END_ALLOW_THREADS; |
| 1921 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 1922 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 1923 | && self->moduleFlags.getReturnsNone) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1924 | err = 0; |
| 1925 | Py_INCREF(Py_None); |
| 1926 | retval = Py_None; |
| 1927 | } |
| 1928 | else if (!err) { |
Neal Norwitz | 994ebed | 2007-06-03 20:32:50 +0000 | [diff] [blame] | 1929 | /* XXX(nnorwitz): can we do: retval = dataobj; Py_INCREF(retval); */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1930 | retval = Build_PyString(data.data, data.size); |
Neal Norwitz | 994ebed | 2007-06-03 20:32:50 +0000 | [diff] [blame] | 1931 | |
| 1932 | /* Even though the flags require DB_DBT_MALLOC, data is not always |
| 1933 | allocated. 4.4: allocated, 4.5: *not* allocated. :-( */ |
| 1934 | if (data.data != orig_data) |
| 1935 | FREE_DBT(data); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | FREE_DBT(key); |
| 1939 | RETURN_IF_ERR(); |
| 1940 | return retval; |
| 1941 | } |
| 1942 | |
| 1943 | |
| 1944 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1945 | DB_get_byteswapped(DBObject* self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1946 | { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1947 | int err = 0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1948 | int retval = -1; |
| 1949 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1950 | CHECK_DB_NOT_CLOSED(self); |
| 1951 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1952 | MYDB_BEGIN_ALLOW_THREADS; |
| 1953 | err = self->db->get_byteswapped(self->db, &retval); |
| 1954 | MYDB_END_ALLOW_THREADS; |
| 1955 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1956 | return NUMBER_FromLong(retval); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
| 1959 | |
| 1960 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1961 | DB_get_type(DBObject* self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1962 | { |
| 1963 | int type; |
| 1964 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1965 | CHECK_DB_NOT_CLOSED(self); |
| 1966 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1967 | type = _DB_get_type(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1968 | if (type == -1) |
| 1969 | return NULL; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1970 | return NUMBER_FromLong(type); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | |
| 1974 | static PyObject* |
| 1975 | DB_join(DBObject* self, PyObject* args) |
| 1976 | { |
| 1977 | int err, flags=0; |
| 1978 | int length, x; |
| 1979 | PyObject* cursorsObj; |
| 1980 | DBC** cursors; |
| 1981 | DBC* dbc; |
| 1982 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1983 | if (!PyArg_ParseTuple(args,"O|i:join", &cursorsObj, &flags)) |
| 1984 | return NULL; |
| 1985 | |
| 1986 | CHECK_DB_NOT_CLOSED(self); |
| 1987 | |
| 1988 | if (!PySequence_Check(cursorsObj)) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1989 | PyErr_SetString(PyExc_TypeError, |
| 1990 | "Sequence of DBCursor objects expected"); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1991 | return NULL; |
| 1992 | } |
| 1993 | |
| 1994 | length = PyObject_Length(cursorsObj); |
| 1995 | cursors = malloc((length+1) * sizeof(DBC*)); |
Neal Norwitz | 5aa9689 | 2006-08-13 18:11:43 +0000 | [diff] [blame] | 1996 | if (!cursors) { |
| 1997 | PyErr_NoMemory(); |
| 1998 | return NULL; |
| 1999 | } |
| 2000 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2001 | cursors[length] = NULL; |
| 2002 | for (x=0; x<length; x++) { |
| 2003 | PyObject* item = PySequence_GetItem(cursorsObj, x); |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2004 | if (item == NULL) { |
| 2005 | free(cursors); |
| 2006 | return NULL; |
| 2007 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2008 | if (!DBCursorObject_Check(item)) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2009 | PyErr_SetString(PyExc_TypeError, |
| 2010 | "Sequence of DBCursor objects expected"); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2011 | free(cursors); |
| 2012 | return NULL; |
| 2013 | } |
| 2014 | cursors[x] = ((DBCursorObject*)item)->dbc; |
Thomas Wouters | b2820ae | 2006-03-12 00:01:38 +0000 | [diff] [blame] | 2015 | Py_DECREF(item); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | MYDB_BEGIN_ALLOW_THREADS; |
| 2019 | err = self->db->join(self->db, cursors, &dbc, flags); |
| 2020 | MYDB_END_ALLOW_THREADS; |
| 2021 | free(cursors); |
| 2022 | RETURN_IF_ERR(); |
| 2023 | |
Gregory P. Smith | 7441e65 | 2003-11-03 21:35:31 +0000 | [diff] [blame] | 2024 | /* FIXME: this is a buggy interface. The returned cursor |
| 2025 | contains internal references to the passed in cursors |
| 2026 | but does not hold python references to them or prevent |
| 2027 | them from being closed prematurely. This can cause |
| 2028 | python to crash when things are done in the wrong order. */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2029 | return (PyObject*) newDBCursorObject(dbc, NULL, self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | |
| 2033 | static PyObject* |
| 2034 | DB_key_range(DBObject* self, PyObject* args, PyObject* kwargs) |
| 2035 | { |
| 2036 | int err, flags=0; |
| 2037 | PyObject* txnobj = NULL; |
| 2038 | PyObject* keyobj; |
| 2039 | DBT key; |
| 2040 | DB_TXN *txn = NULL; |
| 2041 | DB_KEY_RANGE range; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2042 | static char* kwnames[] = { "key", "txn", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2043 | |
| 2044 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Oi:key_range", kwnames, |
| 2045 | &keyobj, &txnobj, &flags)) |
| 2046 | return NULL; |
| 2047 | CHECK_DB_NOT_CLOSED(self); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2048 | if (!make_dbt(keyobj, &key)) |
| 2049 | /* BTree only, don't need to allow for an int key */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2050 | return NULL; |
| 2051 | if (!checkTxnObj(txnobj, &txn)) |
| 2052 | return NULL; |
| 2053 | |
| 2054 | MYDB_BEGIN_ALLOW_THREADS; |
| 2055 | err = self->db->key_range(self->db, txn, &key, &range, flags); |
| 2056 | MYDB_END_ALLOW_THREADS; |
| 2057 | |
| 2058 | RETURN_IF_ERR(); |
| 2059 | return Py_BuildValue("ddd", range.less, range.equal, range.greater); |
| 2060 | } |
| 2061 | |
| 2062 | |
| 2063 | static PyObject* |
| 2064 | DB_open(DBObject* self, PyObject* args, PyObject* kwargs) |
| 2065 | { |
| 2066 | int err, type = DB_UNKNOWN, flags=0, mode=0660; |
| 2067 | char* filename = NULL; |
| 2068 | char* dbname = NULL; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2069 | #if (DBVER >= 41) |
| 2070 | PyObject *txnobj = NULL; |
| 2071 | DB_TXN *txn = NULL; |
| 2072 | /* with dbname */ |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2073 | static char* kwnames[] = { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2074 | "filename", "dbname", "dbtype", "flags", "mode", "txn", NULL}; |
| 2075 | /* without dbname */ |
Tim Peters | 85b1052 | 2006-02-28 18:33:35 +0000 | [diff] [blame] | 2076 | static char* kwnames_basic[] = { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2077 | "filename", "dbtype", "flags", "mode", "txn", NULL}; |
| 2078 | #else |
| 2079 | /* with dbname */ |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2080 | static char* kwnames[] = { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2081 | "filename", "dbname", "dbtype", "flags", "mode", NULL}; |
| 2082 | /* without dbname */ |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2083 | static char* kwnames_basic[] = { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2084 | "filename", "dbtype", "flags", "mode", NULL}; |
| 2085 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2086 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2087 | #if (DBVER >= 41) |
| 2088 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "z|ziiiO:open", kwnames, |
| 2089 | &filename, &dbname, &type, &flags, &mode, |
| 2090 | &txnobj)) |
| 2091 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2092 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "z|ziii:open", kwnames, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2093 | &filename, &dbname, &type, &flags, |
| 2094 | &mode)) |
| 2095 | #endif |
| 2096 | { |
| 2097 | PyErr_Clear(); |
| 2098 | type = DB_UNKNOWN; flags = 0; mode = 0660; |
| 2099 | filename = NULL; dbname = NULL; |
| 2100 | #if (DBVER >= 41) |
| 2101 | if (!PyArg_ParseTupleAndKeywords(args, kwargs,"z|iiiO:open", |
| 2102 | kwnames_basic, |
| 2103 | &filename, &type, &flags, &mode, |
| 2104 | &txnobj)) |
| 2105 | return NULL; |
| 2106 | #else |
| 2107 | if (!PyArg_ParseTupleAndKeywords(args, kwargs,"z|iii:open", |
| 2108 | kwnames_basic, |
| 2109 | &filename, &type, &flags, &mode)) |
| 2110 | return NULL; |
| 2111 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2114 | #if (DBVER >= 41) |
| 2115 | if (!checkTxnObj(txnobj, &txn)) return NULL; |
| 2116 | #endif |
| 2117 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2118 | if (NULL == self->db) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2119 | PyObject *t = Py_BuildValue("(is)", 0, |
| 2120 | "Cannot call open() twice for DB object"); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2121 | if (t) { |
| 2122 | PyErr_SetObject(DBError, t); |
| 2123 | Py_DECREF(t); |
| 2124 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2125 | return NULL; |
| 2126 | } |
| 2127 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2128 | #if (DBVER >= 41) |
| 2129 | if (txn) { /* Can't use 'txnobj' because could be 'txnobj==Py_None' */ |
| 2130 | INSERT_IN_DOUBLE_LINKED_LIST_TXN(((DBTxnObject *)txnobj)->children_dbs,self); |
| 2131 | self->txn=(DBTxnObject *)txnobj; |
| 2132 | } else { |
| 2133 | self->txn=NULL; |
| 2134 | } |
| 2135 | #else |
| 2136 | self->txn=NULL; |
| 2137 | #endif |
| 2138 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2139 | MYDB_BEGIN_ALLOW_THREADS; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2140 | #if (DBVER >= 41) |
| 2141 | err = self->db->open(self->db, txn, filename, dbname, type, flags, mode); |
| 2142 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2143 | err = self->db->open(self->db, filename, dbname, type, flags, mode); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2144 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2145 | MYDB_END_ALLOW_THREADS; |
| 2146 | if (makeDBError(err)) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2147 | PyObject *dummy; |
| 2148 | |
| 2149 | dummy=DB_close_internal(self,0); |
| 2150 | Py_XDECREF(dummy); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2151 | return NULL; |
| 2152 | } |
| 2153 | |
Gregory P. Smith | fc00669 | 2007-11-05 09:06:28 +0000 | [diff] [blame] | 2154 | #if (DBVER >= 42) |
Gregory P. Smith | ec10a4a | 2007-11-05 02:32:26 +0000 | [diff] [blame] | 2155 | self->db->get_flags(self->db, &self->setflags); |
Gregory P. Smith | fc00669 | 2007-11-05 09:06:28 +0000 | [diff] [blame] | 2156 | #endif |
Gregory P. Smith | ec10a4a | 2007-11-05 02:32:26 +0000 | [diff] [blame] | 2157 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2158 | self->flags = flags; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2159 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2160 | RETURN_NONE(); |
| 2161 | } |
| 2162 | |
| 2163 | |
| 2164 | static PyObject* |
| 2165 | DB_put(DBObject* self, PyObject* args, PyObject* kwargs) |
| 2166 | { |
| 2167 | int flags=0; |
| 2168 | PyObject* txnobj = NULL; |
| 2169 | int dlen = -1; |
| 2170 | int doff = -1; |
| 2171 | PyObject* keyobj, *dataobj, *retval; |
| 2172 | DBT key, data; |
| 2173 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2174 | static char* kwnames[] = { "key", "data", "txn", "flags", "dlen", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 2175 | "doff", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2176 | |
| 2177 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|Oiii:put", kwnames, |
| 2178 | &keyobj, &dataobj, &txnobj, &flags, &dlen, &doff)) |
| 2179 | return NULL; |
| 2180 | |
| 2181 | CHECK_DB_NOT_CLOSED(self); |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 2182 | if (!make_key_dbt(self, keyobj, &key, NULL)) |
| 2183 | return NULL; |
| 2184 | if ( !make_dbt(dataobj, &data) || |
| 2185 | !add_partial_dbt(&data, dlen, doff) || |
| 2186 | !checkTxnObj(txnobj, &txn) ) |
| 2187 | { |
| 2188 | FREE_DBT(key); |
| 2189 | return NULL; |
| 2190 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2191 | |
| 2192 | if (-1 == _DB_put(self, txn, &key, &data, flags)) { |
| 2193 | FREE_DBT(key); |
| 2194 | return NULL; |
| 2195 | } |
| 2196 | |
| 2197 | if (flags & DB_APPEND) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2198 | retval = NUMBER_FromLong(*((db_recno_t*)key.data)); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2199 | else { |
| 2200 | retval = Py_None; |
| 2201 | Py_INCREF(retval); |
| 2202 | } |
| 2203 | FREE_DBT(key); |
| 2204 | return retval; |
| 2205 | } |
| 2206 | |
| 2207 | |
| 2208 | |
| 2209 | static PyObject* |
| 2210 | DB_remove(DBObject* self, PyObject* args, PyObject* kwargs) |
| 2211 | { |
| 2212 | char* filename; |
| 2213 | char* database = NULL; |
| 2214 | int err, flags=0; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2215 | static char* kwnames[] = { "filename", "dbname", "flags", NULL}; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2216 | |
| 2217 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|zi:remove", kwnames, |
| 2218 | &filename, &database, &flags)) |
| 2219 | return NULL; |
| 2220 | CHECK_DB_NOT_CLOSED(self); |
| 2221 | |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 2222 | EXTRACT_FROM_DOUBLE_LINKED_LIST_MAYBE_NULL(self); |
| 2223 | |
| 2224 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2225 | err = self->db->remove(self->db, filename, database, flags); |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 2226 | MYDB_END_ALLOW_THREADS; |
| 2227 | |
Gregory P. Smith | f655dff | 2003-05-15 00:13:18 +0000 | [diff] [blame] | 2228 | self->db = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2229 | RETURN_IF_ERR(); |
| 2230 | RETURN_NONE(); |
| 2231 | } |
| 2232 | |
| 2233 | |
| 2234 | |
| 2235 | static PyObject* |
| 2236 | DB_rename(DBObject* self, PyObject* args) |
| 2237 | { |
| 2238 | char* filename; |
| 2239 | char* database; |
| 2240 | char* newname; |
| 2241 | int err, flags=0; |
| 2242 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2243 | if (!PyArg_ParseTuple(args, "sss|i:rename", &filename, &database, &newname, |
| 2244 | &flags)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2245 | return NULL; |
| 2246 | CHECK_DB_NOT_CLOSED(self); |
| 2247 | |
| 2248 | MYDB_BEGIN_ALLOW_THREADS; |
| 2249 | err = self->db->rename(self->db, filename, database, newname, flags); |
| 2250 | MYDB_END_ALLOW_THREADS; |
| 2251 | RETURN_IF_ERR(); |
| 2252 | RETURN_NONE(); |
| 2253 | } |
| 2254 | |
| 2255 | |
| 2256 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2257 | DB_get_private(DBObject* self) |
| 2258 | { |
| 2259 | /* We can give out the private field even if db is closed */ |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 2260 | Py_INCREF(self->private_obj); |
| 2261 | return self->private_obj; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | static PyObject* |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 2265 | DB_set_private(DBObject* self, PyObject* private_obj) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2266 | { |
| 2267 | /* We can set the private field even if db is closed */ |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 2268 | Py_DECREF(self->private_obj); |
| 2269 | Py_INCREF(private_obj); |
| 2270 | self->private_obj = private_obj; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2271 | RETURN_NONE(); |
| 2272 | } |
| 2273 | |
| 2274 | |
| 2275 | static PyObject* |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2276 | DB_set_bt_minkey(DBObject* self, PyObject* args) |
| 2277 | { |
| 2278 | int err, minkey; |
| 2279 | |
| 2280 | if (!PyArg_ParseTuple(args,"i:set_bt_minkey", &minkey )) |
| 2281 | return NULL; |
| 2282 | CHECK_DB_NOT_CLOSED(self); |
| 2283 | |
| 2284 | MYDB_BEGIN_ALLOW_THREADS; |
| 2285 | err = self->db->set_bt_minkey(self->db, minkey); |
| 2286 | MYDB_END_ALLOW_THREADS; |
| 2287 | RETURN_IF_ERR(); |
| 2288 | RETURN_NONE(); |
| 2289 | } |
| 2290 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2291 | static int |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2292 | _default_cmp(const DBT *leftKey, |
| 2293 | const DBT *rightKey) |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2294 | { |
| 2295 | int res; |
| 2296 | int lsize = leftKey->size, rsize = rightKey->size; |
| 2297 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2298 | res = memcmp(leftKey->data, rightKey->data, |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2299 | lsize < rsize ? lsize : rsize); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2300 | |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2301 | if (res == 0) { |
| 2302 | if (lsize < rsize) { |
| 2303 | res = -1; |
| 2304 | } |
| 2305 | else if (lsize > rsize) { |
| 2306 | res = 1; |
| 2307 | } |
| 2308 | } |
| 2309 | return res; |
| 2310 | } |
| 2311 | |
| 2312 | static int |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2313 | _db_compareCallback(DB* db, |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2314 | const DBT *leftKey, |
| 2315 | const DBT *rightKey) |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2316 | { |
| 2317 | int res = 0; |
| 2318 | PyObject *args; |
Thomas Wouters | b2820ae | 2006-03-12 00:01:38 +0000 | [diff] [blame] | 2319 | PyObject *result = NULL; |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2320 | DBObject *self = (DBObject *)db->app_private; |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2321 | |
| 2322 | if (self == NULL || self->btCompareCallback == NULL) { |
| 2323 | MYDB_BEGIN_BLOCK_THREADS; |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2324 | PyErr_SetString(PyExc_TypeError, |
| 2325 | (self == 0 |
| 2326 | ? "DB_bt_compare db is NULL." |
| 2327 | : "DB_bt_compare callback is NULL.")); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2328 | /* we're in a callback within the DB code, we can't raise */ |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2329 | PyErr_Print(); |
| 2330 | res = _default_cmp(leftKey, rightKey); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2331 | MYDB_END_BLOCK_THREADS; |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2332 | } else { |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2333 | MYDB_BEGIN_BLOCK_THREADS; |
| 2334 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2335 | args = BuildValue_SS(leftKey->data, leftKey->size, rightKey->data, rightKey->size); |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2336 | if (args != NULL) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2337 | /* XXX(twouters) I highly doubt this INCREF is correct */ |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2338 | Py_INCREF(self); |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2339 | result = PyEval_CallObject(self->btCompareCallback, args); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2340 | } |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2341 | if (args == NULL || result == NULL) { |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2342 | /* we're in a callback within the DB code, we can't raise */ |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2343 | PyErr_Print(); |
| 2344 | res = _default_cmp(leftKey, rightKey); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2345 | } else if (NUMBER_Check(result)) { |
| 2346 | res = NUMBER_AsLong(result); |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2347 | } else { |
| 2348 | PyErr_SetString(PyExc_TypeError, |
| 2349 | "DB_bt_compare callback MUST return an int."); |
| 2350 | /* we're in a callback within the DB code, we can't raise */ |
| 2351 | PyErr_Print(); |
| 2352 | res = _default_cmp(leftKey, rightKey); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2353 | } |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2354 | |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2355 | Py_XDECREF(args); |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2356 | Py_XDECREF(result); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2357 | |
| 2358 | MYDB_END_BLOCK_THREADS; |
| 2359 | } |
| 2360 | return res; |
| 2361 | } |
| 2362 | |
| 2363 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2364 | DB_set_bt_compare(DBObject* self, PyObject* comparator) |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2365 | { |
| 2366 | int err; |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2367 | PyObject *tuple, *result; |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2368 | |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2369 | CHECK_DB_NOT_CLOSED(self); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2370 | |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2371 | if (!PyCallable_Check(comparator)) { |
| 2372 | makeTypeError("Callable", comparator); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2373 | return NULL; |
| 2374 | } |
| 2375 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2376 | /* |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2377 | * Perform a test call of the comparator function with two empty |
| 2378 | * string objects here. verify that it returns an int (0). |
| 2379 | * err if not. |
| 2380 | */ |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2381 | tuple = Py_BuildValue("(ss)", "", ""); |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2382 | result = PyEval_CallObject(comparator, tuple); |
| 2383 | Py_DECREF(tuple); |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2384 | if (result == NULL) |
| 2385 | return NULL; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2386 | if (!NUMBER_Check(result)) { |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2387 | PyErr_SetString(PyExc_TypeError, |
| 2388 | "callback MUST return an int"); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2389 | return NULL; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2390 | } else if (NUMBER_AsLong(result) != 0) { |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2391 | PyErr_SetString(PyExc_TypeError, |
| 2392 | "callback failed to return 0 on two empty strings"); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2393 | return NULL; |
| 2394 | } |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2395 | Py_DECREF(result); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2396 | |
| 2397 | /* We don't accept multiple set_bt_compare operations, in order to |
| 2398 | * simplify the code. This would have no real use, as one cannot |
| 2399 | * change the function once the db is opened anyway */ |
| 2400 | if (self->btCompareCallback != NULL) { |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2401 | PyErr_SetString(PyExc_RuntimeError, "set_bt_compare() cannot be called more than once"); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2402 | return NULL; |
| 2403 | } |
| 2404 | |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2405 | Py_INCREF(comparator); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2406 | self->btCompareCallback = comparator; |
| 2407 | |
| 2408 | /* This is to workaround a problem with un-initialized threads (see |
| 2409 | comment in DB_associate) */ |
| 2410 | #ifdef WITH_THREAD |
| 2411 | PyEval_InitThreads(); |
| 2412 | #endif |
| 2413 | |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2414 | err = self->db->set_bt_compare(self->db, _db_compareCallback); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2415 | |
| 2416 | if (err) { |
| 2417 | /* restore the old state in case of error */ |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2418 | Py_DECREF(comparator); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2419 | self->btCompareCallback = NULL; |
| 2420 | } |
| 2421 | |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2422 | RETURN_IF_ERR(); |
| 2423 | RETURN_NONE(); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2426 | |
| 2427 | static PyObject* |
| 2428 | DB_set_cachesize(DBObject* self, PyObject* args) |
| 2429 | { |
| 2430 | int err; |
| 2431 | int gbytes = 0, bytes = 0, ncache = 0; |
| 2432 | |
| 2433 | if (!PyArg_ParseTuple(args,"ii|i:set_cachesize", |
| 2434 | &gbytes,&bytes,&ncache)) |
| 2435 | return NULL; |
| 2436 | CHECK_DB_NOT_CLOSED(self); |
| 2437 | |
| 2438 | MYDB_BEGIN_ALLOW_THREADS; |
| 2439 | err = self->db->set_cachesize(self->db, gbytes, bytes, ncache); |
| 2440 | MYDB_END_ALLOW_THREADS; |
| 2441 | RETURN_IF_ERR(); |
| 2442 | RETURN_NONE(); |
| 2443 | } |
| 2444 | |
| 2445 | |
| 2446 | static PyObject* |
| 2447 | DB_set_flags(DBObject* self, PyObject* args) |
| 2448 | { |
| 2449 | int err, flags; |
| 2450 | |
| 2451 | if (!PyArg_ParseTuple(args,"i:set_flags", &flags)) |
| 2452 | return NULL; |
| 2453 | CHECK_DB_NOT_CLOSED(self); |
| 2454 | |
| 2455 | MYDB_BEGIN_ALLOW_THREADS; |
| 2456 | err = self->db->set_flags(self->db, flags); |
| 2457 | MYDB_END_ALLOW_THREADS; |
| 2458 | RETURN_IF_ERR(); |
| 2459 | |
| 2460 | self->setflags |= flags; |
| 2461 | RETURN_NONE(); |
| 2462 | } |
| 2463 | |
| 2464 | |
| 2465 | static PyObject* |
| 2466 | DB_set_h_ffactor(DBObject* self, PyObject* args) |
| 2467 | { |
| 2468 | int err, ffactor; |
| 2469 | |
| 2470 | if (!PyArg_ParseTuple(args,"i:set_h_ffactor", &ffactor)) |
| 2471 | return NULL; |
| 2472 | CHECK_DB_NOT_CLOSED(self); |
| 2473 | |
| 2474 | MYDB_BEGIN_ALLOW_THREADS; |
| 2475 | err = self->db->set_h_ffactor(self->db, ffactor); |
| 2476 | MYDB_END_ALLOW_THREADS; |
| 2477 | RETURN_IF_ERR(); |
| 2478 | RETURN_NONE(); |
| 2479 | } |
| 2480 | |
| 2481 | |
| 2482 | static PyObject* |
| 2483 | DB_set_h_nelem(DBObject* self, PyObject* args) |
| 2484 | { |
| 2485 | int err, nelem; |
| 2486 | |
| 2487 | if (!PyArg_ParseTuple(args,"i:set_h_nelem", &nelem)) |
| 2488 | return NULL; |
| 2489 | CHECK_DB_NOT_CLOSED(self); |
| 2490 | |
| 2491 | MYDB_BEGIN_ALLOW_THREADS; |
| 2492 | err = self->db->set_h_nelem(self->db, nelem); |
| 2493 | MYDB_END_ALLOW_THREADS; |
| 2494 | RETURN_IF_ERR(); |
| 2495 | RETURN_NONE(); |
| 2496 | } |
| 2497 | |
| 2498 | |
| 2499 | static PyObject* |
| 2500 | DB_set_lorder(DBObject* self, PyObject* args) |
| 2501 | { |
| 2502 | int err, lorder; |
| 2503 | |
| 2504 | if (!PyArg_ParseTuple(args,"i:set_lorder", &lorder)) |
| 2505 | return NULL; |
| 2506 | CHECK_DB_NOT_CLOSED(self); |
| 2507 | |
| 2508 | MYDB_BEGIN_ALLOW_THREADS; |
| 2509 | err = self->db->set_lorder(self->db, lorder); |
| 2510 | MYDB_END_ALLOW_THREADS; |
| 2511 | RETURN_IF_ERR(); |
| 2512 | RETURN_NONE(); |
| 2513 | } |
| 2514 | |
| 2515 | |
| 2516 | static PyObject* |
| 2517 | DB_set_pagesize(DBObject* self, PyObject* args) |
| 2518 | { |
| 2519 | int err, pagesize; |
| 2520 | |
| 2521 | if (!PyArg_ParseTuple(args,"i:set_pagesize", &pagesize)) |
| 2522 | return NULL; |
| 2523 | CHECK_DB_NOT_CLOSED(self); |
| 2524 | |
| 2525 | MYDB_BEGIN_ALLOW_THREADS; |
| 2526 | err = self->db->set_pagesize(self->db, pagesize); |
| 2527 | MYDB_END_ALLOW_THREADS; |
| 2528 | RETURN_IF_ERR(); |
| 2529 | RETURN_NONE(); |
| 2530 | } |
| 2531 | |
| 2532 | |
| 2533 | static PyObject* |
| 2534 | DB_set_re_delim(DBObject* self, PyObject* args) |
| 2535 | { |
| 2536 | int err; |
| 2537 | char delim; |
| 2538 | |
| 2539 | if (!PyArg_ParseTuple(args,"b:set_re_delim", &delim)) { |
| 2540 | PyErr_Clear(); |
| 2541 | if (!PyArg_ParseTuple(args,"c:set_re_delim", &delim)) |
| 2542 | return NULL; |
| 2543 | } |
| 2544 | |
| 2545 | CHECK_DB_NOT_CLOSED(self); |
| 2546 | |
| 2547 | MYDB_BEGIN_ALLOW_THREADS; |
| 2548 | err = self->db->set_re_delim(self->db, delim); |
| 2549 | MYDB_END_ALLOW_THREADS; |
| 2550 | RETURN_IF_ERR(); |
| 2551 | RETURN_NONE(); |
| 2552 | } |
| 2553 | |
| 2554 | static PyObject* |
| 2555 | DB_set_re_len(DBObject* self, PyObject* args) |
| 2556 | { |
| 2557 | int err, len; |
| 2558 | |
| 2559 | if (!PyArg_ParseTuple(args,"i:set_re_len", &len)) |
| 2560 | return NULL; |
| 2561 | CHECK_DB_NOT_CLOSED(self); |
| 2562 | |
| 2563 | MYDB_BEGIN_ALLOW_THREADS; |
| 2564 | err = self->db->set_re_len(self->db, len); |
| 2565 | MYDB_END_ALLOW_THREADS; |
| 2566 | RETURN_IF_ERR(); |
| 2567 | RETURN_NONE(); |
| 2568 | } |
| 2569 | |
| 2570 | |
| 2571 | static PyObject* |
| 2572 | DB_set_re_pad(DBObject* self, PyObject* args) |
| 2573 | { |
| 2574 | int err; |
| 2575 | char pad; |
| 2576 | |
| 2577 | if (!PyArg_ParseTuple(args,"b:set_re_pad", &pad)) { |
| 2578 | PyErr_Clear(); |
| 2579 | if (!PyArg_ParseTuple(args,"c:set_re_pad", &pad)) |
| 2580 | return NULL; |
| 2581 | } |
| 2582 | CHECK_DB_NOT_CLOSED(self); |
| 2583 | |
| 2584 | MYDB_BEGIN_ALLOW_THREADS; |
| 2585 | err = self->db->set_re_pad(self->db, pad); |
| 2586 | MYDB_END_ALLOW_THREADS; |
| 2587 | RETURN_IF_ERR(); |
| 2588 | RETURN_NONE(); |
| 2589 | } |
| 2590 | |
| 2591 | |
| 2592 | static PyObject* |
| 2593 | DB_set_re_source(DBObject* self, PyObject* args) |
| 2594 | { |
| 2595 | int err; |
| 2596 | char *re_source; |
| 2597 | |
| 2598 | if (!PyArg_ParseTuple(args,"s:set_re_source", &re_source)) |
| 2599 | return NULL; |
| 2600 | CHECK_DB_NOT_CLOSED(self); |
| 2601 | |
| 2602 | MYDB_BEGIN_ALLOW_THREADS; |
| 2603 | err = self->db->set_re_source(self->db, re_source); |
| 2604 | MYDB_END_ALLOW_THREADS; |
| 2605 | RETURN_IF_ERR(); |
| 2606 | RETURN_NONE(); |
| 2607 | } |
| 2608 | |
| 2609 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2610 | static PyObject* |
| 2611 | DB_set_q_extentsize(DBObject* self, PyObject* args) |
| 2612 | { |
| 2613 | int err; |
| 2614 | int extentsize; |
| 2615 | |
| 2616 | if (!PyArg_ParseTuple(args,"i:set_q_extentsize", &extentsize)) |
| 2617 | return NULL; |
| 2618 | CHECK_DB_NOT_CLOSED(self); |
| 2619 | |
| 2620 | MYDB_BEGIN_ALLOW_THREADS; |
| 2621 | err = self->db->set_q_extentsize(self->db, extentsize); |
| 2622 | MYDB_END_ALLOW_THREADS; |
| 2623 | RETURN_IF_ERR(); |
| 2624 | RETURN_NONE(); |
| 2625 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2626 | |
| 2627 | static PyObject* |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2628 | DB_stat(DBObject* self, PyObject* args, PyObject* kwargs) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2629 | { |
| 2630 | int err, flags = 0, type; |
| 2631 | void* sp; |
| 2632 | PyObject* d; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2633 | #if (DBVER >= 43) |
| 2634 | PyObject* txnobj = NULL; |
| 2635 | DB_TXN *txn = NULL; |
Gregory P. Smith | 2fa0679 | 2006-09-19 17:35:04 +0000 | [diff] [blame] | 2636 | static char* kwnames[] = { "flags", "txn", NULL }; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2637 | #else |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2638 | static char* kwnames[] = { "flags", NULL }; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2639 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2640 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2641 | #if (DBVER >= 43) |
| 2642 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iO:stat", kwnames, |
| 2643 | &flags, &txnobj)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2644 | return NULL; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2645 | if (!checkTxnObj(txnobj, &txn)) |
| 2646 | return NULL; |
| 2647 | #else |
| 2648 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:stat", kwnames, &flags)) |
| 2649 | return NULL; |
| 2650 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2651 | CHECK_DB_NOT_CLOSED(self); |
| 2652 | |
| 2653 | MYDB_BEGIN_ALLOW_THREADS; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2654 | #if (DBVER >= 43) |
| 2655 | err = self->db->stat(self->db, txn, &sp, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2656 | #else |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2657 | err = self->db->stat(self->db, &sp, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2658 | #endif |
| 2659 | MYDB_END_ALLOW_THREADS; |
| 2660 | RETURN_IF_ERR(); |
| 2661 | |
| 2662 | self->haveStat = 1; |
| 2663 | |
| 2664 | /* Turn the stat structure into a dictionary */ |
| 2665 | type = _DB_get_type(self); |
| 2666 | if ((type == -1) || ((d = PyDict_New()) == NULL)) { |
| 2667 | free(sp); |
| 2668 | return NULL; |
| 2669 | } |
| 2670 | |
| 2671 | #define MAKE_HASH_ENTRY(name) _addIntToDict(d, #name, ((DB_HASH_STAT*)sp)->hash_##name) |
| 2672 | #define MAKE_BT_ENTRY(name) _addIntToDict(d, #name, ((DB_BTREE_STAT*)sp)->bt_##name) |
| 2673 | #define MAKE_QUEUE_ENTRY(name) _addIntToDict(d, #name, ((DB_QUEUE_STAT*)sp)->qs_##name) |
| 2674 | |
| 2675 | switch (type) { |
| 2676 | case DB_HASH: |
| 2677 | MAKE_HASH_ENTRY(magic); |
| 2678 | MAKE_HASH_ENTRY(version); |
| 2679 | MAKE_HASH_ENTRY(nkeys); |
| 2680 | MAKE_HASH_ENTRY(ndata); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2681 | #if (DBVER >= 46) |
| 2682 | MAKE_HASH_ENTRY(pagecnt); |
| 2683 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2684 | MAKE_HASH_ENTRY(pagesize); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2685 | #if (DBVER < 41) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2686 | MAKE_HASH_ENTRY(nelem); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2687 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2688 | MAKE_HASH_ENTRY(ffactor); |
| 2689 | MAKE_HASH_ENTRY(buckets); |
| 2690 | MAKE_HASH_ENTRY(free); |
| 2691 | MAKE_HASH_ENTRY(bfree); |
| 2692 | MAKE_HASH_ENTRY(bigpages); |
| 2693 | MAKE_HASH_ENTRY(big_bfree); |
| 2694 | MAKE_HASH_ENTRY(overflows); |
| 2695 | MAKE_HASH_ENTRY(ovfl_free); |
| 2696 | MAKE_HASH_ENTRY(dup); |
| 2697 | MAKE_HASH_ENTRY(dup_free); |
| 2698 | break; |
| 2699 | |
| 2700 | case DB_BTREE: |
| 2701 | case DB_RECNO: |
| 2702 | MAKE_BT_ENTRY(magic); |
| 2703 | MAKE_BT_ENTRY(version); |
| 2704 | MAKE_BT_ENTRY(nkeys); |
| 2705 | MAKE_BT_ENTRY(ndata); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2706 | #if (DBVER >= 46) |
| 2707 | MAKE_BT_ENTRY(pagecnt); |
| 2708 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2709 | MAKE_BT_ENTRY(pagesize); |
| 2710 | MAKE_BT_ENTRY(minkey); |
| 2711 | MAKE_BT_ENTRY(re_len); |
| 2712 | MAKE_BT_ENTRY(re_pad); |
| 2713 | MAKE_BT_ENTRY(levels); |
| 2714 | MAKE_BT_ENTRY(int_pg); |
| 2715 | MAKE_BT_ENTRY(leaf_pg); |
| 2716 | MAKE_BT_ENTRY(dup_pg); |
| 2717 | MAKE_BT_ENTRY(over_pg); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2718 | #if (DBVER >= 43) |
| 2719 | MAKE_BT_ENTRY(empty_pg); |
| 2720 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2721 | MAKE_BT_ENTRY(free); |
| 2722 | MAKE_BT_ENTRY(int_pgfree); |
| 2723 | MAKE_BT_ENTRY(leaf_pgfree); |
| 2724 | MAKE_BT_ENTRY(dup_pgfree); |
| 2725 | MAKE_BT_ENTRY(over_pgfree); |
| 2726 | break; |
| 2727 | |
| 2728 | case DB_QUEUE: |
| 2729 | MAKE_QUEUE_ENTRY(magic); |
| 2730 | MAKE_QUEUE_ENTRY(version); |
| 2731 | MAKE_QUEUE_ENTRY(nkeys); |
| 2732 | MAKE_QUEUE_ENTRY(ndata); |
| 2733 | MAKE_QUEUE_ENTRY(pagesize); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2734 | #if (DBVER >= 41) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2735 | MAKE_QUEUE_ENTRY(extentsize); |
| 2736 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2737 | MAKE_QUEUE_ENTRY(pages); |
| 2738 | MAKE_QUEUE_ENTRY(re_len); |
| 2739 | MAKE_QUEUE_ENTRY(re_pad); |
| 2740 | MAKE_QUEUE_ENTRY(pgfree); |
| 2741 | #if (DBVER == 31) |
| 2742 | MAKE_QUEUE_ENTRY(start); |
| 2743 | #endif |
| 2744 | MAKE_QUEUE_ENTRY(first_recno); |
| 2745 | MAKE_QUEUE_ENTRY(cur_recno); |
| 2746 | break; |
| 2747 | |
| 2748 | default: |
| 2749 | PyErr_SetString(PyExc_TypeError, "Unknown DB type, unable to stat"); |
| 2750 | Py_DECREF(d); |
| 2751 | d = NULL; |
| 2752 | } |
| 2753 | |
| 2754 | #undef MAKE_HASH_ENTRY |
| 2755 | #undef MAKE_BT_ENTRY |
| 2756 | #undef MAKE_QUEUE_ENTRY |
| 2757 | |
| 2758 | free(sp); |
| 2759 | return d; |
| 2760 | } |
| 2761 | |
| 2762 | static PyObject* |
| 2763 | DB_sync(DBObject* self, PyObject* args) |
| 2764 | { |
| 2765 | int err; |
| 2766 | int flags = 0; |
| 2767 | |
| 2768 | if (!PyArg_ParseTuple(args,"|i:sync", &flags )) |
| 2769 | return NULL; |
| 2770 | CHECK_DB_NOT_CLOSED(self); |
| 2771 | |
| 2772 | MYDB_BEGIN_ALLOW_THREADS; |
| 2773 | err = self->db->sync(self->db, flags); |
| 2774 | MYDB_END_ALLOW_THREADS; |
| 2775 | RETURN_IF_ERR(); |
| 2776 | RETURN_NONE(); |
| 2777 | } |
| 2778 | |
| 2779 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2780 | static PyObject* |
| 2781 | DB_truncate(DBObject* self, PyObject* args, PyObject* kwargs) |
| 2782 | { |
| 2783 | int err, flags=0; |
| 2784 | u_int32_t count=0; |
| 2785 | PyObject* txnobj = NULL; |
| 2786 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2787 | static char* kwnames[] = { "txn", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2788 | |
| 2789 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:cursor", kwnames, |
| 2790 | &txnobj, &flags)) |
| 2791 | return NULL; |
| 2792 | CHECK_DB_NOT_CLOSED(self); |
| 2793 | if (!checkTxnObj(txnobj, &txn)) |
| 2794 | return NULL; |
| 2795 | |
| 2796 | MYDB_BEGIN_ALLOW_THREADS; |
| 2797 | err = self->db->truncate(self->db, txn, &count, flags); |
| 2798 | MYDB_END_ALLOW_THREADS; |
| 2799 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2800 | return NUMBER_FromLong(count); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2801 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2802 | |
| 2803 | |
| 2804 | static PyObject* |
| 2805 | DB_upgrade(DBObject* self, PyObject* args) |
| 2806 | { |
| 2807 | int err, flags=0; |
| 2808 | char *filename; |
| 2809 | |
| 2810 | if (!PyArg_ParseTuple(args,"s|i:upgrade", &filename, &flags)) |
| 2811 | return NULL; |
| 2812 | CHECK_DB_NOT_CLOSED(self); |
| 2813 | |
| 2814 | MYDB_BEGIN_ALLOW_THREADS; |
| 2815 | err = self->db->upgrade(self->db, filename, flags); |
| 2816 | MYDB_END_ALLOW_THREADS; |
| 2817 | RETURN_IF_ERR(); |
| 2818 | RETURN_NONE(); |
| 2819 | } |
| 2820 | |
| 2821 | |
| 2822 | static PyObject* |
| 2823 | DB_verify(DBObject* self, PyObject* args, PyObject* kwargs) |
| 2824 | { |
| 2825 | int err, flags=0; |
| 2826 | char* fileName; |
| 2827 | char* dbName=NULL; |
| 2828 | char* outFileName=NULL; |
| 2829 | FILE* outFile=NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2830 | static char* kwnames[] = { "filename", "dbname", "outfile", "flags", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 2831 | NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2832 | |
| 2833 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|zzi:verify", kwnames, |
| 2834 | &fileName, &dbName, &outFileName, &flags)) |
| 2835 | return NULL; |
| 2836 | |
| 2837 | CHECK_DB_NOT_CLOSED(self); |
| 2838 | if (outFileName) |
| 2839 | outFile = fopen(outFileName, "w"); |
Neal Norwitz | 5aa9689 | 2006-08-13 18:11:43 +0000 | [diff] [blame] | 2840 | /* XXX(nnorwitz): it should probably be an exception if outFile |
| 2841 | can't be opened. */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2842 | |
| 2843 | MYDB_BEGIN_ALLOW_THREADS; |
| 2844 | err = self->db->verify(self->db, fileName, dbName, outFile, flags); |
| 2845 | MYDB_END_ALLOW_THREADS; |
Neal Norwitz | 5aa9689 | 2006-08-13 18:11:43 +0000 | [diff] [blame] | 2846 | if (outFile) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2847 | fclose(outFile); |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 2848 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2849 | { /* DB.verify acts as a DB handle destructor (like close) */ |
| 2850 | PyObject *error; |
| 2851 | |
| 2852 | error=DB_close_internal(self,0); |
| 2853 | if (error ) { |
| 2854 | return error; |
| 2855 | } |
| 2856 | } |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 2857 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2858 | RETURN_IF_ERR(); |
| 2859 | RETURN_NONE(); |
| 2860 | } |
| 2861 | |
| 2862 | |
| 2863 | static PyObject* |
| 2864 | DB_set_get_returns_none(DBObject* self, PyObject* args) |
| 2865 | { |
| 2866 | int flags=0; |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 2867 | int oldValue=0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2868 | |
| 2869 | if (!PyArg_ParseTuple(args,"i:set_get_returns_none", &flags)) |
| 2870 | return NULL; |
| 2871 | CHECK_DB_NOT_CLOSED(self); |
| 2872 | |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 2873 | if (self->moduleFlags.getReturnsNone) |
| 2874 | ++oldValue; |
| 2875 | if (self->moduleFlags.cursorSetReturnsNone) |
| 2876 | ++oldValue; |
| 2877 | self->moduleFlags.getReturnsNone = (flags >= 1); |
| 2878 | self->moduleFlags.cursorSetReturnsNone = (flags >= 2); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2879 | return NUMBER_FromLong(oldValue); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2882 | #if (DBVER >= 41) |
| 2883 | static PyObject* |
| 2884 | DB_set_encrypt(DBObject* self, PyObject* args, PyObject* kwargs) |
| 2885 | { |
| 2886 | int err; |
| 2887 | u_int32_t flags=0; |
| 2888 | char *passwd = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2889 | static char* kwnames[] = { "passwd", "flags", NULL }; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2890 | |
| 2891 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|i:set_encrypt", kwnames, |
| 2892 | &passwd, &flags)) { |
| 2893 | return NULL; |
| 2894 | } |
| 2895 | |
| 2896 | MYDB_BEGIN_ALLOW_THREADS; |
| 2897 | err = self->db->set_encrypt(self->db, passwd, flags); |
| 2898 | MYDB_END_ALLOW_THREADS; |
| 2899 | |
| 2900 | RETURN_IF_ERR(); |
| 2901 | RETURN_NONE(); |
| 2902 | } |
| 2903 | #endif /* DBVER >= 41 */ |
| 2904 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2905 | |
| 2906 | /*-------------------------------------------------------------- */ |
| 2907 | /* Mapping and Dictionary-like access routines */ |
| 2908 | |
Martin v. Löwis | 70ee3cc | 2006-06-12 04:26:31 +0000 | [diff] [blame] | 2909 | Py_ssize_t DB_length(PyObject* _self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2910 | { |
| 2911 | int err; |
Gregory P. Smith | 3c228b1 | 2006-06-05 23:59:37 +0000 | [diff] [blame] | 2912 | Py_ssize_t size = 0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2913 | int flags = 0; |
| 2914 | void* sp; |
Martin v. Löwis | 70ee3cc | 2006-06-12 04:26:31 +0000 | [diff] [blame] | 2915 | DBObject* self = (DBObject*)_self; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2916 | |
| 2917 | if (self->db == NULL) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2918 | PyObject *t = Py_BuildValue("(is)", 0, "DB object has been closed"); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2919 | if (t) { |
| 2920 | PyErr_SetObject(DBError, t); |
| 2921 | Py_DECREF(t); |
| 2922 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2923 | return -1; |
| 2924 | } |
| 2925 | |
| 2926 | if (self->haveStat) { /* Has the stat function been called recently? If |
| 2927 | so, we can use the cached value. */ |
Gregory P. Smith | 3c228b1 | 2006-06-05 23:59:37 +0000 | [diff] [blame] | 2928 | flags = DB_FAST_STAT; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
| 2931 | MYDB_BEGIN_ALLOW_THREADS; |
Gregory P. Smith | 3c228b1 | 2006-06-05 23:59:37 +0000 | [diff] [blame] | 2932 | redo_stat_for_length: |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2933 | #if (DBVER >= 43) |
| 2934 | err = self->db->stat(self->db, /*txnid*/ NULL, &sp, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2935 | #else |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2936 | err = self->db->stat(self->db, &sp, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2937 | #endif |
Gregory P. Smith | 3c228b1 | 2006-06-05 23:59:37 +0000 | [diff] [blame] | 2938 | |
| 2939 | /* All the stat structures have matching fields upto the ndata field, |
| 2940 | so we can use any of them for the type cast */ |
| 2941 | size = ((DB_BTREE_STAT*)sp)->bt_ndata; |
| 2942 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2943 | /* A size of 0 could mean that Berkeley DB no longer had the stat values cached. |
Gregory P. Smith | 3c228b1 | 2006-06-05 23:59:37 +0000 | [diff] [blame] | 2944 | * redo a full stat to make sure. |
| 2945 | * Fixes SF python bug 1493322, pybsddb bug 1184012 |
| 2946 | */ |
| 2947 | if (size == 0 && (flags & DB_FAST_STAT)) { |
| 2948 | flags = 0; |
Neal Norwitz | e75cad6 | 2006-06-17 22:38:15 +0000 | [diff] [blame] | 2949 | if (!err) |
| 2950 | free(sp); |
Gregory P. Smith | 3c228b1 | 2006-06-05 23:59:37 +0000 | [diff] [blame] | 2951 | goto redo_stat_for_length; |
| 2952 | } |
| 2953 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2954 | MYDB_END_ALLOW_THREADS; |
| 2955 | |
| 2956 | if (err) |
| 2957 | return -1; |
| 2958 | |
| 2959 | self->haveStat = 1; |
| 2960 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2961 | free(sp); |
| 2962 | return size; |
| 2963 | } |
| 2964 | |
| 2965 | |
| 2966 | PyObject* DB_subscript(DBObject* self, PyObject* keyobj) |
| 2967 | { |
| 2968 | int err; |
| 2969 | PyObject* retval; |
| 2970 | DBT key; |
| 2971 | DBT data; |
| 2972 | |
| 2973 | CHECK_DB_NOT_CLOSED(self); |
| 2974 | if (!make_key_dbt(self, keyobj, &key, NULL)) |
| 2975 | return NULL; |
| 2976 | |
| 2977 | CLEAR_DBT(data); |
| 2978 | if (CHECK_DBFLAG(self, DB_THREAD)) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2979 | /* Tell Berkeley DB to malloc the return value (thread safe) */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2980 | data.flags = DB_DBT_MALLOC; |
| 2981 | } |
| 2982 | MYDB_BEGIN_ALLOW_THREADS; |
| 2983 | err = self->db->get(self->db, NULL, &key, &data, 0); |
| 2984 | MYDB_END_ALLOW_THREADS; |
| 2985 | if (err == DB_NOTFOUND || err == DB_KEYEMPTY) { |
| 2986 | PyErr_SetObject(PyExc_KeyError, keyobj); |
| 2987 | retval = NULL; |
| 2988 | } |
| 2989 | else if (makeDBError(err)) { |
| 2990 | retval = NULL; |
| 2991 | } |
| 2992 | else { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2993 | retval = Build_PyString(data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2994 | FREE_DBT(data); |
| 2995 | } |
| 2996 | |
| 2997 | FREE_DBT(key); |
| 2998 | return retval; |
| 2999 | } |
| 3000 | |
| 3001 | |
| 3002 | static int |
| 3003 | DB_ass_sub(DBObject* self, PyObject* keyobj, PyObject* dataobj) |
| 3004 | { |
| 3005 | DBT key, data; |
| 3006 | int retval; |
| 3007 | int flags = 0; |
| 3008 | |
| 3009 | if (self->db == NULL) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 3010 | PyObject *t = Py_BuildValue("(is)", 0, "DB object has been closed"); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3011 | if (t) { |
| 3012 | PyErr_SetObject(DBError, t); |
| 3013 | Py_DECREF(t); |
| 3014 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3015 | return -1; |
| 3016 | } |
| 3017 | |
| 3018 | if (!make_key_dbt(self, keyobj, &key, NULL)) |
| 3019 | return -1; |
| 3020 | |
| 3021 | if (dataobj != NULL) { |
| 3022 | if (!make_dbt(dataobj, &data)) |
| 3023 | retval = -1; |
| 3024 | else { |
| 3025 | if (self->setflags & (DB_DUP|DB_DUPSORT)) |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3026 | /* dictionaries shouldn't have duplicate keys */ |
| 3027 | flags = DB_NOOVERWRITE; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3028 | retval = _DB_put(self, NULL, &key, &data, flags); |
| 3029 | |
| 3030 | if ((retval == -1) && (self->setflags & (DB_DUP|DB_DUPSORT))) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3031 | /* try deleting any old record that matches and then PUT it |
| 3032 | * again... */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3033 | _DB_delete(self, NULL, &key, 0); |
| 3034 | PyErr_Clear(); |
| 3035 | retval = _DB_put(self, NULL, &key, &data, flags); |
| 3036 | } |
| 3037 | } |
| 3038 | } |
| 3039 | else { |
| 3040 | /* dataobj == NULL, so delete the key */ |
| 3041 | retval = _DB_delete(self, NULL, &key, 0); |
| 3042 | } |
| 3043 | FREE_DBT(key); |
| 3044 | return retval; |
| 3045 | } |
| 3046 | |
| 3047 | |
| 3048 | static PyObject* |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 3049 | DB_has_key(DBObject* self, PyObject* args, PyObject* kwargs) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3050 | { |
| 3051 | int err; |
| 3052 | PyObject* keyobj; |
| 3053 | DBT key, data; |
| 3054 | PyObject* txnobj = NULL; |
| 3055 | DB_TXN *txn = NULL; |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 3056 | static char* kwnames[] = {"key","txn", NULL}; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3057 | |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 3058 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:has_key", kwnames, |
| 3059 | &keyobj, &txnobj)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3060 | return NULL; |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 3061 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3062 | CHECK_DB_NOT_CLOSED(self); |
| 3063 | if (!make_key_dbt(self, keyobj, &key, NULL)) |
| 3064 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3065 | if (!checkTxnObj(txnobj, &txn)) { |
| 3066 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3067 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3068 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3069 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 3070 | /* This causes DB_BUFFER_SMALL to be returned when the db has the key because |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3071 | it has a record but can't allocate a buffer for the data. This saves |
| 3072 | having to deal with data we won't be using. |
| 3073 | */ |
| 3074 | CLEAR_DBT(data); |
| 3075 | data.flags = DB_DBT_USERMEM; |
| 3076 | |
| 3077 | MYDB_BEGIN_ALLOW_THREADS; |
Gregory P. Smith | 0c65771 | 2004-03-16 06:56:58 +0000 | [diff] [blame] | 3078 | err = self->db->get(self->db, txn, &key, &data, 0); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3079 | MYDB_END_ALLOW_THREADS; |
| 3080 | FREE_DBT(key); |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3081 | |
| 3082 | if (err == DB_BUFFER_SMALL || err == 0) { |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3083 | return NUMBER_FromLong(1); |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3084 | } else if (err == DB_NOTFOUND || err == DB_KEYEMPTY) { |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3085 | return NUMBER_FromLong(0); |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
| 3088 | makeDBError(err); |
| 3089 | return NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | |
| 3093 | #define _KEYS_LIST 1 |
| 3094 | #define _VALUES_LIST 2 |
| 3095 | #define _ITEMS_LIST 3 |
| 3096 | |
| 3097 | static PyObject* |
| 3098 | _DB_make_list(DBObject* self, DB_TXN* txn, int type) |
| 3099 | { |
| 3100 | int err, dbtype; |
| 3101 | DBT key; |
| 3102 | DBT data; |
| 3103 | DBC *cursor; |
| 3104 | PyObject* list; |
| 3105 | PyObject* item = NULL; |
| 3106 | |
| 3107 | CHECK_DB_NOT_CLOSED(self); |
| 3108 | CLEAR_DBT(key); |
| 3109 | CLEAR_DBT(data); |
| 3110 | |
| 3111 | dbtype = _DB_get_type(self); |
| 3112 | if (dbtype == -1) |
| 3113 | return NULL; |
| 3114 | |
| 3115 | list = PyList_New(0); |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 3116 | if (list == NULL) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3117 | return NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3118 | |
| 3119 | /* get a cursor */ |
| 3120 | MYDB_BEGIN_ALLOW_THREADS; |
Gregory P. Smith | 442c9fc | 2004-09-04 01:36:59 +0000 | [diff] [blame] | 3121 | err = self->db->cursor(self->db, txn, &cursor, 0); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3122 | MYDB_END_ALLOW_THREADS; |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 3123 | if (makeDBError(err)) { |
| 3124 | Py_DECREF(list); |
| 3125 | return NULL; |
| 3126 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3127 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3128 | while (1) { /* use the cursor to traverse the DB, collecting items */ |
| 3129 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3130 | err = _DBC_get(cursor, &key, &data, DB_NEXT); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3131 | MYDB_END_ALLOW_THREADS; |
| 3132 | |
| 3133 | if (err) { |
| 3134 | /* for any error, break out of the loop */ |
| 3135 | break; |
| 3136 | } |
| 3137 | |
| 3138 | switch (type) { |
| 3139 | case _KEYS_LIST: |
| 3140 | switch(dbtype) { |
| 3141 | case DB_BTREE: |
| 3142 | case DB_HASH: |
| 3143 | default: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3144 | item = Build_PyString(key.data, key.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3145 | break; |
| 3146 | case DB_RECNO: |
| 3147 | case DB_QUEUE: |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3148 | item = NUMBER_FromLong(*((db_recno_t*)key.data)); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3149 | break; |
| 3150 | } |
| 3151 | break; |
| 3152 | |
| 3153 | case _VALUES_LIST: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3154 | item = Build_PyString(data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3155 | break; |
| 3156 | |
| 3157 | case _ITEMS_LIST: |
| 3158 | switch(dbtype) { |
| 3159 | case DB_BTREE: |
| 3160 | case DB_HASH: |
| 3161 | default: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3162 | item = BuildValue_SS(key.data, key.size, data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3163 | break; |
| 3164 | case DB_RECNO: |
| 3165 | case DB_QUEUE: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3166 | item = BuildValue_IS(*((db_recno_t*)key.data), data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3167 | break; |
| 3168 | } |
| 3169 | break; |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 3170 | default: |
| 3171 | PyErr_Format(PyExc_ValueError, "Unknown key type 0x%x", type); |
| 3172 | item = NULL; |
| 3173 | break; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3174 | } |
| 3175 | if (item == NULL) { |
| 3176 | Py_DECREF(list); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3177 | list = NULL; |
| 3178 | goto done; |
| 3179 | } |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3180 | if (PyList_Append(list, item)) { |
| 3181 | Py_DECREF(list); |
| 3182 | Py_DECREF(item); |
| 3183 | list = NULL; |
| 3184 | goto done; |
| 3185 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3186 | Py_DECREF(item); |
| 3187 | } |
| 3188 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3189 | /* DB_NOTFOUND || DB_KEYEMPTY is okay, it means we got to the end */ |
| 3190 | if (err != DB_NOTFOUND && err != DB_KEYEMPTY && makeDBError(err)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3191 | Py_DECREF(list); |
| 3192 | list = NULL; |
| 3193 | } |
| 3194 | |
| 3195 | done: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3196 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3197 | _DBC_close(cursor); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3198 | MYDB_END_ALLOW_THREADS; |
| 3199 | return list; |
| 3200 | } |
| 3201 | |
| 3202 | |
| 3203 | static PyObject* |
| 3204 | DB_keys(DBObject* self, PyObject* args) |
| 3205 | { |
| 3206 | PyObject* txnobj = NULL; |
| 3207 | DB_TXN *txn = NULL; |
| 3208 | |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 3209 | if (!PyArg_UnpackTuple(args, "keys", 0, 1, &txnobj)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3210 | return NULL; |
| 3211 | if (!checkTxnObj(txnobj, &txn)) |
| 3212 | return NULL; |
| 3213 | return _DB_make_list(self, txn, _KEYS_LIST); |
| 3214 | } |
| 3215 | |
| 3216 | |
| 3217 | static PyObject* |
| 3218 | DB_items(DBObject* self, PyObject* args) |
| 3219 | { |
| 3220 | PyObject* txnobj = NULL; |
| 3221 | DB_TXN *txn = NULL; |
| 3222 | |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 3223 | if (!PyArg_UnpackTuple(args, "items", 0, 1, &txnobj)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3224 | return NULL; |
| 3225 | if (!checkTxnObj(txnobj, &txn)) |
| 3226 | return NULL; |
| 3227 | return _DB_make_list(self, txn, _ITEMS_LIST); |
| 3228 | } |
| 3229 | |
| 3230 | |
| 3231 | static PyObject* |
| 3232 | DB_values(DBObject* self, PyObject* args) |
| 3233 | { |
| 3234 | PyObject* txnobj = NULL; |
| 3235 | DB_TXN *txn = NULL; |
| 3236 | |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 3237 | if (!PyArg_UnpackTuple(args, "values", 0, 1, &txnobj)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3238 | return NULL; |
| 3239 | if (!checkTxnObj(txnobj, &txn)) |
| 3240 | return NULL; |
| 3241 | return _DB_make_list(self, txn, _VALUES_LIST); |
| 3242 | } |
| 3243 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3244 | /* --------------------------------------------------------------------- */ |
| 3245 | /* DBCursor methods */ |
| 3246 | |
| 3247 | |
| 3248 | static PyObject* |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3249 | DBC_close_internal(DBCursorObject* self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3250 | { |
| 3251 | int err = 0; |
| 3252 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3253 | if (self->dbc != NULL) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3254 | EXTRACT_FROM_DOUBLE_LINKED_LIST(self); |
| 3255 | if (self->txn) { |
| 3256 | EXTRACT_FROM_DOUBLE_LINKED_LIST_TXN(self); |
| 3257 | self->txn=NULL; |
| 3258 | } |
| 3259 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3260 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3261 | err = _DBC_close(self->dbc); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3262 | MYDB_END_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3263 | self->dbc = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3264 | } |
| 3265 | RETURN_IF_ERR(); |
| 3266 | RETURN_NONE(); |
| 3267 | } |
| 3268 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3269 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3270 | DBC_close(DBCursorObject* self) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3271 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3272 | return DBC_close_internal(self); |
| 3273 | } |
| 3274 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3275 | |
| 3276 | static PyObject* |
| 3277 | DBC_count(DBCursorObject* self, PyObject* args) |
| 3278 | { |
| 3279 | int err = 0; |
| 3280 | db_recno_t count; |
| 3281 | int flags = 0; |
| 3282 | |
| 3283 | if (!PyArg_ParseTuple(args, "|i:count", &flags)) |
| 3284 | return NULL; |
| 3285 | |
| 3286 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3287 | |
| 3288 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3289 | err = _DBC_count(self->dbc, &count, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3290 | MYDB_END_ALLOW_THREADS; |
| 3291 | RETURN_IF_ERR(); |
| 3292 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3293 | return NUMBER_FromLong(count); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
| 3296 | |
| 3297 | static PyObject* |
| 3298 | DBC_current(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3299 | { |
| 3300 | return _DBCursor_get(self,DB_CURRENT,args,kwargs,"|iii:current"); |
| 3301 | } |
| 3302 | |
| 3303 | |
| 3304 | static PyObject* |
| 3305 | DBC_delete(DBCursorObject* self, PyObject* args) |
| 3306 | { |
| 3307 | int err, flags=0; |
| 3308 | |
| 3309 | if (!PyArg_ParseTuple(args, "|i:delete", &flags)) |
| 3310 | return NULL; |
| 3311 | |
| 3312 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3313 | |
| 3314 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3315 | err = _DBC_del(self->dbc, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3316 | MYDB_END_ALLOW_THREADS; |
| 3317 | RETURN_IF_ERR(); |
| 3318 | |
| 3319 | self->mydb->haveStat = 0; |
| 3320 | RETURN_NONE(); |
| 3321 | } |
| 3322 | |
| 3323 | |
| 3324 | static PyObject* |
| 3325 | DBC_dup(DBCursorObject* self, PyObject* args) |
| 3326 | { |
| 3327 | int err, flags =0; |
| 3328 | DBC* dbc = NULL; |
| 3329 | |
| 3330 | if (!PyArg_ParseTuple(args, "|i:dup", &flags)) |
| 3331 | return NULL; |
| 3332 | |
| 3333 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3334 | |
| 3335 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3336 | err = _DBC_dup(self->dbc, &dbc, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3337 | MYDB_END_ALLOW_THREADS; |
| 3338 | RETURN_IF_ERR(); |
| 3339 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3340 | return (PyObject*) newDBCursorObject(dbc, self->txn, self->mydb); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
| 3343 | static PyObject* |
| 3344 | DBC_first(DBCursorObject* self, PyObject* args, PyObject* kwargs) |
| 3345 | { |
| 3346 | return _DBCursor_get(self,DB_FIRST,args,kwargs,"|iii:first"); |
| 3347 | } |
| 3348 | |
| 3349 | |
| 3350 | static PyObject* |
| 3351 | DBC_get(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3352 | { |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 3353 | int err, flags=0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3354 | PyObject* keyobj = NULL; |
| 3355 | PyObject* dataobj = NULL; |
| 3356 | PyObject* retval = NULL; |
| 3357 | int dlen = -1; |
| 3358 | int doff = -1; |
| 3359 | DBT key, data; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3360 | static char* kwnames[] = { "key","data", "flags", "dlen", "doff", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 3361 | NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3362 | |
| 3363 | CLEAR_DBT(key); |
| 3364 | CLEAR_DBT(data); |
| 3365 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii:get", &kwnames[2], |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3366 | &flags, &dlen, &doff)) |
| 3367 | { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3368 | PyErr_Clear(); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3369 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|ii:get", |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 3370 | &kwnames[1], |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3371 | &keyobj, &flags, &dlen, &doff)) |
| 3372 | { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3373 | PyErr_Clear(); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3374 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOi|ii:get", |
| 3375 | kwnames, &keyobj, &dataobj, |
| 3376 | &flags, &dlen, &doff)) |
| 3377 | { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3378 | return NULL; |
| 3379 | } |
| 3380 | } |
| 3381 | } |
| 3382 | |
| 3383 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3384 | |
| 3385 | if (keyobj && !make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 3386 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3387 | if ( (dataobj && !make_dbt(dataobj, &data)) || |
| 3388 | (!add_partial_dbt(&data, dlen, doff)) ) |
| 3389 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3390 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3391 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3392 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3393 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3394 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3395 | err = _DBC_get(self->dbc, &key, &data, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3396 | MYDB_END_ALLOW_THREADS; |
| 3397 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3398 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 3399 | && self->mydb->moduleFlags.getReturnsNone) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3400 | Py_INCREF(Py_None); |
| 3401 | retval = Py_None; |
| 3402 | } |
| 3403 | else if (makeDBError(err)) { |
| 3404 | retval = NULL; |
| 3405 | } |
| 3406 | else { |
| 3407 | switch (_DB_get_type(self->mydb)) { |
| 3408 | case -1: |
| 3409 | retval = NULL; |
| 3410 | break; |
| 3411 | case DB_BTREE: |
| 3412 | case DB_HASH: |
| 3413 | default: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3414 | retval = BuildValue_SS(key.data, key.size, data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3415 | break; |
| 3416 | case DB_RECNO: |
| 3417 | case DB_QUEUE: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3418 | retval = BuildValue_IS(*((db_recno_t*)key.data), data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3419 | break; |
| 3420 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3421 | } |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3422 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3423 | return retval; |
| 3424 | } |
| 3425 | |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3426 | static PyObject* |
| 3427 | DBC_pget(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3428 | { |
| 3429 | int err, flags=0; |
| 3430 | PyObject* keyobj = NULL; |
| 3431 | PyObject* dataobj = NULL; |
| 3432 | PyObject* retval = NULL; |
| 3433 | int dlen = -1; |
| 3434 | int doff = -1; |
| 3435 | DBT key, pkey, data; |
Gregory P. Smith | 372b583 | 2006-06-05 18:48:21 +0000 | [diff] [blame] | 3436 | static char* kwnames_keyOnly[] = { "key", "flags", "dlen", "doff", NULL }; |
| 3437 | static char* kwnames[] = { "key", "data", "flags", "dlen", "doff", NULL }; |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3438 | |
| 3439 | CLEAR_DBT(key); |
| 3440 | CLEAR_DBT(data); |
| 3441 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii:pget", &kwnames[2], |
| 3442 | &flags, &dlen, &doff)) |
| 3443 | { |
| 3444 | PyErr_Clear(); |
| 3445 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|ii:pget", |
Gregory P. Smith | 372b583 | 2006-06-05 18:48:21 +0000 | [diff] [blame] | 3446 | kwnames_keyOnly, |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3447 | &keyobj, &flags, &dlen, &doff)) |
| 3448 | { |
| 3449 | PyErr_Clear(); |
| 3450 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOi|ii:pget", |
| 3451 | kwnames, &keyobj, &dataobj, |
| 3452 | &flags, &dlen, &doff)) |
| 3453 | { |
| 3454 | return NULL; |
| 3455 | } |
| 3456 | } |
| 3457 | } |
| 3458 | |
| 3459 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3460 | |
| 3461 | if (keyobj && !make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 3462 | return NULL; |
| 3463 | if ( (dataobj && !make_dbt(dataobj, &data)) || |
| 3464 | (!add_partial_dbt(&data, dlen, doff)) ) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3465 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3466 | return NULL; |
| 3467 | } |
| 3468 | |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3469 | CLEAR_DBT(pkey); |
| 3470 | pkey.flags = DB_DBT_MALLOC; |
| 3471 | |
| 3472 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3473 | err = _DBC_pget(self->dbc, &key, &pkey, &data, flags); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3474 | MYDB_END_ALLOW_THREADS; |
| 3475 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3476 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 3477 | && self->mydb->moduleFlags.getReturnsNone) { |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3478 | Py_INCREF(Py_None); |
| 3479 | retval = Py_None; |
| 3480 | } |
| 3481 | else if (makeDBError(err)) { |
| 3482 | retval = NULL; |
| 3483 | } |
| 3484 | else { |
| 3485 | PyObject *pkeyObj; |
| 3486 | PyObject *dataObj; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3487 | dataObj = Build_PyString(data.data, data.size); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3488 | |
| 3489 | if (self->mydb->primaryDBType == DB_RECNO || |
| 3490 | self->mydb->primaryDBType == DB_QUEUE) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3491 | pkeyObj = NUMBER_FromLong(*(int *)pkey.data); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3492 | else |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3493 | pkeyObj = Build_PyString(pkey.data, pkey.size); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3494 | |
Gregory P. Smith | 4e414d8 | 2006-01-24 19:55:02 +0000 | [diff] [blame] | 3495 | if (key.data && key.size) /* return key, pkey and data */ |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3496 | { |
| 3497 | PyObject *keyObj; |
| 3498 | int type = _DB_get_type(self->mydb); |
| 3499 | if (type == DB_RECNO || type == DB_QUEUE) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3500 | keyObj = NUMBER_FromLong(*(int *)key.data); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3501 | else |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3502 | keyObj = Build_PyString(key.data, key.size); |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 3503 | #if (PY_VERSION_HEX >= 0x02040000) |
Gregory P. Smith | 4e414d8 | 2006-01-24 19:55:02 +0000 | [diff] [blame] | 3504 | retval = PyTuple_Pack(3, keyObj, pkeyObj, dataObj); |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 3505 | #else |
| 3506 | retval = Py_BuildValue("OOO", keyObj, pkeyObj, dataObj); |
| 3507 | #endif |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 3508 | Py_DECREF(keyObj); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3509 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3510 | } |
| 3511 | else /* return just the pkey and data */ |
| 3512 | { |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 3513 | #if (PY_VERSION_HEX >= 0x02040000) |
Gregory P. Smith | 4e414d8 | 2006-01-24 19:55:02 +0000 | [diff] [blame] | 3514 | retval = PyTuple_Pack(2, pkeyObj, dataObj); |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 3515 | #else |
| 3516 | retval = Py_BuildValue("OO", pkeyObj, dataObj); |
| 3517 | #endif |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3518 | } |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 3519 | Py_DECREF(dataObj); |
| 3520 | Py_DECREF(pkeyObj); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3521 | FREE_DBT(pkey); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3522 | } |
| 3523 | /* the only time REALLOC should be set is if we used an integer |
| 3524 | * key that make_key_dbt malloc'd for us. always free these. */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3525 | if (key.flags & DB_DBT_REALLOC) { /* 'make_key_dbt' could do a 'malloc' */ |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3526 | FREE_DBT(key); |
| 3527 | } |
| 3528 | return retval; |
| 3529 | } |
| 3530 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3531 | |
| 3532 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3533 | DBC_get_recno(DBCursorObject* self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3534 | { |
| 3535 | int err; |
| 3536 | db_recno_t recno; |
| 3537 | DBT key; |
| 3538 | DBT data; |
| 3539 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3540 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3541 | |
| 3542 | CLEAR_DBT(key); |
| 3543 | CLEAR_DBT(data); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3544 | |
| 3545 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3546 | err = _DBC_get(self->dbc, &key, &data, DB_GET_RECNO); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3547 | MYDB_END_ALLOW_THREADS; |
| 3548 | RETURN_IF_ERR(); |
| 3549 | |
| 3550 | recno = *((db_recno_t*)data.data); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3551 | return NUMBER_FromLong(recno); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | |
| 3555 | static PyObject* |
| 3556 | DBC_last(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3557 | { |
| 3558 | return _DBCursor_get(self,DB_LAST,args,kwargs,"|iii:last"); |
| 3559 | } |
| 3560 | |
| 3561 | |
| 3562 | static PyObject* |
| 3563 | DBC_next(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3564 | { |
| 3565 | return _DBCursor_get(self,DB_NEXT,args,kwargs,"|iii:next"); |
| 3566 | } |
| 3567 | |
| 3568 | |
| 3569 | static PyObject* |
| 3570 | DBC_prev(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3571 | { |
| 3572 | return _DBCursor_get(self,DB_PREV,args,kwargs,"|iii:prev"); |
| 3573 | } |
| 3574 | |
| 3575 | |
| 3576 | static PyObject* |
| 3577 | DBC_put(DBCursorObject* self, PyObject* args, PyObject* kwargs) |
| 3578 | { |
| 3579 | int err, flags = 0; |
| 3580 | PyObject* keyobj, *dataobj; |
| 3581 | DBT key, data; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3582 | static char* kwnames[] = { "key", "data", "flags", "dlen", "doff", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 3583 | NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3584 | int dlen = -1; |
| 3585 | int doff = -1; |
| 3586 | |
| 3587 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iii:put", kwnames, |
| 3588 | &keyobj, &dataobj, &flags, &dlen, &doff)) |
| 3589 | return NULL; |
| 3590 | |
| 3591 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3592 | |
| 3593 | if (!make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 3594 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3595 | if (!make_dbt(dataobj, &data) || |
| 3596 | !add_partial_dbt(&data, dlen, doff) ) |
| 3597 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3598 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3599 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3600 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3601 | |
| 3602 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3603 | err = _DBC_put(self->dbc, &key, &data, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3604 | MYDB_END_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3605 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3606 | RETURN_IF_ERR(); |
| 3607 | self->mydb->haveStat = 0; |
| 3608 | RETURN_NONE(); |
| 3609 | } |
| 3610 | |
| 3611 | |
| 3612 | static PyObject* |
| 3613 | DBC_set(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3614 | { |
| 3615 | int err, flags = 0; |
| 3616 | DBT key, data; |
| 3617 | PyObject* retval, *keyobj; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3618 | static char* kwnames[] = { "key", "flags", "dlen", "doff", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3619 | int dlen = -1; |
| 3620 | int doff = -1; |
| 3621 | |
| 3622 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|iii:set", kwnames, |
| 3623 | &keyobj, &flags, &dlen, &doff)) |
| 3624 | return NULL; |
| 3625 | |
| 3626 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3627 | |
| 3628 | if (!make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 3629 | return NULL; |
| 3630 | |
| 3631 | CLEAR_DBT(data); |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3632 | if (!add_partial_dbt(&data, dlen, doff)) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3633 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3634 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3635 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3636 | |
| 3637 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3638 | err = _DBC_get(self->dbc, &key, &data, flags|DB_SET); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3639 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3640 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 3641 | && self->mydb->moduleFlags.cursorSetReturnsNone) { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3642 | Py_INCREF(Py_None); |
| 3643 | retval = Py_None; |
| 3644 | } |
| 3645 | else if (makeDBError(err)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3646 | retval = NULL; |
| 3647 | } |
| 3648 | else { |
| 3649 | switch (_DB_get_type(self->mydb)) { |
| 3650 | case -1: |
| 3651 | retval = NULL; |
| 3652 | break; |
| 3653 | case DB_BTREE: |
| 3654 | case DB_HASH: |
| 3655 | default: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3656 | retval = BuildValue_SS(key.data, key.size, data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3657 | break; |
| 3658 | case DB_RECNO: |
| 3659 | case DB_QUEUE: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3660 | retval = BuildValue_IS(*((db_recno_t*)key.data), data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3661 | break; |
| 3662 | } |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3663 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3664 | } |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3665 | /* the only time REALLOC should be set is if we used an integer |
| 3666 | * key that make_key_dbt malloc'd for us. always free these. */ |
| 3667 | if (key.flags & DB_DBT_REALLOC) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3668 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3669 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3670 | |
| 3671 | return retval; |
| 3672 | } |
| 3673 | |
| 3674 | |
| 3675 | static PyObject* |
| 3676 | DBC_set_range(DBCursorObject* self, PyObject* args, PyObject* kwargs) |
| 3677 | { |
| 3678 | int err, flags = 0; |
| 3679 | DBT key, data; |
| 3680 | PyObject* retval, *keyobj; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3681 | static char* kwnames[] = { "key", "flags", "dlen", "doff", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3682 | int dlen = -1; |
| 3683 | int doff = -1; |
| 3684 | |
| 3685 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|iii:set_range", kwnames, |
| 3686 | &keyobj, &flags, &dlen, &doff)) |
| 3687 | return NULL; |
| 3688 | |
| 3689 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3690 | |
| 3691 | if (!make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 3692 | return NULL; |
| 3693 | |
| 3694 | CLEAR_DBT(data); |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3695 | if (!add_partial_dbt(&data, dlen, doff)) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3696 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3697 | return NULL; |
| 3698 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3699 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3700 | err = _DBC_get(self->dbc, &key, &data, flags|DB_SET_RANGE); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3701 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3702 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 3703 | && self->mydb->moduleFlags.cursorSetReturnsNone) { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3704 | Py_INCREF(Py_None); |
| 3705 | retval = Py_None; |
| 3706 | } |
| 3707 | else if (makeDBError(err)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3708 | retval = NULL; |
| 3709 | } |
| 3710 | else { |
| 3711 | switch (_DB_get_type(self->mydb)) { |
| 3712 | case -1: |
| 3713 | retval = NULL; |
| 3714 | break; |
| 3715 | case DB_BTREE: |
| 3716 | case DB_HASH: |
| 3717 | default: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3718 | retval = BuildValue_SS(key.data, key.size, data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3719 | break; |
| 3720 | case DB_RECNO: |
| 3721 | case DB_QUEUE: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3722 | retval = BuildValue_IS(*((db_recno_t*)key.data), data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3723 | break; |
| 3724 | } |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3725 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3726 | } |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3727 | /* the only time REALLOC should be set is if we used an integer |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3728 | * key that make_key_dbt malloc'd for us. always free these. */ |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3729 | if (key.flags & DB_DBT_REALLOC) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3730 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3731 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3732 | |
| 3733 | return retval; |
| 3734 | } |
| 3735 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3736 | static PyObject* |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3737 | _DBC_get_set_both(DBCursorObject* self, PyObject* keyobj, PyObject* dataobj, |
| 3738 | int flags, unsigned int returnsNone) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3739 | { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3740 | int err; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3741 | DBT key, data; |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3742 | PyObject* retval; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3743 | |
Gregory P. Smith | 7441e65 | 2003-11-03 21:35:31 +0000 | [diff] [blame] | 3744 | /* the caller did this: CHECK_CURSOR_NOT_CLOSED(self); */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3745 | if (!make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 3746 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3747 | if (!make_dbt(dataobj, &data)) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3748 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3749 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3750 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3751 | |
| 3752 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3753 | err = _DBC_get(self->dbc, &key, &data, flags|DB_GET_BOTH); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3754 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3755 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) && returnsNone) { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3756 | Py_INCREF(Py_None); |
| 3757 | retval = Py_None; |
| 3758 | } |
| 3759 | else if (makeDBError(err)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3760 | retval = NULL; |
| 3761 | } |
| 3762 | else { |
| 3763 | switch (_DB_get_type(self->mydb)) { |
| 3764 | case -1: |
| 3765 | retval = NULL; |
| 3766 | break; |
| 3767 | case DB_BTREE: |
| 3768 | case DB_HASH: |
| 3769 | default: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3770 | retval = BuildValue_SS(key.data, key.size, data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3771 | break; |
| 3772 | case DB_RECNO: |
| 3773 | case DB_QUEUE: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3774 | retval = BuildValue_IS(*((db_recno_t*)key.data), data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3775 | break; |
| 3776 | } |
| 3777 | } |
| 3778 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3779 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3780 | return retval; |
| 3781 | } |
| 3782 | |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3783 | static PyObject* |
| 3784 | DBC_get_both(DBCursorObject* self, PyObject* args) |
| 3785 | { |
| 3786 | int flags=0; |
| 3787 | PyObject *keyobj, *dataobj; |
| 3788 | |
| 3789 | if (!PyArg_ParseTuple(args, "OO|i:get_both", &keyobj, &dataobj, &flags)) |
| 3790 | return NULL; |
| 3791 | |
Gregory P. Smith | 7441e65 | 2003-11-03 21:35:31 +0000 | [diff] [blame] | 3792 | /* if the cursor is closed, self->mydb may be invalid */ |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3793 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3794 | |
| 3795 | return _DBC_get_set_both(self, keyobj, dataobj, flags, |
| 3796 | self->mydb->moduleFlags.getReturnsNone); |
| 3797 | } |
| 3798 | |
Gregory P. Smith | be0db8b | 2003-10-01 06:48:51 +0000 | [diff] [blame] | 3799 | /* Return size of entry */ |
| 3800 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3801 | DBC_get_current_size(DBCursorObject* self) |
Gregory P. Smith | be0db8b | 2003-10-01 06:48:51 +0000 | [diff] [blame] | 3802 | { |
| 3803 | int err, flags=DB_CURRENT; |
| 3804 | PyObject* retval = NULL; |
| 3805 | DBT key, data; |
| 3806 | |
Gregory P. Smith | be0db8b | 2003-10-01 06:48:51 +0000 | [diff] [blame] | 3807 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3808 | CLEAR_DBT(key); |
| 3809 | CLEAR_DBT(data); |
| 3810 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 3811 | /* We don't allocate any memory, forcing a DB_BUFFER_SMALL error and thus |
Gregory P. Smith | be0db8b | 2003-10-01 06:48:51 +0000 | [diff] [blame] | 3812 | getting the record size. */ |
| 3813 | data.flags = DB_DBT_USERMEM; |
| 3814 | data.ulen = 0; |
| 3815 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3816 | err = _DBC_get(self->dbc, &key, &data, flags); |
Gregory P. Smith | be0db8b | 2003-10-01 06:48:51 +0000 | [diff] [blame] | 3817 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 3818 | if (err == DB_BUFFER_SMALL || !err) { |
| 3819 | /* DB_BUFFER_SMALL means positive size, !err means zero length value */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3820 | retval = NUMBER_FromLong((long)data.size); |
Gregory P. Smith | be0db8b | 2003-10-01 06:48:51 +0000 | [diff] [blame] | 3821 | err = 0; |
| 3822 | } |
| 3823 | |
Gregory P. Smith | be0db8b | 2003-10-01 06:48:51 +0000 | [diff] [blame] | 3824 | RETURN_IF_ERR(); |
| 3825 | return retval; |
| 3826 | } |
| 3827 | |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3828 | static PyObject* |
| 3829 | DBC_set_both(DBCursorObject* self, PyObject* args) |
| 3830 | { |
| 3831 | int flags=0; |
| 3832 | PyObject *keyobj, *dataobj; |
| 3833 | |
| 3834 | if (!PyArg_ParseTuple(args, "OO|i:set_both", &keyobj, &dataobj, &flags)) |
| 3835 | return NULL; |
| 3836 | |
Gregory P. Smith | 7441e65 | 2003-11-03 21:35:31 +0000 | [diff] [blame] | 3837 | /* if the cursor is closed, self->mydb may be invalid */ |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3838 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3839 | |
| 3840 | return _DBC_get_set_both(self, keyobj, dataobj, flags, |
| 3841 | self->mydb->moduleFlags.cursorSetReturnsNone); |
| 3842 | } |
| 3843 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3844 | |
| 3845 | static PyObject* |
| 3846 | DBC_set_recno(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3847 | { |
| 3848 | int err, irecno, flags=0; |
| 3849 | db_recno_t recno; |
| 3850 | DBT key, data; |
| 3851 | PyObject* retval; |
| 3852 | int dlen = -1; |
| 3853 | int doff = -1; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3854 | static char* kwnames[] = { "recno","flags", "dlen", "doff", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3855 | |
| 3856 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|iii:set_recno", kwnames, |
| 3857 | &irecno, &flags, &dlen, &doff)) |
| 3858 | return NULL; |
| 3859 | |
| 3860 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3861 | |
| 3862 | CLEAR_DBT(key); |
| 3863 | recno = (db_recno_t) irecno; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3864 | /* use allocated space so DB will be able to realloc room for the real |
| 3865 | * key */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3866 | key.data = malloc(sizeof(db_recno_t)); |
| 3867 | if (key.data == NULL) { |
| 3868 | PyErr_SetString(PyExc_MemoryError, "Key memory allocation failed"); |
| 3869 | return NULL; |
| 3870 | } |
| 3871 | key.size = sizeof(db_recno_t); |
| 3872 | key.ulen = key.size; |
| 3873 | memcpy(key.data, &recno, sizeof(db_recno_t)); |
| 3874 | key.flags = DB_DBT_REALLOC; |
| 3875 | |
| 3876 | CLEAR_DBT(data); |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3877 | if (!add_partial_dbt(&data, dlen, doff)) { |
| 3878 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3879 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3880 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3881 | |
| 3882 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3883 | err = _DBC_get(self->dbc, &key, &data, flags|DB_SET_RECNO); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3884 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3885 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 3886 | && self->mydb->moduleFlags.cursorSetReturnsNone) { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3887 | Py_INCREF(Py_None); |
| 3888 | retval = Py_None; |
| 3889 | } |
| 3890 | else if (makeDBError(err)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3891 | retval = NULL; |
| 3892 | } |
| 3893 | else { /* Can only be used for BTrees, so no need to return int key */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3894 | retval = BuildValue_SS(key.data, key.size, data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3895 | } |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3896 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3897 | |
| 3898 | return retval; |
| 3899 | } |
| 3900 | |
| 3901 | |
| 3902 | static PyObject* |
| 3903 | DBC_consume(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3904 | { |
| 3905 | return _DBCursor_get(self,DB_CONSUME,args,kwargs,"|iii:consume"); |
| 3906 | } |
| 3907 | |
| 3908 | |
| 3909 | static PyObject* |
| 3910 | DBC_next_dup(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3911 | { |
| 3912 | return _DBCursor_get(self,DB_NEXT_DUP,args,kwargs,"|iii:next_dup"); |
| 3913 | } |
| 3914 | |
| 3915 | |
| 3916 | static PyObject* |
| 3917 | DBC_next_nodup(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3918 | { |
| 3919 | return _DBCursor_get(self,DB_NEXT_NODUP,args,kwargs,"|iii:next_nodup"); |
| 3920 | } |
| 3921 | |
| 3922 | |
| 3923 | static PyObject* |
| 3924 | DBC_prev_nodup(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3925 | { |
| 3926 | return _DBCursor_get(self,DB_PREV_NODUP,args,kwargs,"|iii:prev_nodup"); |
| 3927 | } |
| 3928 | |
| 3929 | |
| 3930 | static PyObject* |
| 3931 | DBC_join_item(DBCursorObject* self, PyObject* args) |
| 3932 | { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3933 | int err, flags=0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3934 | DBT key, data; |
| 3935 | PyObject* retval; |
| 3936 | |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3937 | if (!PyArg_ParseTuple(args, "|i:join_item", &flags)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3938 | return NULL; |
| 3939 | |
| 3940 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3941 | |
| 3942 | CLEAR_DBT(key); |
| 3943 | CLEAR_DBT(data); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3944 | |
| 3945 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3946 | err = _DBC_get(self->dbc, &key, &data, flags | DB_JOIN_ITEM); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3947 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3948 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 3949 | && self->mydb->moduleFlags.getReturnsNone) { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3950 | Py_INCREF(Py_None); |
| 3951 | retval = Py_None; |
| 3952 | } |
| 3953 | else if (makeDBError(err)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3954 | retval = NULL; |
| 3955 | } |
| 3956 | else { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3957 | retval = BuildValue_S(key.data, key.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
| 3960 | return retval; |
| 3961 | } |
| 3962 | |
| 3963 | |
| 3964 | |
| 3965 | /* --------------------------------------------------------------------- */ |
| 3966 | /* DBEnv methods */ |
| 3967 | |
| 3968 | |
| 3969 | static PyObject* |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3970 | DBEnv_close_internal(DBEnvObject* self, int flags) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3971 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3972 | PyObject *dummy; |
| 3973 | int err; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3974 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3975 | if (!self->closed) { /* Don't close more than once */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3976 | while(self->children_txns) { |
| 3977 | dummy=DBTxn_abort_discard_internal(self->children_txns,0); |
| 3978 | Py_XDECREF(dummy); |
| 3979 | } |
| 3980 | while(self->children_dbs) { |
| 3981 | dummy=DB_close_internal(self->children_dbs,0); |
| 3982 | Py_XDECREF(dummy); |
| 3983 | } |
Jesus Cea | ac25fab | 2008-09-03 17:50:32 +0000 | [diff] [blame] | 3984 | } |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3985 | |
Jesus Cea | ac25fab | 2008-09-03 17:50:32 +0000 | [diff] [blame] | 3986 | self->closed = 1; |
| 3987 | if (self->db_env) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3988 | MYDB_BEGIN_ALLOW_THREADS; |
| 3989 | err = self->db_env->close(self->db_env, flags); |
| 3990 | MYDB_END_ALLOW_THREADS; |
| 3991 | /* after calling DBEnv->close, regardless of error, this DBEnv |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3992 | * may not be accessed again (Berkeley DB docs). */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3993 | self->db_env = NULL; |
| 3994 | RETURN_IF_ERR(); |
| 3995 | } |
| 3996 | RETURN_NONE(); |
| 3997 | } |
| 3998 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3999 | static PyObject* |
| 4000 | DBEnv_close(DBEnvObject* self, PyObject* args) |
| 4001 | { |
| 4002 | int flags = 0; |
| 4003 | |
| 4004 | if (!PyArg_ParseTuple(args, "|i:close", &flags)) |
| 4005 | return NULL; |
| 4006 | return DBEnv_close_internal(self,flags); |
| 4007 | } |
| 4008 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4009 | |
| 4010 | static PyObject* |
| 4011 | DBEnv_open(DBEnvObject* self, PyObject* args) |
| 4012 | { |
| 4013 | int err, flags=0, mode=0660; |
| 4014 | char *db_home; |
| 4015 | |
| 4016 | if (!PyArg_ParseTuple(args, "z|ii:open", &db_home, &flags, &mode)) |
| 4017 | return NULL; |
| 4018 | |
| 4019 | CHECK_ENV_NOT_CLOSED(self); |
| 4020 | |
| 4021 | MYDB_BEGIN_ALLOW_THREADS; |
| 4022 | err = self->db_env->open(self->db_env, db_home, flags, mode); |
| 4023 | MYDB_END_ALLOW_THREADS; |
| 4024 | RETURN_IF_ERR(); |
| 4025 | self->closed = 0; |
| 4026 | self->flags = flags; |
| 4027 | RETURN_NONE(); |
| 4028 | } |
| 4029 | |
| 4030 | |
| 4031 | static PyObject* |
| 4032 | DBEnv_remove(DBEnvObject* self, PyObject* args) |
| 4033 | { |
| 4034 | int err, flags=0; |
| 4035 | char *db_home; |
| 4036 | |
| 4037 | if (!PyArg_ParseTuple(args, "s|i:remove", &db_home, &flags)) |
| 4038 | return NULL; |
| 4039 | CHECK_ENV_NOT_CLOSED(self); |
| 4040 | MYDB_BEGIN_ALLOW_THREADS; |
| 4041 | err = self->db_env->remove(self->db_env, db_home, flags); |
| 4042 | MYDB_END_ALLOW_THREADS; |
| 4043 | RETURN_IF_ERR(); |
| 4044 | RETURN_NONE(); |
| 4045 | } |
| 4046 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 4047 | #if (DBVER >= 41) |
| 4048 | static PyObject* |
| 4049 | DBEnv_dbremove(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 4050 | { |
| 4051 | int err; |
| 4052 | u_int32_t flags=0; |
| 4053 | char *file = NULL; |
| 4054 | char *database = NULL; |
| 4055 | PyObject *txnobj = NULL; |
| 4056 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4057 | static char* kwnames[] = { "file", "database", "txn", "flags", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4058 | NULL }; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 4059 | |
Gregory P. Smith | 641cddf | 2006-07-28 01:35:25 +0000 | [diff] [blame] | 4060 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|zOi:dbremove", kwnames, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 4061 | &file, &database, &txnobj, &flags)) { |
| 4062 | return NULL; |
| 4063 | } |
| 4064 | if (!checkTxnObj(txnobj, &txn)) { |
| 4065 | return NULL; |
| 4066 | } |
| 4067 | CHECK_ENV_NOT_CLOSED(self); |
| 4068 | MYDB_BEGIN_ALLOW_THREADS; |
| 4069 | err = self->db_env->dbremove(self->db_env, txn, file, database, flags); |
| 4070 | MYDB_END_ALLOW_THREADS; |
| 4071 | RETURN_IF_ERR(); |
| 4072 | RETURN_NONE(); |
| 4073 | } |
| 4074 | |
| 4075 | static PyObject* |
| 4076 | DBEnv_dbrename(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 4077 | { |
| 4078 | int err; |
| 4079 | u_int32_t flags=0; |
| 4080 | char *file = NULL; |
| 4081 | char *database = NULL; |
| 4082 | char *newname = NULL; |
| 4083 | PyObject *txnobj = NULL; |
| 4084 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4085 | static char* kwnames[] = { "file", "database", "newname", "txn", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4086 | "flags", NULL }; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 4087 | |
Gregory P. Smith | 641cddf | 2006-07-28 01:35:25 +0000 | [diff] [blame] | 4088 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "szs|Oi:dbrename", kwnames, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 4089 | &file, &database, &newname, &txnobj, &flags)) { |
| 4090 | return NULL; |
| 4091 | } |
| 4092 | if (!checkTxnObj(txnobj, &txn)) { |
| 4093 | return NULL; |
| 4094 | } |
| 4095 | CHECK_ENV_NOT_CLOSED(self); |
| 4096 | MYDB_BEGIN_ALLOW_THREADS; |
| 4097 | err = self->db_env->dbrename(self->db_env, txn, file, database, newname, |
| 4098 | flags); |
| 4099 | MYDB_END_ALLOW_THREADS; |
| 4100 | RETURN_IF_ERR(); |
| 4101 | RETURN_NONE(); |
| 4102 | } |
| 4103 | |
| 4104 | static PyObject* |
| 4105 | DBEnv_set_encrypt(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 4106 | { |
| 4107 | int err; |
| 4108 | u_int32_t flags=0; |
| 4109 | char *passwd = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4110 | static char* kwnames[] = { "passwd", "flags", NULL }; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 4111 | |
| 4112 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|i:set_encrypt", kwnames, |
| 4113 | &passwd, &flags)) { |
| 4114 | return NULL; |
| 4115 | } |
| 4116 | |
| 4117 | MYDB_BEGIN_ALLOW_THREADS; |
| 4118 | err = self->db_env->set_encrypt(self->db_env, passwd, flags); |
| 4119 | MYDB_END_ALLOW_THREADS; |
| 4120 | |
| 4121 | RETURN_IF_ERR(); |
| 4122 | RETURN_NONE(); |
| 4123 | } |
| 4124 | #endif /* DBVER >= 41 */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4125 | |
Gregory P. Smith | fe11d3e | 2003-03-27 17:23:29 +0000 | [diff] [blame] | 4126 | static PyObject* |
| 4127 | DBEnv_set_timeout(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 4128 | { |
| 4129 | int err; |
| 4130 | u_int32_t flags=0; |
| 4131 | u_int32_t timeout = 0; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4132 | static char* kwnames[] = { "timeout", "flags", NULL }; |
Gregory P. Smith | fe11d3e | 2003-03-27 17:23:29 +0000 | [diff] [blame] | 4133 | |
| 4134 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii:set_timeout", kwnames, |
| 4135 | &timeout, &flags)) { |
| 4136 | return NULL; |
| 4137 | } |
| 4138 | |
| 4139 | MYDB_BEGIN_ALLOW_THREADS; |
| 4140 | err = self->db_env->set_timeout(self->db_env, (db_timeout_t)timeout, flags); |
| 4141 | MYDB_END_ALLOW_THREADS; |
| 4142 | |
| 4143 | RETURN_IF_ERR(); |
| 4144 | RETURN_NONE(); |
| 4145 | } |
Gregory P. Smith | fe11d3e | 2003-03-27 17:23:29 +0000 | [diff] [blame] | 4146 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4147 | static PyObject* |
Gregory P. Smith | 6676f6e | 2003-08-28 21:50:30 +0000 | [diff] [blame] | 4148 | DBEnv_set_shm_key(DBEnvObject* self, PyObject* args) |
| 4149 | { |
| 4150 | int err; |
| 4151 | long shm_key = 0; |
| 4152 | |
| 4153 | if (!PyArg_ParseTuple(args, "l:set_shm_key", &shm_key)) |
| 4154 | return NULL; |
| 4155 | CHECK_ENV_NOT_CLOSED(self); |
| 4156 | |
| 4157 | err = self->db_env->set_shm_key(self->db_env, shm_key); |
| 4158 | RETURN_IF_ERR(); |
| 4159 | RETURN_NONE(); |
| 4160 | } |
| 4161 | |
| 4162 | static PyObject* |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4163 | DBEnv_set_cachesize(DBEnvObject* self, PyObject* args) |
| 4164 | { |
| 4165 | int err, gbytes=0, bytes=0, ncache=0; |
| 4166 | |
| 4167 | if (!PyArg_ParseTuple(args, "ii|i:set_cachesize", |
| 4168 | &gbytes, &bytes, &ncache)) |
| 4169 | return NULL; |
| 4170 | CHECK_ENV_NOT_CLOSED(self); |
| 4171 | |
| 4172 | MYDB_BEGIN_ALLOW_THREADS; |
| 4173 | err = self->db_env->set_cachesize(self->db_env, gbytes, bytes, ncache); |
| 4174 | MYDB_END_ALLOW_THREADS; |
| 4175 | RETURN_IF_ERR(); |
| 4176 | RETURN_NONE(); |
| 4177 | } |
| 4178 | |
| 4179 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4180 | static PyObject* |
| 4181 | DBEnv_set_flags(DBEnvObject* self, PyObject* args) |
| 4182 | { |
| 4183 | int err, flags=0, onoff=0; |
| 4184 | |
| 4185 | if (!PyArg_ParseTuple(args, "ii:set_flags", |
| 4186 | &flags, &onoff)) |
| 4187 | return NULL; |
| 4188 | CHECK_ENV_NOT_CLOSED(self); |
| 4189 | |
| 4190 | MYDB_BEGIN_ALLOW_THREADS; |
| 4191 | err = self->db_env->set_flags(self->db_env, flags, onoff); |
| 4192 | MYDB_END_ALLOW_THREADS; |
| 4193 | RETURN_IF_ERR(); |
| 4194 | RETURN_NONE(); |
| 4195 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4196 | |
| 4197 | |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 4198 | #if (DBVER >= 47) |
| 4199 | static PyObject* |
| 4200 | DBEnv_log_set_config(DBEnvObject* self, PyObject* args) |
| 4201 | { |
| 4202 | int err, flags, onoff; |
| 4203 | |
| 4204 | if (!PyArg_ParseTuple(args, "ii:log_set_config", |
| 4205 | &flags, &onoff)) |
| 4206 | return NULL; |
| 4207 | CHECK_ENV_NOT_CLOSED(self); |
| 4208 | |
| 4209 | MYDB_BEGIN_ALLOW_THREADS; |
| 4210 | err = self->db_env->log_set_config(self->db_env, flags, onoff); |
| 4211 | MYDB_END_ALLOW_THREADS; |
| 4212 | RETURN_IF_ERR(); |
| 4213 | RETURN_NONE(); |
| 4214 | } |
| 4215 | #endif /* DBVER >= 47 */ |
| 4216 | |
| 4217 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4218 | static PyObject* |
| 4219 | DBEnv_set_data_dir(DBEnvObject* self, PyObject* args) |
| 4220 | { |
| 4221 | int err; |
| 4222 | char *dir; |
| 4223 | |
| 4224 | if (!PyArg_ParseTuple(args, "s:set_data_dir", &dir)) |
| 4225 | return NULL; |
| 4226 | CHECK_ENV_NOT_CLOSED(self); |
| 4227 | |
| 4228 | MYDB_BEGIN_ALLOW_THREADS; |
| 4229 | err = self->db_env->set_data_dir(self->db_env, dir); |
| 4230 | MYDB_END_ALLOW_THREADS; |
| 4231 | RETURN_IF_ERR(); |
| 4232 | RETURN_NONE(); |
| 4233 | } |
| 4234 | |
| 4235 | |
| 4236 | static PyObject* |
| 4237 | DBEnv_set_lg_bsize(DBEnvObject* self, PyObject* args) |
| 4238 | { |
| 4239 | int err, lg_bsize; |
| 4240 | |
| 4241 | if (!PyArg_ParseTuple(args, "i:set_lg_bsize", &lg_bsize)) |
| 4242 | return NULL; |
| 4243 | CHECK_ENV_NOT_CLOSED(self); |
| 4244 | |
| 4245 | MYDB_BEGIN_ALLOW_THREADS; |
| 4246 | err = self->db_env->set_lg_bsize(self->db_env, lg_bsize); |
| 4247 | MYDB_END_ALLOW_THREADS; |
| 4248 | RETURN_IF_ERR(); |
| 4249 | RETURN_NONE(); |
| 4250 | } |
| 4251 | |
| 4252 | |
| 4253 | static PyObject* |
| 4254 | DBEnv_set_lg_dir(DBEnvObject* self, PyObject* args) |
| 4255 | { |
| 4256 | int err; |
| 4257 | char *dir; |
| 4258 | |
| 4259 | if (!PyArg_ParseTuple(args, "s:set_lg_dir", &dir)) |
| 4260 | return NULL; |
| 4261 | CHECK_ENV_NOT_CLOSED(self); |
| 4262 | |
| 4263 | MYDB_BEGIN_ALLOW_THREADS; |
| 4264 | err = self->db_env->set_lg_dir(self->db_env, dir); |
| 4265 | MYDB_END_ALLOW_THREADS; |
| 4266 | RETURN_IF_ERR(); |
| 4267 | RETURN_NONE(); |
| 4268 | } |
| 4269 | |
| 4270 | static PyObject* |
| 4271 | DBEnv_set_lg_max(DBEnvObject* self, PyObject* args) |
| 4272 | { |
| 4273 | int err, lg_max; |
| 4274 | |
| 4275 | if (!PyArg_ParseTuple(args, "i:set_lg_max", &lg_max)) |
| 4276 | return NULL; |
| 4277 | CHECK_ENV_NOT_CLOSED(self); |
| 4278 | |
| 4279 | MYDB_BEGIN_ALLOW_THREADS; |
| 4280 | err = self->db_env->set_lg_max(self->db_env, lg_max); |
| 4281 | MYDB_END_ALLOW_THREADS; |
| 4282 | RETURN_IF_ERR(); |
| 4283 | RETURN_NONE(); |
| 4284 | } |
| 4285 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4286 | #if (DBVER >= 42) |
| 4287 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4288 | DBEnv_get_lg_max(DBEnvObject* self) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4289 | { |
| 4290 | int err; |
| 4291 | u_int32_t lg_max; |
| 4292 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4293 | CHECK_ENV_NOT_CLOSED(self); |
| 4294 | |
| 4295 | MYDB_BEGIN_ALLOW_THREADS; |
| 4296 | err = self->db_env->get_lg_max(self->db_env, &lg_max); |
| 4297 | MYDB_END_ALLOW_THREADS; |
| 4298 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4299 | return NUMBER_FromLong(lg_max); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4300 | } |
| 4301 | #endif |
| 4302 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4303 | |
| 4304 | static PyObject* |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 4305 | DBEnv_set_lg_regionmax(DBEnvObject* self, PyObject* args) |
| 4306 | { |
| 4307 | int err, lg_max; |
| 4308 | |
| 4309 | if (!PyArg_ParseTuple(args, "i:set_lg_regionmax", &lg_max)) |
| 4310 | return NULL; |
| 4311 | CHECK_ENV_NOT_CLOSED(self); |
| 4312 | |
| 4313 | MYDB_BEGIN_ALLOW_THREADS; |
| 4314 | err = self->db_env->set_lg_regionmax(self->db_env, lg_max); |
| 4315 | MYDB_END_ALLOW_THREADS; |
| 4316 | RETURN_IF_ERR(); |
| 4317 | RETURN_NONE(); |
| 4318 | } |
| 4319 | |
| 4320 | |
| 4321 | static PyObject* |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4322 | DBEnv_set_lk_detect(DBEnvObject* self, PyObject* args) |
| 4323 | { |
| 4324 | int err, lk_detect; |
| 4325 | |
| 4326 | if (!PyArg_ParseTuple(args, "i:set_lk_detect", &lk_detect)) |
| 4327 | return NULL; |
| 4328 | CHECK_ENV_NOT_CLOSED(self); |
| 4329 | |
| 4330 | MYDB_BEGIN_ALLOW_THREADS; |
| 4331 | err = self->db_env->set_lk_detect(self->db_env, lk_detect); |
| 4332 | MYDB_END_ALLOW_THREADS; |
| 4333 | RETURN_IF_ERR(); |
| 4334 | RETURN_NONE(); |
| 4335 | } |
| 4336 | |
| 4337 | |
Gregory P. Smith | 8b96a35 | 2007-01-05 01:59:42 +0000 | [diff] [blame] | 4338 | #if (DBVER < 45) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4339 | static PyObject* |
| 4340 | DBEnv_set_lk_max(DBEnvObject* self, PyObject* args) |
| 4341 | { |
| 4342 | int err, max; |
| 4343 | |
| 4344 | if (!PyArg_ParseTuple(args, "i:set_lk_max", &max)) |
| 4345 | return NULL; |
| 4346 | CHECK_ENV_NOT_CLOSED(self); |
| 4347 | |
| 4348 | MYDB_BEGIN_ALLOW_THREADS; |
| 4349 | err = self->db_env->set_lk_max(self->db_env, max); |
| 4350 | MYDB_END_ALLOW_THREADS; |
| 4351 | RETURN_IF_ERR(); |
| 4352 | RETURN_NONE(); |
| 4353 | } |
Gregory P. Smith | 8b96a35 | 2007-01-05 01:59:42 +0000 | [diff] [blame] | 4354 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4355 | |
| 4356 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4357 | |
| 4358 | static PyObject* |
| 4359 | DBEnv_set_lk_max_locks(DBEnvObject* self, PyObject* args) |
| 4360 | { |
| 4361 | int err, max; |
| 4362 | |
| 4363 | if (!PyArg_ParseTuple(args, "i:set_lk_max_locks", &max)) |
| 4364 | return NULL; |
| 4365 | CHECK_ENV_NOT_CLOSED(self); |
| 4366 | |
| 4367 | MYDB_BEGIN_ALLOW_THREADS; |
| 4368 | err = self->db_env->set_lk_max_locks(self->db_env, max); |
| 4369 | MYDB_END_ALLOW_THREADS; |
| 4370 | RETURN_IF_ERR(); |
| 4371 | RETURN_NONE(); |
| 4372 | } |
| 4373 | |
| 4374 | |
| 4375 | static PyObject* |
| 4376 | DBEnv_set_lk_max_lockers(DBEnvObject* self, PyObject* args) |
| 4377 | { |
| 4378 | int err, max; |
| 4379 | |
| 4380 | if (!PyArg_ParseTuple(args, "i:set_lk_max_lockers", &max)) |
| 4381 | return NULL; |
| 4382 | CHECK_ENV_NOT_CLOSED(self); |
| 4383 | |
| 4384 | MYDB_BEGIN_ALLOW_THREADS; |
| 4385 | err = self->db_env->set_lk_max_lockers(self->db_env, max); |
| 4386 | MYDB_END_ALLOW_THREADS; |
| 4387 | RETURN_IF_ERR(); |
| 4388 | RETURN_NONE(); |
| 4389 | } |
| 4390 | |
| 4391 | |
| 4392 | static PyObject* |
| 4393 | DBEnv_set_lk_max_objects(DBEnvObject* self, PyObject* args) |
| 4394 | { |
| 4395 | int err, max; |
| 4396 | |
| 4397 | if (!PyArg_ParseTuple(args, "i:set_lk_max_objects", &max)) |
| 4398 | return NULL; |
| 4399 | CHECK_ENV_NOT_CLOSED(self); |
| 4400 | |
| 4401 | MYDB_BEGIN_ALLOW_THREADS; |
| 4402 | err = self->db_env->set_lk_max_objects(self->db_env, max); |
| 4403 | MYDB_END_ALLOW_THREADS; |
| 4404 | RETURN_IF_ERR(); |
| 4405 | RETURN_NONE(); |
| 4406 | } |
| 4407 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4408 | |
| 4409 | static PyObject* |
| 4410 | DBEnv_set_mp_mmapsize(DBEnvObject* self, PyObject* args) |
| 4411 | { |
| 4412 | int err, mp_mmapsize; |
| 4413 | |
| 4414 | if (!PyArg_ParseTuple(args, "i:set_mp_mmapsize", &mp_mmapsize)) |
| 4415 | return NULL; |
| 4416 | CHECK_ENV_NOT_CLOSED(self); |
| 4417 | |
| 4418 | MYDB_BEGIN_ALLOW_THREADS; |
| 4419 | err = self->db_env->set_mp_mmapsize(self->db_env, mp_mmapsize); |
| 4420 | MYDB_END_ALLOW_THREADS; |
| 4421 | RETURN_IF_ERR(); |
| 4422 | RETURN_NONE(); |
| 4423 | } |
| 4424 | |
| 4425 | |
| 4426 | static PyObject* |
| 4427 | DBEnv_set_tmp_dir(DBEnvObject* self, PyObject* args) |
| 4428 | { |
| 4429 | int err; |
| 4430 | char *dir; |
| 4431 | |
| 4432 | if (!PyArg_ParseTuple(args, "s:set_tmp_dir", &dir)) |
| 4433 | return NULL; |
| 4434 | CHECK_ENV_NOT_CLOSED(self); |
| 4435 | |
| 4436 | MYDB_BEGIN_ALLOW_THREADS; |
| 4437 | err = self->db_env->set_tmp_dir(self->db_env, dir); |
| 4438 | MYDB_END_ALLOW_THREADS; |
| 4439 | RETURN_IF_ERR(); |
| 4440 | RETURN_NONE(); |
| 4441 | } |
| 4442 | |
| 4443 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4444 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4445 | DBEnv_txn_recover(DBEnvObject* self) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4446 | { |
| 4447 | int flags = DB_FIRST; |
| 4448 | int err, i; |
| 4449 | PyObject *list, *tuple, *gid; |
| 4450 | DBTxnObject *txn; |
| 4451 | #define PREPLIST_LEN 16 |
| 4452 | DB_PREPLIST preplist[PREPLIST_LEN]; |
| 4453 | long retp; |
| 4454 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4455 | CHECK_ENV_NOT_CLOSED(self); |
| 4456 | |
| 4457 | list=PyList_New(0); |
| 4458 | if (!list) |
| 4459 | return NULL; |
| 4460 | while (!0) { |
| 4461 | MYDB_BEGIN_ALLOW_THREADS |
| 4462 | err=self->db_env->txn_recover(self->db_env, |
| 4463 | preplist, PREPLIST_LEN, &retp, flags); |
| 4464 | #undef PREPLIST_LEN |
| 4465 | MYDB_END_ALLOW_THREADS |
| 4466 | if (err) { |
| 4467 | Py_DECREF(list); |
| 4468 | RETURN_IF_ERR(); |
| 4469 | } |
| 4470 | if (!retp) break; |
| 4471 | flags=DB_NEXT; /* Prepare for next loop pass */ |
| 4472 | for (i=0; i<retp; i++) { |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 4473 | gid=PyBytes_FromStringAndSize((char *)(preplist[i].gid), |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4474 | DB_XIDDATASIZE); |
| 4475 | if (!gid) { |
| 4476 | Py_DECREF(list); |
| 4477 | return NULL; |
| 4478 | } |
| 4479 | txn=newDBTxnObject(self, NULL, preplist[i].txn, flags); |
| 4480 | if (!txn) { |
| 4481 | Py_DECREF(list); |
| 4482 | Py_DECREF(gid); |
| 4483 | return NULL; |
| 4484 | } |
| 4485 | txn->flag_prepare=1; /* Recover state */ |
| 4486 | tuple=PyTuple_New(2); |
| 4487 | if (!tuple) { |
| 4488 | Py_DECREF(list); |
| 4489 | Py_DECREF(gid); |
| 4490 | Py_DECREF(txn); |
| 4491 | return NULL; |
| 4492 | } |
| 4493 | if (PyTuple_SetItem(tuple, 0, gid)) { |
| 4494 | Py_DECREF(list); |
| 4495 | Py_DECREF(gid); |
| 4496 | Py_DECREF(txn); |
| 4497 | Py_DECREF(tuple); |
| 4498 | return NULL; |
| 4499 | } |
| 4500 | if (PyTuple_SetItem(tuple, 1, (PyObject *)txn)) { |
| 4501 | Py_DECREF(list); |
| 4502 | Py_DECREF(txn); |
| 4503 | Py_DECREF(tuple); /* This delete the "gid" also */ |
| 4504 | return NULL; |
| 4505 | } |
| 4506 | if (PyList_Append(list, tuple)) { |
| 4507 | Py_DECREF(list); |
| 4508 | Py_DECREF(tuple);/* This delete the "gid" and the "txn" also */ |
| 4509 | return NULL; |
| 4510 | } |
| 4511 | Py_DECREF(tuple); |
| 4512 | } |
| 4513 | } |
| 4514 | return list; |
| 4515 | } |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4516 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4517 | static PyObject* |
| 4518 | DBEnv_txn_begin(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 4519 | { |
| 4520 | int flags = 0; |
| 4521 | PyObject* txnobj = NULL; |
| 4522 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4523 | static char* kwnames[] = { "parent", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4524 | |
| 4525 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:txn_begin", kwnames, |
| 4526 | &txnobj, &flags)) |
| 4527 | return NULL; |
| 4528 | |
| 4529 | if (!checkTxnObj(txnobj, &txn)) |
| 4530 | return NULL; |
| 4531 | CHECK_ENV_NOT_CLOSED(self); |
| 4532 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4533 | return (PyObject*)newDBTxnObject(self, (DBTxnObject *)txnobj, NULL, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
| 4536 | |
| 4537 | static PyObject* |
| 4538 | DBEnv_txn_checkpoint(DBEnvObject* self, PyObject* args) |
| 4539 | { |
| 4540 | int err, kbyte=0, min=0, flags=0; |
| 4541 | |
| 4542 | if (!PyArg_ParseTuple(args, "|iii:txn_checkpoint", &kbyte, &min, &flags)) |
| 4543 | return NULL; |
| 4544 | CHECK_ENV_NOT_CLOSED(self); |
| 4545 | |
| 4546 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4547 | err = self->db_env->txn_checkpoint(self->db_env, kbyte, min, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4548 | MYDB_END_ALLOW_THREADS; |
| 4549 | RETURN_IF_ERR(); |
| 4550 | RETURN_NONE(); |
| 4551 | } |
| 4552 | |
| 4553 | |
| 4554 | static PyObject* |
| 4555 | DBEnv_set_tx_max(DBEnvObject* self, PyObject* args) |
| 4556 | { |
| 4557 | int err, max; |
| 4558 | |
| 4559 | if (!PyArg_ParseTuple(args, "i:set_tx_max", &max)) |
| 4560 | return NULL; |
| 4561 | CHECK_ENV_NOT_CLOSED(self); |
| 4562 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4563 | err = self->db_env->set_tx_max(self->db_env, max); |
Gregory P. Smith | 8a47404 | 2006-01-27 07:05:40 +0000 | [diff] [blame] | 4564 | RETURN_IF_ERR(); |
| 4565 | RETURN_NONE(); |
| 4566 | } |
| 4567 | |
| 4568 | |
| 4569 | static PyObject* |
| 4570 | DBEnv_set_tx_timestamp(DBEnvObject* self, PyObject* args) |
| 4571 | { |
| 4572 | int err; |
Thomas Wouters | 9d63cca | 2006-03-01 01:01:55 +0000 | [diff] [blame] | 4573 | long stamp; |
| 4574 | time_t timestamp; |
Gregory P. Smith | 8a47404 | 2006-01-27 07:05:40 +0000 | [diff] [blame] | 4575 | |
Thomas Wouters | 9d63cca | 2006-03-01 01:01:55 +0000 | [diff] [blame] | 4576 | if (!PyArg_ParseTuple(args, "l:set_tx_timestamp", &stamp)) |
Gregory P. Smith | 8a47404 | 2006-01-27 07:05:40 +0000 | [diff] [blame] | 4577 | return NULL; |
| 4578 | CHECK_ENV_NOT_CLOSED(self); |
Thomas Wouters | 9d63cca | 2006-03-01 01:01:55 +0000 | [diff] [blame] | 4579 | timestamp = (time_t)stamp; |
| 4580 | err = self->db_env->set_tx_timestamp(self->db_env, ×tamp); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4581 | RETURN_IF_ERR(); |
| 4582 | RETURN_NONE(); |
| 4583 | } |
| 4584 | |
| 4585 | |
| 4586 | static PyObject* |
| 4587 | DBEnv_lock_detect(DBEnvObject* self, PyObject* args) |
| 4588 | { |
| 4589 | int err, atype, flags=0; |
| 4590 | int aborted = 0; |
| 4591 | |
| 4592 | if (!PyArg_ParseTuple(args, "i|i:lock_detect", &atype, &flags)) |
| 4593 | return NULL; |
| 4594 | CHECK_ENV_NOT_CLOSED(self); |
| 4595 | |
| 4596 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4597 | err = self->db_env->lock_detect(self->db_env, flags, atype, &aborted); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4598 | MYDB_END_ALLOW_THREADS; |
| 4599 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4600 | return NUMBER_FromLong(aborted); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
| 4603 | |
| 4604 | static PyObject* |
| 4605 | DBEnv_lock_get(DBEnvObject* self, PyObject* args) |
| 4606 | { |
| 4607 | int flags=0; |
| 4608 | int locker, lock_mode; |
| 4609 | DBT obj; |
| 4610 | PyObject* objobj; |
| 4611 | |
| 4612 | if (!PyArg_ParseTuple(args, "iOi|i:lock_get", &locker, &objobj, &lock_mode, &flags)) |
| 4613 | return NULL; |
| 4614 | |
| 4615 | |
| 4616 | if (!make_dbt(objobj, &obj)) |
| 4617 | return NULL; |
| 4618 | |
| 4619 | return (PyObject*)newDBLockObject(self, locker, &obj, lock_mode, flags); |
| 4620 | } |
| 4621 | |
| 4622 | |
| 4623 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4624 | DBEnv_lock_id(DBEnvObject* self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4625 | { |
| 4626 | int err; |
| 4627 | u_int32_t theID; |
| 4628 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4629 | CHECK_ENV_NOT_CLOSED(self); |
| 4630 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4631 | err = self->db_env->lock_id(self->db_env, &theID); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4632 | MYDB_END_ALLOW_THREADS; |
| 4633 | RETURN_IF_ERR(); |
| 4634 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4635 | return NUMBER_FromLong((long)theID); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4636 | } |
| 4637 | |
Gregory P. Smith | ac11e02 | 2007-11-05 02:56:31 +0000 | [diff] [blame] | 4638 | static PyObject* |
| 4639 | DBEnv_lock_id_free(DBEnvObject* self, PyObject* args) |
| 4640 | { |
| 4641 | int err; |
| 4642 | u_int32_t theID; |
| 4643 | |
| 4644 | if (!PyArg_ParseTuple(args, "I:lock_id_free", &theID)) |
| 4645 | return NULL; |
| 4646 | |
| 4647 | CHECK_ENV_NOT_CLOSED(self); |
| 4648 | MYDB_BEGIN_ALLOW_THREADS; |
| 4649 | err = self->db_env->lock_id_free(self->db_env, theID); |
| 4650 | MYDB_END_ALLOW_THREADS; |
| 4651 | RETURN_IF_ERR(); |
| 4652 | RETURN_NONE(); |
| 4653 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4654 | |
| 4655 | static PyObject* |
| 4656 | DBEnv_lock_put(DBEnvObject* self, PyObject* args) |
| 4657 | { |
| 4658 | int err; |
| 4659 | DBLockObject* dblockobj; |
| 4660 | |
| 4661 | if (!PyArg_ParseTuple(args, "O!:lock_put", &DBLock_Type, &dblockobj)) |
| 4662 | return NULL; |
| 4663 | |
| 4664 | CHECK_ENV_NOT_CLOSED(self); |
| 4665 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4666 | err = self->db_env->lock_put(self->db_env, &dblockobj->lock); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4667 | MYDB_END_ALLOW_THREADS; |
| 4668 | RETURN_IF_ERR(); |
| 4669 | RETURN_NONE(); |
| 4670 | } |
| 4671 | |
Gregory P. Smith | db8a807 | 2006-06-05 01:56:15 +0000 | [diff] [blame] | 4672 | #if (DBVER >= 44) |
| 4673 | static PyObject* |
| 4674 | DBEnv_lsn_reset(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 4675 | { |
| 4676 | int err; |
| 4677 | char *file; |
| 4678 | u_int32_t flags = 0; |
| 4679 | static char* kwnames[] = { "file", "flags", NULL}; |
| 4680 | |
| 4681 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "z|i:lsn_reset", kwnames, |
| 4682 | &file, &flags)) |
| 4683 | return NULL; |
| 4684 | CHECK_ENV_NOT_CLOSED(self); |
| 4685 | |
| 4686 | MYDB_BEGIN_ALLOW_THREADS; |
| 4687 | err = self->db_env->lsn_reset(self->db_env, file, flags); |
| 4688 | MYDB_END_ALLOW_THREADS; |
| 4689 | RETURN_IF_ERR(); |
| 4690 | RETURN_NONE(); |
| 4691 | } |
| 4692 | #endif /* DBVER >= 4.4 */ |
| 4693 | |
Gregory P. Smith | 76a82e8 | 2006-06-05 01:39:52 +0000 | [diff] [blame] | 4694 | static PyObject* |
| 4695 | DBEnv_log_stat(DBEnvObject* self, PyObject* args) |
| 4696 | { |
| 4697 | int err; |
| 4698 | DB_LOG_STAT* statp = NULL; |
| 4699 | PyObject* d = NULL; |
| 4700 | u_int32_t flags = 0; |
| 4701 | |
| 4702 | if (!PyArg_ParseTuple(args, "|i:log_stat", &flags)) |
| 4703 | return NULL; |
| 4704 | CHECK_ENV_NOT_CLOSED(self); |
| 4705 | |
| 4706 | MYDB_BEGIN_ALLOW_THREADS; |
| 4707 | err = self->db_env->log_stat(self->db_env, &statp, flags); |
| 4708 | MYDB_END_ALLOW_THREADS; |
| 4709 | RETURN_IF_ERR(); |
| 4710 | |
| 4711 | /* Turn the stat structure into a dictionary */ |
| 4712 | d = PyDict_New(); |
| 4713 | if (d == NULL) { |
| 4714 | if (statp) |
| 4715 | free(statp); |
| 4716 | return NULL; |
| 4717 | } |
| 4718 | |
| 4719 | #define MAKE_ENTRY(name) _addIntToDict(d, #name, statp->st_##name) |
| 4720 | |
| 4721 | MAKE_ENTRY(magic); |
| 4722 | MAKE_ENTRY(version); |
| 4723 | MAKE_ENTRY(mode); |
| 4724 | MAKE_ENTRY(lg_bsize); |
| 4725 | #if (DBVER >= 44) |
| 4726 | MAKE_ENTRY(lg_size); |
| 4727 | MAKE_ENTRY(record); |
| 4728 | #endif |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4729 | #if (DBVER < 41) |
Gregory P. Smith | 76a82e8 | 2006-06-05 01:39:52 +0000 | [diff] [blame] | 4730 | MAKE_ENTRY(lg_max); |
| 4731 | #endif |
| 4732 | MAKE_ENTRY(w_mbytes); |
| 4733 | MAKE_ENTRY(w_bytes); |
| 4734 | MAKE_ENTRY(wc_mbytes); |
| 4735 | MAKE_ENTRY(wc_bytes); |
| 4736 | MAKE_ENTRY(wcount); |
| 4737 | MAKE_ENTRY(wcount_fill); |
| 4738 | #if (DBVER >= 44) |
| 4739 | MAKE_ENTRY(rcount); |
| 4740 | #endif |
| 4741 | MAKE_ENTRY(scount); |
| 4742 | MAKE_ENTRY(cur_file); |
| 4743 | MAKE_ENTRY(cur_offset); |
| 4744 | MAKE_ENTRY(disk_file); |
| 4745 | MAKE_ENTRY(disk_offset); |
| 4746 | MAKE_ENTRY(maxcommitperflush); |
| 4747 | MAKE_ENTRY(mincommitperflush); |
| 4748 | MAKE_ENTRY(regsize); |
| 4749 | MAKE_ENTRY(region_wait); |
| 4750 | MAKE_ENTRY(region_nowait); |
| 4751 | |
| 4752 | #undef MAKE_ENTRY |
| 4753 | free(statp); |
| 4754 | return d; |
| 4755 | } /* DBEnv_log_stat */ |
Gregory P. Smith | 76a82e8 | 2006-06-05 01:39:52 +0000 | [diff] [blame] | 4756 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4757 | |
| 4758 | static PyObject* |
| 4759 | DBEnv_lock_stat(DBEnvObject* self, PyObject* args) |
| 4760 | { |
| 4761 | int err; |
| 4762 | DB_LOCK_STAT* sp; |
| 4763 | PyObject* d = NULL; |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 4764 | u_int32_t flags = 0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4765 | |
| 4766 | if (!PyArg_ParseTuple(args, "|i:lock_stat", &flags)) |
| 4767 | return NULL; |
| 4768 | CHECK_ENV_NOT_CLOSED(self); |
| 4769 | |
| 4770 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4771 | err = self->db_env->lock_stat(self->db_env, &sp, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4772 | MYDB_END_ALLOW_THREADS; |
| 4773 | RETURN_IF_ERR(); |
| 4774 | |
| 4775 | /* Turn the stat structure into a dictionary */ |
| 4776 | d = PyDict_New(); |
| 4777 | if (d == NULL) { |
| 4778 | free(sp); |
| 4779 | return NULL; |
| 4780 | } |
| 4781 | |
| 4782 | #define MAKE_ENTRY(name) _addIntToDict(d, #name, sp->st_##name) |
| 4783 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 4784 | #if (DBVER < 41) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4785 | MAKE_ENTRY(lastid); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 4786 | #endif |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4787 | #if (DBVER >=41) |
| 4788 | MAKE_ENTRY(id); |
| 4789 | MAKE_ENTRY(cur_maxid); |
| 4790 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4791 | MAKE_ENTRY(nmodes); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4792 | MAKE_ENTRY(maxlocks); |
| 4793 | MAKE_ENTRY(maxlockers); |
| 4794 | MAKE_ENTRY(maxobjects); |
| 4795 | MAKE_ENTRY(nlocks); |
| 4796 | MAKE_ENTRY(maxnlocks); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4797 | MAKE_ENTRY(nlockers); |
| 4798 | MAKE_ENTRY(maxnlockers); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4799 | MAKE_ENTRY(nobjects); |
| 4800 | MAKE_ENTRY(maxnobjects); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4801 | MAKE_ENTRY(nrequests); |
| 4802 | MAKE_ENTRY(nreleases); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4803 | #if (DBVER >= 44) |
| 4804 | MAKE_ENTRY(nupgrade); |
| 4805 | MAKE_ENTRY(ndowngrade); |
| 4806 | #endif |
Gregory P. Smith | 29602d2 | 2006-01-24 09:46:48 +0000 | [diff] [blame] | 4807 | #if (DBVER < 44) |
| 4808 | MAKE_ENTRY(nnowaits); /* these were renamed in 4.4 */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4809 | MAKE_ENTRY(nconflicts); |
Gregory P. Smith | 29602d2 | 2006-01-24 09:46:48 +0000 | [diff] [blame] | 4810 | #else |
| 4811 | MAKE_ENTRY(lock_nowait); |
| 4812 | MAKE_ENTRY(lock_wait); |
| 4813 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4814 | MAKE_ENTRY(ndeadlocks); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4815 | #if (DBVER >= 41) |
| 4816 | MAKE_ENTRY(locktimeout); |
| 4817 | MAKE_ENTRY(txntimeout); |
| 4818 | #endif |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4819 | MAKE_ENTRY(nlocktimeouts); |
| 4820 | MAKE_ENTRY(ntxntimeouts); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4821 | #if (DBVER >= 46) |
| 4822 | MAKE_ENTRY(objs_wait); |
| 4823 | MAKE_ENTRY(objs_nowait); |
| 4824 | MAKE_ENTRY(lockers_wait); |
| 4825 | MAKE_ENTRY(lockers_nowait); |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 4826 | #if (DBVER >= 47) |
| 4827 | MAKE_ENTRY(lock_wait); |
| 4828 | MAKE_ENTRY(lock_nowait); |
| 4829 | #else |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4830 | MAKE_ENTRY(locks_wait); |
| 4831 | MAKE_ENTRY(locks_nowait); |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 4832 | #endif |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4833 | MAKE_ENTRY(hash_len); |
| 4834 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4835 | MAKE_ENTRY(regsize); |
| 4836 | MAKE_ENTRY(region_wait); |
| 4837 | MAKE_ENTRY(region_nowait); |
| 4838 | |
| 4839 | #undef MAKE_ENTRY |
| 4840 | free(sp); |
| 4841 | return d; |
| 4842 | } |
| 4843 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4844 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4845 | DBEnv_log_flush(DBEnvObject* self) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4846 | { |
| 4847 | int err; |
| 4848 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4849 | CHECK_ENV_NOT_CLOSED(self); |
| 4850 | |
| 4851 | MYDB_BEGIN_ALLOW_THREADS |
| 4852 | err = self->db_env->log_flush(self->db_env, NULL); |
| 4853 | MYDB_END_ALLOW_THREADS |
| 4854 | |
| 4855 | RETURN_IF_ERR(); |
| 4856 | RETURN_NONE(); |
| 4857 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4858 | |
| 4859 | static PyObject* |
| 4860 | DBEnv_log_archive(DBEnvObject* self, PyObject* args) |
| 4861 | { |
| 4862 | int flags=0; |
| 4863 | int err; |
Gregory P. Smith | 3dd2002 | 2006-06-05 00:31:01 +0000 | [diff] [blame] | 4864 | char **log_list = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4865 | PyObject* list; |
| 4866 | PyObject* item = NULL; |
| 4867 | |
| 4868 | if (!PyArg_ParseTuple(args, "|i:log_archive", &flags)) |
| 4869 | return NULL; |
| 4870 | |
| 4871 | CHECK_ENV_NOT_CLOSED(self); |
| 4872 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4873 | err = self->db_env->log_archive(self->db_env, &log_list, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4874 | MYDB_END_ALLOW_THREADS; |
| 4875 | RETURN_IF_ERR(); |
| 4876 | |
Gregory P. Smith | bad4745 | 2006-06-05 00:33:35 +0000 | [diff] [blame] | 4877 | list = PyList_New(0); |
| 4878 | if (list == NULL) { |
| 4879 | if (log_list) |
| 4880 | free(log_list); |
| 4881 | return NULL; |
| 4882 | } |
| 4883 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4884 | if (log_list) { |
Gregory P. Smith | 3dd2002 | 2006-06-05 00:31:01 +0000 | [diff] [blame] | 4885 | char **log_list_start; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4886 | for (log_list_start = log_list; *log_list != NULL; ++log_list) { |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 4887 | item = PyBytes_FromString (*log_list); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4888 | if (item == NULL) { |
| 4889 | Py_DECREF(list); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4890 | list = NULL; |
| 4891 | break; |
| 4892 | } |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4893 | if (PyList_Append(list, item)) { |
| 4894 | Py_DECREF(list); |
| 4895 | list = NULL; |
| 4896 | Py_DECREF(item); |
| 4897 | break; |
| 4898 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4899 | Py_DECREF(item); |
| 4900 | } |
| 4901 | free(log_list_start); |
| 4902 | } |
| 4903 | return list; |
| 4904 | } |
| 4905 | |
| 4906 | |
| 4907 | static PyObject* |
| 4908 | DBEnv_txn_stat(DBEnvObject* self, PyObject* args) |
| 4909 | { |
| 4910 | int err; |
| 4911 | DB_TXN_STAT* sp; |
| 4912 | PyObject* d = NULL; |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 4913 | u_int32_t flags=0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4914 | |
| 4915 | if (!PyArg_ParseTuple(args, "|i:txn_stat", &flags)) |
| 4916 | return NULL; |
| 4917 | CHECK_ENV_NOT_CLOSED(self); |
| 4918 | |
| 4919 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4920 | err = self->db_env->txn_stat(self->db_env, &sp, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4921 | MYDB_END_ALLOW_THREADS; |
| 4922 | RETURN_IF_ERR(); |
| 4923 | |
| 4924 | /* Turn the stat structure into a dictionary */ |
| 4925 | d = PyDict_New(); |
| 4926 | if (d == NULL) { |
| 4927 | free(sp); |
| 4928 | return NULL; |
| 4929 | } |
| 4930 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4931 | #define MAKE_ENTRY(name) _addIntToDict(d, #name, sp->st_##name) |
| 4932 | #define MAKE_TIME_T_ENTRY(name) _addTimeTToDict(d, #name, sp->st_##name) |
| 4933 | #define MAKE_DB_LSN_ENTRY(name) _addDB_lsnToDict(d, #name, sp->st_##name) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4934 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4935 | MAKE_DB_LSN_ENTRY(last_ckp); |
Kristján Valur Jónsson | bd77c03 | 2007-04-26 15:24:54 +0000 | [diff] [blame] | 4936 | MAKE_TIME_T_ENTRY(time_ckp); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4937 | MAKE_ENTRY(last_txnid); |
| 4938 | MAKE_ENTRY(maxtxns); |
| 4939 | MAKE_ENTRY(nactive); |
| 4940 | MAKE_ENTRY(maxnactive); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4941 | #if (DBVER >= 45) |
| 4942 | MAKE_ENTRY(nsnapshot); |
| 4943 | MAKE_ENTRY(maxnsnapshot); |
| 4944 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4945 | MAKE_ENTRY(nbegins); |
| 4946 | MAKE_ENTRY(naborts); |
| 4947 | MAKE_ENTRY(ncommits); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4948 | MAKE_ENTRY(nrestores); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4949 | MAKE_ENTRY(regsize); |
| 4950 | MAKE_ENTRY(region_wait); |
| 4951 | MAKE_ENTRY(region_nowait); |
| 4952 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4953 | #undef MAKE_DB_LSN_ENTRY |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4954 | #undef MAKE_ENTRY |
Kristján Valur Jónsson | bd77c03 | 2007-04-26 15:24:54 +0000 | [diff] [blame] | 4955 | #undef MAKE_TIME_T_ENTRY |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4956 | free(sp); |
| 4957 | return d; |
| 4958 | } |
| 4959 | |
| 4960 | |
| 4961 | static PyObject* |
| 4962 | DBEnv_set_get_returns_none(DBEnvObject* self, PyObject* args) |
| 4963 | { |
| 4964 | int flags=0; |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 4965 | int oldValue=0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4966 | |
| 4967 | if (!PyArg_ParseTuple(args,"i:set_get_returns_none", &flags)) |
| 4968 | return NULL; |
| 4969 | CHECK_ENV_NOT_CLOSED(self); |
| 4970 | |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 4971 | if (self->moduleFlags.getReturnsNone) |
| 4972 | ++oldValue; |
| 4973 | if (self->moduleFlags.cursorSetReturnsNone) |
| 4974 | ++oldValue; |
| 4975 | self->moduleFlags.getReturnsNone = (flags >= 1); |
| 4976 | self->moduleFlags.cursorSetReturnsNone = (flags >= 2); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4977 | return NUMBER_FromLong(oldValue); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4978 | } |
| 4979 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4980 | static PyObject* |
| 4981 | DBEnv_get_private(DBEnvObject* self) |
| 4982 | { |
| 4983 | /* We can give out the private field even if dbenv is closed */ |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 4984 | Py_INCREF(self->private_obj); |
| 4985 | return self->private_obj; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4986 | } |
| 4987 | |
| 4988 | static PyObject* |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 4989 | DBEnv_set_private(DBEnvObject* self, PyObject* private_obj) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4990 | { |
| 4991 | /* We can set the private field even if dbenv is closed */ |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 4992 | Py_DECREF(self->private_obj); |
| 4993 | Py_INCREF(private_obj); |
| 4994 | self->private_obj = private_obj; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4995 | RETURN_NONE(); |
| 4996 | } |
| 4997 | |
| 4998 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4999 | static PyObject* |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 5000 | DBEnv_set_rpc_server(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 5001 | { |
| 5002 | int err; |
| 5003 | char *host; |
| 5004 | long cl_timeout=0, sv_timeout=0; |
| 5005 | |
| 5006 | static char* kwnames[] = { "host", "cl_timeout", "sv_timeout", NULL}; |
| 5007 | |
| 5008 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ll:set_rpc_server", kwnames, |
| 5009 | &host, &cl_timeout, &sv_timeout)) |
| 5010 | return NULL; |
| 5011 | CHECK_ENV_NOT_CLOSED(self); |
| 5012 | |
| 5013 | MYDB_BEGIN_ALLOW_THREADS; |
| 5014 | err = self->db_env->set_rpc_server(self->db_env, NULL, host, cl_timeout, |
| 5015 | sv_timeout, 0); |
| 5016 | MYDB_END_ALLOW_THREADS; |
| 5017 | RETURN_IF_ERR(); |
| 5018 | RETURN_NONE(); |
| 5019 | } |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 5020 | |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 5021 | static PyObject* |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5022 | DBEnv_set_verbose(DBEnvObject* self, PyObject* args) |
| 5023 | { |
| 5024 | int err; |
| 5025 | int which, onoff; |
| 5026 | |
| 5027 | if (!PyArg_ParseTuple(args, "ii:set_verbose", &which, &onoff)) { |
| 5028 | return NULL; |
| 5029 | } |
| 5030 | CHECK_ENV_NOT_CLOSED(self); |
| 5031 | MYDB_BEGIN_ALLOW_THREADS; |
| 5032 | err = self->db_env->set_verbose(self->db_env, which, onoff); |
| 5033 | MYDB_END_ALLOW_THREADS; |
| 5034 | RETURN_IF_ERR(); |
| 5035 | RETURN_NONE(); |
| 5036 | } |
| 5037 | |
| 5038 | #if (DBVER >= 42) |
| 5039 | static PyObject* |
| 5040 | DBEnv_get_verbose(DBEnvObject* self, PyObject* args) |
| 5041 | { |
| 5042 | int err; |
| 5043 | int which; |
| 5044 | int verbose; |
| 5045 | |
| 5046 | if (!PyArg_ParseTuple(args, "i:get_verbose", &which)) { |
| 5047 | return NULL; |
| 5048 | } |
| 5049 | CHECK_ENV_NOT_CLOSED(self); |
| 5050 | MYDB_BEGIN_ALLOW_THREADS; |
| 5051 | err = self->db_env->get_verbose(self->db_env, which, &verbose); |
| 5052 | MYDB_END_ALLOW_THREADS; |
| 5053 | RETURN_IF_ERR(); |
| 5054 | return PyBool_FromLong(verbose); |
| 5055 | } |
| 5056 | #endif |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5057 | |
| 5058 | #if (DBVER >= 45) |
| 5059 | static void |
| 5060 | _dbenv_event_notifyCallback(DB_ENV* db_env, u_int32_t event, void *event_info) |
| 5061 | { |
| 5062 | DBEnvObject *dbenv; |
| 5063 | PyObject* callback; |
| 5064 | PyObject* args; |
| 5065 | PyObject* result = NULL; |
| 5066 | |
| 5067 | MYDB_BEGIN_BLOCK_THREADS; |
| 5068 | dbenv = (DBEnvObject *)db_env->app_private; |
| 5069 | callback = dbenv->event_notifyCallback; |
| 5070 | if (callback) { |
| 5071 | if (event == DB_EVENT_REP_NEWMASTER) { |
| 5072 | args = Py_BuildValue("(Oii)", dbenv, event, *((int *)event_info)); |
| 5073 | } else { |
| 5074 | args = Py_BuildValue("(OiO)", dbenv, event, Py_None); |
| 5075 | } |
| 5076 | if (args) { |
| 5077 | result = PyEval_CallObject(callback, args); |
| 5078 | } |
| 5079 | if ((!args) || (!result)) { |
| 5080 | PyErr_Print(); |
| 5081 | } |
| 5082 | Py_XDECREF(args); |
| 5083 | Py_XDECREF(result); |
| 5084 | } |
| 5085 | MYDB_END_BLOCK_THREADS; |
| 5086 | } |
| 5087 | #endif |
| 5088 | |
| 5089 | #if (DBVER >= 45) |
| 5090 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5091 | DBEnv_set_event_notify(DBEnvObject* self, PyObject* notifyFunc) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5092 | { |
| 5093 | int err; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5094 | |
| 5095 | CHECK_ENV_NOT_CLOSED(self); |
| 5096 | |
| 5097 | if (!PyCallable_Check(notifyFunc)) { |
| 5098 | makeTypeError("Callable", notifyFunc); |
| 5099 | return NULL; |
| 5100 | } |
| 5101 | |
| 5102 | Py_XDECREF(self->event_notifyCallback); |
| 5103 | Py_INCREF(notifyFunc); |
| 5104 | self->event_notifyCallback = notifyFunc; |
| 5105 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5106 | /* This is to workaround a problem with un-initialized threads (see |
| 5107 | comment in DB_associate) */ |
| 5108 | #ifdef WITH_THREAD |
| 5109 | PyEval_InitThreads(); |
| 5110 | #endif |
| 5111 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5112 | MYDB_BEGIN_ALLOW_THREADS; |
| 5113 | err = self->db_env->set_event_notify(self->db_env, _dbenv_event_notifyCallback); |
| 5114 | MYDB_END_ALLOW_THREADS; |
| 5115 | |
| 5116 | if (err) { |
| 5117 | Py_DECREF(notifyFunc); |
| 5118 | self->event_notifyCallback = NULL; |
| 5119 | } |
| 5120 | |
| 5121 | RETURN_IF_ERR(); |
| 5122 | RETURN_NONE(); |
| 5123 | } |
| 5124 | #endif |
| 5125 | |
| 5126 | |
| 5127 | /* --------------------------------------------------------------------- */ |
| 5128 | /* REPLICATION METHODS: Base Replication */ |
| 5129 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5130 | |
| 5131 | static PyObject* |
| 5132 | DBEnv_rep_process_message(DBEnvObject* self, PyObject* args) |
| 5133 | { |
| 5134 | int err; |
| 5135 | PyObject *control_py, *rec_py; |
| 5136 | DBT control, rec; |
| 5137 | int envid; |
| 5138 | #if (DBVER >= 42) |
| 5139 | DB_LSN lsn; |
| 5140 | #endif |
| 5141 | |
| 5142 | if (!PyArg_ParseTuple(args, "OOi:rep_process_message", &control_py, |
| 5143 | &rec_py, &envid)) |
| 5144 | return NULL; |
| 5145 | CHECK_ENV_NOT_CLOSED(self); |
| 5146 | |
| 5147 | if (!make_dbt(control_py, &control)) |
| 5148 | return NULL; |
| 5149 | if (!make_dbt(rec_py, &rec)) |
| 5150 | return NULL; |
| 5151 | |
| 5152 | MYDB_BEGIN_ALLOW_THREADS; |
| 5153 | #if (DBVER >= 46) |
| 5154 | err = self->db_env->rep_process_message(self->db_env, &control, &rec, |
| 5155 | envid, &lsn); |
| 5156 | #else |
| 5157 | #if (DBVER >= 42) |
| 5158 | err = self->db_env->rep_process_message(self->db_env, &control, &rec, |
| 5159 | &envid, &lsn); |
| 5160 | #else |
| 5161 | err = self->db_env->rep_process_message(self->db_env, &control, &rec, |
| 5162 | &envid); |
| 5163 | #endif |
| 5164 | #endif |
| 5165 | MYDB_END_ALLOW_THREADS; |
| 5166 | switch (err) { |
| 5167 | case DB_REP_NEWMASTER : |
| 5168 | return Py_BuildValue("(iO)", envid, Py_None); |
| 5169 | break; |
| 5170 | |
| 5171 | case DB_REP_DUPMASTER : |
| 5172 | case DB_REP_HOLDELECTION : |
| 5173 | #if (DBVER >= 44) |
| 5174 | case DB_REP_IGNORE : |
| 5175 | case DB_REP_JOIN_FAILURE : |
| 5176 | #endif |
| 5177 | return Py_BuildValue("(iO)", err, Py_None); |
| 5178 | break; |
| 5179 | case DB_REP_NEWSITE : |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 5180 | { |
| 5181 | PyObject *tmp, *r; |
| 5182 | |
| 5183 | if (!(tmp = PyBytes_FromStringAndSize(rec.data, rec.size))) { |
| 5184 | return NULL; |
| 5185 | } |
| 5186 | |
| 5187 | r = Py_BuildValue("(iO)", err, tmp); |
| 5188 | Py_DECREF(tmp); |
| 5189 | return r; |
| 5190 | break; |
| 5191 | } |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5192 | #if (DBVER >= 42) |
| 5193 | case DB_REP_NOTPERM : |
| 5194 | case DB_REP_ISPERM : |
| 5195 | return Py_BuildValue("(i(ll))", err, lsn.file, lsn.offset); |
| 5196 | break; |
| 5197 | #endif |
| 5198 | } |
| 5199 | RETURN_IF_ERR(); |
| 5200 | return Py_BuildValue("(OO)", Py_None, Py_None); |
| 5201 | } |
| 5202 | |
| 5203 | static int |
| 5204 | _DBEnv_rep_transportCallback(DB_ENV* db_env, const DBT* control, const DBT* rec, |
| 5205 | const DB_LSN *lsn, int envid, u_int32_t flags) |
| 5206 | { |
| 5207 | DBEnvObject *dbenv; |
| 5208 | PyObject* rep_transport; |
| 5209 | PyObject* args; |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 5210 | PyObject *a, *b; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5211 | PyObject* result = NULL; |
| 5212 | int ret=0; |
| 5213 | |
| 5214 | MYDB_BEGIN_BLOCK_THREADS; |
| 5215 | dbenv = (DBEnvObject *)db_env->app_private; |
| 5216 | rep_transport = dbenv->rep_transport; |
| 5217 | |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 5218 | /* |
| 5219 | ** The errors in 'a' or 'b' are detected in "Py_BuildValue". |
| 5220 | */ |
| 5221 | a = PyBytes_FromStringAndSize(control->data, control->size); |
| 5222 | b = PyBytes_FromStringAndSize(rec->data, rec->size); |
| 5223 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5224 | args = Py_BuildValue( |
| 5225 | #if (PY_VERSION_HEX >= 0x02040000) |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 5226 | "(OOO(ll)iI)", |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5227 | #else |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 5228 | "(OOO(ll)ii)", |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5229 | #endif |
| 5230 | dbenv, |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 5231 | a, b, |
| 5232 | lsn->file, lsn->offset, envid, flags); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5233 | if (args) { |
| 5234 | result = PyEval_CallObject(rep_transport, args); |
| 5235 | } |
| 5236 | |
| 5237 | if ((!args) || (!result)) { |
| 5238 | PyErr_Print(); |
| 5239 | ret = -1; |
| 5240 | } |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 5241 | Py_XDECREF(a); |
| 5242 | Py_XDECREF(b); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5243 | Py_XDECREF(args); |
| 5244 | Py_XDECREF(result); |
| 5245 | MYDB_END_BLOCK_THREADS; |
| 5246 | return ret; |
| 5247 | } |
| 5248 | |
| 5249 | #if (DBVER <= 41) |
| 5250 | static int |
| 5251 | _DBEnv_rep_transportCallbackOLD(DB_ENV* db_env, const DBT* control, const DBT* rec, |
| 5252 | int envid, u_int32_t flags) |
| 5253 | { |
| 5254 | DB_LSN lsn; |
| 5255 | |
| 5256 | lsn.file = -1; /* Dummy values */ |
| 5257 | lsn.offset = -1; |
| 5258 | return _DBEnv_rep_transportCallback(db_env, control, rec, &lsn, envid, |
| 5259 | flags); |
| 5260 | } |
| 5261 | #endif |
| 5262 | |
| 5263 | static PyObject* |
| 5264 | DBEnv_rep_set_transport(DBEnvObject* self, PyObject* args) |
| 5265 | { |
| 5266 | int err; |
| 5267 | int envid; |
| 5268 | PyObject *rep_transport; |
| 5269 | |
| 5270 | if (!PyArg_ParseTuple(args, "iO:rep_set_transport", &envid, &rep_transport)) |
| 5271 | return NULL; |
| 5272 | CHECK_ENV_NOT_CLOSED(self); |
| 5273 | if (!PyCallable_Check(rep_transport)) { |
| 5274 | makeTypeError("Callable", rep_transport); |
| 5275 | return NULL; |
| 5276 | } |
| 5277 | |
| 5278 | MYDB_BEGIN_ALLOW_THREADS; |
| 5279 | #if (DBVER >=45) |
| 5280 | err = self->db_env->rep_set_transport(self->db_env, envid, |
| 5281 | &_DBEnv_rep_transportCallback); |
| 5282 | #else |
| 5283 | #if (DBVER >= 42) |
| 5284 | err = self->db_env->set_rep_transport(self->db_env, envid, |
| 5285 | &_DBEnv_rep_transportCallback); |
| 5286 | #else |
| 5287 | err = self->db_env->set_rep_transport(self->db_env, envid, |
| 5288 | &_DBEnv_rep_transportCallbackOLD); |
| 5289 | #endif |
| 5290 | #endif |
| 5291 | MYDB_END_ALLOW_THREADS; |
| 5292 | RETURN_IF_ERR(); |
| 5293 | |
| 5294 | Py_DECREF(self->rep_transport); |
| 5295 | Py_INCREF(rep_transport); |
| 5296 | self->rep_transport = rep_transport; |
| 5297 | RETURN_NONE(); |
| 5298 | } |
| 5299 | |
| 5300 | #if (DBVER >= 47) |
| 5301 | static PyObject* |
| 5302 | DBEnv_rep_set_request(DBEnvObject* self, PyObject* args) |
| 5303 | { |
| 5304 | int err; |
| 5305 | unsigned int minimum, maximum; |
| 5306 | |
| 5307 | if (!PyArg_ParseTuple(args,"II:rep_set_request", &minimum, &maximum)) |
| 5308 | return NULL; |
| 5309 | CHECK_ENV_NOT_CLOSED(self); |
| 5310 | |
| 5311 | MYDB_BEGIN_ALLOW_THREADS; |
| 5312 | err = self->db_env->rep_set_request(self->db_env, minimum, maximum); |
| 5313 | MYDB_END_ALLOW_THREADS; |
| 5314 | RETURN_IF_ERR(); |
| 5315 | RETURN_NONE(); |
| 5316 | } |
| 5317 | |
| 5318 | static PyObject* |
| 5319 | DBEnv_rep_get_request(DBEnvObject* self) |
| 5320 | { |
| 5321 | int err; |
| 5322 | u_int32_t minimum, maximum; |
| 5323 | |
| 5324 | CHECK_ENV_NOT_CLOSED(self); |
| 5325 | MYDB_BEGIN_ALLOW_THREADS; |
| 5326 | err = self->db_env->rep_get_request(self->db_env, &minimum, &maximum); |
| 5327 | MYDB_END_ALLOW_THREADS; |
| 5328 | RETURN_IF_ERR(); |
| 5329 | #if (PY_VERSION_HEX >= 0x02040000) |
| 5330 | return Py_BuildValue("II", minimum, maximum); |
| 5331 | #else |
| 5332 | return Py_BuildValue("ii", minimum, maximum); |
| 5333 | #endif |
| 5334 | } |
| 5335 | #endif |
| 5336 | |
| 5337 | #if (DBVER >= 45) |
| 5338 | static PyObject* |
| 5339 | DBEnv_rep_set_limit(DBEnvObject* self, PyObject* args) |
| 5340 | { |
| 5341 | int err; |
| 5342 | int limit; |
| 5343 | |
| 5344 | if (!PyArg_ParseTuple(args,"i:rep_set_limit", &limit)) |
| 5345 | return NULL; |
| 5346 | CHECK_ENV_NOT_CLOSED(self); |
| 5347 | |
| 5348 | MYDB_BEGIN_ALLOW_THREADS; |
| 5349 | err = self->db_env->rep_set_limit(self->db_env, 0, limit); |
| 5350 | MYDB_END_ALLOW_THREADS; |
| 5351 | RETURN_IF_ERR(); |
| 5352 | RETURN_NONE(); |
| 5353 | } |
| 5354 | |
| 5355 | static PyObject* |
| 5356 | DBEnv_rep_get_limit(DBEnvObject* self) |
| 5357 | { |
| 5358 | int err; |
| 5359 | u_int32_t gbytes, bytes; |
| 5360 | |
| 5361 | CHECK_ENV_NOT_CLOSED(self); |
| 5362 | MYDB_BEGIN_ALLOW_THREADS; |
| 5363 | err = self->db_env->rep_get_limit(self->db_env, &gbytes, &bytes); |
| 5364 | MYDB_END_ALLOW_THREADS; |
| 5365 | RETURN_IF_ERR(); |
| 5366 | return NUMBER_FromLong(bytes); |
| 5367 | } |
| 5368 | #endif |
| 5369 | |
| 5370 | #if (DBVER >= 44) |
| 5371 | static PyObject* |
| 5372 | DBEnv_rep_set_config(DBEnvObject* self, PyObject* args) |
| 5373 | { |
| 5374 | int err; |
| 5375 | int which; |
| 5376 | int onoff; |
| 5377 | |
| 5378 | if (!PyArg_ParseTuple(args,"ii:rep_set_config", &which, &onoff)) |
| 5379 | return NULL; |
| 5380 | CHECK_ENV_NOT_CLOSED(self); |
| 5381 | |
| 5382 | MYDB_BEGIN_ALLOW_THREADS; |
| 5383 | err = self->db_env->rep_set_config(self->db_env, which, onoff); |
| 5384 | MYDB_END_ALLOW_THREADS; |
| 5385 | RETURN_IF_ERR(); |
| 5386 | RETURN_NONE(); |
| 5387 | } |
| 5388 | |
| 5389 | static PyObject* |
| 5390 | DBEnv_rep_get_config(DBEnvObject* self, PyObject* args) |
| 5391 | { |
| 5392 | int err; |
| 5393 | int which; |
| 5394 | int onoff; |
| 5395 | |
| 5396 | if (!PyArg_ParseTuple(args, "i:rep_get_config", &which)) { |
| 5397 | return NULL; |
| 5398 | } |
| 5399 | CHECK_ENV_NOT_CLOSED(self); |
| 5400 | MYDB_BEGIN_ALLOW_THREADS; |
| 5401 | err = self->db_env->rep_get_config(self->db_env, which, &onoff); |
| 5402 | MYDB_END_ALLOW_THREADS; |
| 5403 | RETURN_IF_ERR(); |
| 5404 | return PyBool_FromLong(onoff); |
| 5405 | } |
| 5406 | #endif |
| 5407 | |
| 5408 | #if (DBVER >= 46) |
| 5409 | static PyObject* |
| 5410 | DBEnv_rep_elect(DBEnvObject* self, PyObject* args) |
| 5411 | { |
| 5412 | int err; |
| 5413 | u_int32_t nsites, nvotes; |
| 5414 | |
| 5415 | if (!PyArg_ParseTuple(args, "II:rep_elect", &nsites, &nvotes)) { |
| 5416 | return NULL; |
| 5417 | } |
| 5418 | CHECK_ENV_NOT_CLOSED(self); |
| 5419 | MYDB_BEGIN_ALLOW_THREADS; |
| 5420 | err = self->db_env->rep_elect(self->db_env, nvotes, nvotes, 0); |
| 5421 | MYDB_END_ALLOW_THREADS; |
| 5422 | RETURN_IF_ERR(); |
| 5423 | RETURN_NONE(); |
| 5424 | } |
| 5425 | #endif |
| 5426 | |
| 5427 | static PyObject* |
| 5428 | DBEnv_rep_start(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 5429 | { |
| 5430 | int err; |
| 5431 | PyObject *cdata_py = Py_None; |
| 5432 | DBT cdata; |
| 5433 | int flags; |
| 5434 | static char* kwnames[] = {"flags","cdata", NULL}; |
| 5435 | |
| 5436 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 5437 | "i|O:rep_start", kwnames, &flags, &cdata_py)) |
| 5438 | { |
| 5439 | return NULL; |
| 5440 | } |
| 5441 | CHECK_ENV_NOT_CLOSED(self); |
| 5442 | |
| 5443 | if (!make_dbt(cdata_py, &cdata)) |
| 5444 | return NULL; |
| 5445 | |
| 5446 | MYDB_BEGIN_ALLOW_THREADS; |
| 5447 | err = self->db_env->rep_start(self->db_env, cdata.size ? &cdata : NULL, |
| 5448 | flags); |
| 5449 | MYDB_END_ALLOW_THREADS; |
| 5450 | RETURN_IF_ERR(); |
| 5451 | RETURN_NONE(); |
| 5452 | } |
| 5453 | |
| 5454 | #if (DBVER >= 44) |
| 5455 | static PyObject* |
| 5456 | DBEnv_rep_sync(DBEnvObject* self) |
| 5457 | { |
| 5458 | int err; |
| 5459 | |
| 5460 | CHECK_ENV_NOT_CLOSED(self); |
| 5461 | MYDB_BEGIN_ALLOW_THREADS; |
| 5462 | err = self->db_env->rep_sync(self->db_env, 0); |
| 5463 | MYDB_END_ALLOW_THREADS; |
| 5464 | RETURN_IF_ERR(); |
| 5465 | RETURN_NONE(); |
| 5466 | } |
| 5467 | #endif |
| 5468 | |
| 5469 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5470 | #if (DBVER >= 45) |
| 5471 | static PyObject* |
| 5472 | DBEnv_rep_set_nsites(DBEnvObject* self, PyObject* args) |
| 5473 | { |
| 5474 | int err; |
| 5475 | int nsites; |
| 5476 | |
| 5477 | if (!PyArg_ParseTuple(args, "i:rep_set_nsites", &nsites)) { |
| 5478 | return NULL; |
| 5479 | } |
| 5480 | CHECK_ENV_NOT_CLOSED(self); |
| 5481 | MYDB_BEGIN_ALLOW_THREADS; |
| 5482 | err = self->db_env->rep_set_nsites(self->db_env, nsites); |
| 5483 | MYDB_END_ALLOW_THREADS; |
| 5484 | RETURN_IF_ERR(); |
| 5485 | RETURN_NONE(); |
| 5486 | } |
| 5487 | |
| 5488 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5489 | DBEnv_rep_get_nsites(DBEnvObject* self) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5490 | { |
| 5491 | int err; |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 5492 | #if (DBVER >= 47) |
| 5493 | u_int32_t nsites; |
| 5494 | #else |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5495 | int nsites; |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 5496 | #endif |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5497 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5498 | CHECK_ENV_NOT_CLOSED(self); |
| 5499 | MYDB_BEGIN_ALLOW_THREADS; |
| 5500 | err = self->db_env->rep_get_nsites(self->db_env, &nsites); |
| 5501 | MYDB_END_ALLOW_THREADS; |
| 5502 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5503 | return NUMBER_FromLong(nsites); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5504 | } |
| 5505 | |
| 5506 | static PyObject* |
| 5507 | DBEnv_rep_set_priority(DBEnvObject* self, PyObject* args) |
| 5508 | { |
| 5509 | int err; |
| 5510 | int priority; |
| 5511 | |
| 5512 | if (!PyArg_ParseTuple(args, "i:rep_set_priority", &priority)) { |
| 5513 | return NULL; |
| 5514 | } |
| 5515 | CHECK_ENV_NOT_CLOSED(self); |
| 5516 | MYDB_BEGIN_ALLOW_THREADS; |
| 5517 | err = self->db_env->rep_set_priority(self->db_env, priority); |
| 5518 | MYDB_END_ALLOW_THREADS; |
| 5519 | RETURN_IF_ERR(); |
| 5520 | RETURN_NONE(); |
| 5521 | } |
| 5522 | |
| 5523 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5524 | DBEnv_rep_get_priority(DBEnvObject* self) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5525 | { |
| 5526 | int err; |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 5527 | #if (DBVER >= 47) |
| 5528 | u_int32_t priority; |
| 5529 | #else |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5530 | int priority; |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 5531 | #endif |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5532 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5533 | CHECK_ENV_NOT_CLOSED(self); |
| 5534 | MYDB_BEGIN_ALLOW_THREADS; |
| 5535 | err = self->db_env->rep_get_priority(self->db_env, &priority); |
| 5536 | MYDB_END_ALLOW_THREADS; |
| 5537 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5538 | return NUMBER_FromLong(priority); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5539 | } |
| 5540 | |
| 5541 | static PyObject* |
| 5542 | DBEnv_rep_set_timeout(DBEnvObject* self, PyObject* args) |
| 5543 | { |
| 5544 | int err; |
| 5545 | int which, timeout; |
| 5546 | |
| 5547 | if (!PyArg_ParseTuple(args, "ii:rep_set_timeout", &which, &timeout)) { |
| 5548 | return NULL; |
| 5549 | } |
| 5550 | CHECK_ENV_NOT_CLOSED(self); |
| 5551 | MYDB_BEGIN_ALLOW_THREADS; |
| 5552 | err = self->db_env->rep_set_timeout(self->db_env, which, timeout); |
| 5553 | MYDB_END_ALLOW_THREADS; |
| 5554 | RETURN_IF_ERR(); |
| 5555 | RETURN_NONE(); |
| 5556 | } |
| 5557 | |
| 5558 | static PyObject* |
| 5559 | DBEnv_rep_get_timeout(DBEnvObject* self, PyObject* args) |
| 5560 | { |
| 5561 | int err; |
| 5562 | int which; |
| 5563 | u_int32_t timeout; |
| 5564 | |
| 5565 | if (!PyArg_ParseTuple(args, "i:rep_get_timeout", &which)) { |
| 5566 | return NULL; |
| 5567 | } |
| 5568 | CHECK_ENV_NOT_CLOSED(self); |
| 5569 | MYDB_BEGIN_ALLOW_THREADS; |
| 5570 | err = self->db_env->rep_get_timeout(self->db_env, which, &timeout); |
| 5571 | MYDB_END_ALLOW_THREADS; |
| 5572 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5573 | return NUMBER_FromLong(timeout); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5574 | } |
| 5575 | #endif |
| 5576 | |
| 5577 | /* --------------------------------------------------------------------- */ |
| 5578 | /* REPLICATION METHODS: Replication Manager */ |
| 5579 | |
| 5580 | #if (DBVER >= 45) |
| 5581 | static PyObject* |
| 5582 | DBEnv_repmgr_start(DBEnvObject* self, PyObject* args, PyObject* |
| 5583 | kwargs) |
| 5584 | { |
| 5585 | int err; |
| 5586 | int nthreads, flags; |
| 5587 | static char* kwnames[] = {"nthreads","flags", NULL}; |
| 5588 | |
| 5589 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 5590 | "ii:repmgr_start", kwnames, &nthreads, &flags)) |
| 5591 | { |
| 5592 | return NULL; |
| 5593 | } |
| 5594 | CHECK_ENV_NOT_CLOSED(self); |
| 5595 | MYDB_BEGIN_ALLOW_THREADS; |
| 5596 | err = self->db_env->repmgr_start(self->db_env, nthreads, flags); |
| 5597 | MYDB_END_ALLOW_THREADS; |
| 5598 | RETURN_IF_ERR(); |
| 5599 | RETURN_NONE(); |
| 5600 | } |
| 5601 | |
| 5602 | static PyObject* |
| 5603 | DBEnv_repmgr_set_local_site(DBEnvObject* self, PyObject* args, PyObject* |
| 5604 | kwargs) |
| 5605 | { |
| 5606 | int err; |
| 5607 | char *host; |
| 5608 | int port; |
| 5609 | int flags = 0; |
| 5610 | static char* kwnames[] = {"host", "port", "flags", NULL}; |
| 5611 | |
| 5612 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 5613 | "si|i:repmgr_set_local_site", kwnames, &host, &port, &flags)) |
| 5614 | { |
| 5615 | return NULL; |
| 5616 | } |
| 5617 | CHECK_ENV_NOT_CLOSED(self); |
| 5618 | MYDB_BEGIN_ALLOW_THREADS; |
| 5619 | err = self->db_env->repmgr_set_local_site(self->db_env, host, port, flags); |
| 5620 | MYDB_END_ALLOW_THREADS; |
| 5621 | RETURN_IF_ERR(); |
| 5622 | RETURN_NONE(); |
| 5623 | } |
| 5624 | |
| 5625 | static PyObject* |
| 5626 | DBEnv_repmgr_add_remote_site(DBEnvObject* self, PyObject* args, PyObject* |
| 5627 | kwargs) |
| 5628 | { |
| 5629 | int err; |
| 5630 | char *host; |
| 5631 | int port; |
| 5632 | int flags = 0; |
| 5633 | int eidp; |
| 5634 | static char* kwnames[] = {"host", "port", "flags", NULL}; |
| 5635 | |
| 5636 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 5637 | "si|i:repmgr_add_remote_site", kwnames, &host, &port, &flags)) |
| 5638 | { |
| 5639 | return NULL; |
| 5640 | } |
| 5641 | CHECK_ENV_NOT_CLOSED(self); |
| 5642 | MYDB_BEGIN_ALLOW_THREADS; |
| 5643 | err = self->db_env->repmgr_add_remote_site(self->db_env, host, port, &eidp, flags); |
| 5644 | MYDB_END_ALLOW_THREADS; |
| 5645 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5646 | return NUMBER_FromLong(eidp); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5647 | } |
| 5648 | |
| 5649 | static PyObject* |
| 5650 | DBEnv_repmgr_set_ack_policy(DBEnvObject* self, PyObject* args) |
| 5651 | { |
| 5652 | int err; |
| 5653 | int ack_policy; |
| 5654 | |
| 5655 | if (!PyArg_ParseTuple(args, "i:repmgr_set_ack_policy", &ack_policy)) |
| 5656 | { |
| 5657 | return NULL; |
| 5658 | } |
| 5659 | CHECK_ENV_NOT_CLOSED(self); |
| 5660 | MYDB_BEGIN_ALLOW_THREADS; |
| 5661 | err = self->db_env->repmgr_set_ack_policy(self->db_env, ack_policy); |
| 5662 | MYDB_END_ALLOW_THREADS; |
| 5663 | RETURN_IF_ERR(); |
| 5664 | RETURN_NONE(); |
| 5665 | } |
| 5666 | |
| 5667 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5668 | DBEnv_repmgr_get_ack_policy(DBEnvObject* self) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5669 | { |
| 5670 | int err; |
| 5671 | int ack_policy; |
| 5672 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5673 | CHECK_ENV_NOT_CLOSED(self); |
| 5674 | MYDB_BEGIN_ALLOW_THREADS; |
| 5675 | err = self->db_env->repmgr_get_ack_policy(self->db_env, &ack_policy); |
| 5676 | MYDB_END_ALLOW_THREADS; |
| 5677 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5678 | return NUMBER_FromLong(ack_policy); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5679 | } |
| 5680 | |
| 5681 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5682 | DBEnv_repmgr_site_list(DBEnvObject* self) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5683 | { |
| 5684 | int err; |
| 5685 | unsigned int countp; |
| 5686 | DB_REPMGR_SITE *listp; |
| 5687 | PyObject *stats, *key, *tuple; |
| 5688 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5689 | CHECK_ENV_NOT_CLOSED(self); |
| 5690 | MYDB_BEGIN_ALLOW_THREADS; |
| 5691 | err = self->db_env->repmgr_site_list(self->db_env, &countp, &listp); |
| 5692 | MYDB_END_ALLOW_THREADS; |
| 5693 | RETURN_IF_ERR(); |
| 5694 | |
| 5695 | stats=PyDict_New(); |
| 5696 | if (stats == NULL) { |
| 5697 | free(listp); |
| 5698 | return NULL; |
| 5699 | } |
| 5700 | |
| 5701 | for(;countp--;) { |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5702 | key=NUMBER_FromLong(listp[countp].eid); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5703 | if(!key) { |
| 5704 | Py_DECREF(stats); |
| 5705 | free(listp); |
| 5706 | return NULL; |
| 5707 | } |
| 5708 | #if (PY_VERSION_HEX >= 0x02040000) |
| 5709 | tuple=Py_BuildValue("(sII)", listp[countp].host, |
| 5710 | listp[countp].port, listp[countp].status); |
| 5711 | #else |
| 5712 | tuple=Py_BuildValue("(sii)", listp[countp].host, |
| 5713 | listp[countp].port, listp[countp].status); |
| 5714 | #endif |
| 5715 | if(!tuple) { |
| 5716 | Py_DECREF(key); |
| 5717 | Py_DECREF(stats); |
| 5718 | free(listp); |
| 5719 | return NULL; |
| 5720 | } |
| 5721 | if(PyDict_SetItem(stats, key, tuple)) { |
| 5722 | Py_DECREF(key); |
| 5723 | Py_DECREF(tuple); |
| 5724 | Py_DECREF(stats); |
| 5725 | free(listp); |
| 5726 | return NULL; |
| 5727 | } |
| 5728 | } |
| 5729 | free(listp); |
| 5730 | return stats; |
| 5731 | } |
| 5732 | #endif |
| 5733 | |
| 5734 | #if (DBVER >= 46) |
| 5735 | static PyObject* |
| 5736 | DBEnv_repmgr_stat_print(DBEnvObject* self, PyObject* args, PyObject *kwargs) |
| 5737 | { |
| 5738 | int err; |
| 5739 | int flags=0; |
| 5740 | static char* kwnames[] = { "flags", NULL }; |
| 5741 | |
| 5742 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:repmgr_stat_print", |
| 5743 | kwnames, &flags)) |
| 5744 | { |
| 5745 | return NULL; |
| 5746 | } |
| 5747 | CHECK_ENV_NOT_CLOSED(self); |
| 5748 | MYDB_BEGIN_ALLOW_THREADS; |
| 5749 | err = self->db_env->repmgr_stat_print(self->db_env, flags); |
| 5750 | MYDB_END_ALLOW_THREADS; |
| 5751 | RETURN_IF_ERR(); |
| 5752 | RETURN_NONE(); |
| 5753 | } |
| 5754 | |
| 5755 | static PyObject* |
| 5756 | DBEnv_repmgr_stat(DBEnvObject* self, PyObject* args, PyObject *kwargs) |
| 5757 | { |
| 5758 | int err; |
| 5759 | int flags=0; |
| 5760 | DB_REPMGR_STAT *statp; |
| 5761 | PyObject *stats; |
| 5762 | static char* kwnames[] = { "flags", NULL }; |
| 5763 | |
| 5764 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:repmgr_stat", |
| 5765 | kwnames, &flags)) |
| 5766 | { |
| 5767 | return NULL; |
| 5768 | } |
| 5769 | CHECK_ENV_NOT_CLOSED(self); |
| 5770 | MYDB_BEGIN_ALLOW_THREADS; |
| 5771 | err = self->db_env->repmgr_stat(self->db_env, &statp, flags); |
| 5772 | MYDB_END_ALLOW_THREADS; |
| 5773 | RETURN_IF_ERR(); |
| 5774 | |
| 5775 | stats=PyDict_New(); |
| 5776 | if (stats == NULL) { |
| 5777 | free(statp); |
| 5778 | return NULL; |
| 5779 | } |
| 5780 | |
| 5781 | #define MAKE_ENTRY(name) _addIntToDict(stats, #name, statp->st_##name) |
| 5782 | |
| 5783 | MAKE_ENTRY(perm_failed); |
| 5784 | MAKE_ENTRY(msgs_queued); |
| 5785 | MAKE_ENTRY(msgs_dropped); |
| 5786 | MAKE_ENTRY(connection_drop); |
| 5787 | MAKE_ENTRY(connect_fail); |
| 5788 | |
| 5789 | #undef MAKE_ENTRY |
| 5790 | |
| 5791 | free(statp); |
| 5792 | return stats; |
| 5793 | } |
| 5794 | #endif |
| 5795 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5796 | |
| 5797 | /* --------------------------------------------------------------------- */ |
| 5798 | /* DBTxn methods */ |
| 5799 | |
| 5800 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5801 | static void _close_transaction_cursors(DBTxnObject* txn) |
| 5802 | { |
| 5803 | PyObject *dummy; |
| 5804 | |
| 5805 | while(txn->children_cursors) { |
| 5806 | PyErr_Warn(PyExc_RuntimeWarning, |
| 5807 | "Must close cursors before resolving a transaction."); |
| 5808 | dummy=DBC_close_internal(txn->children_cursors); |
| 5809 | Py_XDECREF(dummy); |
| 5810 | } |
| 5811 | } |
| 5812 | |
| 5813 | static void _promote_transaction_dbs_and_sequences(DBTxnObject *txn) |
| 5814 | { |
| 5815 | DBObject *db; |
| 5816 | #if (DBVER >= 43) |
| 5817 | DBSequenceObject *dbs; |
| 5818 | #endif |
| 5819 | |
| 5820 | while (txn->children_dbs) { |
| 5821 | db=txn->children_dbs; |
| 5822 | EXTRACT_FROM_DOUBLE_LINKED_LIST_TXN(db); |
| 5823 | if (txn->parent_txn) { |
| 5824 | INSERT_IN_DOUBLE_LINKED_LIST_TXN(txn->parent_txn->children_dbs,db); |
| 5825 | db->txn=txn->parent_txn; |
| 5826 | } else { |
| 5827 | /* The db is already linked to its environment, |
| 5828 | ** so nothing to do. |
| 5829 | */ |
| 5830 | db->txn=NULL; |
| 5831 | } |
| 5832 | } |
| 5833 | |
| 5834 | #if (DBVER >= 43) |
| 5835 | while (txn->children_sequences) { |
| 5836 | dbs=txn->children_sequences; |
| 5837 | EXTRACT_FROM_DOUBLE_LINKED_LIST_TXN(dbs); |
| 5838 | if (txn->parent_txn) { |
| 5839 | INSERT_IN_DOUBLE_LINKED_LIST_TXN(txn->parent_txn->children_sequences,dbs); |
| 5840 | dbs->txn=txn->parent_txn; |
| 5841 | } else { |
| 5842 | /* The sequence is already linked to its |
| 5843 | ** parent db. Nothing to do. |
| 5844 | */ |
| 5845 | dbs->txn=NULL; |
| 5846 | } |
| 5847 | } |
| 5848 | #endif |
| 5849 | } |
| 5850 | |
| 5851 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5852 | static PyObject* |
| 5853 | DBTxn_commit(DBTxnObject* self, PyObject* args) |
| 5854 | { |
| 5855 | int flags=0, err; |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 5856 | DB_TXN *txn; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5857 | |
| 5858 | if (!PyArg_ParseTuple(args, "|i:commit", &flags)) |
| 5859 | return NULL; |
| 5860 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5861 | _close_transaction_cursors(self); |
| 5862 | |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 5863 | if (!self->txn) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 5864 | PyObject *t = Py_BuildValue("(is)", 0, "DBTxn must not be used " |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5865 | "after txn_commit, txn_abort " |
| 5866 | "or txn_discard"); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5867 | if (t) { |
| 5868 | PyErr_SetObject(DBError, t); |
| 5869 | Py_DECREF(t); |
| 5870 | } |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 5871 | return NULL; |
| 5872 | } |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5873 | self->flag_prepare=0; |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 5874 | txn = self->txn; |
| 5875 | self->txn = NULL; /* this DB_TXN is no longer valid after this call */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5876 | |
| 5877 | EXTRACT_FROM_DOUBLE_LINKED_LIST(self); |
| 5878 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5879 | MYDB_BEGIN_ALLOW_THREADS; |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 5880 | err = txn->commit(txn, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5881 | MYDB_END_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5882 | |
| 5883 | _promote_transaction_dbs_and_sequences(self); |
| 5884 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5885 | RETURN_IF_ERR(); |
| 5886 | RETURN_NONE(); |
| 5887 | } |
| 5888 | |
| 5889 | static PyObject* |
| 5890 | DBTxn_prepare(DBTxnObject* self, PyObject* args) |
| 5891 | { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5892 | int err; |
| 5893 | char* gid=NULL; |
| 5894 | int gid_size=0; |
| 5895 | |
| 5896 | if (!PyArg_ParseTuple(args, "s#:prepare", &gid, &gid_size)) |
| 5897 | return NULL; |
| 5898 | |
| 5899 | if (gid_size != DB_XIDDATASIZE) { |
| 5900 | PyErr_SetString(PyExc_TypeError, |
| 5901 | "gid must be DB_XIDDATASIZE bytes long"); |
| 5902 | return NULL; |
| 5903 | } |
| 5904 | |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 5905 | if (!self->txn) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 5906 | PyObject *t = Py_BuildValue("(is)", 0,"DBTxn must not be used " |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5907 | "after txn_commit, txn_abort " |
| 5908 | "or txn_discard"); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5909 | if (t) { |
| 5910 | PyErr_SetObject(DBError, t); |
| 5911 | Py_DECREF(t); |
| 5912 | } |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 5913 | return NULL; |
| 5914 | } |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5915 | self->flag_prepare=1; /* Prepare state */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5916 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5917 | err = self->txn->prepare(self->txn, (u_int8_t*)gid); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5918 | MYDB_END_ALLOW_THREADS; |
| 5919 | RETURN_IF_ERR(); |
| 5920 | RETURN_NONE(); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5921 | } |
| 5922 | |
| 5923 | |
| 5924 | static PyObject* |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5925 | DBTxn_abort_discard_internal(DBTxnObject* self, int discard) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5926 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5927 | PyObject *dummy; |
| 5928 | int err=0; |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 5929 | DB_TXN *txn; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5930 | |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 5931 | if (!self->txn) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 5932 | PyObject *t = Py_BuildValue("(is)", 0, "DBTxn must not be used " |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5933 | "after txn_commit, txn_abort " |
| 5934 | "or txn_discard"); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5935 | if (t) { |
| 5936 | PyErr_SetObject(DBError, t); |
| 5937 | Py_DECREF(t); |
| 5938 | } |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 5939 | return NULL; |
| 5940 | } |
| 5941 | txn = self->txn; |
| 5942 | self->txn = NULL; /* this DB_TXN is no longer valid after this call */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5943 | |
| 5944 | _close_transaction_cursors(self); |
| 5945 | #if (DBVER >= 43) |
| 5946 | while (self->children_sequences) { |
| 5947 | dummy=DBSequence_close_internal(self->children_sequences,0,0); |
| 5948 | Py_XDECREF(dummy); |
| 5949 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5950 | #endif |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5951 | while (self->children_dbs) { |
| 5952 | dummy=DB_close_internal(self->children_dbs,0); |
| 5953 | Py_XDECREF(dummy); |
| 5954 | } |
| 5955 | |
| 5956 | EXTRACT_FROM_DOUBLE_LINKED_LIST(self); |
| 5957 | |
| 5958 | MYDB_BEGIN_ALLOW_THREADS; |
| 5959 | if (discard) { |
| 5960 | assert(!self->flag_prepare); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5961 | err = txn->discard(txn,0); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5962 | } else { |
| 5963 | /* |
| 5964 | ** If the transaction is in the "prepare" or "recover" state, |
| 5965 | ** we better do not implicitly abort it. |
| 5966 | */ |
| 5967 | if (!self->flag_prepare) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5968 | err = txn->abort(txn); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5969 | } |
| 5970 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5971 | MYDB_END_ALLOW_THREADS; |
| 5972 | RETURN_IF_ERR(); |
| 5973 | RETURN_NONE(); |
| 5974 | } |
| 5975 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5976 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5977 | DBTxn_abort(DBTxnObject* self) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5978 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5979 | self->flag_prepare=0; |
| 5980 | _close_transaction_cursors(self); |
| 5981 | |
| 5982 | return DBTxn_abort_discard_internal(self,0); |
| 5983 | } |
| 5984 | |
| 5985 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5986 | DBTxn_discard(DBTxnObject* self) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5987 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5988 | self->flag_prepare=0; |
| 5989 | _close_transaction_cursors(self); |
| 5990 | |
| 5991 | return DBTxn_abort_discard_internal(self,1); |
| 5992 | } |
| 5993 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5994 | |
| 5995 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5996 | DBTxn_id(DBTxnObject* self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5997 | { |
| 5998 | int id; |
| 5999 | |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 6000 | if (!self->txn) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 6001 | PyObject *t = Py_BuildValue("(is)", 0, "DBTxn must not be used " |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6002 | "after txn_commit, txn_abort " |
| 6003 | "or txn_discard"); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6004 | if (t) { |
| 6005 | PyErr_SetObject(DBError, t); |
| 6006 | Py_DECREF(t); |
| 6007 | } |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 6008 | return NULL; |
| 6009 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6010 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6011 | id = self->txn->id(self->txn); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6012 | MYDB_END_ALLOW_THREADS; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6013 | return NUMBER_FromLong(id); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6014 | } |
| 6015 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6016 | #if (DBVER >= 43) |
| 6017 | /* --------------------------------------------------------------------- */ |
| 6018 | /* DBSequence methods */ |
| 6019 | |
| 6020 | |
| 6021 | static PyObject* |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6022 | DBSequence_close_internal(DBSequenceObject* self, int flags, int do_not_close) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6023 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6024 | int err=0; |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6025 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6026 | if (self->sequence!=NULL) { |
| 6027 | EXTRACT_FROM_DOUBLE_LINKED_LIST(self); |
| 6028 | if (self->txn) { |
| 6029 | EXTRACT_FROM_DOUBLE_LINKED_LIST_TXN(self); |
| 6030 | self->txn=NULL; |
| 6031 | } |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6032 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6033 | if (!do_not_close) { |
| 6034 | MYDB_BEGIN_ALLOW_THREADS |
| 6035 | err = self->sequence->close(self->sequence, flags); |
| 6036 | MYDB_END_ALLOW_THREADS |
| 6037 | } |
| 6038 | self->sequence = NULL; |
| 6039 | |
| 6040 | RETURN_IF_ERR(); |
| 6041 | } |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6042 | |
| 6043 | RETURN_NONE(); |
| 6044 | } |
| 6045 | |
| 6046 | static PyObject* |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6047 | DBSequence_close(DBSequenceObject* self, PyObject* args) |
| 6048 | { |
| 6049 | int flags=0; |
| 6050 | if (!PyArg_ParseTuple(args,"|i:close", &flags)) |
| 6051 | return NULL; |
| 6052 | |
| 6053 | return DBSequence_close_internal(self,flags,0); |
| 6054 | } |
| 6055 | |
| 6056 | static PyObject* |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6057 | DBSequence_get(DBSequenceObject* self, PyObject* args, PyObject* kwargs) |
| 6058 | { |
| 6059 | int err, flags = 0; |
| 6060 | int delta = 1; |
| 6061 | db_seq_t value; |
| 6062 | PyObject *txnobj = NULL; |
| 6063 | DB_TXN *txn = NULL; |
| 6064 | static char* kwnames[] = {"delta", "txn", "flags", NULL }; |
| 6065 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iOi:get", kwnames, &delta, &txnobj, &flags)) |
| 6066 | return NULL; |
| 6067 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6068 | |
| 6069 | if (!checkTxnObj(txnobj, &txn)) |
| 6070 | return NULL; |
| 6071 | |
| 6072 | MYDB_BEGIN_ALLOW_THREADS |
| 6073 | err = self->sequence->get(self->sequence, txn, delta, &value, flags); |
| 6074 | MYDB_END_ALLOW_THREADS |
| 6075 | |
| 6076 | RETURN_IF_ERR(); |
| 6077 | return PyLong_FromLongLong(value); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6078 | } |
| 6079 | |
| 6080 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6081 | DBSequence_get_dbp(DBSequenceObject* self) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6082 | { |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6083 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6084 | Py_INCREF(self->mydb); |
| 6085 | return (PyObject* )self->mydb; |
| 6086 | } |
| 6087 | |
| 6088 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6089 | DBSequence_get_key(DBSequenceObject* self) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6090 | { |
| 6091 | int err; |
| 6092 | DBT key; |
Neal Norwitz | 088beae | 2007-10-12 03:01:54 +0000 | [diff] [blame] | 6093 | PyObject *retval = NULL; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6094 | |
Gregory P. Smith | e70be5c | 2007-10-06 07:48:10 +0000 | [diff] [blame] | 6095 | key.flags = DB_DBT_MALLOC; |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6096 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6097 | MYDB_BEGIN_ALLOW_THREADS |
| 6098 | err = self->sequence->get_key(self->sequence, &key); |
| 6099 | MYDB_END_ALLOW_THREADS |
| 6100 | |
Gregory P. Smith | e70be5c | 2007-10-06 07:48:10 +0000 | [diff] [blame] | 6101 | if (!err) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6102 | retval = Build_PyString(key.data, key.size); |
Gregory P. Smith | e70be5c | 2007-10-06 07:48:10 +0000 | [diff] [blame] | 6103 | |
| 6104 | FREE_DBT(key); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6105 | RETURN_IF_ERR(); |
| 6106 | |
Gregory P. Smith | e70be5c | 2007-10-06 07:48:10 +0000 | [diff] [blame] | 6107 | return retval; |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6108 | } |
| 6109 | |
| 6110 | static PyObject* |
| 6111 | DBSequence_init_value(DBSequenceObject* self, PyObject* args) |
| 6112 | { |
| 6113 | int err; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6114 | PY_LONG_LONG value; |
| 6115 | db_seq_t value2; |
Neal Norwitz | dd2a6bf | 2006-06-06 07:23:01 +0000 | [diff] [blame] | 6116 | if (!PyArg_ParseTuple(args,"L:init_value", &value)) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6117 | return NULL; |
| 6118 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6119 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6120 | value2=value; /* If truncation, compiler should show a warning */ |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6121 | MYDB_BEGIN_ALLOW_THREADS |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6122 | err = self->sequence->initial_value(self->sequence, value2); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6123 | MYDB_END_ALLOW_THREADS |
| 6124 | |
| 6125 | RETURN_IF_ERR(); |
| 6126 | |
| 6127 | RETURN_NONE(); |
| 6128 | } |
| 6129 | |
| 6130 | static PyObject* |
| 6131 | DBSequence_open(DBSequenceObject* self, PyObject* args, PyObject* kwargs) |
| 6132 | { |
| 6133 | int err, flags = 0; |
| 6134 | PyObject* keyobj; |
| 6135 | PyObject *txnobj = NULL; |
| 6136 | DB_TXN *txn = NULL; |
| 6137 | DBT key; |
| 6138 | |
| 6139 | static char* kwnames[] = {"key", "txn", "flags", NULL }; |
Neal Norwitz | dd2a6bf | 2006-06-06 07:23:01 +0000 | [diff] [blame] | 6140 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Oi:open", kwnames, &keyobj, &txnobj, &flags)) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6141 | return NULL; |
| 6142 | |
| 6143 | if (!checkTxnObj(txnobj, &txn)) |
| 6144 | return NULL; |
| 6145 | |
| 6146 | if (!make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 6147 | return NULL; |
| 6148 | |
| 6149 | MYDB_BEGIN_ALLOW_THREADS |
| 6150 | err = self->sequence->open(self->sequence, txn, &key, flags); |
| 6151 | MYDB_END_ALLOW_THREADS |
| 6152 | |
Jesus Cea | ac25fab | 2008-09-03 17:50:32 +0000 | [diff] [blame] | 6153 | FREE_DBT(key); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6154 | RETURN_IF_ERR(); |
| 6155 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6156 | if (txn) { |
| 6157 | INSERT_IN_DOUBLE_LINKED_LIST_TXN(((DBTxnObject *)txnobj)->children_sequences,self); |
| 6158 | self->txn=(DBTxnObject *)txnobj; |
| 6159 | } |
| 6160 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6161 | RETURN_NONE(); |
| 6162 | } |
| 6163 | |
| 6164 | static PyObject* |
| 6165 | DBSequence_remove(DBSequenceObject* self, PyObject* args, PyObject* kwargs) |
| 6166 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6167 | PyObject *dummy; |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6168 | int err, flags = 0; |
| 6169 | PyObject *txnobj = NULL; |
| 6170 | DB_TXN *txn = NULL; |
| 6171 | |
| 6172 | static char* kwnames[] = {"txn", "flags", NULL }; |
Neal Norwitz | dd2a6bf | 2006-06-06 07:23:01 +0000 | [diff] [blame] | 6173 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:remove", kwnames, &txnobj, &flags)) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6174 | return NULL; |
| 6175 | |
| 6176 | if (!checkTxnObj(txnobj, &txn)) |
| 6177 | return NULL; |
| 6178 | |
| 6179 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6180 | |
| 6181 | MYDB_BEGIN_ALLOW_THREADS |
| 6182 | err = self->sequence->remove(self->sequence, txn, flags); |
| 6183 | MYDB_END_ALLOW_THREADS |
| 6184 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6185 | dummy=DBSequence_close_internal(self,flags,1); |
| 6186 | Py_XDECREF(dummy); |
| 6187 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6188 | RETURN_IF_ERR(); |
| 6189 | RETURN_NONE(); |
| 6190 | } |
| 6191 | |
| 6192 | static PyObject* |
| 6193 | DBSequence_set_cachesize(DBSequenceObject* self, PyObject* args) |
| 6194 | { |
| 6195 | int err, size; |
Neal Norwitz | dd2a6bf | 2006-06-06 07:23:01 +0000 | [diff] [blame] | 6196 | if (!PyArg_ParseTuple(args,"i:set_cachesize", &size)) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6197 | return NULL; |
| 6198 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6199 | |
| 6200 | MYDB_BEGIN_ALLOW_THREADS |
| 6201 | err = self->sequence->set_cachesize(self->sequence, size); |
| 6202 | MYDB_END_ALLOW_THREADS |
| 6203 | |
| 6204 | RETURN_IF_ERR(); |
| 6205 | RETURN_NONE(); |
| 6206 | } |
| 6207 | |
| 6208 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6209 | DBSequence_get_cachesize(DBSequenceObject* self) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6210 | { |
| 6211 | int err, size; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6212 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6213 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6214 | |
| 6215 | MYDB_BEGIN_ALLOW_THREADS |
| 6216 | err = self->sequence->get_cachesize(self->sequence, &size); |
| 6217 | MYDB_END_ALLOW_THREADS |
| 6218 | |
| 6219 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6220 | return NUMBER_FromLong(size); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6221 | } |
| 6222 | |
| 6223 | static PyObject* |
| 6224 | DBSequence_set_flags(DBSequenceObject* self, PyObject* args) |
| 6225 | { |
| 6226 | int err, flags = 0; |
Neal Norwitz | dd2a6bf | 2006-06-06 07:23:01 +0000 | [diff] [blame] | 6227 | if (!PyArg_ParseTuple(args,"i:set_flags", &flags)) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6228 | return NULL; |
| 6229 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6230 | |
| 6231 | MYDB_BEGIN_ALLOW_THREADS |
| 6232 | err = self->sequence->set_flags(self->sequence, flags); |
| 6233 | MYDB_END_ALLOW_THREADS |
| 6234 | |
| 6235 | RETURN_IF_ERR(); |
| 6236 | RETURN_NONE(); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6237 | } |
| 6238 | |
| 6239 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6240 | DBSequence_get_flags(DBSequenceObject* self) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6241 | { |
| 6242 | unsigned int flags; |
| 6243 | int err; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6244 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6245 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6246 | |
| 6247 | MYDB_BEGIN_ALLOW_THREADS |
| 6248 | err = self->sequence->get_flags(self->sequence, &flags); |
| 6249 | MYDB_END_ALLOW_THREADS |
| 6250 | |
| 6251 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6252 | return NUMBER_FromLong((int)flags); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6253 | } |
| 6254 | |
| 6255 | static PyObject* |
| 6256 | DBSequence_set_range(DBSequenceObject* self, PyObject* args) |
| 6257 | { |
| 6258 | int err; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6259 | PY_LONG_LONG min, max; |
| 6260 | db_seq_t min2, max2; |
Tim Peters | bb21b2c | 2006-06-06 15:50:17 +0000 | [diff] [blame] | 6261 | if (!PyArg_ParseTuple(args,"(LL):set_range", &min, &max)) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6262 | return NULL; |
| 6263 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6264 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6265 | min2=min; /* If truncation, compiler should show a warning */ |
| 6266 | max2=max; |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6267 | MYDB_BEGIN_ALLOW_THREADS |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6268 | err = self->sequence->set_range(self->sequence, min2, max2); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6269 | MYDB_END_ALLOW_THREADS |
| 6270 | |
| 6271 | RETURN_IF_ERR(); |
| 6272 | RETURN_NONE(); |
| 6273 | } |
| 6274 | |
| 6275 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6276 | DBSequence_get_range(DBSequenceObject* self) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6277 | { |
| 6278 | int err; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6279 | PY_LONG_LONG min, max; |
| 6280 | db_seq_t min2, max2; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6281 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6282 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6283 | |
| 6284 | MYDB_BEGIN_ALLOW_THREADS |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6285 | err = self->sequence->get_range(self->sequence, &min2, &max2); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6286 | MYDB_END_ALLOW_THREADS |
| 6287 | |
| 6288 | RETURN_IF_ERR(); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6289 | min=min2; /* If truncation, compiler should show a warning */ |
| 6290 | max=max2; |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6291 | return Py_BuildValue("(LL)", min, max); |
| 6292 | } |
| 6293 | |
| 6294 | static PyObject* |
| 6295 | DBSequence_stat(DBSequenceObject* self, PyObject* args, PyObject* kwargs) |
| 6296 | { |
| 6297 | int err, flags = 0; |
| 6298 | DB_SEQUENCE_STAT* sp = NULL; |
| 6299 | PyObject* dict_stat; |
| 6300 | static char* kwnames[] = {"flags", NULL }; |
| 6301 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:stat", kwnames, &flags)) |
| 6302 | return NULL; |
| 6303 | CHECK_SEQUENCE_NOT_CLOSED(self); |
| 6304 | |
| 6305 | MYDB_BEGIN_ALLOW_THREADS; |
| 6306 | err = self->sequence->stat(self->sequence, &sp, flags); |
| 6307 | MYDB_END_ALLOW_THREADS; |
| 6308 | RETURN_IF_ERR(); |
| 6309 | |
| 6310 | if ((dict_stat = PyDict_New()) == NULL) { |
| 6311 | free(sp); |
| 6312 | return NULL; |
| 6313 | } |
| 6314 | |
| 6315 | |
| 6316 | #define MAKE_INT_ENTRY(name) _addIntToDict(dict_stat, #name, sp->st_##name) |
| 6317 | #define MAKE_LONG_LONG_ENTRY(name) _addDb_seq_tToDict(dict_stat, #name, sp->st_##name) |
| 6318 | |
| 6319 | MAKE_INT_ENTRY(wait); |
| 6320 | MAKE_INT_ENTRY(nowait); |
| 6321 | MAKE_LONG_LONG_ENTRY(current); |
| 6322 | MAKE_LONG_LONG_ENTRY(value); |
| 6323 | MAKE_LONG_LONG_ENTRY(last_value); |
| 6324 | MAKE_LONG_LONG_ENTRY(min); |
| 6325 | MAKE_LONG_LONG_ENTRY(max); |
| 6326 | MAKE_INT_ENTRY(cache_size); |
| 6327 | MAKE_INT_ENTRY(flags); |
| 6328 | |
| 6329 | #undef MAKE_INT_ENTRY |
| 6330 | #undef MAKE_LONG_LONG_ENTRY |
| 6331 | |
| 6332 | free(sp); |
| 6333 | return dict_stat; |
| 6334 | } |
| 6335 | #endif |
| 6336 | |
| 6337 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6338 | /* --------------------------------------------------------------------- */ |
| 6339 | /* Method definition tables and type objects */ |
| 6340 | |
| 6341 | static PyMethodDef DB_methods[] = { |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 6342 | {"append", (PyCFunction)DB_append, METH_VARARGS|METH_KEYWORDS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6343 | {"associate", (PyCFunction)DB_associate, METH_VARARGS|METH_KEYWORDS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6344 | {"close", (PyCFunction)DB_close, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6345 | {"consume", (PyCFunction)DB_consume, METH_VARARGS|METH_KEYWORDS}, |
| 6346 | {"consume_wait", (PyCFunction)DB_consume_wait, METH_VARARGS|METH_KEYWORDS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6347 | {"cursor", (PyCFunction)DB_cursor, METH_VARARGS|METH_KEYWORDS}, |
| 6348 | {"delete", (PyCFunction)DB_delete, METH_VARARGS|METH_KEYWORDS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6349 | {"fd", (PyCFunction)DB_fd, METH_NOARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6350 | {"get", (PyCFunction)DB_get, METH_VARARGS|METH_KEYWORDS}, |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 6351 | {"pget", (PyCFunction)DB_pget, METH_VARARGS|METH_KEYWORDS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6352 | {"get_both", (PyCFunction)DB_get_both, METH_VARARGS|METH_KEYWORDS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6353 | {"get_byteswapped", (PyCFunction)DB_get_byteswapped,METH_NOARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6354 | {"get_size", (PyCFunction)DB_get_size, METH_VARARGS|METH_KEYWORDS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6355 | {"get_type", (PyCFunction)DB_get_type, METH_NOARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6356 | {"join", (PyCFunction)DB_join, METH_VARARGS}, |
| 6357 | {"key_range", (PyCFunction)DB_key_range, METH_VARARGS|METH_KEYWORDS}, |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 6358 | {"has_key", (PyCFunction)DB_has_key, METH_VARARGS|METH_KEYWORDS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6359 | {"items", (PyCFunction)DB_items, METH_VARARGS}, |
| 6360 | {"keys", (PyCFunction)DB_keys, METH_VARARGS}, |
| 6361 | {"open", (PyCFunction)DB_open, METH_VARARGS|METH_KEYWORDS}, |
| 6362 | {"put", (PyCFunction)DB_put, METH_VARARGS|METH_KEYWORDS}, |
| 6363 | {"remove", (PyCFunction)DB_remove, METH_VARARGS|METH_KEYWORDS}, |
| 6364 | {"rename", (PyCFunction)DB_rename, METH_VARARGS}, |
| 6365 | {"set_bt_minkey", (PyCFunction)DB_set_bt_minkey, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6366 | {"set_bt_compare", (PyCFunction)DB_set_bt_compare, METH_O}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6367 | {"set_cachesize", (PyCFunction)DB_set_cachesize, METH_VARARGS}, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 6368 | #if (DBVER >= 41) |
| 6369 | {"set_encrypt", (PyCFunction)DB_set_encrypt, METH_VARARGS|METH_KEYWORDS}, |
| 6370 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6371 | {"set_flags", (PyCFunction)DB_set_flags, METH_VARARGS}, |
| 6372 | {"set_h_ffactor", (PyCFunction)DB_set_h_ffactor, METH_VARARGS}, |
| 6373 | {"set_h_nelem", (PyCFunction)DB_set_h_nelem, METH_VARARGS}, |
| 6374 | {"set_lorder", (PyCFunction)DB_set_lorder, METH_VARARGS}, |
| 6375 | {"set_pagesize", (PyCFunction)DB_set_pagesize, METH_VARARGS}, |
| 6376 | {"set_re_delim", (PyCFunction)DB_set_re_delim, METH_VARARGS}, |
| 6377 | {"set_re_len", (PyCFunction)DB_set_re_len, METH_VARARGS}, |
| 6378 | {"set_re_pad", (PyCFunction)DB_set_re_pad, METH_VARARGS}, |
| 6379 | {"set_re_source", (PyCFunction)DB_set_re_source, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6380 | {"set_q_extentsize",(PyCFunction)DB_set_q_extentsize, METH_VARARGS}, |
| 6381 | {"set_private", (PyCFunction)DB_set_private, METH_O}, |
| 6382 | {"get_private", (PyCFunction)DB_get_private, METH_NOARGS}, |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 6383 | {"stat", (PyCFunction)DB_stat, METH_VARARGS|METH_KEYWORDS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6384 | {"sync", (PyCFunction)DB_sync, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6385 | {"truncate", (PyCFunction)DB_truncate, METH_VARARGS|METH_KEYWORDS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6386 | {"type", (PyCFunction)DB_get_type, METH_NOARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6387 | {"upgrade", (PyCFunction)DB_upgrade, METH_VARARGS}, |
| 6388 | {"values", (PyCFunction)DB_values, METH_VARARGS}, |
| 6389 | {"verify", (PyCFunction)DB_verify, METH_VARARGS|METH_KEYWORDS}, |
| 6390 | {"set_get_returns_none",(PyCFunction)DB_set_get_returns_none, METH_VARARGS}, |
| 6391 | {NULL, NULL} /* sentinel */ |
| 6392 | }; |
| 6393 | |
| 6394 | |
| 6395 | static PyMappingMethods DB_mapping = { |
Martin v. Löwis | 70ee3cc | 2006-06-12 04:26:31 +0000 | [diff] [blame] | 6396 | DB_length, /*mp_length*/ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6397 | (binaryfunc)DB_subscript, /*mp_subscript*/ |
| 6398 | (objobjargproc)DB_ass_sub, /*mp_ass_subscript*/ |
| 6399 | }; |
| 6400 | |
| 6401 | |
| 6402 | static PyMethodDef DBCursor_methods[] = { |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6403 | {"close", (PyCFunction)DBC_close, METH_NOARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6404 | {"count", (PyCFunction)DBC_count, METH_VARARGS}, |
| 6405 | {"current", (PyCFunction)DBC_current, METH_VARARGS|METH_KEYWORDS}, |
| 6406 | {"delete", (PyCFunction)DBC_delete, METH_VARARGS}, |
| 6407 | {"dup", (PyCFunction)DBC_dup, METH_VARARGS}, |
| 6408 | {"first", (PyCFunction)DBC_first, METH_VARARGS|METH_KEYWORDS}, |
| 6409 | {"get", (PyCFunction)DBC_get, METH_VARARGS|METH_KEYWORDS}, |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 6410 | {"pget", (PyCFunction)DBC_pget, METH_VARARGS|METH_KEYWORDS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6411 | {"get_recno", (PyCFunction)DBC_get_recno, METH_NOARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6412 | {"last", (PyCFunction)DBC_last, METH_VARARGS|METH_KEYWORDS}, |
| 6413 | {"next", (PyCFunction)DBC_next, METH_VARARGS|METH_KEYWORDS}, |
| 6414 | {"prev", (PyCFunction)DBC_prev, METH_VARARGS|METH_KEYWORDS}, |
| 6415 | {"put", (PyCFunction)DBC_put, METH_VARARGS|METH_KEYWORDS}, |
| 6416 | {"set", (PyCFunction)DBC_set, METH_VARARGS|METH_KEYWORDS}, |
| 6417 | {"set_range", (PyCFunction)DBC_set_range, METH_VARARGS|METH_KEYWORDS}, |
| 6418 | {"get_both", (PyCFunction)DBC_get_both, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6419 | {"get_current_size",(PyCFunction)DBC_get_current_size, METH_NOARGS}, |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 6420 | {"set_both", (PyCFunction)DBC_set_both, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6421 | {"set_recno", (PyCFunction)DBC_set_recno, METH_VARARGS|METH_KEYWORDS}, |
| 6422 | {"consume", (PyCFunction)DBC_consume, METH_VARARGS|METH_KEYWORDS}, |
| 6423 | {"next_dup", (PyCFunction)DBC_next_dup, METH_VARARGS|METH_KEYWORDS}, |
| 6424 | {"next_nodup", (PyCFunction)DBC_next_nodup, METH_VARARGS|METH_KEYWORDS}, |
| 6425 | {"prev_nodup", (PyCFunction)DBC_prev_nodup, METH_VARARGS|METH_KEYWORDS}, |
| 6426 | {"join_item", (PyCFunction)DBC_join_item, METH_VARARGS}, |
| 6427 | {NULL, NULL} /* sentinel */ |
| 6428 | }; |
| 6429 | |
| 6430 | |
| 6431 | static PyMethodDef DBEnv_methods[] = { |
| 6432 | {"close", (PyCFunction)DBEnv_close, METH_VARARGS}, |
| 6433 | {"open", (PyCFunction)DBEnv_open, METH_VARARGS}, |
| 6434 | {"remove", (PyCFunction)DBEnv_remove, METH_VARARGS}, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 6435 | #if (DBVER >= 41) |
| 6436 | {"dbremove", (PyCFunction)DBEnv_dbremove, METH_VARARGS|METH_KEYWORDS}, |
| 6437 | {"dbrename", (PyCFunction)DBEnv_dbrename, METH_VARARGS|METH_KEYWORDS}, |
| 6438 | {"set_encrypt", (PyCFunction)DBEnv_set_encrypt, METH_VARARGS|METH_KEYWORDS}, |
| 6439 | #endif |
Gregory P. Smith | fe11d3e | 2003-03-27 17:23:29 +0000 | [diff] [blame] | 6440 | {"set_timeout", (PyCFunction)DBEnv_set_timeout, METH_VARARGS|METH_KEYWORDS}, |
Gregory P. Smith | 6676f6e | 2003-08-28 21:50:30 +0000 | [diff] [blame] | 6441 | {"set_shm_key", (PyCFunction)DBEnv_set_shm_key, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6442 | {"set_cachesize", (PyCFunction)DBEnv_set_cachesize, METH_VARARGS}, |
| 6443 | {"set_data_dir", (PyCFunction)DBEnv_set_data_dir, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6444 | {"set_flags", (PyCFunction)DBEnv_set_flags, METH_VARARGS}, |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 6445 | #if (DBVER >= 47) |
| 6446 | {"log_set_config", (PyCFunction)DBEnv_log_set_config, METH_VARARGS}, |
| 6447 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6448 | {"set_lg_bsize", (PyCFunction)DBEnv_set_lg_bsize, METH_VARARGS}, |
| 6449 | {"set_lg_dir", (PyCFunction)DBEnv_set_lg_dir, METH_VARARGS}, |
| 6450 | {"set_lg_max", (PyCFunction)DBEnv_set_lg_max, METH_VARARGS}, |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6451 | #if (DBVER >= 42) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6452 | {"get_lg_max", (PyCFunction)DBEnv_get_lg_max, METH_NOARGS}, |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6453 | #endif |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 6454 | {"set_lg_regionmax",(PyCFunction)DBEnv_set_lg_regionmax, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6455 | {"set_lk_detect", (PyCFunction)DBEnv_set_lk_detect, METH_VARARGS}, |
Gregory P. Smith | 8b96a35 | 2007-01-05 01:59:42 +0000 | [diff] [blame] | 6456 | #if (DBVER < 45) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6457 | {"set_lk_max", (PyCFunction)DBEnv_set_lk_max, METH_VARARGS}, |
Gregory P. Smith | 8b96a35 | 2007-01-05 01:59:42 +0000 | [diff] [blame] | 6458 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6459 | {"set_lk_max_locks", (PyCFunction)DBEnv_set_lk_max_locks, METH_VARARGS}, |
| 6460 | {"set_lk_max_lockers", (PyCFunction)DBEnv_set_lk_max_lockers, METH_VARARGS}, |
| 6461 | {"set_lk_max_objects", (PyCFunction)DBEnv_set_lk_max_objects, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6462 | {"set_mp_mmapsize", (PyCFunction)DBEnv_set_mp_mmapsize, METH_VARARGS}, |
| 6463 | {"set_tmp_dir", (PyCFunction)DBEnv_set_tmp_dir, METH_VARARGS}, |
| 6464 | {"txn_begin", (PyCFunction)DBEnv_txn_begin, METH_VARARGS|METH_KEYWORDS}, |
| 6465 | {"txn_checkpoint", (PyCFunction)DBEnv_txn_checkpoint, METH_VARARGS}, |
| 6466 | {"txn_stat", (PyCFunction)DBEnv_txn_stat, METH_VARARGS}, |
| 6467 | {"set_tx_max", (PyCFunction)DBEnv_set_tx_max, METH_VARARGS}, |
Gregory P. Smith | 8a47404 | 2006-01-27 07:05:40 +0000 | [diff] [blame] | 6468 | {"set_tx_timestamp", (PyCFunction)DBEnv_set_tx_timestamp, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6469 | {"lock_detect", (PyCFunction)DBEnv_lock_detect, METH_VARARGS}, |
| 6470 | {"lock_get", (PyCFunction)DBEnv_lock_get, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6471 | {"lock_id", (PyCFunction)DBEnv_lock_id, METH_NOARGS}, |
Gregory P. Smith | ac11e02 | 2007-11-05 02:56:31 +0000 | [diff] [blame] | 6472 | {"lock_id_free", (PyCFunction)DBEnv_lock_id_free, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6473 | {"lock_put", (PyCFunction)DBEnv_lock_put, METH_VARARGS}, |
| 6474 | {"lock_stat", (PyCFunction)DBEnv_lock_stat, METH_VARARGS}, |
| 6475 | {"log_archive", (PyCFunction)DBEnv_log_archive, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6476 | {"log_flush", (PyCFunction)DBEnv_log_flush, METH_NOARGS}, |
Gregory P. Smith | 76a82e8 | 2006-06-05 01:39:52 +0000 | [diff] [blame] | 6477 | {"log_stat", (PyCFunction)DBEnv_log_stat, METH_VARARGS}, |
Gregory P. Smith | db8a807 | 2006-06-05 01:56:15 +0000 | [diff] [blame] | 6478 | #if (DBVER >= 44) |
| 6479 | {"lsn_reset", (PyCFunction)DBEnv_lsn_reset, METH_VARARGS|METH_KEYWORDS}, |
| 6480 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6481 | {"set_get_returns_none",(PyCFunction)DBEnv_set_get_returns_none, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6482 | {"txn_recover", (PyCFunction)DBEnv_txn_recover, METH_NOARGS}, |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 6483 | {"set_rpc_server", (PyCFunction)DBEnv_set_rpc_server, |
| 6484 | METH_VARARGS||METH_KEYWORDS}, |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6485 | {"set_verbose", (PyCFunction)DBEnv_set_verbose, METH_VARARGS}, |
| 6486 | #if (DBVER >= 42) |
| 6487 | {"get_verbose", (PyCFunction)DBEnv_get_verbose, METH_VARARGS}, |
| 6488 | #endif |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6489 | {"set_private", (PyCFunction)DBEnv_set_private, METH_O}, |
| 6490 | {"get_private", (PyCFunction)DBEnv_get_private, METH_NOARGS}, |
| 6491 | {"rep_start", (PyCFunction)DBEnv_rep_start, |
| 6492 | METH_VARARGS|METH_KEYWORDS}, |
| 6493 | {"rep_set_transport", (PyCFunction)DBEnv_rep_set_transport, METH_VARARGS}, |
| 6494 | {"rep_process_message", (PyCFunction)DBEnv_rep_process_message, |
| 6495 | METH_VARARGS}, |
| 6496 | #if (DBVER >= 46) |
| 6497 | {"rep_elect", (PyCFunction)DBEnv_rep_elect, METH_VARARGS}, |
| 6498 | #endif |
| 6499 | #if (DBVER >= 44) |
| 6500 | {"rep_set_config", (PyCFunction)DBEnv_rep_set_config, METH_VARARGS}, |
| 6501 | {"rep_get_config", (PyCFunction)DBEnv_rep_get_config, METH_VARARGS}, |
| 6502 | {"rep_sync", (PyCFunction)DBEnv_rep_sync, METH_NOARGS}, |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6503 | #endif |
| 6504 | #if (DBVER >= 45) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6505 | {"rep_set_limit", (PyCFunction)DBEnv_rep_set_limit, METH_VARARGS}, |
| 6506 | {"rep_get_limit", (PyCFunction)DBEnv_rep_get_limit, METH_NOARGS}, |
| 6507 | #endif |
| 6508 | #if (DBVER >= 47) |
| 6509 | {"rep_set_request", (PyCFunction)DBEnv_rep_set_request, METH_VARARGS}, |
| 6510 | {"rep_get_request", (PyCFunction)DBEnv_rep_get_request, METH_NOARGS}, |
| 6511 | #endif |
| 6512 | #if (DBVER >= 45) |
| 6513 | {"set_event_notify", (PyCFunction)DBEnv_set_event_notify, METH_O}, |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6514 | #endif |
| 6515 | #if (DBVER >= 45) |
| 6516 | {"rep_set_nsites", (PyCFunction)DBEnv_rep_set_nsites, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6517 | {"rep_get_nsites", (PyCFunction)DBEnv_rep_get_nsites, METH_NOARGS}, |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6518 | {"rep_set_priority", (PyCFunction)DBEnv_rep_set_priority, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6519 | {"rep_get_priority", (PyCFunction)DBEnv_rep_get_priority, METH_NOARGS}, |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6520 | {"rep_set_timeout", (PyCFunction)DBEnv_rep_set_timeout, METH_VARARGS}, |
| 6521 | {"rep_get_timeout", (PyCFunction)DBEnv_rep_get_timeout, METH_VARARGS}, |
| 6522 | #endif |
| 6523 | #if (DBVER >= 45) |
| 6524 | {"repmgr_start", (PyCFunction)DBEnv_repmgr_start, |
| 6525 | METH_VARARGS|METH_KEYWORDS}, |
| 6526 | {"repmgr_set_local_site", (PyCFunction)DBEnv_repmgr_set_local_site, |
| 6527 | METH_VARARGS|METH_KEYWORDS}, |
| 6528 | {"repmgr_add_remote_site", (PyCFunction)DBEnv_repmgr_add_remote_site, |
| 6529 | METH_VARARGS|METH_KEYWORDS}, |
| 6530 | {"repmgr_set_ack_policy", (PyCFunction)DBEnv_repmgr_set_ack_policy, |
| 6531 | METH_VARARGS}, |
| 6532 | {"repmgr_get_ack_policy", (PyCFunction)DBEnv_repmgr_get_ack_policy, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6533 | METH_NOARGS}, |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6534 | {"repmgr_site_list", (PyCFunction)DBEnv_repmgr_site_list, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6535 | METH_NOARGS}, |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6536 | #endif |
| 6537 | #if (DBVER >= 46) |
| 6538 | {"repmgr_stat", (PyCFunction)DBEnv_repmgr_stat, |
| 6539 | METH_VARARGS|METH_KEYWORDS}, |
| 6540 | {"repmgr_stat_print", (PyCFunction)DBEnv_repmgr_stat_print, |
| 6541 | METH_VARARGS|METH_KEYWORDS}, |
| 6542 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6543 | {NULL, NULL} /* sentinel */ |
| 6544 | }; |
| 6545 | |
| 6546 | |
| 6547 | static PyMethodDef DBTxn_methods[] = { |
| 6548 | {"commit", (PyCFunction)DBTxn_commit, METH_VARARGS}, |
| 6549 | {"prepare", (PyCFunction)DBTxn_prepare, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6550 | {"discard", (PyCFunction)DBTxn_discard, METH_NOARGS}, |
| 6551 | {"abort", (PyCFunction)DBTxn_abort, METH_NOARGS}, |
| 6552 | {"id", (PyCFunction)DBTxn_id, METH_NOARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6553 | {NULL, NULL} /* sentinel */ |
| 6554 | }; |
| 6555 | |
| 6556 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6557 | #if (DBVER >= 43) |
| 6558 | static PyMethodDef DBSequence_methods[] = { |
| 6559 | {"close", (PyCFunction)DBSequence_close, METH_VARARGS}, |
| 6560 | {"get", (PyCFunction)DBSequence_get, METH_VARARGS|METH_KEYWORDS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6561 | {"get_dbp", (PyCFunction)DBSequence_get_dbp, METH_NOARGS}, |
| 6562 | {"get_key", (PyCFunction)DBSequence_get_key, METH_NOARGS}, |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6563 | {"init_value", (PyCFunction)DBSequence_init_value, METH_VARARGS}, |
| 6564 | {"open", (PyCFunction)DBSequence_open, METH_VARARGS|METH_KEYWORDS}, |
| 6565 | {"remove", (PyCFunction)DBSequence_remove, METH_VARARGS|METH_KEYWORDS}, |
| 6566 | {"set_cachesize", (PyCFunction)DBSequence_set_cachesize, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6567 | {"get_cachesize", (PyCFunction)DBSequence_get_cachesize, METH_NOARGS}, |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6568 | {"set_flags", (PyCFunction)DBSequence_set_flags, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6569 | {"get_flags", (PyCFunction)DBSequence_get_flags, METH_NOARGS}, |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6570 | {"set_range", (PyCFunction)DBSequence_set_range, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6571 | {"get_range", (PyCFunction)DBSequence_get_range, METH_NOARGS}, |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6572 | {"stat", (PyCFunction)DBSequence_stat, METH_VARARGS|METH_KEYWORDS}, |
| 6573 | {NULL, NULL} /* sentinel */ |
| 6574 | }; |
| 6575 | #endif |
| 6576 | |
| 6577 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6578 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6579 | DBEnv_db_home_get(DBEnvObject* self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6580 | { |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6581 | const char *home = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6582 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6583 | CHECK_ENV_NOT_CLOSED(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6584 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6585 | #if (DBVER >= 42) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6586 | self->db_env->get_home(self->db_env, &home); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6587 | #else |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6588 | home=self->db_env->db_home; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6589 | #endif |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6590 | |
| 6591 | if (home == NULL) { |
| 6592 | RETURN_NONE(); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6593 | } |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6594 | return PyBytes_FromString(home); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6595 | } |
| 6596 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6597 | static PyGetSetDef DBEnv_getsets[] = { |
| 6598 | {"db_home", (getter)DBEnv_db_home_get, NULL,}, |
| 6599 | {NULL} |
| 6600 | }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6601 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6602 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6603 | statichere PyTypeObject DB_Type = { |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6604 | #if (PY_VERSION_HEX < 0x03000000) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6605 | PyObject_HEAD_INIT(NULL) |
| 6606 | 0, /*ob_size*/ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6607 | #else |
| 6608 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6609 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6610 | "DB", /*tp_name*/ |
| 6611 | sizeof(DBObject), /*tp_basicsize*/ |
| 6612 | 0, /*tp_itemsize*/ |
| 6613 | /* methods */ |
| 6614 | (destructor)DB_dealloc, /*tp_dealloc*/ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6615 | 0, /*tp_print*/ |
| 6616 | 0, /*tp_getattr*/ |
| 6617 | 0, /*tp_setattr*/ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6618 | 0, /*tp_compare*/ |
| 6619 | 0, /*tp_repr*/ |
| 6620 | 0, /*tp_as_number*/ |
| 6621 | 0, /*tp_as_sequence*/ |
| 6622 | &DB_mapping,/*tp_as_mapping*/ |
| 6623 | 0, /*tp_hash*/ |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 6624 | 0, /* tp_call */ |
| 6625 | 0, /* tp_str */ |
| 6626 | 0, /* tp_getattro */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6627 | 0, /* tp_setattro */ |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 6628 | 0, /* tp_as_buffer */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6629 | #if (PY_VERSION_HEX < 0x03000000) |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 6630 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6631 | #else |
| 6632 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 6633 | #endif |
| 6634 | 0, /* tp_doc */ |
| 6635 | 0, /* tp_traverse */ |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 6636 | 0, /* tp_clear */ |
| 6637 | 0, /* tp_richcompare */ |
| 6638 | offsetof(DBObject, in_weakreflist), /* tp_weaklistoffset */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6639 | 0, /*tp_iter*/ |
| 6640 | 0, /*tp_iternext*/ |
| 6641 | DB_methods, /*tp_methods*/ |
| 6642 | 0, /*tp_members*/ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6643 | }; |
| 6644 | |
| 6645 | |
| 6646 | statichere PyTypeObject DBCursor_Type = { |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6647 | #if (PY_VERSION_HEX < 0x03000000) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6648 | PyObject_HEAD_INIT(NULL) |
| 6649 | 0, /*ob_size*/ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6650 | #else |
| 6651 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6652 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6653 | "DBCursor", /*tp_name*/ |
| 6654 | sizeof(DBCursorObject), /*tp_basicsize*/ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6655 | 0, /*tp_itemsize*/ |
| 6656 | /* methods */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6657 | (destructor)DBCursor_dealloc,/*tp_dealloc*/ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6658 | 0, /*tp_print*/ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6659 | 0, /*tp_getattr*/ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6660 | 0, /*tp_setattr*/ |
| 6661 | 0, /*tp_compare*/ |
| 6662 | 0, /*tp_repr*/ |
| 6663 | 0, /*tp_as_number*/ |
| 6664 | 0, /*tp_as_sequence*/ |
| 6665 | 0, /*tp_as_mapping*/ |
| 6666 | 0, /*tp_hash*/ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6667 | 0, /*tp_call*/ |
| 6668 | 0, /*tp_str*/ |
| 6669 | 0, /*tp_getattro*/ |
| 6670 | 0, /*tp_setattro*/ |
| 6671 | 0, /*tp_as_buffer*/ |
| 6672 | #if (PY_VERSION_HEX < 0x03000000) |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 6673 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6674 | #else |
| 6675 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 6676 | #endif |
| 6677 | 0, /* tp_doc */ |
| 6678 | 0, /* tp_traverse */ |
| 6679 | 0, /* tp_clear */ |
| 6680 | 0, /* tp_richcompare */ |
| 6681 | offsetof(DBCursorObject, in_weakreflist), /* tp_weaklistoffset */ |
| 6682 | 0, /*tp_iter*/ |
| 6683 | 0, /*tp_iternext*/ |
| 6684 | DBCursor_methods, /*tp_methods*/ |
| 6685 | 0, /*tp_members*/ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6686 | }; |
| 6687 | |
| 6688 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6689 | statichere PyTypeObject DBEnv_Type = { |
| 6690 | #if (PY_VERSION_HEX < 0x03000000) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6691 | PyObject_HEAD_INIT(NULL) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6692 | 0, /*ob_size*/ |
| 6693 | #else |
| 6694 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6695 | #endif |
| 6696 | "DBEnv", /*tp_name*/ |
| 6697 | sizeof(DBEnvObject), /*tp_basicsize*/ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6698 | 0, /*tp_itemsize*/ |
| 6699 | /* methods */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6700 | (destructor)DBEnv_dealloc, /*tp_dealloc*/ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6701 | 0, /*tp_print*/ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6702 | 0, /*tp_getattr*/ |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6703 | 0, /*tp_setattr*/ |
| 6704 | 0, /*tp_compare*/ |
| 6705 | 0, /*tp_repr*/ |
| 6706 | 0, /*tp_as_number*/ |
| 6707 | 0, /*tp_as_sequence*/ |
| 6708 | 0, /*tp_as_mapping*/ |
| 6709 | 0, /*tp_hash*/ |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6710 | 0, /* tp_call */ |
| 6711 | 0, /* tp_str */ |
| 6712 | 0, /* tp_getattro */ |
| 6713 | 0, /* tp_setattro */ |
| 6714 | 0, /* tp_as_buffer */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6715 | #if (PY_VERSION_HEX < 0x03000000) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6716 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6717 | #else |
| 6718 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 6719 | #endif |
| 6720 | 0, /* tp_doc */ |
| 6721 | 0, /* tp_traverse */ |
| 6722 | 0, /* tp_clear */ |
| 6723 | 0, /* tp_richcompare */ |
| 6724 | offsetof(DBEnvObject, in_weakreflist), /* tp_weaklistoffset */ |
| 6725 | 0, /* tp_iter */ |
| 6726 | 0, /* tp_iternext */ |
| 6727 | DBEnv_methods, /* tp_methods */ |
| 6728 | 0, /* tp_members */ |
| 6729 | DBEnv_getsets, /* tp_getsets */ |
| 6730 | }; |
| 6731 | |
| 6732 | statichere PyTypeObject DBTxn_Type = { |
| 6733 | #if (PY_VERSION_HEX < 0x03000000) |
| 6734 | PyObject_HEAD_INIT(NULL) |
| 6735 | 0, /*ob_size*/ |
| 6736 | #else |
| 6737 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6738 | #endif |
| 6739 | "DBTxn", /*tp_name*/ |
| 6740 | sizeof(DBTxnObject), /*tp_basicsize*/ |
| 6741 | 0, /*tp_itemsize*/ |
| 6742 | /* methods */ |
| 6743 | (destructor)DBTxn_dealloc, /*tp_dealloc*/ |
| 6744 | 0, /*tp_print*/ |
| 6745 | 0, /*tp_getattr*/ |
| 6746 | 0, /*tp_setattr*/ |
| 6747 | 0, /*tp_compare*/ |
| 6748 | 0, /*tp_repr*/ |
| 6749 | 0, /*tp_as_number*/ |
| 6750 | 0, /*tp_as_sequence*/ |
| 6751 | 0, /*tp_as_mapping*/ |
| 6752 | 0, /*tp_hash*/ |
| 6753 | 0, /* tp_call */ |
| 6754 | 0, /* tp_str */ |
| 6755 | 0, /* tp_getattro */ |
| 6756 | 0, /* tp_setattro */ |
| 6757 | 0, /* tp_as_buffer */ |
| 6758 | #if (PY_VERSION_HEX < 0x03000000) |
| 6759 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
| 6760 | #else |
| 6761 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 6762 | #endif |
| 6763 | 0, /* tp_doc */ |
| 6764 | 0, /* tp_traverse */ |
| 6765 | 0, /* tp_clear */ |
| 6766 | 0, /* tp_richcompare */ |
| 6767 | offsetof(DBTxnObject, in_weakreflist), /* tp_weaklistoffset */ |
| 6768 | 0, /*tp_iter*/ |
| 6769 | 0, /*tp_iternext*/ |
| 6770 | DBTxn_methods, /*tp_methods*/ |
| 6771 | 0, /*tp_members*/ |
| 6772 | }; |
| 6773 | |
| 6774 | |
| 6775 | statichere PyTypeObject DBLock_Type = { |
| 6776 | #if (PY_VERSION_HEX < 0x03000000) |
| 6777 | PyObject_HEAD_INIT(NULL) |
| 6778 | 0, /*ob_size*/ |
| 6779 | #else |
| 6780 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6781 | #endif |
| 6782 | "DBLock", /*tp_name*/ |
| 6783 | sizeof(DBLockObject), /*tp_basicsize*/ |
| 6784 | 0, /*tp_itemsize*/ |
| 6785 | /* methods */ |
| 6786 | (destructor)DBLock_dealloc, /*tp_dealloc*/ |
| 6787 | 0, /*tp_print*/ |
| 6788 | 0, /*tp_getattr*/ |
| 6789 | 0, /*tp_setattr*/ |
| 6790 | 0, /*tp_compare*/ |
| 6791 | 0, /*tp_repr*/ |
| 6792 | 0, /*tp_as_number*/ |
| 6793 | 0, /*tp_as_sequence*/ |
| 6794 | 0, /*tp_as_mapping*/ |
| 6795 | 0, /*tp_hash*/ |
| 6796 | 0, /* tp_call */ |
| 6797 | 0, /* tp_str */ |
| 6798 | 0, /* tp_getattro */ |
| 6799 | 0, /* tp_setattro */ |
| 6800 | 0, /* tp_as_buffer */ |
| 6801 | #if (PY_VERSION_HEX < 0x03000000) |
| 6802 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
| 6803 | #else |
| 6804 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 6805 | #endif |
| 6806 | 0, /* tp_doc */ |
| 6807 | 0, /* tp_traverse */ |
| 6808 | 0, /* tp_clear */ |
| 6809 | 0, /* tp_richcompare */ |
| 6810 | offsetof(DBLockObject, in_weakreflist), /* tp_weaklistoffset */ |
| 6811 | }; |
| 6812 | |
| 6813 | #if (DBVER >= 43) |
| 6814 | statichere PyTypeObject DBSequence_Type = { |
| 6815 | #if (PY_VERSION_HEX < 0x03000000) |
| 6816 | PyObject_HEAD_INIT(NULL) |
| 6817 | 0, /*ob_size*/ |
| 6818 | #else |
| 6819 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6820 | #endif |
| 6821 | "DBSequence", /*tp_name*/ |
| 6822 | sizeof(DBSequenceObject), /*tp_basicsize*/ |
| 6823 | 0, /*tp_itemsize*/ |
| 6824 | /* methods */ |
| 6825 | (destructor)DBSequence_dealloc, /*tp_dealloc*/ |
| 6826 | 0, /*tp_print*/ |
| 6827 | 0, /*tp_getattr*/ |
| 6828 | 0, /*tp_setattr*/ |
| 6829 | 0, /*tp_compare*/ |
| 6830 | 0, /*tp_repr*/ |
| 6831 | 0, /*tp_as_number*/ |
| 6832 | 0, /*tp_as_sequence*/ |
| 6833 | 0, /*tp_as_mapping*/ |
| 6834 | 0, /*tp_hash*/ |
| 6835 | 0, /* tp_call */ |
| 6836 | 0, /* tp_str */ |
| 6837 | 0, /* tp_getattro */ |
| 6838 | 0, /* tp_setattro */ |
| 6839 | 0, /* tp_as_buffer */ |
| 6840 | #if (PY_VERSION_HEX < 0x03000000) |
| 6841 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
| 6842 | #else |
| 6843 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 6844 | #endif |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6845 | 0, /* tp_doc */ |
| 6846 | 0, /* tp_traverse */ |
| 6847 | 0, /* tp_clear */ |
| 6848 | 0, /* tp_richcompare */ |
| 6849 | offsetof(DBSequenceObject, in_weakreflist), /* tp_weaklistoffset */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6850 | 0, /*tp_iter*/ |
| 6851 | 0, /*tp_iternext*/ |
| 6852 | DBSequence_methods, /*tp_methods*/ |
| 6853 | 0, /*tp_members*/ |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6854 | }; |
| 6855 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6856 | |
| 6857 | /* --------------------------------------------------------------------- */ |
| 6858 | /* Module-level functions */ |
| 6859 | |
| 6860 | static PyObject* |
| 6861 | DB_construct(PyObject* self, PyObject* args, PyObject* kwargs) |
| 6862 | { |
| 6863 | PyObject* dbenvobj = NULL; |
| 6864 | int flags = 0; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 6865 | static char* kwnames[] = { "dbEnv", "flags", NULL}; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6866 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 6867 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:DB", kwnames, |
| 6868 | &dbenvobj, &flags)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6869 | return NULL; |
| 6870 | if (dbenvobj == Py_None) |
| 6871 | dbenvobj = NULL; |
| 6872 | else if (dbenvobj && !DBEnvObject_Check(dbenvobj)) { |
| 6873 | makeTypeError("DBEnv", dbenvobj); |
| 6874 | return NULL; |
| 6875 | } |
| 6876 | |
| 6877 | return (PyObject* )newDBObject((DBEnvObject*)dbenvobj, flags); |
| 6878 | } |
| 6879 | |
| 6880 | |
| 6881 | static PyObject* |
| 6882 | DBEnv_construct(PyObject* self, PyObject* args) |
| 6883 | { |
| 6884 | int flags = 0; |
| 6885 | if (!PyArg_ParseTuple(args, "|i:DbEnv", &flags)) return NULL; |
| 6886 | return (PyObject* )newDBEnvObject(flags); |
| 6887 | } |
| 6888 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6889 | #if (DBVER >= 43) |
| 6890 | static PyObject* |
| 6891 | DBSequence_construct(PyObject* self, PyObject* args, PyObject* kwargs) |
| 6892 | { |
Neal Norwitz | b4fcf8d | 2006-06-11 05:44:18 +0000 | [diff] [blame] | 6893 | PyObject* dbobj; |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6894 | int flags = 0; |
| 6895 | static char* kwnames[] = { "db", "flags", NULL}; |
| 6896 | |
| 6897 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:DBSequence", kwnames, &dbobj, &flags)) |
| 6898 | return NULL; |
Neal Norwitz | b4fcf8d | 2006-06-11 05:44:18 +0000 | [diff] [blame] | 6899 | if (!DBObject_Check(dbobj)) { |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6900 | makeTypeError("DB", dbobj); |
| 6901 | return NULL; |
| 6902 | } |
| 6903 | return (PyObject* )newDBSequenceObject((DBObject*)dbobj, flags); |
| 6904 | } |
| 6905 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6906 | |
| 6907 | static char bsddb_version_doc[] = |
| 6908 | "Returns a tuple of major, minor, and patch release numbers of the\n\ |
| 6909 | underlying DB library."; |
| 6910 | |
| 6911 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6912 | bsddb_version(PyObject* self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6913 | { |
| 6914 | int major, minor, patch; |
| 6915 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6916 | db_version(&major, &minor, &patch); |
| 6917 | return Py_BuildValue("(iii)", major, minor, patch); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6918 | } |
| 6919 | |
| 6920 | |
| 6921 | /* List of functions defined in the module */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6922 | static PyMethodDef bsddb_methods[] = { |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6923 | {"DB", (PyCFunction)DB_construct, METH_VARARGS | METH_KEYWORDS }, |
| 6924 | {"DBEnv", (PyCFunction)DBEnv_construct, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6925 | #if (DBVER >= 43) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6926 | {"DBSequence", (PyCFunction)DBSequence_construct, METH_VARARGS | METH_KEYWORDS }, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6927 | #endif |
| 6928 | {"version", (PyCFunction)bsddb_version, METH_NOARGS, bsddb_version_doc}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6929 | {NULL, NULL} /* sentinel */ |
| 6930 | }; |
| 6931 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6932 | |
Gregory P. Smith | 3925053 | 2007-10-09 06:02:21 +0000 | [diff] [blame] | 6933 | /* API structure */ |
| 6934 | static BSDDB_api bsddb_api; |
| 6935 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6936 | |
| 6937 | /* --------------------------------------------------------------------- */ |
| 6938 | /* Module initialization */ |
| 6939 | |
| 6940 | |
| 6941 | /* Convenience routine to export an integer value. |
| 6942 | * Errors are silently ignored, for better or for worse... |
| 6943 | */ |
| 6944 | #define ADD_INT(dict, NAME) _addIntToDict(dict, #NAME, NAME) |
| 6945 | |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 6946 | #define MODULE_NAME_MAX_LEN 11 |
| 6947 | static char _bsddbModuleName[MODULE_NAME_MAX_LEN+1] = "_bsddb"; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6948 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6949 | #if (PY_VERSION_HEX >= 0x03000000) |
| 6950 | static struct PyModuleDef bsddbmodule = { |
| 6951 | PyModuleDef_HEAD_INIT, |
| 6952 | _bsddbModuleName, /* Name of module */ |
| 6953 | NULL, /* module documentation, may be NULL */ |
| 6954 | -1, /* size of per-interpreter state of the module, |
| 6955 | or -1 if the module keeps state in global variables. */ |
| 6956 | bsddb_methods, |
| 6957 | NULL, /* Reload */ |
| 6958 | NULL, /* Traverse */ |
| 6959 | NULL, /* Clear */ |
| 6960 | NULL /* Free */ |
| 6961 | }; |
| 6962 | #endif |
| 6963 | |
| 6964 | |
| 6965 | #if (PY_VERSION_HEX < 0x03000000) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6966 | DL_EXPORT(void) init_bsddb(void) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6967 | #else |
| 6968 | PyMODINIT_FUNC PyInit__bsddb(void) /* Note the two underscores */ |
| 6969 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6970 | { |
| 6971 | PyObject* m; |
| 6972 | PyObject* d; |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 6973 | PyObject* pybsddb_version_s = PyBytes_FromString( PY_BSDDB_VERSION ); |
| 6974 | PyObject* db_version_s = PyBytes_FromString( DB_VERSION_STRING ); |
| 6975 | PyObject* cvsid_s = PyBytes_FromString( rcs_id ); |
Gregory P. Smith | 3925053 | 2007-10-09 06:02:21 +0000 | [diff] [blame] | 6976 | PyObject* py_api; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6977 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6978 | /* Initialize object types */ |
| 6979 | if ((PyType_Ready(&DB_Type) < 0) |
| 6980 | || (PyType_Ready(&DBCursor_Type) < 0) |
| 6981 | || (PyType_Ready(&DBEnv_Type) < 0) |
| 6982 | || (PyType_Ready(&DBTxn_Type) < 0) |
| 6983 | || (PyType_Ready(&DBLock_Type) < 0) |
| 6984 | #if (DBVER >= 43) |
| 6985 | || (PyType_Ready(&DBSequence_Type) < 0) |
| 6986 | #endif |
| 6987 | ) { |
| 6988 | #if (PY_VERSION_HEX < 0x03000000) |
| 6989 | return; |
| 6990 | #else |
| 6991 | return NULL; |
| 6992 | #endif |
| 6993 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6994 | |
Mark Hammond | a69d409 | 2003-04-22 23:13:27 +0000 | [diff] [blame] | 6995 | #if defined(WITH_THREAD) && !defined(MYDB_USE_GILSTATE) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6996 | /* Save the current interpreter, so callbacks can do the right thing. */ |
Nicholas Bastin | 2786d90 | 2004-03-25 02:16:23 +0000 | [diff] [blame] | 6997 | _db_interpreterState = PyThreadState_GET()->interp; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6998 | #endif |
| 6999 | |
| 7000 | /* Create the module and add the functions */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7001 | #if (PY_VERSION_HEX < 0x03000000) |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7002 | m = Py_InitModule(_bsddbModuleName, bsddb_methods); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7003 | #else |
| 7004 | m=PyModule_Create(&bsddbmodule); |
| 7005 | #endif |
| 7006 | if (m == NULL) { |
| 7007 | #if (PY_VERSION_HEX < 0x03000000) |
| 7008 | return; |
| 7009 | #else |
| 7010 | return NULL; |
| 7011 | #endif |
| 7012 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7013 | |
| 7014 | /* Add some symbolic constants to the module */ |
| 7015 | d = PyModule_GetDict(m); |
| 7016 | PyDict_SetItemString(d, "__version__", pybsddb_version_s); |
| 7017 | PyDict_SetItemString(d, "cvsid", cvsid_s); |
| 7018 | PyDict_SetItemString(d, "DB_VERSION_STRING", db_version_s); |
| 7019 | Py_DECREF(pybsddb_version_s); |
| 7020 | pybsddb_version_s = NULL; |
| 7021 | Py_DECREF(cvsid_s); |
| 7022 | cvsid_s = NULL; |
| 7023 | Py_DECREF(db_version_s); |
| 7024 | db_version_s = NULL; |
| 7025 | |
| 7026 | ADD_INT(d, DB_VERSION_MAJOR); |
| 7027 | ADD_INT(d, DB_VERSION_MINOR); |
| 7028 | ADD_INT(d, DB_VERSION_PATCH); |
| 7029 | |
| 7030 | ADD_INT(d, DB_MAX_PAGES); |
| 7031 | ADD_INT(d, DB_MAX_RECORDS); |
| 7032 | |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7033 | #if (DBVER >= 42) |
| 7034 | ADD_INT(d, DB_RPCCLIENT); |
| 7035 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7036 | ADD_INT(d, DB_CLIENT); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7037 | /* allow apps to be written using DB_RPCCLIENT on older Berkeley DB */ |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7038 | _addIntToDict(d, "DB_RPCCLIENT", DB_CLIENT); |
| 7039 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7040 | ADD_INT(d, DB_XA_CREATE); |
| 7041 | |
| 7042 | ADD_INT(d, DB_CREATE); |
| 7043 | ADD_INT(d, DB_NOMMAP); |
| 7044 | ADD_INT(d, DB_THREAD); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7045 | #if (DBVER >= 45) |
| 7046 | ADD_INT(d, DB_MULTIVERSION); |
| 7047 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7048 | |
| 7049 | ADD_INT(d, DB_FORCE); |
| 7050 | ADD_INT(d, DB_INIT_CDB); |
| 7051 | ADD_INT(d, DB_INIT_LOCK); |
| 7052 | ADD_INT(d, DB_INIT_LOG); |
| 7053 | ADD_INT(d, DB_INIT_MPOOL); |
| 7054 | ADD_INT(d, DB_INIT_TXN); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7055 | ADD_INT(d, DB_JOINENV); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7056 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7057 | ADD_INT(d, DB_XIDDATASIZE); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7058 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7059 | ADD_INT(d, DB_RECOVER); |
| 7060 | ADD_INT(d, DB_RECOVER_FATAL); |
| 7061 | ADD_INT(d, DB_TXN_NOSYNC); |
| 7062 | ADD_INT(d, DB_USE_ENVIRON); |
| 7063 | ADD_INT(d, DB_USE_ENVIRON_ROOT); |
| 7064 | |
| 7065 | ADD_INT(d, DB_LOCKDOWN); |
| 7066 | ADD_INT(d, DB_PRIVATE); |
| 7067 | ADD_INT(d, DB_SYSTEM_MEM); |
| 7068 | |
| 7069 | ADD_INT(d, DB_TXN_SYNC); |
| 7070 | ADD_INT(d, DB_TXN_NOWAIT); |
| 7071 | |
| 7072 | ADD_INT(d, DB_EXCL); |
| 7073 | ADD_INT(d, DB_FCNTL_LOCKING); |
| 7074 | ADD_INT(d, DB_ODDFILESIZE); |
| 7075 | ADD_INT(d, DB_RDWRMASTER); |
| 7076 | ADD_INT(d, DB_RDONLY); |
| 7077 | ADD_INT(d, DB_TRUNCATE); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7078 | ADD_INT(d, DB_EXTENT); |
| 7079 | ADD_INT(d, DB_CDB_ALLDB); |
| 7080 | ADD_INT(d, DB_VERIFY); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7081 | ADD_INT(d, DB_UPGRADE); |
| 7082 | |
| 7083 | ADD_INT(d, DB_AGGRESSIVE); |
| 7084 | ADD_INT(d, DB_NOORDERCHK); |
| 7085 | ADD_INT(d, DB_ORDERCHKONLY); |
| 7086 | ADD_INT(d, DB_PR_PAGE); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7087 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7088 | ADD_INT(d, DB_PR_RECOVERYTEST); |
| 7089 | ADD_INT(d, DB_SALVAGE); |
| 7090 | |
| 7091 | ADD_INT(d, DB_LOCK_NORUN); |
| 7092 | ADD_INT(d, DB_LOCK_DEFAULT); |
| 7093 | ADD_INT(d, DB_LOCK_OLDEST); |
| 7094 | ADD_INT(d, DB_LOCK_RANDOM); |
| 7095 | ADD_INT(d, DB_LOCK_YOUNGEST); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7096 | ADD_INT(d, DB_LOCK_MAXLOCKS); |
| 7097 | ADD_INT(d, DB_LOCK_MINLOCKS); |
| 7098 | ADD_INT(d, DB_LOCK_MINWRITE); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7099 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7100 | ADD_INT(d, DB_LOCK_EXPIRE); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7101 | #if (DBVER >= 43) |
| 7102 | ADD_INT(d, DB_LOCK_MAXWRITE); |
| 7103 | #endif |
| 7104 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 7105 | _addIntToDict(d, "DB_LOCK_CONFLICT", 0); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7106 | |
| 7107 | ADD_INT(d, DB_LOCK_DUMP); |
| 7108 | ADD_INT(d, DB_LOCK_GET); |
| 7109 | ADD_INT(d, DB_LOCK_INHERIT); |
| 7110 | ADD_INT(d, DB_LOCK_PUT); |
| 7111 | ADD_INT(d, DB_LOCK_PUT_ALL); |
| 7112 | ADD_INT(d, DB_LOCK_PUT_OBJ); |
| 7113 | |
| 7114 | ADD_INT(d, DB_LOCK_NG); |
| 7115 | ADD_INT(d, DB_LOCK_READ); |
| 7116 | ADD_INT(d, DB_LOCK_WRITE); |
| 7117 | ADD_INT(d, DB_LOCK_NOWAIT); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7118 | ADD_INT(d, DB_LOCK_WAIT); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7119 | ADD_INT(d, DB_LOCK_IWRITE); |
| 7120 | ADD_INT(d, DB_LOCK_IREAD); |
| 7121 | ADD_INT(d, DB_LOCK_IWR); |
Gregory P. Smith | 29602d2 | 2006-01-24 09:46:48 +0000 | [diff] [blame] | 7122 | #if (DBVER < 44) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7123 | ADD_INT(d, DB_LOCK_DIRTY); |
Gregory P. Smith | 29602d2 | 2006-01-24 09:46:48 +0000 | [diff] [blame] | 7124 | #else |
| 7125 | ADD_INT(d, DB_LOCK_READ_UNCOMMITTED); /* renamed in 4.4 */ |
| 7126 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7127 | ADD_INT(d, DB_LOCK_WWRITE); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7128 | |
| 7129 | ADD_INT(d, DB_LOCK_RECORD); |
| 7130 | ADD_INT(d, DB_LOCK_UPGRADE); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7131 | ADD_INT(d, DB_LOCK_SWITCH); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7132 | ADD_INT(d, DB_LOCK_UPGRADE_WRITE); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7133 | |
| 7134 | ADD_INT(d, DB_LOCK_NOWAIT); |
| 7135 | ADD_INT(d, DB_LOCK_RECORD); |
| 7136 | ADD_INT(d, DB_LOCK_UPGRADE); |
| 7137 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7138 | ADD_INT(d, DB_LSTAT_ABORTED); |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 7139 | #if (DBVER < 43) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7140 | ADD_INT(d, DB_LSTAT_ERR); |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 7141 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7142 | ADD_INT(d, DB_LSTAT_FREE); |
| 7143 | ADD_INT(d, DB_LSTAT_HELD); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7144 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7145 | ADD_INT(d, DB_LSTAT_PENDING); |
| 7146 | ADD_INT(d, DB_LSTAT_WAITING); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7147 | |
| 7148 | ADD_INT(d, DB_ARCH_ABS); |
| 7149 | ADD_INT(d, DB_ARCH_DATA); |
| 7150 | ADD_INT(d, DB_ARCH_LOG); |
Gregory P. Smith | 3dd2002 | 2006-06-05 00:31:01 +0000 | [diff] [blame] | 7151 | #if (DBVER >= 42) |
| 7152 | ADD_INT(d, DB_ARCH_REMOVE); |
| 7153 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7154 | |
| 7155 | ADD_INT(d, DB_BTREE); |
| 7156 | ADD_INT(d, DB_HASH); |
| 7157 | ADD_INT(d, DB_RECNO); |
| 7158 | ADD_INT(d, DB_QUEUE); |
| 7159 | ADD_INT(d, DB_UNKNOWN); |
| 7160 | |
| 7161 | ADD_INT(d, DB_DUP); |
| 7162 | ADD_INT(d, DB_DUPSORT); |
| 7163 | ADD_INT(d, DB_RECNUM); |
| 7164 | ADD_INT(d, DB_RENUMBER); |
| 7165 | ADD_INT(d, DB_REVSPLITOFF); |
| 7166 | ADD_INT(d, DB_SNAPSHOT); |
| 7167 | |
| 7168 | ADD_INT(d, DB_JOIN_NOSORT); |
| 7169 | |
| 7170 | ADD_INT(d, DB_AFTER); |
| 7171 | ADD_INT(d, DB_APPEND); |
| 7172 | ADD_INT(d, DB_BEFORE); |
Gregory P. Smith | 8b96a35 | 2007-01-05 01:59:42 +0000 | [diff] [blame] | 7173 | #if (DBVER < 45) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7174 | ADD_INT(d, DB_CACHED_COUNTS); |
Gregory P. Smith | 8b96a35 | 2007-01-05 01:59:42 +0000 | [diff] [blame] | 7175 | #endif |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 7176 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 7177 | #if (DBVER >= 41) |
| 7178 | _addIntToDict(d, "DB_CHECKPOINT", 0); |
| 7179 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7180 | ADD_INT(d, DB_CHECKPOINT); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 7181 | ADD_INT(d, DB_CURLSN); |
| 7182 | #endif |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7183 | #if (DBVER <= 41) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7184 | ADD_INT(d, DB_COMMIT); |
| 7185 | #endif |
| 7186 | ADD_INT(d, DB_CONSUME); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7187 | ADD_INT(d, DB_CONSUME_WAIT); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7188 | ADD_INT(d, DB_CURRENT); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7189 | ADD_INT(d, DB_FAST_STAT); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7190 | ADD_INT(d, DB_FIRST); |
| 7191 | ADD_INT(d, DB_FLUSH); |
| 7192 | ADD_INT(d, DB_GET_BOTH); |
| 7193 | ADD_INT(d, DB_GET_RECNO); |
| 7194 | ADD_INT(d, DB_JOIN_ITEM); |
| 7195 | ADD_INT(d, DB_KEYFIRST); |
| 7196 | ADD_INT(d, DB_KEYLAST); |
| 7197 | ADD_INT(d, DB_LAST); |
| 7198 | ADD_INT(d, DB_NEXT); |
| 7199 | ADD_INT(d, DB_NEXT_DUP); |
| 7200 | ADD_INT(d, DB_NEXT_NODUP); |
| 7201 | ADD_INT(d, DB_NODUPDATA); |
| 7202 | ADD_INT(d, DB_NOOVERWRITE); |
| 7203 | ADD_INT(d, DB_NOSYNC); |
| 7204 | ADD_INT(d, DB_POSITION); |
| 7205 | ADD_INT(d, DB_PREV); |
| 7206 | ADD_INT(d, DB_PREV_NODUP); |
Gregory P. Smith | 8b96a35 | 2007-01-05 01:59:42 +0000 | [diff] [blame] | 7207 | #if (DBVER < 45) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7208 | ADD_INT(d, DB_RECORDCOUNT); |
Gregory P. Smith | 8b96a35 | 2007-01-05 01:59:42 +0000 | [diff] [blame] | 7209 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7210 | ADD_INT(d, DB_SET); |
| 7211 | ADD_INT(d, DB_SET_RANGE); |
| 7212 | ADD_INT(d, DB_SET_RECNO); |
| 7213 | ADD_INT(d, DB_WRITECURSOR); |
| 7214 | |
| 7215 | ADD_INT(d, DB_OPFLAGS_MASK); |
| 7216 | ADD_INT(d, DB_RMW); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7217 | ADD_INT(d, DB_DIRTY_READ); |
| 7218 | ADD_INT(d, DB_MULTIPLE); |
| 7219 | ADD_INT(d, DB_MULTIPLE_KEY); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7220 | |
Gregory P. Smith | 29602d2 | 2006-01-24 09:46:48 +0000 | [diff] [blame] | 7221 | #if (DBVER >= 44) |
| 7222 | ADD_INT(d, DB_READ_UNCOMMITTED); /* replaces DB_DIRTY_READ in 4.4 */ |
| 7223 | ADD_INT(d, DB_READ_COMMITTED); |
| 7224 | #endif |
| 7225 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7226 | ADD_INT(d, DB_DONOTINDEX); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7227 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 7228 | #if (DBVER >= 41) |
| 7229 | _addIntToDict(d, "DB_INCOMPLETE", 0); |
| 7230 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7231 | ADD_INT(d, DB_INCOMPLETE); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 7232 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7233 | ADD_INT(d, DB_KEYEMPTY); |
| 7234 | ADD_INT(d, DB_KEYEXIST); |
| 7235 | ADD_INT(d, DB_LOCK_DEADLOCK); |
| 7236 | ADD_INT(d, DB_LOCK_NOTGRANTED); |
| 7237 | ADD_INT(d, DB_NOSERVER); |
| 7238 | ADD_INT(d, DB_NOSERVER_HOME); |
| 7239 | ADD_INT(d, DB_NOSERVER_ID); |
| 7240 | ADD_INT(d, DB_NOTFOUND); |
| 7241 | ADD_INT(d, DB_OLD_VERSION); |
| 7242 | ADD_INT(d, DB_RUNRECOVERY); |
| 7243 | ADD_INT(d, DB_VERIFY_BAD); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7244 | ADD_INT(d, DB_PAGE_NOTFOUND); |
| 7245 | ADD_INT(d, DB_SECONDARY_BAD); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7246 | ADD_INT(d, DB_STAT_CLEAR); |
| 7247 | ADD_INT(d, DB_REGION_INIT); |
| 7248 | ADD_INT(d, DB_NOLOCKING); |
| 7249 | ADD_INT(d, DB_YIELDCPU); |
| 7250 | ADD_INT(d, DB_PANIC_ENVIRONMENT); |
| 7251 | ADD_INT(d, DB_NOPANIC); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7252 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7253 | #if (DBVER >= 41) |
| 7254 | ADD_INT(d, DB_OVERWRITE); |
| 7255 | #endif |
| 7256 | |
Gregory P. Smith | aae141a | 2007-11-01 21:08:14 +0000 | [diff] [blame] | 7257 | #ifdef DB_REGISTER |
| 7258 | ADD_INT(d, DB_REGISTER); |
| 7259 | #endif |
| 7260 | |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7261 | #if (DBVER >= 42) |
| 7262 | ADD_INT(d, DB_TIME_NOTGRANTED); |
| 7263 | ADD_INT(d, DB_TXN_NOT_DURABLE); |
| 7264 | ADD_INT(d, DB_TXN_WRITE_NOSYNC); |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7265 | ADD_INT(d, DB_DIRECT_DB); |
| 7266 | ADD_INT(d, DB_INIT_REP); |
| 7267 | ADD_INT(d, DB_ENCRYPT); |
| 7268 | ADD_INT(d, DB_CHKSUM); |
| 7269 | #endif |
| 7270 | |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 7271 | #if (DBVER >= 42) && (DBVER < 47) |
| 7272 | ADD_INT(d, DB_LOG_AUTOREMOVE); |
| 7273 | ADD_INT(d, DB_DIRECT_LOG); |
| 7274 | #endif |
| 7275 | |
| 7276 | #if (DBVER >= 47) |
| 7277 | ADD_INT(d, DB_LOG_DIRECT); |
| 7278 | ADD_INT(d, DB_LOG_DSYNC); |
| 7279 | ADD_INT(d, DB_LOG_IN_MEMORY); |
| 7280 | ADD_INT(d, DB_LOG_AUTO_REMOVE); |
| 7281 | ADD_INT(d, DB_LOG_ZERO); |
| 7282 | #endif |
| 7283 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7284 | #if (DBVER >= 44) |
| 7285 | ADD_INT(d, DB_DSYNC_DB); |
| 7286 | #endif |
| 7287 | |
| 7288 | #if (DBVER >= 45) |
| 7289 | ADD_INT(d, DB_TXN_SNAPSHOT); |
| 7290 | #endif |
| 7291 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7292 | ADD_INT(d, DB_VERB_DEADLOCK); |
| 7293 | #if (DBVER >= 46) |
| 7294 | ADD_INT(d, DB_VERB_FILEOPS); |
| 7295 | ADD_INT(d, DB_VERB_FILEOPS_ALL); |
| 7296 | #endif |
| 7297 | ADD_INT(d, DB_VERB_RECOVERY); |
| 7298 | #if (DBVER >= 44) |
| 7299 | ADD_INT(d, DB_VERB_REGISTER); |
| 7300 | #endif |
| 7301 | ADD_INT(d, DB_VERB_REPLICATION); |
| 7302 | ADD_INT(d, DB_VERB_WAITSFOR); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7303 | |
| 7304 | #if (DBVER >= 45) |
| 7305 | ADD_INT(d, DB_EVENT_PANIC); |
| 7306 | ADD_INT(d, DB_EVENT_REP_CLIENT); |
| 7307 | #if (DBVER >= 46) |
| 7308 | ADD_INT(d, DB_EVENT_REP_ELECTED); |
| 7309 | #endif |
| 7310 | ADD_INT(d, DB_EVENT_REP_MASTER); |
| 7311 | ADD_INT(d, DB_EVENT_REP_NEWMASTER); |
| 7312 | #if (DBVER >= 46) |
| 7313 | ADD_INT(d, DB_EVENT_REP_PERM_FAILED); |
| 7314 | #endif |
| 7315 | ADD_INT(d, DB_EVENT_REP_STARTUPDONE); |
| 7316 | ADD_INT(d, DB_EVENT_WRITE_FAILED); |
| 7317 | #endif |
| 7318 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7319 | ADD_INT(d, DB_REP_DUPMASTER); |
| 7320 | ADD_INT(d, DB_REP_HOLDELECTION); |
| 7321 | #if (DBVER >= 44) |
| 7322 | ADD_INT(d, DB_REP_IGNORE); |
| 7323 | ADD_INT(d, DB_REP_JOIN_FAILURE); |
| 7324 | #endif |
| 7325 | #if (DBVER >= 42) |
| 7326 | ADD_INT(d, DB_REP_ISPERM); |
| 7327 | ADD_INT(d, DB_REP_NOTPERM); |
| 7328 | #endif |
| 7329 | ADD_INT(d, DB_REP_NEWSITE); |
| 7330 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7331 | ADD_INT(d, DB_REP_MASTER); |
| 7332 | ADD_INT(d, DB_REP_CLIENT); |
| 7333 | #if (DBVER >= 45) |
| 7334 | ADD_INT(d, DB_REP_ELECTION); |
| 7335 | |
| 7336 | ADD_INT(d, DB_REP_ACK_TIMEOUT); |
| 7337 | ADD_INT(d, DB_REP_CONNECTION_RETRY); |
| 7338 | ADD_INT(d, DB_REP_ELECTION_TIMEOUT); |
| 7339 | ADD_INT(d, DB_REP_ELECTION_RETRY); |
| 7340 | #endif |
| 7341 | #if (DBVER >= 46) |
| 7342 | ADD_INT(d, DB_REP_CHECKPOINT_DELAY); |
| 7343 | ADD_INT(d, DB_REP_FULL_ELECTION_TIMEOUT); |
| 7344 | #endif |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7345 | |
| 7346 | #if (DBVER >= 45) |
| 7347 | ADD_INT(d, DB_REPMGR_PEER); |
| 7348 | ADD_INT(d, DB_REPMGR_ACKS_ALL); |
| 7349 | ADD_INT(d, DB_REPMGR_ACKS_ALL_PEERS); |
| 7350 | ADD_INT(d, DB_REPMGR_ACKS_NONE); |
| 7351 | ADD_INT(d, DB_REPMGR_ACKS_ONE); |
| 7352 | ADD_INT(d, DB_REPMGR_ACKS_ONE_PEER); |
| 7353 | ADD_INT(d, DB_REPMGR_ACKS_QUORUM); |
| 7354 | ADD_INT(d, DB_REPMGR_CONNECTED); |
| 7355 | ADD_INT(d, DB_REPMGR_DISCONNECTED); |
| 7356 | ADD_INT(d, DB_STAT_CLEAR); |
| 7357 | ADD_INT(d, DB_STAT_ALL); |
| 7358 | #endif |
| 7359 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 7360 | #if (DBVER >= 43) |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 7361 | ADD_INT(d, DB_BUFFER_SMALL); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 7362 | ADD_INT(d, DB_SEQ_DEC); |
| 7363 | ADD_INT(d, DB_SEQ_INC); |
| 7364 | ADD_INT(d, DB_SEQ_WRAP); |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 7365 | #endif |
| 7366 | |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 7367 | #if (DBVER >= 43) && (DBVER < 47) |
| 7368 | ADD_INT(d, DB_LOG_INMEMORY); |
| 7369 | ADD_INT(d, DB_DSYNC_LOG); |
| 7370 | #endif |
| 7371 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 7372 | #if (DBVER >= 41) |
| 7373 | ADD_INT(d, DB_ENCRYPT_AES); |
| 7374 | ADD_INT(d, DB_AUTO_COMMIT); |
| 7375 | #else |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7376 | /* allow Berkeley DB 4.1 aware apps to run on older versions */ |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 7377 | _addIntToDict(d, "DB_AUTO_COMMIT", 0); |
| 7378 | #endif |
| 7379 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7380 | ADD_INT(d, EINVAL); |
| 7381 | ADD_INT(d, EACCES); |
| 7382 | ADD_INT(d, ENOSPC); |
| 7383 | ADD_INT(d, ENOMEM); |
| 7384 | ADD_INT(d, EAGAIN); |
| 7385 | ADD_INT(d, EBUSY); |
| 7386 | ADD_INT(d, EEXIST); |
| 7387 | ADD_INT(d, ENOENT); |
| 7388 | ADD_INT(d, EPERM); |
| 7389 | |
Barry Warsaw | 1baa982 | 2003-03-31 19:51:29 +0000 | [diff] [blame] | 7390 | ADD_INT(d, DB_SET_LOCK_TIMEOUT); |
| 7391 | ADD_INT(d, DB_SET_TXN_TIMEOUT); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7392 | |
Gregory P. Smith | 7f5b6f4 | 2006-04-08 07:10:51 +0000 | [diff] [blame] | 7393 | /* The exception name must be correct for pickled exception * |
| 7394 | * objects to unpickle properly. */ |
| 7395 | #ifdef PYBSDDB_STANDALONE /* different value needed for standalone pybsddb */ |
| 7396 | #define PYBSDDB_EXCEPTION_BASE "bsddb3.db." |
| 7397 | #else |
| 7398 | #define PYBSDDB_EXCEPTION_BASE "bsddb.db." |
| 7399 | #endif |
| 7400 | |
| 7401 | /* All the rest of the exceptions derive only from DBError */ |
| 7402 | #define MAKE_EX(name) name = PyErr_NewException(PYBSDDB_EXCEPTION_BASE #name, DBError, NULL); \ |
| 7403 | PyDict_SetItemString(d, #name, name) |
| 7404 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7405 | /* The base exception class is DBError */ |
Gregory P. Smith | 7f5b6f4 | 2006-04-08 07:10:51 +0000 | [diff] [blame] | 7406 | DBError = NULL; /* used in MAKE_EX so that it derives from nothing */ |
| 7407 | MAKE_EX(DBError); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7408 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7409 | #if (PY_VERSION_HEX < 0x03000000) |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 7410 | /* Some magic to make DBNotFoundError and DBKeyEmptyError derive |
| 7411 | * from both DBError and KeyError, since the API only supports |
| 7412 | * using one base class. */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7413 | PyDict_SetItemString(d, "KeyError", PyExc_KeyError); |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 7414 | PyRun_String("class DBNotFoundError(DBError, KeyError): pass\n" |
| 7415 | "class DBKeyEmptyError(DBError, KeyError): pass", |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7416 | Py_file_input, d, d); |
| 7417 | DBNotFoundError = PyDict_GetItemString(d, "DBNotFoundError"); |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 7418 | DBKeyEmptyError = PyDict_GetItemString(d, "DBKeyEmptyError"); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7419 | PyDict_DelItemString(d, "KeyError"); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7420 | #else |
| 7421 | /* Since Python 2.5, PyErr_NewException() accepts a tuple, to be able to |
| 7422 | ** derive from several classes. We use this new API only for Python 3.0, |
| 7423 | ** though. |
| 7424 | */ |
| 7425 | { |
| 7426 | PyObject* bases; |
| 7427 | |
| 7428 | bases = PyTuple_Pack(2, DBError, PyExc_KeyError); |
| 7429 | |
| 7430 | #define MAKE_EX2(name) name = PyErr_NewException(PYBSDDB_EXCEPTION_BASE #name, bases, NULL); \ |
| 7431 | PyDict_SetItemString(d, #name, name) |
| 7432 | MAKE_EX2(DBNotFoundError); |
| 7433 | MAKE_EX2(DBKeyEmptyError); |
| 7434 | |
| 7435 | #undef MAKE_EX2 |
| 7436 | |
| 7437 | Py_XDECREF(bases); |
| 7438 | } |
| 7439 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7440 | |
| 7441 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7442 | #if !INCOMPLETE_IS_WARNING |
| 7443 | MAKE_EX(DBIncompleteError); |
| 7444 | #endif |
Gregory P. Smith | e276717 | 2003-11-02 08:06:29 +0000 | [diff] [blame] | 7445 | MAKE_EX(DBCursorClosedError); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7446 | MAKE_EX(DBKeyEmptyError); |
| 7447 | MAKE_EX(DBKeyExistError); |
| 7448 | MAKE_EX(DBLockDeadlockError); |
| 7449 | MAKE_EX(DBLockNotGrantedError); |
| 7450 | MAKE_EX(DBOldVersionError); |
| 7451 | MAKE_EX(DBRunRecoveryError); |
| 7452 | MAKE_EX(DBVerifyBadError); |
| 7453 | MAKE_EX(DBNoServerError); |
| 7454 | MAKE_EX(DBNoServerHomeError); |
| 7455 | MAKE_EX(DBNoServerIDError); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7456 | MAKE_EX(DBPageNotFoundError); |
| 7457 | MAKE_EX(DBSecondaryBadError); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7458 | |
| 7459 | MAKE_EX(DBInvalidArgError); |
| 7460 | MAKE_EX(DBAccessError); |
| 7461 | MAKE_EX(DBNoSpaceError); |
| 7462 | MAKE_EX(DBNoMemoryError); |
| 7463 | MAKE_EX(DBAgainError); |
| 7464 | MAKE_EX(DBBusyError); |
| 7465 | MAKE_EX(DBFileExistsError); |
| 7466 | MAKE_EX(DBNoSuchFileError); |
| 7467 | MAKE_EX(DBPermissionsError); |
| 7468 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7469 | #if (DBVER >= 42) |
| 7470 | MAKE_EX(DBRepHandleDeadError); |
| 7471 | #endif |
| 7472 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7473 | MAKE_EX(DBRepUnavailError); |
| 7474 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7475 | #undef MAKE_EX |
| 7476 | |
Gregory P. Smith | 3925053 | 2007-10-09 06:02:21 +0000 | [diff] [blame] | 7477 | /* Initiliase the C API structure and add it to the module */ |
| 7478 | bsddb_api.db_type = &DB_Type; |
| 7479 | bsddb_api.dbcursor_type = &DBCursor_Type; |
| 7480 | bsddb_api.dbenv_type = &DBEnv_Type; |
| 7481 | bsddb_api.dbtxn_type = &DBTxn_Type; |
| 7482 | bsddb_api.dblock_type = &DBLock_Type; |
Neal Norwitz | f3ca169 | 2007-10-12 03:52:34 +0000 | [diff] [blame] | 7483 | #if (DBVER >= 43) |
Gregory P. Smith | 3925053 | 2007-10-09 06:02:21 +0000 | [diff] [blame] | 7484 | bsddb_api.dbsequence_type = &DBSequence_Type; |
Neal Norwitz | f3ca169 | 2007-10-12 03:52:34 +0000 | [diff] [blame] | 7485 | #endif |
Gregory P. Smith | 3925053 | 2007-10-09 06:02:21 +0000 | [diff] [blame] | 7486 | bsddb_api.makeDBError = makeDBError; |
| 7487 | |
| 7488 | py_api = PyCObject_FromVoidPtr((void*)&bsddb_api, NULL); |
| 7489 | PyDict_SetItemString(d, "api", py_api); |
| 7490 | Py_DECREF(py_api); |
| 7491 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7492 | /* Check for errors */ |
| 7493 | if (PyErr_Occurred()) { |
| 7494 | PyErr_Print(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7495 | Py_FatalError("can't initialize module _bsddb/_pybsddb"); |
| 7496 | Py_DECREF(m); |
| 7497 | m = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7498 | } |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7499 | #if (PY_VERSION_HEX < 0x03000000) |
| 7500 | return; |
| 7501 | #else |
| 7502 | return m; |
| 7503 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7504 | } |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7505 | |
| 7506 | /* allow this module to be named _pybsddb so that it can be installed |
| 7507 | * and imported on top of python >= 2.3 that includes its own older |
| 7508 | * copy of the library named _bsddb without importing the old version. */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7509 | #if (PY_VERSION_HEX < 0x03000000) |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7510 | DL_EXPORT(void) init_pybsddb(void) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7511 | #else |
| 7512 | PyMODINIT_FUNC PyInit__pybsddb(void) /* Note the two underscores */ |
| 7513 | #endif |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7514 | { |
| 7515 | strncpy(_bsddbModuleName, "_pybsddb", MODULE_NAME_MAX_LEN); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7516 | #if (PY_VERSION_HEX < 0x03000000) |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7517 | init_bsddb(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7518 | #else |
| 7519 | return PyInit__bsddb(); /* Note the two underscores */ |
| 7520 | #endif |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7521 | } |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7522 | |