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