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 | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 44 | |
| 45 | #include <sys/types.h> |
| 46 | #include <sys/stat.h> |
| 47 | #include <fcntl.h> |
| 48 | #include <db.h> |
| 49 | /* Please don't include internal header files of the Berkeley db package |
| 50 | (it messes up the info required in the Setup file) */ |
| 51 | |
| 52 | typedef struct { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 53 | PyObject_HEAD |
| 54 | DB *di_bsddb; |
| 55 | int di_size; /* -1 means recompute */ |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 56 | } bsddbobject; |
| 57 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 58 | staticforward PyTypeObject Bsddbtype; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 59 | |
| 60 | #define is_bsddbobject(v) ((v)->ob_type == &Bsddbtype) |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 61 | #define check_bsddbobject_open(v) if ((v)->di_bsddb == NULL) \ |
| 62 | { PyErr_SetString(BsddbError, "BSDDB object has already been closed"); \ |
| 63 | return NULL; } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 65 | static PyObject *BsddbError; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 67 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 68 | newdbhashobject(file, flags, mode, |
| 69 | bsize, ffactor, nelem, cachesize, hash, lorder) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 70 | char *file; |
| 71 | int flags; |
| 72 | int mode; |
| 73 | int bsize; |
| 74 | int ffactor; |
| 75 | int nelem; |
| 76 | int cachesize; |
| 77 | int hash; /* XXX ignored */ |
| 78 | int lorder; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 79 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 80 | bsddbobject *dp; |
| 81 | HASHINFO info; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 82 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 83 | if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL) |
| 84 | return NULL; |
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 | info.bsize = bsize; |
| 87 | info.ffactor = ffactor; |
| 88 | info.nelem = nelem; |
| 89 | info.cachesize = cachesize; |
| 90 | info.hash = NULL; /* XXX should derive from hash argument */ |
| 91 | info.lorder = lorder; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 92 | |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 93 | #ifdef O_BINARY |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 94 | flags |= O_BINARY; |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 95 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 96 | if ((dp->di_bsddb = dbopen(file, flags, |
| 97 | mode, DB_HASH, &info)) == NULL) { |
| 98 | PyErr_SetFromErrno(BsddbError); |
| 99 | Py_DECREF(dp); |
| 100 | return NULL; |
| 101 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 102 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 103 | dp->di_size = -1; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 104 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 105 | return (PyObject *)dp; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 108 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 109 | newdbbtobject(file, flags, mode, |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 110 | btflags, cachesize, maxkeypage, minkeypage, psize, lorder) |
| 111 | char *file; |
| 112 | int flags; |
| 113 | int mode; |
| 114 | int btflags; |
| 115 | int cachesize; |
| 116 | int maxkeypage; |
| 117 | int minkeypage; |
| 118 | int psize; |
| 119 | int lorder; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 120 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 121 | bsddbobject *dp; |
| 122 | BTREEINFO info; |
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 | if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL) |
| 125 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 126 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 127 | info.flags = btflags; |
| 128 | info.cachesize = cachesize; |
| 129 | info.maxkeypage = maxkeypage; |
| 130 | info.minkeypage = minkeypage; |
| 131 | info.psize = psize; |
| 132 | info.lorder = lorder; |
| 133 | info.compare = 0; /* Use default comparison functions, for now..*/ |
| 134 | info.prefix = 0; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 135 | |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 136 | #ifdef O_BINARY |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 137 | flags |= O_BINARY; |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 138 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 139 | if ((dp->di_bsddb = dbopen(file, flags, |
| 140 | mode, DB_BTREE, &info)) == NULL) { |
| 141 | PyErr_SetFromErrno(BsddbError); |
| 142 | Py_DECREF(dp); |
| 143 | return NULL; |
| 144 | } |
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 | dp->di_size = -1; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 147 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 148 | return (PyObject *)dp; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 151 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 152 | newdbrnobject(file, flags, mode, |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 153 | rnflags, cachesize, psize, lorder, reclen, bval, bfname) |
| 154 | char *file; |
| 155 | int flags; |
| 156 | int mode; |
| 157 | int rnflags; |
| 158 | int cachesize; |
| 159 | int psize; |
| 160 | int lorder; |
| 161 | size_t reclen; |
| 162 | u_char bval; |
| 163 | char *bfname; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 164 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 165 | bsddbobject *dp; |
| 166 | RECNOINFO info; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 167 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 168 | if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL) |
| 169 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 170 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 171 | info.flags = rnflags; |
| 172 | info.cachesize = cachesize; |
| 173 | info.psize = psize; |
| 174 | info.lorder = lorder; |
| 175 | info.reclen = reclen; |
| 176 | info.bval = bval; |
| 177 | info.bfname = bfname; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 178 | |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 179 | #ifdef O_BINARY |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 180 | flags |= O_BINARY; |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 181 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 182 | if ((dp->di_bsddb = dbopen(file, flags, mode, |
| 183 | DB_RECNO, &info)) == NULL) { |
| 184 | PyErr_SetFromErrno(BsddbError); |
| 185 | Py_DECREF(dp); |
| 186 | return NULL; |
| 187 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 188 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 189 | dp->di_size = -1; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 190 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 191 | return (PyObject *)dp; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | |
| 195 | static void |
| 196 | bsddb_dealloc(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 197 | bsddbobject *dp; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 198 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 199 | if (dp->di_bsddb != NULL) { |
| 200 | if ((dp->di_bsddb->close)(dp->di_bsddb) != 0) |
| 201 | fprintf(stderr, |
| 202 | "Python bsddb: close errno %d in dealloc\n", |
| 203 | errno); |
| 204 | } |
| 205 | PyMem_DEL(dp); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static int |
| 209 | bsddb_length(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 210 | bsddbobject *dp; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 211 | { |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 212 | if (dp->di_bsddb == NULL) { |
| 213 | PyErr_SetString(BsddbError, "BSDDB object has already been closed"); |
| 214 | return -1; |
| 215 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 216 | if (dp->di_size < 0) { |
| 217 | DBT krec, drec; |
| 218 | int status; |
| 219 | int size = 0; |
| 220 | for (status = (dp->di_bsddb->seq)(dp->di_bsddb, |
| 221 | &krec, &drec,R_FIRST); |
| 222 | status == 0; |
| 223 | status = (dp->di_bsddb->seq)(dp->di_bsddb, |
| 224 | &krec, &drec, R_NEXT)) |
| 225 | size++; |
| 226 | if (status < 0) { |
| 227 | PyErr_SetFromErrno(BsddbError); |
| 228 | return -1; |
| 229 | } |
| 230 | dp->di_size = size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 231 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 232 | return dp->di_size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 235 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 236 | bsddb_subscript(dp, key) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 237 | bsddbobject *dp; |
| 238 | PyObject *key; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 239 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 240 | int status; |
| 241 | DBT krec, drec; |
| 242 | char *data; |
| 243 | int size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 244 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 245 | if (!PyArg_Parse(key, "s#", &data, &size)) |
| 246 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 247 | check_bsddbobject_open(dp); |
| 248 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 249 | krec.data = data; |
| 250 | krec.size = size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 251 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 252 | status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0); |
| 253 | if (status != 0) { |
| 254 | if (status < 0) |
| 255 | PyErr_SetFromErrno(BsddbError); |
| 256 | else |
| 257 | PyErr_SetObject(PyExc_KeyError, key); |
| 258 | return NULL; |
| 259 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 260 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 261 | return PyString_FromStringAndSize((char *)drec.data, (int)drec.size); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static int |
| 265 | bsddb_ass_sub(dp, key, value) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 266 | bsddbobject *dp; |
| 267 | PyObject *key, *value; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 268 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 269 | int status; |
| 270 | DBT krec, drec; |
| 271 | char *data; |
| 272 | int size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 273 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 274 | if (!PyArg_Parse(key, "s#", &data, &size)) { |
| 275 | PyErr_SetString(PyExc_TypeError, |
| 276 | "bsddb key type must be string"); |
| 277 | return -1; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 278 | } |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 279 | if (dp->di_bsddb == NULL) { |
| 280 | PyErr_SetString(BsddbError, "BSDDB object has already been closed"); |
| 281 | return -1; |
| 282 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 283 | krec.data = data; |
| 284 | krec.size = size; |
| 285 | dp->di_size = -1; |
| 286 | if (value == NULL) { |
| 287 | status = (dp->di_bsddb->del)(dp->di_bsddb, &krec, 0); |
| 288 | } |
| 289 | else { |
| 290 | if (!PyArg_Parse(value, "s#", &data, &size)) { |
| 291 | PyErr_SetString(PyExc_TypeError, |
| 292 | "bsddb value type must be string"); |
| 293 | return -1; |
| 294 | } |
| 295 | drec.data = data; |
| 296 | drec.size = size; |
| 297 | #if 0 |
| 298 | /* For RECNO, put fails with 'No space left on device' |
| 299 | after a few short records are added?? Looks fine |
| 300 | to this point... linked with 1.85 on Solaris Intel |
| 301 | Roger E. Masse 1/16/97 |
| 302 | */ |
| 303 | printf("before put data: '%s', size: %d\n", |
| 304 | drec.data, drec.size); |
| 305 | printf("before put key= '%s', size= %d\n", |
| 306 | krec.data, krec.size); |
| 307 | #endif |
| 308 | status = (dp->di_bsddb->put)(dp->di_bsddb, &krec, &drec, 0); |
| 309 | } |
| 310 | if (status != 0) { |
| 311 | if (status < 0) |
| 312 | PyErr_SetFromErrno(BsddbError); |
| 313 | else |
| 314 | PyErr_SetObject(PyExc_KeyError, key); |
| 315 | return -1; |
| 316 | } |
| 317 | return 0; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 320 | static PyMappingMethods bsddb_as_mapping = { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 321 | (inquiry)bsddb_length, /*mp_length*/ |
| 322 | (binaryfunc)bsddb_subscript, /*mp_subscript*/ |
| 323 | (objobjargproc)bsddb_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 324 | }; |
| 325 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 326 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 327 | bsddb_close(dp, args) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 328 | bsddbobject *dp; |
| 329 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 330 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 331 | if (!PyArg_NoArgs(args)) |
| 332 | return NULL; |
| 333 | if (dp->di_bsddb != NULL) { |
| 334 | if ((dp->di_bsddb->close)(dp->di_bsddb) != 0) { |
| 335 | dp->di_bsddb = NULL; |
| 336 | PyErr_SetFromErrno(BsddbError); |
| 337 | return NULL; |
| 338 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 339 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 340 | dp->di_bsddb = NULL; |
| 341 | Py_INCREF(Py_None); |
| 342 | return Py_None; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 345 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 346 | bsddb_keys(dp, args) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 347 | bsddbobject *dp; |
| 348 | PyObject *args; |
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 | PyObject *list, *item; |
| 351 | DBT krec, drec; |
| 352 | int status; |
| 353 | int err; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 354 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 355 | if (!PyArg_NoArgs(args)) |
| 356 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 357 | check_bsddbobject_open(dp); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 358 | list = PyList_New(0); |
| 359 | if (list == NULL) |
| 360 | return NULL; |
| 361 | for (status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_FIRST); |
| 362 | status == 0; |
| 363 | status = (dp->di_bsddb->seq)(dp->di_bsddb, |
| 364 | &krec, &drec, R_NEXT)) { |
| 365 | item = PyString_FromStringAndSize((char *)krec.data, |
| 366 | (int)krec.size); |
| 367 | if (item == NULL) { |
| 368 | Py_DECREF(list); |
| 369 | return NULL; |
| 370 | } |
| 371 | err = PyList_Append(list, item); |
| 372 | Py_DECREF(item); |
| 373 | if (err != 0) { |
| 374 | Py_DECREF(list); |
| 375 | return NULL; |
| 376 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 377 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 378 | if (status < 0) { |
| 379 | PyErr_SetFromErrno(BsddbError); |
| 380 | Py_DECREF(list); |
| 381 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 382 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 383 | if (dp->di_size < 0) |
| 384 | dp->di_size = PyList_Size(list); /* We just did the work */ |
| 385 | return list; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 388 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 389 | bsddb_has_key(dp, args) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 390 | bsddbobject *dp; |
| 391 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 392 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 393 | DBT krec, drec; |
| 394 | int status; |
| 395 | char *data; |
| 396 | int size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 397 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 398 | if (!PyArg_Parse(args, "s#", &data, &size)) |
| 399 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 400 | check_bsddbobject_open(dp); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 401 | krec.data = data; |
| 402 | krec.size = size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 403 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 404 | status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0); |
| 405 | if (status < 0) { |
| 406 | PyErr_SetFromErrno(BsddbError); |
| 407 | return NULL; |
| 408 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 409 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 410 | return PyInt_FromLong(status == 0); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 413 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 414 | bsddb_set_location(dp, key) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 415 | bsddbobject *dp; |
| 416 | PyObject *key; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 417 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 418 | int status; |
| 419 | DBT krec, drec; |
| 420 | char *data; |
| 421 | int size; |
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_Parse(key, "s#", &data, &size)) |
| 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 | krec.data = data; |
| 427 | krec.size = size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 428 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 429 | status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_CURSOR); |
| 430 | if (status != 0) { |
| 431 | if (status < 0) |
| 432 | PyErr_SetFromErrno(BsddbError); |
| 433 | else |
| 434 | PyErr_SetObject(PyExc_KeyError, key); |
| 435 | return NULL; |
| 436 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 437 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 438 | return Py_BuildValue("s#s#", krec.data, krec.size, |
| 439 | drec.data, drec.size); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 442 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 443 | bsddb_seq(dp, args, sequence_request) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 444 | bsddbobject *dp; |
| 445 | PyObject *args; |
| 446 | int sequence_request; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 447 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 448 | int status; |
| 449 | DBT krec, drec; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 450 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 451 | if (!PyArg_NoArgs(args)) |
| 452 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 453 | |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 454 | check_bsddbobject_open(dp); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 455 | krec.data = 0; |
| 456 | krec.size = 0; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 457 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 458 | status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, |
| 459 | &drec, sequence_request); |
| 460 | if (status != 0) { |
| 461 | if (status < 0) |
| 462 | PyErr_SetFromErrno(BsddbError); |
| 463 | else |
| 464 | PyErr_SetObject(PyExc_KeyError, args); |
| 465 | return NULL; |
| 466 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 467 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 468 | return Py_BuildValue("s#s#", krec.data, krec.size, |
| 469 | drec.data, drec.size); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 472 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 473 | bsddb_next(dp, key) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 474 | bsddbobject *dp; |
| 475 | PyObject *key; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 476 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 477 | return bsddb_seq(dp, key, R_NEXT); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 478 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 479 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 480 | bsddb_previous(dp, key) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 481 | bsddbobject *dp; |
| 482 | PyObject *key; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 483 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 484 | return bsddb_seq(dp, key, R_PREV); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 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_first(dp, key) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 488 | bsddbobject *dp; |
| 489 | PyObject *key; |
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 | return bsddb_seq(dp, key, R_FIRST); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 492 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 493 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 494 | bsddb_last(dp, key) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 495 | bsddbobject *dp; |
| 496 | PyObject *key; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 497 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 498 | return bsddb_seq(dp, key, R_LAST); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 499 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 500 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 501 | bsddb_sync(dp, args) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 502 | bsddbobject *dp; |
| 503 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 504 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 505 | int status; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 506 | |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 507 | if (!PyArg_NoArgs(args)) |
| 508 | return NULL; |
| 509 | check_bsddbobject_open(dp); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 510 | status = (dp->di_bsddb->sync)(dp->di_bsddb, 0); |
| 511 | if (status != 0) { |
| 512 | PyErr_SetFromErrno(BsddbError); |
| 513 | return NULL; |
| 514 | } |
| 515 | return PyInt_FromLong(status = 0); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 516 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 517 | static PyMethodDef bsddb_methods[] = { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 518 | {"close", (PyCFunction)bsddb_close}, |
| 519 | {"keys", (PyCFunction)bsddb_keys}, |
| 520 | {"has_key", (PyCFunction)bsddb_has_key}, |
| 521 | {"set_location", (PyCFunction)bsddb_set_location}, |
| 522 | {"next", (PyCFunction)bsddb_next}, |
| 523 | {"previous", (PyCFunction)bsddb_previous}, |
| 524 | {"first", (PyCFunction)bsddb_first}, |
| 525 | {"last", (PyCFunction)bsddb_last}, |
| 526 | {"sync", (PyCFunction)bsddb_sync}, |
| 527 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 528 | }; |
| 529 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 530 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 531 | bsddb_getattr(dp, name) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 532 | PyObject *dp; |
| 533 | char *name; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 534 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 535 | return Py_FindMethod(bsddb_methods, dp, name); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 538 | static PyTypeObject Bsddbtype = { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 539 | PyObject_HEAD_INIT(NULL) |
| 540 | 0, |
| 541 | "bsddb", |
| 542 | sizeof(bsddbobject), |
| 543 | 0, |
| 544 | (destructor)bsddb_dealloc, /*tp_dealloc*/ |
| 545 | 0, /*tp_print*/ |
| 546 | (getattrfunc)bsddb_getattr, /*tp_getattr*/ |
| 547 | 0, /*tp_setattr*/ |
| 548 | 0, /*tp_compare*/ |
| 549 | 0, /*tp_repr*/ |
| 550 | 0, /*tp_as_number*/ |
| 551 | 0, /*tp_as_sequence*/ |
| 552 | &bsddb_as_mapping, /*tp_as_mapping*/ |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 553 | }; |
| 554 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 555 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 556 | bsdhashopen(self, args) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 557 | PyObject *self; |
| 558 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 559 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 560 | char *file; |
| 561 | char *flag = NULL; |
| 562 | int flags = O_RDONLY; |
| 563 | int mode = 0666; |
| 564 | int bsize = 0; |
| 565 | int ffactor = 0; |
| 566 | int nelem = 0; |
| 567 | int cachesize = 0; |
| 568 | int hash = 0; /* XXX currently ignored */ |
| 569 | int lorder = 0; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 570 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 571 | if (!PyArg_ParseTuple(args, "s|siiiiiii", |
| 572 | &file, &flag, &mode, |
| 573 | &bsize, &ffactor, &nelem, &cachesize, |
| 574 | &hash, &lorder)) |
| 575 | return NULL; |
| 576 | if (flag != NULL) { |
| 577 | /* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */ |
| 578 | if (flag[0] == 'r') |
| 579 | flags = O_RDONLY; |
| 580 | else if (flag[0] == 'w') |
| 581 | flags = O_RDWR; |
| 582 | else if (flag[0] == 'c') |
| 583 | flags = O_RDWR|O_CREAT; |
| 584 | else if (flag[0] == 'n') |
| 585 | flags = O_RDWR|O_CREAT|O_TRUNC; |
| 586 | else { |
| 587 | PyErr_SetString(BsddbError, |
| 588 | "Flag should begin with 'r', 'w', 'c' or 'n'"); |
| 589 | return NULL; |
| 590 | } |
| 591 | if (flag[1] == 'l') { |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 592 | #if defined(O_EXLOCK) && defined(O_SHLOCK) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 593 | if (flag[0] == 'r') |
| 594 | flags |= O_SHLOCK; |
| 595 | else |
| 596 | flags |= O_EXLOCK; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 597 | #else |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 598 | PyErr_SetString(BsddbError, |
| 599 | "locking not supported on this platform"); |
| 600 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 601 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 602 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 603 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 604 | return newdbhashobject(file, flags, mode, |
| 605 | bsize, ffactor, nelem, cachesize, hash, lorder); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 608 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 609 | bsdbtopen(self, args) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 610 | PyObject *self; |
| 611 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 612 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 613 | char *file; |
| 614 | char *flag = NULL; |
| 615 | int flags = O_RDONLY; |
| 616 | int mode = 0666; |
| 617 | int cachesize = 0; |
| 618 | int maxkeypage = 0; |
| 619 | int minkeypage = 0; |
| 620 | int btflags = 0; |
| 621 | unsigned int psize = 0; |
| 622 | int lorder = 0; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 623 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 624 | if (!PyArg_ParseTuple(args, "s|siiiiiii", |
| 625 | &file, &flag, &mode, |
| 626 | &btflags, &cachesize, &maxkeypage, &minkeypage, |
| 627 | &psize, &lorder)) |
| 628 | return NULL; |
| 629 | if (flag != NULL) { |
| 630 | /* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */ |
| 631 | if (flag[0] == 'r') |
| 632 | flags = O_RDONLY; |
| 633 | else if (flag[0] == 'w') |
| 634 | flags = O_RDWR; |
| 635 | else if (flag[0] == 'c') |
| 636 | flags = O_RDWR|O_CREAT; |
| 637 | else if (flag[0] == 'n') |
| 638 | flags = O_RDWR|O_CREAT|O_TRUNC; |
| 639 | else { |
| 640 | PyErr_SetString(BsddbError, |
| 641 | "Flag should begin with 'r', 'w', 'c' or 'n'"); |
| 642 | return NULL; |
| 643 | } |
| 644 | if (flag[1] == 'l') { |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 645 | #if defined(O_EXLOCK) && defined(O_SHLOCK) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 646 | if (flag[0] == 'r') |
| 647 | flags |= O_SHLOCK; |
| 648 | else |
| 649 | flags |= O_EXLOCK; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 650 | #else |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 651 | PyErr_SetString(BsddbError, |
| 652 | "locking not supported on this platform"); |
| 653 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 654 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 655 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 656 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 657 | return newdbbtobject(file, flags, mode, |
| 658 | btflags, cachesize, maxkeypage, minkeypage, |
| 659 | psize, lorder); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 660 | } |
Guido van Rossum | dd96ca7 | 1996-05-23 22:57:54 +0000 | [diff] [blame] | 661 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 662 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 663 | bsdrnopen(self, args) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 664 | PyObject *self; |
| 665 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 666 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 667 | char *file; |
| 668 | char *flag = NULL; |
| 669 | int flags = O_RDONLY; |
| 670 | int mode = 0666; |
| 671 | int cachesize = 0; |
| 672 | int rnflags = 0; |
| 673 | unsigned int psize = 0; |
| 674 | int lorder = 0; |
| 675 | size_t reclen = 0; |
| 676 | char *bval = ""; |
| 677 | char *bfname = NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 678 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 679 | if (!PyArg_ParseTuple(args, "s|siiiiiiss", |
| 680 | &file, &flag, &mode, |
| 681 | &rnflags, &cachesize, &psize, &lorder, |
| 682 | &reclen, &bval, &bfname)) |
| 683 | return NULL; |
| 684 | |
| 685 | # if 0 |
| 686 | printf("file: %s\n", file); |
| 687 | printf("flag: %s\n", flag); |
| 688 | printf("mode: %d\n", mode); |
| 689 | printf("rnflags: 0x%x\n", rnflags); |
| 690 | printf("cachesize: %d\n", cachesize); |
| 691 | printf("psize: %d\n", psize); |
| 692 | printf("lorder: %d\n", 0); |
| 693 | printf("reclen: %d\n", reclen); |
| 694 | printf("bval: %c\n", bval[0]); |
| 695 | printf("bfname %s\n", bfname); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 696 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 697 | |
| 698 | if (flag != NULL) { |
| 699 | /* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */ |
| 700 | if (flag[0] == 'r') |
| 701 | flags = O_RDONLY; |
| 702 | else if (flag[0] == 'w') |
| 703 | flags = O_RDWR; |
| 704 | else if (flag[0] == 'c') |
| 705 | flags = O_RDWR|O_CREAT; |
| 706 | else if (flag[0] == 'n') |
| 707 | flags = O_RDWR|O_CREAT|O_TRUNC; |
| 708 | else { |
| 709 | PyErr_SetString(BsddbError, |
| 710 | "Flag should begin with 'r', 'w', 'c' or 'n'"); |
| 711 | return NULL; |
| 712 | } |
| 713 | if (flag[1] == 'l') { |
| 714 | #if defined(O_EXLOCK) && defined(O_SHLOCK) |
| 715 | if (flag[0] == 'r') |
| 716 | flags |= O_SHLOCK; |
| 717 | else |
| 718 | flags |= O_EXLOCK; |
| 719 | #else |
| 720 | PyErr_SetString(BsddbError, |
| 721 | "locking not supported on this platform"); |
| 722 | return NULL; |
| 723 | #endif |
| 724 | } |
| 725 | else if (flag[1] != '\0') { |
| 726 | PyErr_SetString(BsddbError, |
| 727 | "Flag char 2 should be 'l' or absent"); |
| 728 | return NULL; |
| 729 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 730 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 731 | return newdbrnobject(file, flags, mode, rnflags, cachesize, |
| 732 | psize, lorder, reclen, bval[0], bfname); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 735 | static PyMethodDef bsddbmodule_methods[] = { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 736 | {"hashopen", (PyCFunction)bsdhashopen, 1}, |
| 737 | {"btopen", (PyCFunction)bsdbtopen, 1}, |
| 738 | {"rnopen", (PyCFunction)bsdrnopen, 1}, |
| 739 | {0, 0}, |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 740 | }; |
| 741 | |
| 742 | void |
| 743 | initbsddb() { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 744 | PyObject *m, *d; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 745 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 746 | Bsddbtype.ob_type = &PyType_Type; |
| 747 | m = Py_InitModule("bsddb", bsddbmodule_methods); |
| 748 | d = PyModule_GetDict(m); |
| 749 | BsddbError = PyString_FromString("bsddb.error"); |
| 750 | if (BsddbError == NULL || PyDict_SetItemString(d, "error", BsddbError)) |
| 751 | Py_FatalError("can't define bsddb.error"); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 752 | } |