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; |
| 269 | memcpy(data,drec.data,drec.size); |
| 270 | } |
| 271 | BSDDB_END_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 272 | if (status != 0) { |
| 273 | if (status < 0) |
| 274 | PyErr_SetFromErrno(BsddbError); |
| 275 | else |
| 276 | PyErr_SetObject(PyExc_KeyError, key); |
| 277 | return NULL; |
| 278 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 279 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 280 | result = PyString_FromStringAndSize(data, (int)drec.size); |
| 281 | if (data != buf) free(data); |
| 282 | return result; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static int |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 286 | bsddb_ass_sub(bsddbobject *dp, PyObject *key, PyObject *value) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 287 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 288 | int status; |
| 289 | DBT krec, drec; |
| 290 | char *data; |
| 291 | int size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 292 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 293 | if (!PyArg_Parse(key, "s#", &data, &size)) { |
| 294 | PyErr_SetString(PyExc_TypeError, |
| 295 | "bsddb key type must be string"); |
| 296 | return -1; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 297 | } |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 298 | if (dp->di_bsddb == NULL) { |
| 299 | PyErr_SetString(BsddbError, "BSDDB object has already been closed"); |
| 300 | return -1; |
| 301 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 302 | krec.data = data; |
| 303 | krec.size = size; |
| 304 | dp->di_size = -1; |
| 305 | if (value == NULL) { |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 306 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 307 | status = (dp->di_bsddb->del)(dp->di_bsddb, &krec, 0); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 308 | BSDDB_END_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 309 | } |
| 310 | else { |
| 311 | if (!PyArg_Parse(value, "s#", &data, &size)) { |
| 312 | PyErr_SetString(PyExc_TypeError, |
| 313 | "bsddb value type must be string"); |
| 314 | return -1; |
| 315 | } |
| 316 | drec.data = data; |
| 317 | drec.size = size; |
| 318 | #if 0 |
| 319 | /* For RECNO, put fails with 'No space left on device' |
| 320 | after a few short records are added?? Looks fine |
| 321 | to this point... linked with 1.85 on Solaris Intel |
| 322 | Roger E. Masse 1/16/97 |
| 323 | */ |
| 324 | printf("before put data: '%s', size: %d\n", |
| 325 | drec.data, drec.size); |
| 326 | printf("before put key= '%s', size= %d\n", |
| 327 | krec.data, krec.size); |
| 328 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 329 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 330 | status = (dp->di_bsddb->put)(dp->di_bsddb, &krec, &drec, 0); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 331 | BSDDB_END_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 332 | } |
| 333 | if (status != 0) { |
| 334 | if (status < 0) |
| 335 | PyErr_SetFromErrno(BsddbError); |
| 336 | else |
| 337 | PyErr_SetObject(PyExc_KeyError, key); |
| 338 | return -1; |
| 339 | } |
| 340 | return 0; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 343 | static PyMappingMethods bsddb_as_mapping = { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 344 | (inquiry)bsddb_length, /*mp_length*/ |
| 345 | (binaryfunc)bsddb_subscript, /*mp_subscript*/ |
| 346 | (objobjargproc)bsddb_ass_sub, /*mp_ass_subscript*/ |
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 PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 350 | bsddb_close(bsddbobject *dp, PyObject *args) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 351 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 352 | if (!PyArg_NoArgs(args)) |
| 353 | return NULL; |
| 354 | if (dp->di_bsddb != NULL) { |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 355 | int status; |
| 356 | BSDDB_BGN_SAVE(dp) |
| 357 | status = (dp->di_bsddb->close)(dp->di_bsddb); |
| 358 | BSDDB_END_SAVE(dp) |
| 359 | if (status != 0) { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 360 | dp->di_bsddb = NULL; |
| 361 | PyErr_SetFromErrno(BsddbError); |
| 362 | return NULL; |
| 363 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 364 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 365 | dp->di_bsddb = NULL; |
| 366 | Py_INCREF(Py_None); |
| 367 | return Py_None; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 370 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 371 | bsddb_keys(bsddbobject *dp, PyObject *args) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 372 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 373 | PyObject *list, *item; |
| 374 | DBT krec, drec; |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 375 | char *data=NULL,buf[4096]; |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 376 | int status; |
| 377 | int err; |
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 | if (!PyArg_NoArgs(args)) |
| 380 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 381 | check_bsddbobject_open(dp); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 382 | list = PyList_New(0); |
| 383 | if (list == NULL) |
| 384 | return NULL; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 385 | BSDDB_BGN_SAVE(dp) |
| 386 | status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_FIRST); |
| 387 | if (status == 0) { |
| 388 | if (krec.size > sizeof(buf)) data = malloc(krec.size); |
| 389 | else data = buf; |
| 390 | memcpy(data,krec.data,krec.size); |
| 391 | } |
| 392 | BSDDB_END_SAVE(dp) |
| 393 | while (status == 0) { |
| 394 | item = PyString_FromStringAndSize(data, (int)krec.size); |
| 395 | if (data != buf) free(data); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 396 | if (item == NULL) { |
| 397 | Py_DECREF(list); |
| 398 | return NULL; |
| 399 | } |
| 400 | err = PyList_Append(list, item); |
| 401 | Py_DECREF(item); |
| 402 | if (err != 0) { |
| 403 | Py_DECREF(list); |
| 404 | return NULL; |
| 405 | } |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 406 | BSDDB_BGN_SAVE(dp) |
| 407 | status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_NEXT); |
| 408 | if (status == 0) { |
| 409 | if (krec.size > sizeof(buf)) data = malloc(krec.size); |
| 410 | else data = buf; |
| 411 | memcpy(data,krec.data,krec.size); |
| 412 | } |
| 413 | BSDDB_END_SAVE(dp) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 414 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 415 | if (status < 0) { |
| 416 | PyErr_SetFromErrno(BsddbError); |
| 417 | Py_DECREF(list); |
| 418 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 419 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 420 | if (dp->di_size < 0) |
| 421 | dp->di_size = PyList_Size(list); /* We just did the work */ |
| 422 | return list; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 425 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 426 | bsddb_has_key(bsddbobject *dp, PyObject *args) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 427 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 428 | DBT krec, drec; |
| 429 | int status; |
| 430 | char *data; |
| 431 | int size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 432 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 433 | if (!PyArg_Parse(args, "s#", &data, &size)) |
| 434 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 435 | check_bsddbobject_open(dp); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 436 | krec.data = data; |
| 437 | krec.size = size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 438 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 439 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 440 | status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 441 | BSDDB_END_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 442 | if (status < 0) { |
| 443 | PyErr_SetFromErrno(BsddbError); |
| 444 | return NULL; |
| 445 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 446 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 447 | return PyInt_FromLong(status == 0); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 450 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 451 | bsddb_set_location(bsddbobject *dp, PyObject *key) |
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 | int status; |
| 454 | DBT krec, drec; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 455 | char *data,buf[4096]; |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 456 | int size; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 457 | PyObject *result; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 458 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 459 | if (!PyArg_Parse(key, "s#", &data, &size)) |
| 460 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 461 | check_bsddbobject_open(dp); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 462 | krec.data = data; |
| 463 | krec.size = size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 464 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 465 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 466 | 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] | 467 | if (status == 0) { |
| 468 | if (drec.size > sizeof(buf)) data = malloc(drec.size); |
| 469 | else data = buf; |
| 470 | memcpy(data,drec.data,drec.size); |
| 471 | } |
| 472 | BSDDB_END_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 473 | if (status != 0) { |
| 474 | if (status < 0) |
| 475 | PyErr_SetFromErrno(BsddbError); |
| 476 | else |
| 477 | PyErr_SetObject(PyExc_KeyError, key); |
| 478 | return NULL; |
| 479 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 480 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 481 | result = Py_BuildValue("s#s#", krec.data, krec.size, data, drec.size); |
| 482 | if (data != buf) free(data); |
| 483 | return result; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 486 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 487 | bsddb_seq(bsddbobject *dp, PyObject *args, int sequence_request) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 488 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 489 | int status; |
| 490 | DBT krec, drec; |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 491 | char *kdata=NULL,kbuf[4096]; |
| 492 | char *ddata=NULL,dbuf[4096]; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 493 | PyObject *result; |
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 | if (!PyArg_NoArgs(args)) |
| 496 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 497 | |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 498 | check_bsddbobject_open(dp); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 499 | krec.data = 0; |
| 500 | krec.size = 0; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 501 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 502 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 503 | status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, |
| 504 | &drec, sequence_request); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 505 | if (status == 0) { |
| 506 | if (krec.size > sizeof(kbuf)) kdata = malloc(krec.size); |
| 507 | else kdata = kbuf; |
| 508 | memcpy(kdata,krec.data,krec.size); |
| 509 | if (drec.size > sizeof(dbuf)) ddata = malloc(drec.size); |
| 510 | else ddata = dbuf; |
| 511 | memcpy(ddata,drec.data,drec.size); |
| 512 | } |
| 513 | BSDDB_END_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 514 | if (status != 0) { |
| 515 | if (status < 0) |
| 516 | PyErr_SetFromErrno(BsddbError); |
| 517 | else |
| 518 | PyErr_SetObject(PyExc_KeyError, args); |
| 519 | return NULL; |
| 520 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 521 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 522 | result = Py_BuildValue("s#s#", kdata, krec.size, ddata, drec.size); |
| 523 | if (kdata != kbuf) free(kdata); |
| 524 | if (ddata != dbuf) free(ddata); |
| 525 | return result; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 528 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 529 | bsddb_next(bsddbobject *dp, PyObject *key) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 530 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 531 | return bsddb_seq(dp, key, R_NEXT); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 532 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 533 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 534 | bsddb_previous(bsddbobject *dp, PyObject *key) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 535 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 536 | return bsddb_seq(dp, key, R_PREV); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 537 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 538 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 539 | bsddb_first(bsddbobject *dp, PyObject *key) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 540 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 541 | return bsddb_seq(dp, key, R_FIRST); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 542 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 543 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 544 | bsddb_last(bsddbobject *dp, PyObject *key) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 545 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 546 | return bsddb_seq(dp, key, R_LAST); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 547 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 548 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 549 | bsddb_sync(bsddbobject *dp, PyObject *args) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 550 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 551 | int status; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 552 | |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 553 | if (!PyArg_NoArgs(args)) |
| 554 | return NULL; |
| 555 | check_bsddbobject_open(dp); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 556 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 557 | status = (dp->di_bsddb->sync)(dp->di_bsddb, 0); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 558 | BSDDB_END_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 559 | if (status != 0) { |
| 560 | PyErr_SetFromErrno(BsddbError); |
| 561 | return NULL; |
| 562 | } |
| 563 | return PyInt_FromLong(status = 0); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 564 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 565 | static PyMethodDef bsddb_methods[] = { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 566 | {"close", (PyCFunction)bsddb_close}, |
| 567 | {"keys", (PyCFunction)bsddb_keys}, |
| 568 | {"has_key", (PyCFunction)bsddb_has_key}, |
| 569 | {"set_location", (PyCFunction)bsddb_set_location}, |
| 570 | {"next", (PyCFunction)bsddb_next}, |
| 571 | {"previous", (PyCFunction)bsddb_previous}, |
| 572 | {"first", (PyCFunction)bsddb_first}, |
| 573 | {"last", (PyCFunction)bsddb_last}, |
| 574 | {"sync", (PyCFunction)bsddb_sync}, |
| 575 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 576 | }; |
| 577 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 578 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 579 | bsddb_getattr(PyObject *dp, char *name) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 580 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 581 | return Py_FindMethod(bsddb_methods, dp, name); |
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 PyTypeObject Bsddbtype = { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 585 | PyObject_HEAD_INIT(NULL) |
| 586 | 0, |
| 587 | "bsddb", |
| 588 | sizeof(bsddbobject), |
| 589 | 0, |
| 590 | (destructor)bsddb_dealloc, /*tp_dealloc*/ |
| 591 | 0, /*tp_print*/ |
| 592 | (getattrfunc)bsddb_getattr, /*tp_getattr*/ |
| 593 | 0, /*tp_setattr*/ |
| 594 | 0, /*tp_compare*/ |
| 595 | 0, /*tp_repr*/ |
| 596 | 0, /*tp_as_number*/ |
| 597 | 0, /*tp_as_sequence*/ |
| 598 | &bsddb_as_mapping, /*tp_as_mapping*/ |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 599 | }; |
| 600 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 601 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 602 | bsdhashopen(PyObject *self, PyObject *args) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 603 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 604 | char *file; |
| 605 | char *flag = NULL; |
| 606 | int flags = O_RDONLY; |
| 607 | int mode = 0666; |
| 608 | int bsize = 0; |
| 609 | int ffactor = 0; |
| 610 | int nelem = 0; |
| 611 | int cachesize = 0; |
| 612 | int hash = 0; /* XXX currently ignored */ |
| 613 | int lorder = 0; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 614 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 615 | if (!PyArg_ParseTuple(args, "s|siiiiiii:hashopen", |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 616 | &file, &flag, &mode, |
| 617 | &bsize, &ffactor, &nelem, &cachesize, |
| 618 | &hash, &lorder)) |
| 619 | return NULL; |
| 620 | if (flag != NULL) { |
| 621 | /* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */ |
| 622 | if (flag[0] == 'r') |
| 623 | flags = O_RDONLY; |
| 624 | else if (flag[0] == 'w') |
| 625 | flags = O_RDWR; |
| 626 | else if (flag[0] == 'c') |
| 627 | flags = O_RDWR|O_CREAT; |
| 628 | else if (flag[0] == 'n') |
| 629 | flags = O_RDWR|O_CREAT|O_TRUNC; |
| 630 | else { |
| 631 | PyErr_SetString(BsddbError, |
| 632 | "Flag should begin with 'r', 'w', 'c' or 'n'"); |
| 633 | return NULL; |
| 634 | } |
| 635 | if (flag[1] == 'l') { |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 636 | #if defined(O_EXLOCK) && defined(O_SHLOCK) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 637 | if (flag[0] == 'r') |
| 638 | flags |= O_SHLOCK; |
| 639 | else |
| 640 | flags |= O_EXLOCK; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 641 | #else |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 642 | PyErr_SetString(BsddbError, |
| 643 | "locking not supported on this platform"); |
| 644 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 645 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 646 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 647 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 648 | return newdbhashobject(file, flags, mode, |
| 649 | bsize, ffactor, nelem, cachesize, hash, lorder); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 652 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 653 | bsdbtopen(PyObject *self, PyObject *args) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 654 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 655 | char *file; |
| 656 | char *flag = NULL; |
| 657 | int flags = O_RDONLY; |
| 658 | int mode = 0666; |
| 659 | int cachesize = 0; |
| 660 | int maxkeypage = 0; |
| 661 | int minkeypage = 0; |
| 662 | int btflags = 0; |
| 663 | unsigned int psize = 0; |
| 664 | int lorder = 0; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 665 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 666 | if (!PyArg_ParseTuple(args, "s|siiiiiii:btopen", |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 667 | &file, &flag, &mode, |
| 668 | &btflags, &cachesize, &maxkeypage, &minkeypage, |
| 669 | &psize, &lorder)) |
| 670 | return NULL; |
| 671 | if (flag != NULL) { |
| 672 | /* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */ |
| 673 | if (flag[0] == 'r') |
| 674 | flags = O_RDONLY; |
| 675 | else if (flag[0] == 'w') |
| 676 | flags = O_RDWR; |
| 677 | else if (flag[0] == 'c') |
| 678 | flags = O_RDWR|O_CREAT; |
| 679 | else if (flag[0] == 'n') |
| 680 | flags = O_RDWR|O_CREAT|O_TRUNC; |
| 681 | else { |
| 682 | PyErr_SetString(BsddbError, |
| 683 | "Flag should begin with 'r', 'w', 'c' or 'n'"); |
| 684 | return NULL; |
| 685 | } |
| 686 | if (flag[1] == 'l') { |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 687 | #if defined(O_EXLOCK) && defined(O_SHLOCK) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 688 | if (flag[0] == 'r') |
| 689 | flags |= O_SHLOCK; |
| 690 | else |
| 691 | flags |= O_EXLOCK; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 692 | #else |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 693 | PyErr_SetString(BsddbError, |
| 694 | "locking not supported on this platform"); |
| 695 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 696 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 697 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 698 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 699 | return newdbbtobject(file, flags, mode, |
| 700 | btflags, cachesize, maxkeypage, minkeypage, |
| 701 | psize, lorder); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 702 | } |
Guido van Rossum | dd96ca7 | 1996-05-23 22:57:54 +0000 | [diff] [blame] | 703 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 704 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 705 | bsdrnopen(PyObject *self, PyObject *args) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 706 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 707 | char *file; |
| 708 | char *flag = NULL; |
| 709 | int flags = O_RDONLY; |
| 710 | int mode = 0666; |
| 711 | int cachesize = 0; |
| 712 | int rnflags = 0; |
| 713 | unsigned int psize = 0; |
| 714 | int lorder = 0; |
| 715 | size_t reclen = 0; |
| 716 | char *bval = ""; |
| 717 | char *bfname = NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 718 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 719 | if (!PyArg_ParseTuple(args, "s|siiiiiiss:rnopen", |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 720 | &file, &flag, &mode, |
| 721 | &rnflags, &cachesize, &psize, &lorder, |
| 722 | &reclen, &bval, &bfname)) |
| 723 | return NULL; |
| 724 | |
| 725 | # if 0 |
| 726 | printf("file: %s\n", file); |
| 727 | printf("flag: %s\n", flag); |
| 728 | printf("mode: %d\n", mode); |
| 729 | printf("rnflags: 0x%x\n", rnflags); |
| 730 | printf("cachesize: %d\n", cachesize); |
| 731 | printf("psize: %d\n", psize); |
| 732 | printf("lorder: %d\n", 0); |
| 733 | printf("reclen: %d\n", reclen); |
| 734 | printf("bval: %c\n", bval[0]); |
| 735 | printf("bfname %s\n", bfname); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 736 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 737 | |
| 738 | if (flag != NULL) { |
| 739 | /* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */ |
| 740 | if (flag[0] == 'r') |
| 741 | flags = O_RDONLY; |
| 742 | else if (flag[0] == 'w') |
| 743 | flags = O_RDWR; |
| 744 | else if (flag[0] == 'c') |
| 745 | flags = O_RDWR|O_CREAT; |
| 746 | else if (flag[0] == 'n') |
| 747 | flags = O_RDWR|O_CREAT|O_TRUNC; |
| 748 | else { |
| 749 | PyErr_SetString(BsddbError, |
| 750 | "Flag should begin with 'r', 'w', 'c' or 'n'"); |
| 751 | return NULL; |
| 752 | } |
| 753 | if (flag[1] == 'l') { |
| 754 | #if defined(O_EXLOCK) && defined(O_SHLOCK) |
| 755 | if (flag[0] == 'r') |
| 756 | flags |= O_SHLOCK; |
| 757 | else |
| 758 | flags |= O_EXLOCK; |
| 759 | #else |
| 760 | PyErr_SetString(BsddbError, |
| 761 | "locking not supported on this platform"); |
| 762 | return NULL; |
| 763 | #endif |
| 764 | } |
| 765 | else if (flag[1] != '\0') { |
| 766 | PyErr_SetString(BsddbError, |
| 767 | "Flag char 2 should be 'l' or absent"); |
| 768 | return NULL; |
| 769 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 770 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 771 | return newdbrnobject(file, flags, mode, rnflags, cachesize, |
| 772 | psize, lorder, reclen, bval[0], bfname); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 775 | static PyMethodDef bsddbmodule_methods[] = { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 776 | {"hashopen", (PyCFunction)bsdhashopen, 1}, |
| 777 | {"btopen", (PyCFunction)bsdbtopen, 1}, |
| 778 | {"rnopen", (PyCFunction)bsdrnopen, 1}, |
| 779 | {0, 0}, |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 780 | }; |
| 781 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 782 | DL_EXPORT(void) |
Thomas Wouters | 58d0510 | 2000-07-24 14:43:35 +0000 | [diff] [blame] | 783 | initbsddb(void) { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 784 | PyObject *m, *d; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 785 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 786 | Bsddbtype.ob_type = &PyType_Type; |
| 787 | m = Py_InitModule("bsddb", bsddbmodule_methods); |
| 788 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 789 | BsddbError = PyErr_NewException("bsddb.error", NULL, NULL); |
| 790 | if (BsddbError != NULL) |
| 791 | PyDict_SetItemString(d, "error", BsddbError); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 792 | } |