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