1995-08-31 07:43:03 +08:00
|
|
|
/* Berkeley DB interface.
|
|
|
|
Author: Michael McLay
|
|
|
|
Hacked: Guido van Rossum
|
|
|
|
Btree and Recno additions plus sequence methods: David Ely
|
2001-02-28 02:56:46 +08:00
|
|
|
Hacked by Gustavo Niemeyer <niemeyer@conectiva.com> fixing recno
|
|
|
|
support.
|
1995-08-31 07:43:03 +08:00
|
|
|
|
|
|
|
XXX To do:
|
|
|
|
- provide a way to access the various hash functions
|
|
|
|
- support more open flags
|
1999-09-20 21:28:18 +08:00
|
|
|
|
|
|
|
The windows port of the Berkeley DB code is hard to find on the web:
|
|
|
|
www.nightmare.com/software.html
|
|
|
|
*/
|
1995-08-31 07:43:03 +08:00
|
|
|
|
1996-07-24 08:51:20 +08:00
|
|
|
#include "Python.h"
|
1998-04-10 04:56:35 +08:00
|
|
|
#ifdef WITH_THREAD
|
1998-10-02 04:42:43 +08:00
|
|
|
#include "pythread.h"
|
1998-04-10 04:56:35 +08:00
|
|
|
#endif
|
1995-08-31 07:43:03 +08:00
|
|
|
|
2001-02-28 02:56:46 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
1995-08-31 07:43:03 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2000-09-01 00:11:07 +08:00
|
|
|
#ifdef HAVE_DB_185_H
|
|
|
|
#include <db_185.h>
|
|
|
|
#else
|
1995-08-31 07:43:03 +08:00
|
|
|
#include <db.h>
|
2000-09-01 00:11:07 +08:00
|
|
|
#endif
|
1995-08-31 07:43:03 +08:00
|
|
|
/* Please don't include internal header files of the Berkeley db package
|
|
|
|
(it messes up the info required in the Setup file) */
|
|
|
|
|
|
|
|
typedef struct {
|
1997-01-17 06:05:33 +08:00
|
|
|
PyObject_HEAD
|
|
|
|
DB *di_bsddb;
|
|
|
|
int di_size; /* -1 means recompute */
|
2001-02-28 02:56:46 +08:00
|
|
|
int di_type;
|
1998-04-10 04:56:35 +08:00
|
|
|
#ifdef WITH_THREAD
|
1998-12-22 03:32:43 +08:00
|
|
|
PyThread_type_lock di_lock;
|
1998-04-10 04:56:35 +08:00
|
|
|
#endif
|
1995-08-31 07:43:03 +08:00
|
|
|
} bsddbobject;
|
|
|
|
|
1996-07-24 08:51:20 +08:00
|
|
|
staticforward PyTypeObject Bsddbtype;
|
1995-08-31 07:43:03 +08:00
|
|
|
|
|
|
|
#define is_bsddbobject(v) ((v)->ob_type == &Bsddbtype)
|
2001-02-28 02:56:46 +08:00
|
|
|
#define check_bsddbobject_open(v, r) if ((v)->di_bsddb == NULL) \
|
2001-02-20 05:16:00 +08:00
|
|
|
{ PyErr_SetString(BsddbError, \
|
|
|
|
"BSDDB object has already been closed"); \
|
2001-02-28 02:56:46 +08:00
|
|
|
return r; }
|
1995-08-31 07:43:03 +08:00
|
|
|
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyObject *BsddbError;
|
1995-08-31 07:43:03 +08:00
|
|
|
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyObject *
|
2000-07-11 01:06:38 +08:00
|
|
|
newdbhashobject(char *file, int flags, int mode,
|
2001-02-20 05:16:00 +08:00
|
|
|
int bsize, int ffactor, int nelem, int cachesize,
|
|
|
|
int hash, int lorder)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
1997-01-17 06:05:33 +08:00
|
|
|
bsddbobject *dp;
|
|
|
|
HASHINFO info;
|
1995-08-31 07:43:03 +08:00
|
|
|
|
2000-05-04 07:44:39 +08:00
|
|
|
if ((dp = PyObject_New(bsddbobject, &Bsddbtype)) == NULL)
|
1997-01-17 06:05:33 +08:00
|
|
|
return NULL;
|
1995-08-31 07:43:03 +08:00
|
|
|
|
1997-01-17 06:05:33 +08:00
|
|
|
info.bsize = bsize;
|
|
|
|
info.ffactor = ffactor;
|
|
|
|
info.nelem = nelem;
|
|
|
|
info.cachesize = cachesize;
|
|
|
|
info.hash = NULL; /* XXX should derive from hash argument */
|
|
|
|
info.lorder = lorder;
|
1995-08-31 07:43:03 +08:00
|
|
|
|
1996-09-12 07:22:25 +08:00
|
|
|
#ifdef O_BINARY
|
1997-01-17 06:05:33 +08:00
|
|
|
flags |= O_BINARY;
|
1996-09-12 07:22:25 +08:00
|
|
|
#endif
|
1998-04-10 04:56:35 +08:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
dp->di_bsddb = dbopen(file, flags, mode, DB_HASH, &info);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (dp->di_bsddb == NULL) {
|
1997-01-17 06:05:33 +08:00
|
|
|
PyErr_SetFromErrno(BsddbError);
|
1998-04-10 04:56:35 +08:00
|
|
|
#ifdef WITH_THREAD
|
|
|
|
dp->di_lock = NULL;
|
|
|
|
#endif
|
1997-01-17 06:05:33 +08:00
|
|
|
Py_DECREF(dp);
|
|
|
|
return NULL;
|
|
|
|
}
|
1995-08-31 07:43:03 +08:00
|
|
|
|
1997-01-17 06:05:33 +08:00
|
|
|
dp->di_size = -1;
|
2001-02-28 02:56:46 +08:00
|
|
|
dp->di_type = DB_HASH;
|
|
|
|
|
1998-04-10 04:56:35 +08:00
|
|
|
#ifdef WITH_THREAD
|
1998-12-22 03:32:43 +08:00
|
|
|
dp->di_lock = PyThread_allocate_lock();
|
1998-04-10 04:56:35 +08:00
|
|
|
if (dp->di_lock == NULL) {
|
|
|
|
PyErr_SetString(BsddbError, "can't allocate lock");
|
|
|
|
Py_DECREF(dp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
1995-08-31 07:43:03 +08:00
|
|
|
|
1997-01-17 06:05:33 +08:00
|
|
|
return (PyObject *)dp;
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
|
|
|
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyObject *
|
2000-07-11 01:06:38 +08:00
|
|
|
newdbbtobject(char *file, int flags, int mode,
|
2001-02-20 05:16:00 +08:00
|
|
|
int btflags, int cachesize, int maxkeypage,
|
|
|
|
int minkeypage, int psize, int lorder)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
1997-01-17 06:05:33 +08:00
|
|
|
bsddbobject *dp;
|
|
|
|
BTREEINFO info;
|
1995-08-31 07:43:03 +08:00
|
|
|
|
2000-05-04 07:44:39 +08:00
|
|
|
if ((dp = PyObject_New(bsddbobject, &Bsddbtype)) == NULL)
|
1997-01-17 06:05:33 +08:00
|
|
|
return NULL;
|
1995-08-31 07:43:03 +08:00
|
|
|
|
1997-01-17 06:05:33 +08:00
|
|
|
info.flags = btflags;
|
|
|
|
info.cachesize = cachesize;
|
|
|
|
info.maxkeypage = maxkeypage;
|
|
|
|
info.minkeypage = minkeypage;
|
|
|
|
info.psize = psize;
|
|
|
|
info.lorder = lorder;
|
|
|
|
info.compare = 0; /* Use default comparison functions, for now..*/
|
|
|
|
info.prefix = 0;
|
1995-08-31 07:43:03 +08:00
|
|
|
|
1996-09-12 07:22:25 +08:00
|
|
|
#ifdef O_BINARY
|
1997-01-17 06:05:33 +08:00
|
|
|
flags |= O_BINARY;
|
1996-09-12 07:22:25 +08:00
|
|
|
#endif
|
1998-04-10 04:56:35 +08:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
dp->di_bsddb = dbopen(file, flags, mode, DB_BTREE, &info);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (dp->di_bsddb == NULL) {
|
1997-01-17 06:05:33 +08:00
|
|
|
PyErr_SetFromErrno(BsddbError);
|
1998-04-10 04:56:35 +08:00
|
|
|
#ifdef WITH_THREAD
|
|
|
|
dp->di_lock = NULL;
|
|
|
|
#endif
|
1997-01-17 06:05:33 +08:00
|
|
|
Py_DECREF(dp);
|
|
|
|
return NULL;
|
|
|
|
}
|
1995-08-31 07:43:03 +08:00
|
|
|
|
1997-01-17 06:05:33 +08:00
|
|
|
dp->di_size = -1;
|
2001-02-28 02:56:46 +08:00
|
|
|
dp->di_type = DB_BTREE;
|
|
|
|
|
1998-04-10 04:56:35 +08:00
|
|
|
#ifdef WITH_THREAD
|
1998-12-22 03:32:43 +08:00
|
|
|
dp->di_lock = PyThread_allocate_lock();
|
1998-04-10 04:56:35 +08:00
|
|
|
if (dp->di_lock == NULL) {
|
|
|
|
PyErr_SetString(BsddbError, "can't allocate lock");
|
|
|
|
Py_DECREF(dp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
1995-08-31 07:43:03 +08:00
|
|
|
|
1997-01-17 06:05:33 +08:00
|
|
|
return (PyObject *)dp;
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
|
|
|
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyObject *
|
2000-07-11 01:06:38 +08:00
|
|
|
newdbrnobject(char *file, int flags, int mode,
|
2001-02-20 05:16:00 +08:00
|
|
|
int rnflags, int cachesize, int psize, int lorder,
|
|
|
|
size_t reclen, u_char bval, char *bfname)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
1997-01-17 06:05:33 +08:00
|
|
|
bsddbobject *dp;
|
|
|
|
RECNOINFO info;
|
2001-02-28 02:56:46 +08:00
|
|
|
int fd;
|
1995-08-31 07:43:03 +08:00
|
|
|
|
2000-05-04 07:44:39 +08:00
|
|
|
if ((dp = PyObject_New(bsddbobject, &Bsddbtype)) == NULL)
|
1997-01-17 06:05:33 +08:00
|
|
|
return NULL;
|
1995-08-31 07:43:03 +08:00
|
|
|
|
1997-01-17 06:05:33 +08:00
|
|
|
info.flags = rnflags;
|
|
|
|
info.cachesize = cachesize;
|
|
|
|
info.psize = psize;
|
|
|
|
info.lorder = lorder;
|
|
|
|
info.reclen = reclen;
|
|
|
|
info.bval = bval;
|
|
|
|
info.bfname = bfname;
|
1995-08-31 07:43:03 +08:00
|
|
|
|
1996-09-12 07:22:25 +08:00
|
|
|
#ifdef O_BINARY
|
1997-01-17 06:05:33 +08:00
|
|
|
flags |= O_BINARY;
|
1996-09-12 07:22:25 +08:00
|
|
|
#endif
|
2001-02-28 02:56:46 +08:00
|
|
|
/* This is a hack to avoid a dbopen() bug that happens when
|
|
|
|
* it fails. */
|
|
|
|
fd = open(file, flags);
|
|
|
|
if (fd == -1) {
|
|
|
|
dp->di_bsddb = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
close(fd);
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
dp->di_bsddb = dbopen(file, flags, mode, DB_RECNO, &info);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
}
|
1998-04-10 04:56:35 +08:00
|
|
|
if (dp->di_bsddb == NULL) {
|
1997-01-17 06:05:33 +08:00
|
|
|
PyErr_SetFromErrno(BsddbError);
|
1998-04-10 04:56:35 +08:00
|
|
|
#ifdef WITH_THREAD
|
|
|
|
dp->di_lock = NULL;
|
|
|
|
#endif
|
1997-01-17 06:05:33 +08:00
|
|
|
Py_DECREF(dp);
|
|
|
|
return NULL;
|
|
|
|
}
|
1995-08-31 07:43:03 +08:00
|
|
|
|
1997-01-17 06:05:33 +08:00
|
|
|
dp->di_size = -1;
|
2001-02-28 02:56:46 +08:00
|
|
|
dp->di_type = DB_RECNO;
|
|
|
|
|
1998-04-10 04:56:35 +08:00
|
|
|
#ifdef WITH_THREAD
|
1998-12-22 03:32:43 +08:00
|
|
|
dp->di_lock = PyThread_allocate_lock();
|
1998-04-10 04:56:35 +08:00
|
|
|
if (dp->di_lock == NULL) {
|
|
|
|
PyErr_SetString(BsddbError, "can't allocate lock");
|
|
|
|
Py_DECREF(dp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
1995-08-31 07:43:03 +08:00
|
|
|
|
1997-01-17 06:05:33 +08:00
|
|
|
return (PyObject *)dp;
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-11 01:06:38 +08:00
|
|
|
bsddb_dealloc(bsddbobject *dp)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
1998-04-10 04:56:35 +08:00
|
|
|
#ifdef WITH_THREAD
|
|
|
|
if (dp->di_lock) {
|
1998-12-22 03:32:43 +08:00
|
|
|
PyThread_acquire_lock(dp->di_lock, 0);
|
|
|
|
PyThread_release_lock(dp->di_lock);
|
|
|
|
PyThread_free_lock(dp->di_lock);
|
1998-04-10 04:56:35 +08:00
|
|
|
dp->di_lock = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
1997-01-17 06:05:33 +08:00
|
|
|
if (dp->di_bsddb != NULL) {
|
1998-04-10 04:56:35 +08:00
|
|
|
int status;
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
status = (dp->di_bsddb->close)(dp->di_bsddb);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (status != 0)
|
1997-01-17 06:05:33 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"Python bsddb: close errno %d in dealloc\n",
|
|
|
|
errno);
|
|
|
|
}
|
2000-05-04 07:44:39 +08:00
|
|
|
PyObject_Del(dp);
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
|
|
|
|
1998-04-10 04:56:35 +08:00
|
|
|
#ifdef WITH_THREAD
|
2001-02-20 05:16:00 +08:00
|
|
|
#define BSDDB_BGN_SAVE(_dp) \
|
|
|
|
Py_BEGIN_ALLOW_THREADS PyThread_acquire_lock(_dp->di_lock,1);
|
|
|
|
#define BSDDB_END_SAVE(_dp) \
|
|
|
|
PyThread_release_lock(_dp->di_lock); Py_END_ALLOW_THREADS
|
1998-04-10 04:56:35 +08:00
|
|
|
#else
|
|
|
|
#define BSDDB_BGN_SAVE(_dp) Py_BEGIN_ALLOW_THREADS
|
|
|
|
#define BSDDB_END_SAVE(_dp) Py_END_ALLOW_THREADS
|
|
|
|
#endif
|
|
|
|
|
1995-08-31 07:43:03 +08:00
|
|
|
static int
|
2000-07-11 01:06:38 +08:00
|
|
|
bsddb_length(bsddbobject *dp)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
2001-02-28 02:56:46 +08:00
|
|
|
check_bsddbobject_open(dp, -1);
|
1997-01-17 06:05:33 +08:00
|
|
|
if (dp->di_size < 0) {
|
|
|
|
DBT krec, drec;
|
|
|
|
int status;
|
|
|
|
int size = 0;
|
1998-04-10 04:56:35 +08:00
|
|
|
BSDDB_BGN_SAVE(dp)
|
1997-01-17 06:05:33 +08:00
|
|
|
for (status = (dp->di_bsddb->seq)(dp->di_bsddb,
|
|
|
|
&krec, &drec,R_FIRST);
|
|
|
|
status == 0;
|
|
|
|
status = (dp->di_bsddb->seq)(dp->di_bsddb,
|
|
|
|
&krec, &drec, R_NEXT))
|
|
|
|
size++;
|
1998-04-10 04:56:35 +08:00
|
|
|
BSDDB_END_SAVE(dp)
|
1997-01-17 06:05:33 +08:00
|
|
|
if (status < 0) {
|
|
|
|
PyErr_SetFromErrno(BsddbError);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dp->di_size = size;
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
1997-01-17 06:05:33 +08:00
|
|
|
return dp->di_size;
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
|
|
|
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyObject *
|
2000-07-11 01:06:38 +08:00
|
|
|
bsddb_subscript(bsddbobject *dp, PyObject *key)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
1997-01-17 06:05:33 +08:00
|
|
|
int status;
|
|
|
|
DBT krec, drec;
|
1998-04-10 04:56:35 +08:00
|
|
|
char *data,buf[4096];
|
1997-01-17 06:05:33 +08:00
|
|
|
int size;
|
1998-04-10 04:56:35 +08:00
|
|
|
PyObject *result;
|
2001-02-28 02:56:46 +08:00
|
|
|
recno_t recno;
|
1997-07-18 06:56:01 +08:00
|
|
|
|
2001-02-28 02:56:46 +08:00
|
|
|
if (dp->di_type == DB_RECNO) {
|
|
|
|
if (!PyArg_Parse(key, "i", &recno)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"key type must be integer");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
krec.data = &recno;
|
|
|
|
krec.size = sizeof(recno);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!PyArg_Parse(key, "s#", &data, &size)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"key type must be string");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
krec.data = data;
|
|
|
|
krec.size = size;
|
|
|
|
}
|
|
|
|
check_bsddbobject_open(dp, NULL);
|
1997-01-17 06:05:33 +08:00
|
|
|
|
1998-04-10 04:56:35 +08:00
|
|
|
BSDDB_BGN_SAVE(dp)
|
1997-01-17 06:05:33 +08:00
|
|
|
status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0);
|
1998-04-10 04:56:35 +08:00
|
|
|
if (status == 0) {
|
|
|
|
if (drec.size > sizeof(buf)) data = malloc(drec.size);
|
|
|
|
else data = buf;
|
2000-12-15 08:59:32 +08:00
|
|
|
if (data!=NULL) memcpy(data,drec.data,drec.size);
|
1998-04-10 04:56:35 +08:00
|
|
|
}
|
|
|
|
BSDDB_END_SAVE(dp)
|
2000-12-15 08:59:32 +08:00
|
|
|
if (data==NULL) return PyErr_NoMemory();
|
1997-01-17 06:05:33 +08:00
|
|
|
if (status != 0) {
|
|
|
|
if (status < 0)
|
|
|
|
PyErr_SetFromErrno(BsddbError);
|
|
|
|
else
|
|
|
|
PyErr_SetObject(PyExc_KeyError, key);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1998-04-10 04:56:35 +08:00
|
|
|
result = PyString_FromStringAndSize(data, (int)drec.size);
|
|
|
|
if (data != buf) free(data);
|
|
|
|
return result;
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-07-11 01:06:38 +08:00
|
|
|
bsddb_ass_sub(bsddbobject *dp, PyObject *key, PyObject *value)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
1997-01-17 06:05:33 +08:00
|
|
|
int status;
|
|
|
|
DBT krec, drec;
|
|
|
|
char *data;
|
|
|
|
int size;
|
2001-02-28 02:56:46 +08:00
|
|
|
recno_t recno;
|
1997-01-17 06:05:33 +08:00
|
|
|
|
2001-02-28 02:56:46 +08:00
|
|
|
if (dp->di_type == DB_RECNO) {
|
|
|
|
if (!PyArg_Parse(key, "i", &recno)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"bsddb key type must be integer");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
krec.data = &recno;
|
|
|
|
krec.size = sizeof(recno);
|
1997-01-17 06:05:33 +08:00
|
|
|
}
|
2001-02-28 02:56:46 +08:00
|
|
|
else {
|
|
|
|
if (!PyArg_Parse(key, "s#", &data, &size)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"bsddb key type must be string");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
krec.data = data;
|
|
|
|
krec.size = size;
|
|
|
|
}
|
|
|
|
check_bsddbobject_open(dp, -1);
|
1997-01-17 06:05:33 +08:00
|
|
|
dp->di_size = -1;
|
|
|
|
if (value == NULL) {
|
1998-04-10 04:56:35 +08:00
|
|
|
BSDDB_BGN_SAVE(dp)
|
1997-01-17 06:05:33 +08:00
|
|
|
status = (dp->di_bsddb->del)(dp->di_bsddb, &krec, 0);
|
1998-04-10 04:56:35 +08:00
|
|
|
BSDDB_END_SAVE(dp)
|
1997-01-17 06:05:33 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!PyArg_Parse(value, "s#", &data, &size)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"bsddb value type must be string");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
drec.data = data;
|
|
|
|
drec.size = size;
|
1998-04-10 04:56:35 +08:00
|
|
|
BSDDB_BGN_SAVE(dp)
|
1997-01-17 06:05:33 +08:00
|
|
|
status = (dp->di_bsddb->put)(dp->di_bsddb, &krec, &drec, 0);
|
1998-04-10 04:56:35 +08:00
|
|
|
BSDDB_END_SAVE(dp)
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
1997-01-17 06:05:33 +08:00
|
|
|
if (status != 0) {
|
|
|
|
if (status < 0)
|
|
|
|
PyErr_SetFromErrno(BsddbError);
|
|
|
|
else
|
|
|
|
PyErr_SetObject(PyExc_KeyError, key);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
|
|
|
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyMappingMethods bsddb_as_mapping = {
|
1997-01-17 06:05:33 +08:00
|
|
|
(inquiry)bsddb_length, /*mp_length*/
|
|
|
|
(binaryfunc)bsddb_subscript, /*mp_subscript*/
|
|
|
|
(objobjargproc)bsddb_ass_sub, /*mp_ass_subscript*/
|
1995-08-31 07:43:03 +08:00
|
|
|
};
|
|
|
|
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyObject *
|
2000-07-11 01:06:38 +08:00
|
|
|
bsddb_close(bsddbobject *dp, PyObject *args)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
1997-01-17 06:05:33 +08:00
|
|
|
if (!PyArg_NoArgs(args))
|
|
|
|
return NULL;
|
|
|
|
if (dp->di_bsddb != NULL) {
|
1998-04-10 04:56:35 +08:00
|
|
|
int status;
|
|
|
|
BSDDB_BGN_SAVE(dp)
|
|
|
|
status = (dp->di_bsddb->close)(dp->di_bsddb);
|
|
|
|
BSDDB_END_SAVE(dp)
|
|
|
|
if (status != 0) {
|
1997-01-17 06:05:33 +08:00
|
|
|
dp->di_bsddb = NULL;
|
|
|
|
PyErr_SetFromErrno(BsddbError);
|
|
|
|
return NULL;
|
|
|
|
}
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
1997-01-17 06:05:33 +08:00
|
|
|
dp->di_bsddb = NULL;
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
|
|
|
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyObject *
|
2000-07-11 01:06:38 +08:00
|
|
|
bsddb_keys(bsddbobject *dp, PyObject *args)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
2001-02-28 02:56:46 +08:00
|
|
|
PyObject *list, *item=NULL;
|
1997-01-17 06:05:33 +08:00
|
|
|
DBT krec, drec;
|
1998-04-11 06:27:42 +08:00
|
|
|
char *data=NULL,buf[4096];
|
1997-01-17 06:05:33 +08:00
|
|
|
int status;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!PyArg_NoArgs(args))
|
|
|
|
return NULL;
|
2001-02-28 02:56:46 +08:00
|
|
|
check_bsddbobject_open(dp, NULL);
|
1997-01-17 06:05:33 +08:00
|
|
|
list = PyList_New(0);
|
|
|
|
if (list == NULL)
|
|
|
|
return NULL;
|
1998-04-10 04:56:35 +08:00
|
|
|
BSDDB_BGN_SAVE(dp)
|
|
|
|
status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_FIRST);
|
|
|
|
if (status == 0) {
|
|
|
|
if (krec.size > sizeof(buf)) data = malloc(krec.size);
|
|
|
|
else data = buf;
|
2001-02-28 02:56:46 +08:00
|
|
|
if (data != NULL) memcpy(data,krec.data,krec.size);
|
1998-04-10 04:56:35 +08:00
|
|
|
}
|
|
|
|
BSDDB_END_SAVE(dp)
|
2001-03-22 08:19:22 +08:00
|
|
|
if (status == 0 && data==NULL) return PyErr_NoMemory();
|
1998-04-10 04:56:35 +08:00
|
|
|
while (status == 0) {
|
2001-02-28 02:56:46 +08:00
|
|
|
if (dp->di_type == DB_RECNO)
|
|
|
|
item = PyInt_FromLong(*((int*)data));
|
|
|
|
else
|
|
|
|
item = PyString_FromStringAndSize(data,
|
|
|
|
(int)krec.size);
|
1998-04-10 04:56:35 +08:00
|
|
|
if (data != buf) free(data);
|
1997-01-17 06:05:33 +08:00
|
|
|
if (item == NULL) {
|
|
|
|
Py_DECREF(list);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
err = PyList_Append(list, item);
|
|
|
|
Py_DECREF(item);
|
|
|
|
if (err != 0) {
|
|
|
|
Py_DECREF(list);
|
|
|
|
return NULL;
|
|
|
|
}
|
1998-04-10 04:56:35 +08:00
|
|
|
BSDDB_BGN_SAVE(dp)
|
2001-02-20 05:16:00 +08:00
|
|
|
status = (dp->di_bsddb->seq)
|
|
|
|
(dp->di_bsddb, &krec, &drec, R_NEXT);
|
1998-04-10 04:56:35 +08:00
|
|
|
if (status == 0) {
|
2001-02-20 05:16:00 +08:00
|
|
|
if (krec.size > sizeof(buf))
|
|
|
|
data = malloc(krec.size);
|
1998-04-10 04:56:35 +08:00
|
|
|
else data = buf;
|
2001-02-20 05:16:00 +08:00
|
|
|
if (data != NULL)
|
|
|
|
memcpy(data,krec.data,krec.size);
|
1998-04-10 04:56:35 +08:00
|
|
|
}
|
|
|
|
BSDDB_END_SAVE(dp)
|
2001-02-20 05:16:00 +08:00
|
|
|
if (data == NULL) return PyErr_NoMemory();
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
1997-01-17 06:05:33 +08:00
|
|
|
if (status < 0) {
|
|
|
|
PyErr_SetFromErrno(BsddbError);
|
|
|
|
Py_DECREF(list);
|
|
|
|
return NULL;
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
1997-01-17 06:05:33 +08:00
|
|
|
if (dp->di_size < 0)
|
|
|
|
dp->di_size = PyList_Size(list); /* We just did the work */
|
|
|
|
return list;
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
|
|
|
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyObject *
|
2000-07-11 01:06:38 +08:00
|
|
|
bsddb_has_key(bsddbobject *dp, PyObject *args)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
1997-01-17 06:05:33 +08:00
|
|
|
DBT krec, drec;
|
|
|
|
int status;
|
|
|
|
char *data;
|
|
|
|
int size;
|
2001-02-28 02:56:46 +08:00
|
|
|
recno_t recno;
|
1997-01-17 06:05:33 +08:00
|
|
|
|
2001-02-28 02:56:46 +08:00
|
|
|
if (dp->di_type == DB_RECNO) {
|
|
|
|
if (!PyArg_Parse(args, "i", &recno)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"key type must be integer");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
krec.data = &recno;
|
|
|
|
krec.size = sizeof(recno);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!PyArg_Parse(args, "s#", &data, &size)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"key type must be string");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
krec.data = data;
|
|
|
|
krec.size = size;
|
|
|
|
}
|
|
|
|
check_bsddbobject_open(dp, NULL);
|
1997-01-17 06:05:33 +08:00
|
|
|
|
1998-04-10 04:56:35 +08:00
|
|
|
BSDDB_BGN_SAVE(dp)
|
1997-01-17 06:05:33 +08:00
|
|
|
status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0);
|
1998-04-10 04:56:35 +08:00
|
|
|
BSDDB_END_SAVE(dp)
|
1997-01-17 06:05:33 +08:00
|
|
|
if (status < 0) {
|
|
|
|
PyErr_SetFromErrno(BsddbError);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PyInt_FromLong(status == 0);
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
|
|
|
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyObject *
|
2000-07-11 01:06:38 +08:00
|
|
|
bsddb_set_location(bsddbobject *dp, PyObject *key)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
1997-01-17 06:05:33 +08:00
|
|
|
int status;
|
|
|
|
DBT krec, drec;
|
1998-04-10 04:56:35 +08:00
|
|
|
char *data,buf[4096];
|
1997-01-17 06:05:33 +08:00
|
|
|
int size;
|
1998-04-10 04:56:35 +08:00
|
|
|
PyObject *result;
|
2001-02-28 02:56:46 +08:00
|
|
|
recno_t recno;
|
1997-01-17 06:05:33 +08:00
|
|
|
|
2001-02-28 02:56:46 +08:00
|
|
|
if (dp->di_type == DB_RECNO) {
|
|
|
|
if (!PyArg_Parse(key, "i", &recno)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"key type must be integer");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
krec.data = &recno;
|
|
|
|
krec.size = sizeof(recno);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!PyArg_Parse(key, "s#", &data, &size)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"key type must be string");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
krec.data = data;
|
|
|
|
krec.size = size;
|
|
|
|
}
|
|
|
|
check_bsddbobject_open(dp, NULL);
|
1997-01-17 06:05:33 +08:00
|
|
|
|
1998-04-10 04:56:35 +08:00
|
|
|
BSDDB_BGN_SAVE(dp)
|
1997-01-17 06:05:33 +08:00
|
|
|
status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_CURSOR);
|
1998-04-10 04:56:35 +08:00
|
|
|
if (status == 0) {
|
|
|
|
if (drec.size > sizeof(buf)) data = malloc(drec.size);
|
|
|
|
else data = buf;
|
2000-12-15 08:59:32 +08:00
|
|
|
if (data!=NULL) memcpy(data,drec.data,drec.size);
|
1998-04-10 04:56:35 +08:00
|
|
|
}
|
|
|
|
BSDDB_END_SAVE(dp)
|
2000-12-15 08:59:32 +08:00
|
|
|
if (data==NULL) return PyErr_NoMemory();
|
1997-01-17 06:05:33 +08:00
|
|
|
if (status != 0) {
|
|
|
|
if (status < 0)
|
|
|
|
PyErr_SetFromErrno(BsddbError);
|
|
|
|
else
|
|
|
|
PyErr_SetObject(PyExc_KeyError, key);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-02-28 02:56:46 +08:00
|
|
|
if (dp->di_type == DB_RECNO)
|
|
|
|
result = Py_BuildValue("is#", *((int*)krec.data),
|
|
|
|
data, drec.size);
|
|
|
|
else
|
|
|
|
result = Py_BuildValue("s#s#", krec.data, krec.size,
|
|
|
|
data, drec.size);
|
1998-04-10 04:56:35 +08:00
|
|
|
if (data != buf) free(data);
|
|
|
|
return result;
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
|
|
|
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyObject *
|
2000-07-11 01:06:38 +08:00
|
|
|
bsddb_seq(bsddbobject *dp, PyObject *args, int sequence_request)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
1997-01-17 06:05:33 +08:00
|
|
|
int status;
|
|
|
|
DBT krec, drec;
|
1998-04-11 06:27:42 +08:00
|
|
|
char *kdata=NULL,kbuf[4096];
|
|
|
|
char *ddata=NULL,dbuf[4096];
|
1998-04-10 04:56:35 +08:00
|
|
|
PyObject *result;
|
1995-08-31 07:43:03 +08:00
|
|
|
|
1997-01-17 06:05:33 +08:00
|
|
|
if (!PyArg_NoArgs(args))
|
|
|
|
return NULL;
|
1995-08-31 07:43:03 +08:00
|
|
|
|
2001-02-28 02:56:46 +08:00
|
|
|
check_bsddbobject_open(dp, NULL);
|
1997-01-17 06:05:33 +08:00
|
|
|
krec.data = 0;
|
|
|
|
krec.size = 0;
|
1995-08-31 07:43:03 +08:00
|
|
|
|
1998-04-10 04:56:35 +08:00
|
|
|
BSDDB_BGN_SAVE(dp)
|
1997-01-17 06:05:33 +08:00
|
|
|
status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec,
|
|
|
|
&drec, sequence_request);
|
1998-04-10 04:56:35 +08:00
|
|
|
if (status == 0) {
|
|
|
|
if (krec.size > sizeof(kbuf)) kdata = malloc(krec.size);
|
|
|
|
else kdata = kbuf;
|
2001-02-28 02:56:46 +08:00
|
|
|
if (kdata != NULL) memcpy(kdata,krec.data,krec.size);
|
1998-04-10 04:56:35 +08:00
|
|
|
if (drec.size > sizeof(dbuf)) ddata = malloc(drec.size);
|
|
|
|
else ddata = dbuf;
|
2001-02-28 02:56:46 +08:00
|
|
|
if (ddata != NULL) memcpy(ddata,drec.data,drec.size);
|
1998-04-10 04:56:35 +08:00
|
|
|
}
|
|
|
|
BSDDB_END_SAVE(dp)
|
2000-12-15 08:59:32 +08:00
|
|
|
if (status == 0) {
|
2001-02-28 02:56:46 +08:00
|
|
|
if ((kdata == NULL) || (ddata == NULL))
|
2000-12-15 08:59:32 +08:00
|
|
|
return PyErr_NoMemory();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* (status != 0) */
|
1997-01-17 06:05:33 +08:00
|
|
|
if (status < 0)
|
|
|
|
PyErr_SetFromErrno(BsddbError);
|
|
|
|
else
|
|
|
|
PyErr_SetObject(PyExc_KeyError, args);
|
|
|
|
return NULL;
|
|
|
|
}
|
1995-08-31 07:43:03 +08:00
|
|
|
|
2001-02-28 02:56:46 +08:00
|
|
|
|
|
|
|
if (dp->di_type == DB_RECNO)
|
|
|
|
result = Py_BuildValue("is#", *((int*)kdata),
|
|
|
|
ddata, drec.size);
|
|
|
|
else
|
|
|
|
result = Py_BuildValue("s#s#", kdata, krec.size,
|
|
|
|
ddata, drec.size);
|
1998-04-10 04:56:35 +08:00
|
|
|
if (kdata != kbuf) free(kdata);
|
|
|
|
if (ddata != dbuf) free(ddata);
|
|
|
|
return result;
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
|
|
|
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyObject *
|
2000-07-11 01:06:38 +08:00
|
|
|
bsddb_next(bsddbobject *dp, PyObject *key)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
1997-01-17 06:05:33 +08:00
|
|
|
return bsddb_seq(dp, key, R_NEXT);
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyObject *
|
2000-07-11 01:06:38 +08:00
|
|
|
bsddb_previous(bsddbobject *dp, PyObject *key)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
1997-01-17 06:05:33 +08:00
|
|
|
return bsddb_seq(dp, key, R_PREV);
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyObject *
|
2000-07-11 01:06:38 +08:00
|
|
|
bsddb_first(bsddbobject *dp, PyObject *key)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
1997-01-17 06:05:33 +08:00
|
|
|
return bsddb_seq(dp, key, R_FIRST);
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyObject *
|
2000-07-11 01:06:38 +08:00
|
|
|
bsddb_last(bsddbobject *dp, PyObject *key)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
1997-01-17 06:05:33 +08:00
|
|
|
return bsddb_seq(dp, key, R_LAST);
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyObject *
|
2000-07-11 01:06:38 +08:00
|
|
|
bsddb_sync(bsddbobject *dp, PyObject *args)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
1997-01-17 06:05:33 +08:00
|
|
|
int status;
|
|
|
|
|
1997-07-18 06:56:01 +08:00
|
|
|
if (!PyArg_NoArgs(args))
|
|
|
|
return NULL;
|
2001-02-28 02:56:46 +08:00
|
|
|
check_bsddbobject_open(dp, NULL);
|
1998-04-10 04:56:35 +08:00
|
|
|
BSDDB_BGN_SAVE(dp)
|
1997-01-17 06:05:33 +08:00
|
|
|
status = (dp->di_bsddb->sync)(dp->di_bsddb, 0);
|
1998-04-10 04:56:35 +08:00
|
|
|
BSDDB_END_SAVE(dp)
|
1997-01-17 06:05:33 +08:00
|
|
|
if (status != 0) {
|
|
|
|
PyErr_SetFromErrno(BsddbError);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyInt_FromLong(status = 0);
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyMethodDef bsddb_methods[] = {
|
1997-01-17 06:05:33 +08:00
|
|
|
{"close", (PyCFunction)bsddb_close},
|
|
|
|
{"keys", (PyCFunction)bsddb_keys},
|
|
|
|
{"has_key", (PyCFunction)bsddb_has_key},
|
|
|
|
{"set_location", (PyCFunction)bsddb_set_location},
|
|
|
|
{"next", (PyCFunction)bsddb_next},
|
|
|
|
{"previous", (PyCFunction)bsddb_previous},
|
|
|
|
{"first", (PyCFunction)bsddb_first},
|
|
|
|
{"last", (PyCFunction)bsddb_last},
|
|
|
|
{"sync", (PyCFunction)bsddb_sync},
|
|
|
|
{NULL, NULL} /* sentinel */
|
1995-08-31 07:43:03 +08:00
|
|
|
};
|
|
|
|
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyObject *
|
2000-07-11 01:06:38 +08:00
|
|
|
bsddb_getattr(PyObject *dp, char *name)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
1997-01-17 06:05:33 +08:00
|
|
|
return Py_FindMethod(bsddb_methods, dp, name);
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
|
|
|
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyTypeObject Bsddbtype = {
|
1997-01-17 06:05:33 +08:00
|
|
|
PyObject_HEAD_INIT(NULL)
|
|
|
|
0,
|
|
|
|
"bsddb",
|
|
|
|
sizeof(bsddbobject),
|
|
|
|
0,
|
|
|
|
(destructor)bsddb_dealloc, /*tp_dealloc*/
|
|
|
|
0, /*tp_print*/
|
|
|
|
(getattrfunc)bsddb_getattr, /*tp_getattr*/
|
|
|
|
0, /*tp_setattr*/
|
|
|
|
0, /*tp_compare*/
|
|
|
|
0, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
&bsddb_as_mapping, /*tp_as_mapping*/
|
1995-08-31 07:43:03 +08:00
|
|
|
};
|
|
|
|
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyObject *
|
2000-07-11 01:06:38 +08:00
|
|
|
bsdhashopen(PyObject *self, PyObject *args)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
1997-01-17 06:05:33 +08:00
|
|
|
char *file;
|
|
|
|
char *flag = NULL;
|
|
|
|
int flags = O_RDONLY;
|
|
|
|
int mode = 0666;
|
|
|
|
int bsize = 0;
|
|
|
|
int ffactor = 0;
|
|
|
|
int nelem = 0;
|
|
|
|
int cachesize = 0;
|
|
|
|
int hash = 0; /* XXX currently ignored */
|
|
|
|
int lorder = 0;
|
|
|
|
|
2000-02-29 21:59:29 +08:00
|
|
|
if (!PyArg_ParseTuple(args, "s|siiiiiii:hashopen",
|
1997-01-17 06:05:33 +08:00
|
|
|
&file, &flag, &mode,
|
|
|
|
&bsize, &ffactor, &nelem, &cachesize,
|
|
|
|
&hash, &lorder))
|
|
|
|
return NULL;
|
|
|
|
if (flag != NULL) {
|
|
|
|
/* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */
|
|
|
|
if (flag[0] == 'r')
|
|
|
|
flags = O_RDONLY;
|
|
|
|
else if (flag[0] == 'w')
|
|
|
|
flags = O_RDWR;
|
|
|
|
else if (flag[0] == 'c')
|
|
|
|
flags = O_RDWR|O_CREAT;
|
|
|
|
else if (flag[0] == 'n')
|
|
|
|
flags = O_RDWR|O_CREAT|O_TRUNC;
|
|
|
|
else {
|
|
|
|
PyErr_SetString(BsddbError,
|
|
|
|
"Flag should begin with 'r', 'w', 'c' or 'n'");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (flag[1] == 'l') {
|
1995-08-31 07:43:03 +08:00
|
|
|
#if defined(O_EXLOCK) && defined(O_SHLOCK)
|
1997-01-17 06:05:33 +08:00
|
|
|
if (flag[0] == 'r')
|
|
|
|
flags |= O_SHLOCK;
|
|
|
|
else
|
|
|
|
flags |= O_EXLOCK;
|
1995-08-31 07:43:03 +08:00
|
|
|
#else
|
1997-01-17 06:05:33 +08:00
|
|
|
PyErr_SetString(BsddbError,
|
|
|
|
"locking not supported on this platform");
|
|
|
|
return NULL;
|
1995-08-31 07:43:03 +08:00
|
|
|
#endif
|
1997-01-17 06:05:33 +08:00
|
|
|
}
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
1997-01-17 06:05:33 +08:00
|
|
|
return newdbhashobject(file, flags, mode,
|
|
|
|
bsize, ffactor, nelem, cachesize, hash, lorder);
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
|
|
|
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyObject *
|
2000-07-11 01:06:38 +08:00
|
|
|
bsdbtopen(PyObject *self, PyObject *args)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
1997-01-17 06:05:33 +08:00
|
|
|
char *file;
|
|
|
|
char *flag = NULL;
|
|
|
|
int flags = O_RDONLY;
|
|
|
|
int mode = 0666;
|
|
|
|
int cachesize = 0;
|
|
|
|
int maxkeypage = 0;
|
|
|
|
int minkeypage = 0;
|
|
|
|
int btflags = 0;
|
|
|
|
unsigned int psize = 0;
|
|
|
|
int lorder = 0;
|
|
|
|
|
2000-02-29 21:59:29 +08:00
|
|
|
if (!PyArg_ParseTuple(args, "s|siiiiiii:btopen",
|
1997-01-17 06:05:33 +08:00
|
|
|
&file, &flag, &mode,
|
|
|
|
&btflags, &cachesize, &maxkeypage, &minkeypage,
|
|
|
|
&psize, &lorder))
|
|
|
|
return NULL;
|
|
|
|
if (flag != NULL) {
|
|
|
|
/* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */
|
|
|
|
if (flag[0] == 'r')
|
|
|
|
flags = O_RDONLY;
|
|
|
|
else if (flag[0] == 'w')
|
|
|
|
flags = O_RDWR;
|
|
|
|
else if (flag[0] == 'c')
|
|
|
|
flags = O_RDWR|O_CREAT;
|
|
|
|
else if (flag[0] == 'n')
|
|
|
|
flags = O_RDWR|O_CREAT|O_TRUNC;
|
|
|
|
else {
|
|
|
|
PyErr_SetString(BsddbError,
|
|
|
|
"Flag should begin with 'r', 'w', 'c' or 'n'");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (flag[1] == 'l') {
|
1995-08-31 07:43:03 +08:00
|
|
|
#if defined(O_EXLOCK) && defined(O_SHLOCK)
|
1997-01-17 06:05:33 +08:00
|
|
|
if (flag[0] == 'r')
|
|
|
|
flags |= O_SHLOCK;
|
|
|
|
else
|
|
|
|
flags |= O_EXLOCK;
|
1995-08-31 07:43:03 +08:00
|
|
|
#else
|
1997-01-17 06:05:33 +08:00
|
|
|
PyErr_SetString(BsddbError,
|
|
|
|
"locking not supported on this platform");
|
|
|
|
return NULL;
|
1995-08-31 07:43:03 +08:00
|
|
|
#endif
|
1997-01-17 06:05:33 +08:00
|
|
|
}
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
1997-01-17 06:05:33 +08:00
|
|
|
return newdbbtobject(file, flags, mode,
|
|
|
|
btflags, cachesize, maxkeypage, minkeypage,
|
|
|
|
psize, lorder);
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
1996-05-24 06:57:54 +08:00
|
|
|
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyObject *
|
2000-07-11 01:06:38 +08:00
|
|
|
bsdrnopen(PyObject *self, PyObject *args)
|
1995-08-31 07:43:03 +08:00
|
|
|
{
|
1997-01-17 06:05:33 +08:00
|
|
|
char *file;
|
|
|
|
char *flag = NULL;
|
|
|
|
int flags = O_RDONLY;
|
|
|
|
int mode = 0666;
|
|
|
|
int cachesize = 0;
|
|
|
|
int rnflags = 0;
|
|
|
|
unsigned int psize = 0;
|
|
|
|
int lorder = 0;
|
|
|
|
size_t reclen = 0;
|
|
|
|
char *bval = "";
|
|
|
|
char *bfname = NULL;
|
|
|
|
|
2000-02-29 21:59:29 +08:00
|
|
|
if (!PyArg_ParseTuple(args, "s|siiiiiiss:rnopen",
|
1997-01-17 06:05:33 +08:00
|
|
|
&file, &flag, &mode,
|
|
|
|
&rnflags, &cachesize, &psize, &lorder,
|
|
|
|
&reclen, &bval, &bfname))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (flag != NULL) {
|
|
|
|
/* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */
|
|
|
|
if (flag[0] == 'r')
|
|
|
|
flags = O_RDONLY;
|
|
|
|
else if (flag[0] == 'w')
|
|
|
|
flags = O_RDWR;
|
|
|
|
else if (flag[0] == 'c')
|
|
|
|
flags = O_RDWR|O_CREAT;
|
|
|
|
else if (flag[0] == 'n')
|
|
|
|
flags = O_RDWR|O_CREAT|O_TRUNC;
|
|
|
|
else {
|
|
|
|
PyErr_SetString(BsddbError,
|
|
|
|
"Flag should begin with 'r', 'w', 'c' or 'n'");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (flag[1] == 'l') {
|
1995-08-31 07:43:03 +08:00
|
|
|
#if defined(O_EXLOCK) && defined(O_SHLOCK)
|
1997-01-17 06:05:33 +08:00
|
|
|
if (flag[0] == 'r')
|
|
|
|
flags |= O_SHLOCK;
|
|
|
|
else
|
|
|
|
flags |= O_EXLOCK;
|
1995-08-31 07:43:03 +08:00
|
|
|
#else
|
1997-01-17 06:05:33 +08:00
|
|
|
PyErr_SetString(BsddbError,
|
|
|
|
"locking not supported on this platform");
|
|
|
|
return NULL;
|
1995-08-31 07:43:03 +08:00
|
|
|
#endif
|
1997-01-17 06:05:33 +08:00
|
|
|
}
|
|
|
|
else if (flag[1] != '\0') {
|
|
|
|
PyErr_SetString(BsddbError,
|
|
|
|
"Flag char 2 should be 'l' or absent");
|
|
|
|
return NULL;
|
|
|
|
}
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
1997-01-17 06:05:33 +08:00
|
|
|
return newdbrnobject(file, flags, mode, rnflags, cachesize,
|
|
|
|
psize, lorder, reclen, bval[0], bfname);
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|
|
|
|
|
1996-07-24 08:51:20 +08:00
|
|
|
static PyMethodDef bsddbmodule_methods[] = {
|
1997-01-17 06:05:33 +08:00
|
|
|
{"hashopen", (PyCFunction)bsdhashopen, 1},
|
|
|
|
{"btopen", (PyCFunction)bsdbtopen, 1},
|
|
|
|
{"rnopen", (PyCFunction)bsdrnopen, 1},
|
|
|
|
{0, 0},
|
1995-08-31 07:43:03 +08:00
|
|
|
};
|
|
|
|
|
1998-12-05 02:50:17 +08:00
|
|
|
DL_EXPORT(void)
|
2000-07-24 22:43:35 +08:00
|
|
|
initbsddb(void) {
|
1997-01-17 06:05:33 +08:00
|
|
|
PyObject *m, *d;
|
|
|
|
|
|
|
|
Bsddbtype.ob_type = &PyType_Type;
|
|
|
|
m = Py_InitModule("bsddb", bsddbmodule_methods);
|
|
|
|
d = PyModule_GetDict(m);
|
1997-10-01 12:29:29 +08:00
|
|
|
BsddbError = PyErr_NewException("bsddb.error", NULL, NULL);
|
|
|
|
if (BsddbError != NULL)
|
|
|
|
PyDict_SetItemString(d, "error", BsddbError);
|
1995-08-31 07:43:03 +08:00
|
|
|
}
|