Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Berkeley DB interface. |
| 33 | Author: Michael McLay |
| 34 | Hacked: Guido van Rossum |
| 35 | Btree and Recno additions plus sequence methods: David Ely |
| 36 | |
| 37 | XXX To do: |
| 38 | - provide interface to the B-tree and record libraries too |
| 39 | - provide a way to access the various hash functions |
| 40 | - support more open flags |
| 41 | */ |
| 42 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 43 | #include "Python.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 44 | #ifdef WITH_THREAD |
| 45 | #include "thread.h" |
| 46 | #endif |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 47 | |
| 48 | #include <sys/types.h> |
| 49 | #include <sys/stat.h> |
| 50 | #include <fcntl.h> |
| 51 | #include <db.h> |
| 52 | /* Please don't include internal header files of the Berkeley db package |
| 53 | (it messes up the info required in the Setup file) */ |
| 54 | |
| 55 | typedef struct { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 56 | PyObject_HEAD |
| 57 | DB *di_bsddb; |
| 58 | int di_size; /* -1 means recompute */ |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 59 | #ifdef WITH_THREAD |
| 60 | type_lock di_lock; |
| 61 | #endif |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 62 | } bsddbobject; |
| 63 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 64 | staticforward PyTypeObject Bsddbtype; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 65 | |
| 66 | #define is_bsddbobject(v) ((v)->ob_type == &Bsddbtype) |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 67 | #define check_bsddbobject_open(v) if ((v)->di_bsddb == NULL) \ |
| 68 | { PyErr_SetString(BsddbError, "BSDDB object has already been closed"); \ |
| 69 | return NULL; } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 70 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 71 | static PyObject *BsddbError; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 72 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 73 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 74 | newdbhashobject(file, flags, mode, |
| 75 | bsize, ffactor, nelem, cachesize, hash, lorder) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 76 | char *file; |
| 77 | int flags; |
| 78 | int mode; |
| 79 | int bsize; |
| 80 | int ffactor; |
| 81 | int nelem; |
| 82 | int cachesize; |
| 83 | int hash; /* XXX ignored */ |
| 84 | int lorder; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 85 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 86 | bsddbobject *dp; |
| 87 | HASHINFO info; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 88 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 89 | if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL) |
| 90 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 91 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 92 | info.bsize = bsize; |
| 93 | info.ffactor = ffactor; |
| 94 | info.nelem = nelem; |
| 95 | info.cachesize = cachesize; |
| 96 | info.hash = NULL; /* XXX should derive from hash argument */ |
| 97 | info.lorder = lorder; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 99 | #ifdef O_BINARY |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 100 | flags |= O_BINARY; |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 101 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 102 | Py_BEGIN_ALLOW_THREADS |
| 103 | dp->di_bsddb = dbopen(file, flags, mode, DB_HASH, &info); |
| 104 | Py_END_ALLOW_THREADS |
| 105 | if (dp->di_bsddb == NULL) { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 106 | PyErr_SetFromErrno(BsddbError); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 107 | #ifdef WITH_THREAD |
| 108 | dp->di_lock = NULL; |
| 109 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 110 | Py_DECREF(dp); |
| 111 | return NULL; |
| 112 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 113 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 114 | dp->di_size = -1; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 115 | #ifdef WITH_THREAD |
| 116 | dp->di_lock = allocate_lock(); |
| 117 | if (dp->di_lock == NULL) { |
| 118 | PyErr_SetString(BsddbError, "can't allocate lock"); |
| 119 | Py_DECREF(dp); |
| 120 | return NULL; |
| 121 | } |
| 122 | #endif |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 123 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 124 | return (PyObject *)dp; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 127 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 128 | newdbbtobject(file, flags, mode, |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 129 | btflags, cachesize, maxkeypage, minkeypage, psize, lorder) |
| 130 | char *file; |
| 131 | int flags; |
| 132 | int mode; |
| 133 | int btflags; |
| 134 | int cachesize; |
| 135 | int maxkeypage; |
| 136 | int minkeypage; |
| 137 | int psize; |
| 138 | int lorder; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 139 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 140 | bsddbobject *dp; |
| 141 | BTREEINFO info; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 142 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 143 | if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL) |
| 144 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 145 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 146 | info.flags = btflags; |
| 147 | info.cachesize = cachesize; |
| 148 | info.maxkeypage = maxkeypage; |
| 149 | info.minkeypage = minkeypage; |
| 150 | info.psize = psize; |
| 151 | info.lorder = lorder; |
| 152 | info.compare = 0; /* Use default comparison functions, for now..*/ |
| 153 | info.prefix = 0; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 154 | |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 155 | #ifdef O_BINARY |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 156 | flags |= O_BINARY; |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 157 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 158 | Py_BEGIN_ALLOW_THREADS |
| 159 | dp->di_bsddb = dbopen(file, flags, mode, DB_BTREE, &info); |
| 160 | Py_END_ALLOW_THREADS |
| 161 | if (dp->di_bsddb == NULL) { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 162 | PyErr_SetFromErrno(BsddbError); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 163 | #ifdef WITH_THREAD |
| 164 | dp->di_lock = NULL; |
| 165 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 166 | Py_DECREF(dp); |
| 167 | return NULL; |
| 168 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 169 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 170 | dp->di_size = -1; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 171 | #ifdef WITH_THREAD |
| 172 | dp->di_lock = allocate_lock(); |
| 173 | if (dp->di_lock == NULL) { |
| 174 | PyErr_SetString(BsddbError, "can't allocate lock"); |
| 175 | Py_DECREF(dp); |
| 176 | return NULL; |
| 177 | } |
| 178 | #endif |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 179 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 180 | return (PyObject *)dp; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 183 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 184 | newdbrnobject(file, flags, mode, |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 185 | rnflags, cachesize, psize, lorder, reclen, bval, bfname) |
| 186 | char *file; |
| 187 | int flags; |
| 188 | int mode; |
| 189 | int rnflags; |
| 190 | int cachesize; |
| 191 | int psize; |
| 192 | int lorder; |
| 193 | size_t reclen; |
| 194 | u_char bval; |
| 195 | char *bfname; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 196 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 197 | bsddbobject *dp; |
| 198 | RECNOINFO info; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 199 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 200 | if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL) |
| 201 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 202 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 203 | info.flags = rnflags; |
| 204 | info.cachesize = cachesize; |
| 205 | info.psize = psize; |
| 206 | info.lorder = lorder; |
| 207 | info.reclen = reclen; |
| 208 | info.bval = bval; |
| 209 | info.bfname = bfname; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 210 | |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 211 | #ifdef O_BINARY |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 212 | flags |= O_BINARY; |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 213 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 214 | Py_BEGIN_ALLOW_THREADS |
| 215 | dp->di_bsddb = dbopen(file, flags, mode, DB_RECNO, &info); |
| 216 | Py_END_ALLOW_THREADS |
| 217 | if (dp->di_bsddb == NULL) { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 218 | PyErr_SetFromErrno(BsddbError); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 219 | #ifdef WITH_THREAD |
| 220 | dp->di_lock = NULL; |
| 221 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 222 | Py_DECREF(dp); |
| 223 | return NULL; |
| 224 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 225 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 226 | dp->di_size = -1; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 227 | #ifdef WITH_THREAD |
| 228 | dp->di_lock = allocate_lock(); |
| 229 | if (dp->di_lock == NULL) { |
| 230 | PyErr_SetString(BsddbError, "can't allocate lock"); |
| 231 | Py_DECREF(dp); |
| 232 | return NULL; |
| 233 | } |
| 234 | #endif |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 235 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 236 | return (PyObject *)dp; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 239 | static void |
| 240 | bsddb_dealloc(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 241 | bsddbobject *dp; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 242 | { |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 243 | #ifdef WITH_THREAD |
| 244 | if (dp->di_lock) { |
| 245 | acquire_lock(dp->di_lock, 0); |
| 246 | release_lock(dp->di_lock); |
| 247 | free_lock(dp->di_lock); |
| 248 | dp->di_lock = NULL; |
| 249 | } |
| 250 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 251 | if (dp->di_bsddb != NULL) { |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 252 | int status; |
| 253 | Py_BEGIN_ALLOW_THREADS |
| 254 | status = (dp->di_bsddb->close)(dp->di_bsddb); |
| 255 | Py_END_ALLOW_THREADS |
| 256 | if (status != 0) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 257 | fprintf(stderr, |
| 258 | "Python bsddb: close errno %d in dealloc\n", |
| 259 | errno); |
| 260 | } |
| 261 | PyMem_DEL(dp); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 264 | #ifdef WITH_THREAD |
| 265 | #define BSDDB_BGN_SAVE(_dp) Py_BEGIN_ALLOW_THREADS acquire_lock(_dp->di_lock,1); |
| 266 | #define BSDDB_END_SAVE(_dp) release_lock(_dp->di_lock); Py_END_ALLOW_THREADS |
| 267 | #else |
| 268 | #define BSDDB_BGN_SAVE(_dp) Py_BEGIN_ALLOW_THREADS |
| 269 | #define BSDDB_END_SAVE(_dp) Py_END_ALLOW_THREADS |
| 270 | #endif |
| 271 | |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 272 | static int |
| 273 | bsddb_length(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 274 | bsddbobject *dp; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 275 | { |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 276 | if (dp->di_bsddb == NULL) { |
| 277 | PyErr_SetString(BsddbError, "BSDDB object has already been closed"); |
| 278 | return -1; |
| 279 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 280 | if (dp->di_size < 0) { |
| 281 | DBT krec, drec; |
| 282 | int status; |
| 283 | int size = 0; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 284 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 285 | for (status = (dp->di_bsddb->seq)(dp->di_bsddb, |
| 286 | &krec, &drec,R_FIRST); |
| 287 | status == 0; |
| 288 | status = (dp->di_bsddb->seq)(dp->di_bsddb, |
| 289 | &krec, &drec, R_NEXT)) |
| 290 | size++; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 291 | BSDDB_END_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 292 | if (status < 0) { |
| 293 | PyErr_SetFromErrno(BsddbError); |
| 294 | return -1; |
| 295 | } |
| 296 | dp->di_size = size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 297 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 298 | return dp->di_size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 301 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 302 | bsddb_subscript(dp, key) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 303 | bsddbobject *dp; |
| 304 | PyObject *key; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 305 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 306 | int status; |
| 307 | DBT krec, drec; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 308 | char *data,buf[4096]; |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 309 | int size; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 310 | PyObject *result; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 311 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 312 | if (!PyArg_Parse(key, "s#", &data, &size)) |
| 313 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 314 | check_bsddbobject_open(dp); |
| 315 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 316 | krec.data = data; |
| 317 | krec.size = size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 318 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 319 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 320 | status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 321 | if (status == 0) { |
| 322 | if (drec.size > sizeof(buf)) data = malloc(drec.size); |
| 323 | else data = buf; |
| 324 | memcpy(data,drec.data,drec.size); |
| 325 | } |
| 326 | BSDDB_END_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 327 | if (status != 0) { |
| 328 | if (status < 0) |
| 329 | PyErr_SetFromErrno(BsddbError); |
| 330 | else |
| 331 | PyErr_SetObject(PyExc_KeyError, key); |
| 332 | return NULL; |
| 333 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 334 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 335 | result = PyString_FromStringAndSize(data, (int)drec.size); |
| 336 | if (data != buf) free(data); |
| 337 | return result; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | static int |
| 341 | bsddb_ass_sub(dp, key, value) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 342 | bsddbobject *dp; |
| 343 | PyObject *key, *value; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 344 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 345 | int status; |
| 346 | DBT krec, drec; |
| 347 | char *data; |
| 348 | int size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 349 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 350 | if (!PyArg_Parse(key, "s#", &data, &size)) { |
| 351 | PyErr_SetString(PyExc_TypeError, |
| 352 | "bsddb key type must be string"); |
| 353 | return -1; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 354 | } |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 355 | if (dp->di_bsddb == NULL) { |
| 356 | PyErr_SetString(BsddbError, "BSDDB object has already been closed"); |
| 357 | return -1; |
| 358 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 359 | krec.data = data; |
| 360 | krec.size = size; |
| 361 | dp->di_size = -1; |
| 362 | if (value == NULL) { |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 363 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 364 | status = (dp->di_bsddb->del)(dp->di_bsddb, &krec, 0); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 365 | BSDDB_END_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 366 | } |
| 367 | else { |
| 368 | if (!PyArg_Parse(value, "s#", &data, &size)) { |
| 369 | PyErr_SetString(PyExc_TypeError, |
| 370 | "bsddb value type must be string"); |
| 371 | return -1; |
| 372 | } |
| 373 | drec.data = data; |
| 374 | drec.size = size; |
| 375 | #if 0 |
| 376 | /* For RECNO, put fails with 'No space left on device' |
| 377 | after a few short records are added?? Looks fine |
| 378 | to this point... linked with 1.85 on Solaris Intel |
| 379 | Roger E. Masse 1/16/97 |
| 380 | */ |
| 381 | printf("before put data: '%s', size: %d\n", |
| 382 | drec.data, drec.size); |
| 383 | printf("before put key= '%s', size= %d\n", |
| 384 | krec.data, krec.size); |
| 385 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 386 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 387 | status = (dp->di_bsddb->put)(dp->di_bsddb, &krec, &drec, 0); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 388 | BSDDB_END_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 389 | } |
| 390 | if (status != 0) { |
| 391 | if (status < 0) |
| 392 | PyErr_SetFromErrno(BsddbError); |
| 393 | else |
| 394 | PyErr_SetObject(PyExc_KeyError, key); |
| 395 | return -1; |
| 396 | } |
| 397 | return 0; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 400 | static PyMappingMethods bsddb_as_mapping = { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 401 | (inquiry)bsddb_length, /*mp_length*/ |
| 402 | (binaryfunc)bsddb_subscript, /*mp_subscript*/ |
| 403 | (objobjargproc)bsddb_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 404 | }; |
| 405 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 406 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 407 | bsddb_close(dp, args) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 408 | bsddbobject *dp; |
| 409 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 410 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 411 | if (!PyArg_NoArgs(args)) |
| 412 | return NULL; |
| 413 | if (dp->di_bsddb != NULL) { |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 414 | int status; |
| 415 | BSDDB_BGN_SAVE(dp) |
| 416 | status = (dp->di_bsddb->close)(dp->di_bsddb); |
| 417 | BSDDB_END_SAVE(dp) |
| 418 | if (status != 0) { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 419 | dp->di_bsddb = NULL; |
| 420 | PyErr_SetFromErrno(BsddbError); |
| 421 | return NULL; |
| 422 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 423 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 424 | dp->di_bsddb = NULL; |
| 425 | Py_INCREF(Py_None); |
| 426 | return Py_None; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 429 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 430 | bsddb_keys(dp, args) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 431 | bsddbobject *dp; |
| 432 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 433 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 434 | PyObject *list, *item; |
| 435 | DBT krec, drec; |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 436 | char *data=NULL,buf[4096]; |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 437 | int status; |
| 438 | int err; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 439 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 440 | if (!PyArg_NoArgs(args)) |
| 441 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 442 | check_bsddbobject_open(dp); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 443 | list = PyList_New(0); |
| 444 | if (list == NULL) |
| 445 | return NULL; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 446 | BSDDB_BGN_SAVE(dp) |
| 447 | status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_FIRST); |
| 448 | if (status == 0) { |
| 449 | if (krec.size > sizeof(buf)) data = malloc(krec.size); |
| 450 | else data = buf; |
| 451 | memcpy(data,krec.data,krec.size); |
| 452 | } |
| 453 | BSDDB_END_SAVE(dp) |
| 454 | while (status == 0) { |
| 455 | item = PyString_FromStringAndSize(data, (int)krec.size); |
| 456 | if (data != buf) free(data); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 457 | if (item == NULL) { |
| 458 | Py_DECREF(list); |
| 459 | return NULL; |
| 460 | } |
| 461 | err = PyList_Append(list, item); |
| 462 | Py_DECREF(item); |
| 463 | if (err != 0) { |
| 464 | Py_DECREF(list); |
| 465 | return NULL; |
| 466 | } |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 467 | BSDDB_BGN_SAVE(dp) |
| 468 | status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_NEXT); |
| 469 | if (status == 0) { |
| 470 | if (krec.size > sizeof(buf)) data = malloc(krec.size); |
| 471 | else data = buf; |
| 472 | memcpy(data,krec.data,krec.size); |
| 473 | } |
| 474 | BSDDB_END_SAVE(dp) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 475 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 476 | if (status < 0) { |
| 477 | PyErr_SetFromErrno(BsddbError); |
| 478 | Py_DECREF(list); |
| 479 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 480 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 481 | if (dp->di_size < 0) |
| 482 | dp->di_size = PyList_Size(list); /* We just did the work */ |
| 483 | return list; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 486 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 487 | bsddb_has_key(dp, args) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 488 | bsddbobject *dp; |
| 489 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 490 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 491 | DBT krec, drec; |
| 492 | int status; |
| 493 | char *data; |
| 494 | int size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 495 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 496 | if (!PyArg_Parse(args, "s#", &data, &size)) |
| 497 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 498 | check_bsddbobject_open(dp); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 499 | krec.data = data; |
| 500 | krec.size = size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 501 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 502 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 503 | status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 504 | BSDDB_END_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 505 | if (status < 0) { |
| 506 | PyErr_SetFromErrno(BsddbError); |
| 507 | return NULL; |
| 508 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 509 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 510 | return PyInt_FromLong(status == 0); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 513 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 514 | bsddb_set_location(dp, key) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 515 | bsddbobject *dp; |
| 516 | PyObject *key; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 517 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 518 | int status; |
| 519 | DBT krec, drec; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 520 | char *data,buf[4096]; |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 521 | int size; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 522 | PyObject *result; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 523 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 524 | if (!PyArg_Parse(key, "s#", &data, &size)) |
| 525 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 526 | check_bsddbobject_open(dp); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 527 | krec.data = data; |
| 528 | krec.size = size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 529 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 530 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 531 | status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_CURSOR); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 532 | if (status == 0) { |
| 533 | if (drec.size > sizeof(buf)) data = malloc(drec.size); |
| 534 | else data = buf; |
| 535 | memcpy(data,drec.data,drec.size); |
| 536 | } |
| 537 | BSDDB_END_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 538 | if (status != 0) { |
| 539 | if (status < 0) |
| 540 | PyErr_SetFromErrno(BsddbError); |
| 541 | else |
| 542 | PyErr_SetObject(PyExc_KeyError, key); |
| 543 | return NULL; |
| 544 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 545 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 546 | result = Py_BuildValue("s#s#", krec.data, krec.size, data, drec.size); |
| 547 | if (data != buf) free(data); |
| 548 | return result; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 551 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 552 | bsddb_seq(dp, args, sequence_request) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 553 | bsddbobject *dp; |
| 554 | PyObject *args; |
| 555 | int sequence_request; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 556 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 557 | int status; |
| 558 | DBT krec, drec; |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 559 | char *kdata=NULL,kbuf[4096]; |
| 560 | char *ddata=NULL,dbuf[4096]; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 561 | PyObject *result; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 562 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 563 | if (!PyArg_NoArgs(args)) |
| 564 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 565 | |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 566 | check_bsddbobject_open(dp); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 567 | krec.data = 0; |
| 568 | krec.size = 0; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 569 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 570 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 571 | status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, |
| 572 | &drec, sequence_request); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 573 | if (status == 0) { |
| 574 | if (krec.size > sizeof(kbuf)) kdata = malloc(krec.size); |
| 575 | else kdata = kbuf; |
| 576 | memcpy(kdata,krec.data,krec.size); |
| 577 | if (drec.size > sizeof(dbuf)) ddata = malloc(drec.size); |
| 578 | else ddata = dbuf; |
| 579 | memcpy(ddata,drec.data,drec.size); |
| 580 | } |
| 581 | BSDDB_END_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 582 | if (status != 0) { |
| 583 | if (status < 0) |
| 584 | PyErr_SetFromErrno(BsddbError); |
| 585 | else |
| 586 | PyErr_SetObject(PyExc_KeyError, args); |
| 587 | return NULL; |
| 588 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 589 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 590 | result = Py_BuildValue("s#s#", kdata, krec.size, ddata, drec.size); |
| 591 | if (kdata != kbuf) free(kdata); |
| 592 | if (ddata != dbuf) free(ddata); |
| 593 | return result; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 596 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 597 | bsddb_next(dp, key) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 598 | bsddbobject *dp; |
| 599 | PyObject *key; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 600 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 601 | return bsddb_seq(dp, key, R_NEXT); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 602 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 603 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 604 | bsddb_previous(dp, key) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 605 | bsddbobject *dp; |
| 606 | PyObject *key; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 607 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 608 | return bsddb_seq(dp, key, R_PREV); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 609 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 610 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 611 | bsddb_first(dp, key) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 612 | bsddbobject *dp; |
| 613 | PyObject *key; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 614 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 615 | return bsddb_seq(dp, key, R_FIRST); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 616 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 617 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 618 | bsddb_last(dp, key) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 619 | bsddbobject *dp; |
| 620 | PyObject *key; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 621 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 622 | return bsddb_seq(dp, key, R_LAST); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 623 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 624 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 625 | bsddb_sync(dp, args) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 626 | bsddbobject *dp; |
| 627 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 628 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 629 | int status; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 630 | |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 631 | if (!PyArg_NoArgs(args)) |
| 632 | return NULL; |
| 633 | check_bsddbobject_open(dp); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 634 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 635 | status = (dp->di_bsddb->sync)(dp->di_bsddb, 0); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 636 | BSDDB_END_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 637 | if (status != 0) { |
| 638 | PyErr_SetFromErrno(BsddbError); |
| 639 | return NULL; |
| 640 | } |
| 641 | return PyInt_FromLong(status = 0); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 642 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 643 | static PyMethodDef bsddb_methods[] = { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 644 | {"close", (PyCFunction)bsddb_close}, |
| 645 | {"keys", (PyCFunction)bsddb_keys}, |
| 646 | {"has_key", (PyCFunction)bsddb_has_key}, |
| 647 | {"set_location", (PyCFunction)bsddb_set_location}, |
| 648 | {"next", (PyCFunction)bsddb_next}, |
| 649 | {"previous", (PyCFunction)bsddb_previous}, |
| 650 | {"first", (PyCFunction)bsddb_first}, |
| 651 | {"last", (PyCFunction)bsddb_last}, |
| 652 | {"sync", (PyCFunction)bsddb_sync}, |
| 653 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 654 | }; |
| 655 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 656 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 657 | bsddb_getattr(dp, name) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 658 | PyObject *dp; |
| 659 | char *name; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 660 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 661 | return Py_FindMethod(bsddb_methods, dp, name); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 664 | static PyTypeObject Bsddbtype = { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 665 | PyObject_HEAD_INIT(NULL) |
| 666 | 0, |
| 667 | "bsddb", |
| 668 | sizeof(bsddbobject), |
| 669 | 0, |
| 670 | (destructor)bsddb_dealloc, /*tp_dealloc*/ |
| 671 | 0, /*tp_print*/ |
| 672 | (getattrfunc)bsddb_getattr, /*tp_getattr*/ |
| 673 | 0, /*tp_setattr*/ |
| 674 | 0, /*tp_compare*/ |
| 675 | 0, /*tp_repr*/ |
| 676 | 0, /*tp_as_number*/ |
| 677 | 0, /*tp_as_sequence*/ |
| 678 | &bsddb_as_mapping, /*tp_as_mapping*/ |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 679 | }; |
| 680 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 681 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 682 | bsdhashopen(self, args) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 683 | PyObject *self; |
| 684 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 685 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 686 | char *file; |
| 687 | char *flag = NULL; |
| 688 | int flags = O_RDONLY; |
| 689 | int mode = 0666; |
| 690 | int bsize = 0; |
| 691 | int ffactor = 0; |
| 692 | int nelem = 0; |
| 693 | int cachesize = 0; |
| 694 | int hash = 0; /* XXX currently ignored */ |
| 695 | int lorder = 0; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 696 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 697 | if (!PyArg_ParseTuple(args, "s|siiiiiii", |
| 698 | &file, &flag, &mode, |
| 699 | &bsize, &ffactor, &nelem, &cachesize, |
| 700 | &hash, &lorder)) |
| 701 | return NULL; |
| 702 | if (flag != NULL) { |
| 703 | /* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */ |
| 704 | if (flag[0] == 'r') |
| 705 | flags = O_RDONLY; |
| 706 | else if (flag[0] == 'w') |
| 707 | flags = O_RDWR; |
| 708 | else if (flag[0] == 'c') |
| 709 | flags = O_RDWR|O_CREAT; |
| 710 | else if (flag[0] == 'n') |
| 711 | flags = O_RDWR|O_CREAT|O_TRUNC; |
| 712 | else { |
| 713 | PyErr_SetString(BsddbError, |
| 714 | "Flag should begin with 'r', 'w', 'c' or 'n'"); |
| 715 | return NULL; |
| 716 | } |
| 717 | if (flag[1] == 'l') { |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 718 | #if defined(O_EXLOCK) && defined(O_SHLOCK) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 719 | if (flag[0] == 'r') |
| 720 | flags |= O_SHLOCK; |
| 721 | else |
| 722 | flags |= O_EXLOCK; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 723 | #else |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 724 | PyErr_SetString(BsddbError, |
| 725 | "locking not supported on this platform"); |
| 726 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 727 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 728 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 729 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 730 | return newdbhashobject(file, flags, mode, |
| 731 | bsize, ffactor, nelem, cachesize, hash, lorder); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 734 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 735 | bsdbtopen(self, args) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 736 | PyObject *self; |
| 737 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 738 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 739 | char *file; |
| 740 | char *flag = NULL; |
| 741 | int flags = O_RDONLY; |
| 742 | int mode = 0666; |
| 743 | int cachesize = 0; |
| 744 | int maxkeypage = 0; |
| 745 | int minkeypage = 0; |
| 746 | int btflags = 0; |
| 747 | unsigned int psize = 0; |
| 748 | int lorder = 0; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 749 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 750 | if (!PyArg_ParseTuple(args, "s|siiiiiii", |
| 751 | &file, &flag, &mode, |
| 752 | &btflags, &cachesize, &maxkeypage, &minkeypage, |
| 753 | &psize, &lorder)) |
| 754 | return NULL; |
| 755 | if (flag != NULL) { |
| 756 | /* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */ |
| 757 | if (flag[0] == 'r') |
| 758 | flags = O_RDONLY; |
| 759 | else if (flag[0] == 'w') |
| 760 | flags = O_RDWR; |
| 761 | else if (flag[0] == 'c') |
| 762 | flags = O_RDWR|O_CREAT; |
| 763 | else if (flag[0] == 'n') |
| 764 | flags = O_RDWR|O_CREAT|O_TRUNC; |
| 765 | else { |
| 766 | PyErr_SetString(BsddbError, |
| 767 | "Flag should begin with 'r', 'w', 'c' or 'n'"); |
| 768 | return NULL; |
| 769 | } |
| 770 | if (flag[1] == 'l') { |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 771 | #if defined(O_EXLOCK) && defined(O_SHLOCK) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 772 | if (flag[0] == 'r') |
| 773 | flags |= O_SHLOCK; |
| 774 | else |
| 775 | flags |= O_EXLOCK; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 776 | #else |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 777 | PyErr_SetString(BsddbError, |
| 778 | "locking not supported on this platform"); |
| 779 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 780 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 781 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 782 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 783 | return newdbbtobject(file, flags, mode, |
| 784 | btflags, cachesize, maxkeypage, minkeypage, |
| 785 | psize, lorder); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 786 | } |
Guido van Rossum | dd96ca7 | 1996-05-23 22:57:54 +0000 | [diff] [blame] | 787 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 788 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 789 | bsdrnopen(self, args) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 790 | PyObject *self; |
| 791 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 792 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 793 | char *file; |
| 794 | char *flag = NULL; |
| 795 | int flags = O_RDONLY; |
| 796 | int mode = 0666; |
| 797 | int cachesize = 0; |
| 798 | int rnflags = 0; |
| 799 | unsigned int psize = 0; |
| 800 | int lorder = 0; |
| 801 | size_t reclen = 0; |
| 802 | char *bval = ""; |
| 803 | char *bfname = NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 804 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 805 | if (!PyArg_ParseTuple(args, "s|siiiiiiss", |
| 806 | &file, &flag, &mode, |
| 807 | &rnflags, &cachesize, &psize, &lorder, |
| 808 | &reclen, &bval, &bfname)) |
| 809 | return NULL; |
| 810 | |
| 811 | # if 0 |
| 812 | printf("file: %s\n", file); |
| 813 | printf("flag: %s\n", flag); |
| 814 | printf("mode: %d\n", mode); |
| 815 | printf("rnflags: 0x%x\n", rnflags); |
| 816 | printf("cachesize: %d\n", cachesize); |
| 817 | printf("psize: %d\n", psize); |
| 818 | printf("lorder: %d\n", 0); |
| 819 | printf("reclen: %d\n", reclen); |
| 820 | printf("bval: %c\n", bval[0]); |
| 821 | printf("bfname %s\n", bfname); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 822 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 823 | |
| 824 | if (flag != NULL) { |
| 825 | /* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */ |
| 826 | if (flag[0] == 'r') |
| 827 | flags = O_RDONLY; |
| 828 | else if (flag[0] == 'w') |
| 829 | flags = O_RDWR; |
| 830 | else if (flag[0] == 'c') |
| 831 | flags = O_RDWR|O_CREAT; |
| 832 | else if (flag[0] == 'n') |
| 833 | flags = O_RDWR|O_CREAT|O_TRUNC; |
| 834 | else { |
| 835 | PyErr_SetString(BsddbError, |
| 836 | "Flag should begin with 'r', 'w', 'c' or 'n'"); |
| 837 | return NULL; |
| 838 | } |
| 839 | if (flag[1] == 'l') { |
| 840 | #if defined(O_EXLOCK) && defined(O_SHLOCK) |
| 841 | if (flag[0] == 'r') |
| 842 | flags |= O_SHLOCK; |
| 843 | else |
| 844 | flags |= O_EXLOCK; |
| 845 | #else |
| 846 | PyErr_SetString(BsddbError, |
| 847 | "locking not supported on this platform"); |
| 848 | return NULL; |
| 849 | #endif |
| 850 | } |
| 851 | else if (flag[1] != '\0') { |
| 852 | PyErr_SetString(BsddbError, |
| 853 | "Flag char 2 should be 'l' or absent"); |
| 854 | return NULL; |
| 855 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 856 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 857 | return newdbrnobject(file, flags, mode, rnflags, cachesize, |
| 858 | psize, lorder, reclen, bval[0], bfname); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 861 | static PyMethodDef bsddbmodule_methods[] = { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 862 | {"hashopen", (PyCFunction)bsdhashopen, 1}, |
| 863 | {"btopen", (PyCFunction)bsdbtopen, 1}, |
| 864 | {"rnopen", (PyCFunction)bsdrnopen, 1}, |
| 865 | {0, 0}, |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 866 | }; |
| 867 | |
| 868 | void |
| 869 | initbsddb() { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 870 | PyObject *m, *d; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 871 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 872 | Bsddbtype.ob_type = &PyType_Type; |
| 873 | m = Py_InitModule("bsddb", bsddbmodule_methods); |
| 874 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 875 | BsddbError = PyErr_NewException("bsddb.error", NULL, NULL); |
| 876 | if (BsddbError != NULL) |
| 877 | PyDict_SetItemString(d, "error", BsddbError); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 878 | } |