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