1995-08-30 20:43:03 -03:00
|
|
|
/***********************************************************
|
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 11:44:06 -03:00
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
1995-08-30 20:43:03 -03:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 11:44:06 -03:00
|
|
|
both that copyright notice and this permission notice appear in
|
1995-08-30 20:43:03 -03:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 11:44:06 -03:00
|
|
|
Centrum or CWI or Corporation for National Research Initiatives or
|
|
|
|
CNRI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior
|
|
|
|
permission.
|
|
|
|
|
|
|
|
While CWI is the initial source for this software, a modified version
|
|
|
|
is made available by the Corporation for National Research Initiatives
|
|
|
|
(CNRI) at the Internet address ftp://ftp.python.org.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
|
|
|
|
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
|
|
|
|
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
PERFORMANCE OF THIS SOFTWARE.
|
1995-08-30 20:43:03 -03:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
/* Berkeley DB interface.
|
|
|
|
Author: Michael McLay
|
|
|
|
Hacked: Guido van Rossum
|
|
|
|
Btree and Recno additions plus sequence methods: David Ely
|
|
|
|
|
|
|
|
XXX To do:
|
|
|
|
- provide interface to the B-tree and record libraries too
|
|
|
|
- provide a way to access the various hash functions
|
|
|
|
- support more open flags
|
|
|
|
*/
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
#include "Python.h"
|
1995-08-30 20:43:03 -03:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <db.h>
|
|
|
|
/* Please don't include internal header files of the Berkeley db package
|
|
|
|
(it messes up the info required in the Setup file) */
|
|
|
|
|
|
|
|
typedef struct {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject_HEAD
|
1995-08-30 20:43:03 -03:00
|
|
|
DB *di_bsddb;
|
|
|
|
int di_size; /* -1 means recompute */
|
|
|
|
} bsddbobject;
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
staticforward PyTypeObject Bsddbtype;
|
1995-08-30 20:43:03 -03:00
|
|
|
|
|
|
|
#define is_bsddbobject(v) ((v)->ob_type == &Bsddbtype)
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *BsddbError;
|
1995-08-30 20:43:03 -03:00
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1995-08-30 20:43:03 -03:00
|
|
|
newdbhashobject(file, flags, mode,
|
|
|
|
bsize, ffactor, nelem, cachesize, hash, lorder)
|
|
|
|
char *file;
|
|
|
|
int flags;
|
|
|
|
int mode;
|
|
|
|
int bsize;
|
|
|
|
int ffactor;
|
|
|
|
int nelem;
|
|
|
|
int cachesize;
|
|
|
|
int hash; /* XXX ignored */
|
|
|
|
int lorder;
|
|
|
|
{
|
|
|
|
bsddbobject *dp;
|
|
|
|
HASHINFO info;
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL)
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
info.bsize = bsize;
|
|
|
|
info.ffactor = ffactor;
|
|
|
|
info.nelem = nelem;
|
|
|
|
info.cachesize = cachesize;
|
|
|
|
info.hash = NULL; /* XXX should derive from hash argument */
|
|
|
|
info.lorder = lorder;
|
|
|
|
|
1996-09-11 20:22:25 -03:00
|
|
|
#ifdef O_BINARY
|
|
|
|
flags |= O_BINARY;
|
|
|
|
#endif
|
1995-08-30 20:43:03 -03:00
|
|
|
if ((dp->di_bsddb = dbopen(file, flags, mode, DB_HASH, &info)) == NULL) {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetFromErrno(BsddbError);
|
|
|
|
Py_DECREF(dp);
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dp->di_size = -1;
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
return (PyObject *)dp;
|
1995-08-30 20:43:03 -03:00
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1995-08-30 20:43:03 -03:00
|
|
|
newdbbtobject(file, flags, mode,
|
|
|
|
btflags, cachesize, maxkeypage, minkeypage, psize, lorder)
|
|
|
|
char *file;
|
|
|
|
int flags;
|
|
|
|
int mode;
|
|
|
|
int btflags;
|
|
|
|
int cachesize;
|
|
|
|
int maxkeypage;
|
|
|
|
int minkeypage;
|
|
|
|
int psize;
|
|
|
|
int lorder;
|
|
|
|
{
|
|
|
|
bsddbobject *dp;
|
|
|
|
BTREEINFO info;
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL)
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
info.flags = btflags;
|
|
|
|
info.cachesize = cachesize;
|
|
|
|
info.maxkeypage = maxkeypage;
|
|
|
|
info.minkeypage = minkeypage;
|
|
|
|
info.psize = psize;
|
|
|
|
info.lorder = lorder;
|
1996-09-11 20:22:25 -03:00
|
|
|
info.compare = 0; /* Use default comparison functions, for now..*/
|
1995-08-30 20:43:03 -03:00
|
|
|
info.prefix = 0;
|
|
|
|
|
1996-09-11 20:22:25 -03:00
|
|
|
#ifdef O_BINARY
|
|
|
|
flags |= O_BINARY;
|
|
|
|
#endif
|
1995-08-30 20:43:03 -03:00
|
|
|
if ((dp->di_bsddb = dbopen(file, flags, mode, DB_BTREE, &info)) == NULL) {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetFromErrno(BsddbError);
|
|
|
|
Py_DECREF(dp);
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dp->di_size = -1;
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
return (PyObject *)dp;
|
1995-08-30 20:43:03 -03:00
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1995-08-30 20:43:03 -03:00
|
|
|
newdbrnobject(file, flags, mode,
|
|
|
|
rnflags, cachesize, psize, lorder, reclen, bval, bfname)
|
|
|
|
char *file;
|
|
|
|
int flags;
|
|
|
|
int mode;
|
|
|
|
int rnflags;
|
|
|
|
int cachesize;
|
|
|
|
int psize;
|
|
|
|
int lorder;
|
|
|
|
size_t reclen;
|
|
|
|
u_char bval;
|
|
|
|
char *bfname;
|
|
|
|
{
|
|
|
|
bsddbobject *dp;
|
|
|
|
RECNOINFO info;
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL)
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
info.flags = rnflags;
|
|
|
|
info.cachesize = cachesize;
|
|
|
|
info.psize = psize;
|
|
|
|
info.lorder = lorder;
|
|
|
|
info.reclen = reclen;
|
|
|
|
info.bval = bval;
|
|
|
|
info.bfname = bfname;
|
|
|
|
|
1996-09-11 20:22:25 -03:00
|
|
|
#ifdef O_BINARY
|
|
|
|
flags |= O_BINARY;
|
|
|
|
#endif
|
1995-08-30 20:43:03 -03:00
|
|
|
if ((dp->di_bsddb = dbopen(file, flags, mode, DB_RECNO, &info)) == NULL) {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetFromErrno(BsddbError);
|
|
|
|
Py_DECREF(dp);
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dp->di_size = -1;
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
return (PyObject *)dp;
|
1995-08-30 20:43:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
bsddb_dealloc(dp)
|
|
|
|
bsddbobject *dp;
|
|
|
|
{
|
|
|
|
if (dp->di_bsddb != NULL) {
|
|
|
|
if ((dp->di_bsddb->close)(dp->di_bsddb) != 0)
|
|
|
|
fprintf(stderr,
|
|
|
|
"Python bsddb: close errno %s in dealloc\n", errno);
|
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
PyMem_DEL(dp);
|
1995-08-30 20:43:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
bsddb_length(dp)
|
|
|
|
bsddbobject *dp;
|
|
|
|
{
|
|
|
|
if (dp->di_size < 0) {
|
|
|
|
DBT krec, drec;
|
|
|
|
int status;
|
|
|
|
int size = 0;
|
|
|
|
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++;
|
|
|
|
if (status < 0) {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetFromErrno(BsddbError);
|
1995-08-30 20:43:03 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dp->di_size = size;
|
|
|
|
}
|
|
|
|
return dp->di_size;
|
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1995-08-30 20:43:03 -03:00
|
|
|
bsddb_subscript(dp, key)
|
|
|
|
bsddbobject *dp;
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *key;
|
1995-08-30 20:43:03 -03:00
|
|
|
{
|
|
|
|
int status;
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *v;
|
1995-08-30 20:43:03 -03:00
|
|
|
DBT krec, drec;
|
|
|
|
char *data;
|
|
|
|
int size;
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
if (!PyArg_Parse(key, "s#", &data, &size))
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
krec.data = data;
|
|
|
|
krec.size = size;
|
|
|
|
|
|
|
|
status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0);
|
|
|
|
if (status != 0) {
|
|
|
|
if (status < 0)
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetFromErrno(BsddbError);
|
1995-08-30 20:43:03 -03:00
|
|
|
else
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetObject(PyExc_KeyError, key);
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
return PyString_FromStringAndSize((char *)drec.data, (int)drec.size);
|
1995-08-30 20:43:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
bsddb_ass_sub(dp, key, value)
|
|
|
|
bsddbobject *dp;
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *key, *value;
|
1995-08-30 20:43:03 -03:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
DBT krec, drec;
|
|
|
|
char *data;
|
|
|
|
int size;
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
if (!PyArg_Parse(key, "s#", &data, &size)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "bsddb key type must be string");
|
1995-08-30 20:43:03 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
krec.data = data;
|
|
|
|
krec.size = size;
|
|
|
|
dp->di_size = -1;
|
|
|
|
if (value == NULL) {
|
|
|
|
status = (dp->di_bsddb->del)(dp->di_bsddb, &krec, 0);
|
|
|
|
}
|
|
|
|
else {
|
1996-07-23 21:51:20 -03:00
|
|
|
if (!PyArg_Parse(value, "s#", &data, &size)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "bsddb value type must be string");
|
1995-08-30 20:43:03 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
drec.data = data;
|
|
|
|
drec.size = size;
|
|
|
|
status = (dp->di_bsddb->put)(dp->di_bsddb, &krec, &drec, 0);
|
|
|
|
}
|
|
|
|
if (status != 0) {
|
|
|
|
if (status < 0)
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetFromErrno(BsddbError);
|
1995-08-30 20:43:03 -03:00
|
|
|
else
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetObject(PyExc_KeyError, key);
|
1995-08-30 20:43:03 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyMappingMethods bsddb_as_mapping = {
|
1995-08-30 20:43:03 -03:00
|
|
|
(inquiry)bsddb_length, /*mp_length*/
|
|
|
|
(binaryfunc)bsddb_subscript, /*mp_subscript*/
|
|
|
|
(objobjargproc)bsddb_ass_sub, /*mp_ass_subscript*/
|
|
|
|
};
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1995-08-30 20:43:03 -03:00
|
|
|
bsddb_close(dp, args)
|
|
|
|
bsddbobject *dp;
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *args;
|
1995-08-30 20:43:03 -03:00
|
|
|
{
|
1996-07-23 21:51:20 -03:00
|
|
|
if (!PyArg_NoArgs(args))
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
if (dp->di_bsddb != NULL) {
|
|
|
|
if ((dp->di_bsddb->close)(dp->di_bsddb) != 0) {
|
|
|
|
dp->di_bsddb = NULL;
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetFromErrno(BsddbError);
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dp->di_bsddb = NULL;
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1995-08-30 20:43:03 -03:00
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1995-08-30 20:43:03 -03:00
|
|
|
bsddb_keys(dp, args)
|
|
|
|
bsddbobject *dp;
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *args;
|
1995-08-30 20:43:03 -03:00
|
|
|
{
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *list, *item;
|
1995-08-30 20:43:03 -03:00
|
|
|
DBT krec, drec;
|
|
|
|
int status;
|
|
|
|
int err;
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
if (!PyArg_NoArgs(args))
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
1996-07-23 21:51:20 -03:00
|
|
|
list = PyList_New(0);
|
1995-08-30 20:43:03 -03:00
|
|
|
if (list == NULL)
|
|
|
|
return NULL;
|
|
|
|
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)) {
|
1996-07-23 21:51:20 -03:00
|
|
|
item = PyString_FromStringAndSize((char *)krec.data, (int)krec.size);
|
1995-08-30 20:43:03 -03:00
|
|
|
if (item == NULL) {
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_DECREF(list);
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
err = PyList_Append(list, item);
|
|
|
|
Py_DECREF(item);
|
1995-08-30 20:43:03 -03:00
|
|
|
if (err != 0) {
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_DECREF(list);
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (status < 0) {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetFromErrno(BsddbError);
|
|
|
|
Py_DECREF(list);
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (dp->di_size < 0)
|
1996-07-23 21:51:20 -03:00
|
|
|
dp->di_size = PyList_Size(list); /* We just did the work for this... */
|
1995-08-30 20:43:03 -03:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1995-08-30 20:43:03 -03:00
|
|
|
bsddb_has_key(dp, args)
|
|
|
|
bsddbobject *dp;
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *args;
|
1995-08-30 20:43:03 -03:00
|
|
|
{
|
|
|
|
DBT krec, drec;
|
|
|
|
int status;
|
|
|
|
char *data;
|
|
|
|
int size;
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
if (!PyArg_Parse(args, "s#", &data, &size))
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
krec.data = data;
|
|
|
|
krec.size = size;
|
|
|
|
|
|
|
|
status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0);
|
|
|
|
if (status < 0) {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetFromErrno(BsddbError);
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
return PyInt_FromLong(status == 0);
|
1995-08-30 20:43:03 -03:00
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1995-08-30 20:43:03 -03:00
|
|
|
bsddb_set_location(dp, key)
|
|
|
|
bsddbobject *dp;
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *key;
|
1995-08-30 20:43:03 -03:00
|
|
|
{
|
|
|
|
int status;
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *v;
|
1995-08-30 20:43:03 -03:00
|
|
|
DBT krec, drec;
|
|
|
|
char *data;
|
|
|
|
int size;
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
if (!PyArg_Parse(key, "s#", &data, &size))
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
krec.data = data;
|
|
|
|
krec.size = size;
|
|
|
|
|
|
|
|
status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_CURSOR);
|
|
|
|
if (status != 0) {
|
|
|
|
if (status < 0)
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetFromErrno(BsddbError);
|
1995-08-30 20:43:03 -03:00
|
|
|
else
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetObject(PyExc_KeyError, key);
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
return Py_BuildValue("s#s#", krec.data, krec.size, drec.data, drec.size);
|
1995-08-30 20:43:03 -03:00
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1995-08-30 20:43:03 -03:00
|
|
|
bsddb_seq(dp, args, sequence_request)
|
|
|
|
bsddbobject *dp;
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *args;
|
1995-08-30 20:43:03 -03:00
|
|
|
int sequence_request;
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
DBT krec, drec;
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
if (!PyArg_NoArgs(args))
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
krec.data = 0;
|
|
|
|
krec.size = 0;
|
|
|
|
|
|
|
|
status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, sequence_request);
|
|
|
|
if (status != 0) {
|
|
|
|
if (status < 0)
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetFromErrno(BsddbError);
|
1995-08-30 20:43:03 -03:00
|
|
|
else
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetObject(PyExc_KeyError, args);
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
return Py_BuildValue("s#s#", krec.data, krec.size, drec.data, drec.size);
|
1995-08-30 20:43:03 -03:00
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1995-08-30 20:43:03 -03:00
|
|
|
bsddb_next(dp, key)
|
|
|
|
bsddbobject *dp;
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *key;
|
1995-08-30 20:43:03 -03:00
|
|
|
{
|
|
|
|
return bsddb_seq(dp, key, R_NEXT);
|
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1995-08-30 20:43:03 -03:00
|
|
|
bsddb_previous(dp, key)
|
|
|
|
bsddbobject *dp;
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *key;
|
1995-08-30 20:43:03 -03:00
|
|
|
{
|
|
|
|
return bsddb_seq(dp, key, R_PREV);
|
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1995-08-30 20:43:03 -03:00
|
|
|
bsddb_first(dp, key)
|
|
|
|
bsddbobject *dp;
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *key;
|
1995-08-30 20:43:03 -03:00
|
|
|
{
|
|
|
|
return bsddb_seq(dp, key, R_FIRST);
|
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1995-08-30 20:43:03 -03:00
|
|
|
bsddb_last(dp, key)
|
|
|
|
bsddbobject *dp;
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *key;
|
1995-08-30 20:43:03 -03:00
|
|
|
{
|
|
|
|
return bsddb_seq(dp, key, R_LAST);
|
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1995-08-30 20:43:03 -03:00
|
|
|
bsddb_sync(dp, args)
|
|
|
|
bsddbobject *dp;
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *args;
|
1995-08-30 20:43:03 -03:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
status = (dp->di_bsddb->sync)(dp->di_bsddb, 0);
|
|
|
|
if (status != 0) {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetFromErrno(BsddbError);
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
return PyInt_FromLong(status = 0);
|
1995-08-30 20:43:03 -03:00
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyMethodDef bsddb_methods[] = {
|
|
|
|
{"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},
|
1995-08-30 20:43:03 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1995-08-30 20:43:03 -03:00
|
|
|
bsddb_getattr(dp, name)
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *dp;
|
1995-08-30 20:43:03 -03:00
|
|
|
char *name;
|
|
|
|
{
|
1996-07-23 21:51:20 -03:00
|
|
|
return Py_FindMethod(bsddb_methods, dp, name);
|
1995-08-30 20:43:03 -03:00
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyTypeObject Bsddbtype = {
|
1996-09-11 20:18:43 -03:00
|
|
|
PyObject_HEAD_INIT(NULL)
|
1995-08-30 20:43:03 -03:00
|
|
|
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*/
|
|
|
|
};
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1995-08-30 20:43:03 -03:00
|
|
|
bsdhashopen(self, args)
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1995-08-30 20:43:03 -03: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;
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "s|siiiiiii",
|
1995-08-30 20:43:03 -03:00
|
|
|
&file, &flag, &mode,
|
|
|
|
&bsize, &ffactor, &nelem, &cachesize, &hash, &lorder))
|
|
|
|
return NULL;
|
|
|
|
if (flag != NULL) {
|
|
|
|
/* XXX need a way 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 {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetString(BsddbError,
|
1995-08-30 20:43:03 -03:00
|
|
|
"Flag should begin with 'r', 'w', 'c' or 'n'");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (flag[1] == 'l') {
|
|
|
|
#if defined(O_EXLOCK) && defined(O_SHLOCK)
|
|
|
|
if (flag[0] == 'r')
|
|
|
|
flags |= O_SHLOCK;
|
|
|
|
else
|
|
|
|
flags |= O_EXLOCK;
|
|
|
|
#else
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetString(BsddbError, "locking not supported on this platform");
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newdbhashobject(file, flags, mode,
|
|
|
|
bsize, ffactor, nelem, cachesize, hash, lorder);
|
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1995-08-30 20:43:03 -03:00
|
|
|
bsdbtopen(self, args)
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1995-08-30 20:43:03 -03:00
|
|
|
{
|
|
|
|
char *file;
|
|
|
|
char *flag = NULL;
|
|
|
|
int flags = O_RDONLY;
|
|
|
|
int mode = 0666;
|
|
|
|
int cachesize = 0;
|
1996-05-23 19:57:54 -03:00
|
|
|
int maxkeypage = 0;
|
|
|
|
int minkeypage = 0;
|
|
|
|
int btflags = 0;
|
|
|
|
unsigned int psize = 0;
|
|
|
|
int lorder = 0;
|
1995-08-30 20:43:03 -03:00
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "s|siiiiiii",
|
1996-05-23 19:57:54 -03:00
|
|
|
&file, &flag, &mode,
|
|
|
|
&btflags, &cachesize, &maxkeypage, &minkeypage,
|
|
|
|
&psize, &lorder))
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
if (flag != NULL) {
|
|
|
|
/* XXX need a way 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 {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetString(BsddbError,
|
1995-08-30 20:43:03 -03:00
|
|
|
"Flag should begin with 'r', 'w', 'c' or 'n'");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (flag[1] == 'l') {
|
|
|
|
#if defined(O_EXLOCK) && defined(O_SHLOCK)
|
|
|
|
if (flag[0] == 'r')
|
|
|
|
flags |= O_SHLOCK;
|
|
|
|
else
|
|
|
|
flags |= O_EXLOCK;
|
|
|
|
#else
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetString(BsddbError, "locking not supported on this platform");
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newdbbtobject(file, flags, mode,
|
1996-05-23 19:57:54 -03:00
|
|
|
btflags, cachesize, maxkeypage, minkeypage,
|
|
|
|
psize, lorder);
|
1995-08-30 20:43:03 -03:00
|
|
|
}
|
1996-05-23 19:57:54 -03:00
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1995-08-30 20:43:03 -03:00
|
|
|
bsdrnopen(self, args)
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1995-08-30 20:43:03 -03:00
|
|
|
{
|
|
|
|
char *file;
|
|
|
|
char *flag = NULL;
|
|
|
|
int flags = O_RDONLY;
|
|
|
|
int mode = 0666;
|
|
|
|
int cachesize = 0;
|
1996-05-23 19:57:54 -03:00
|
|
|
int rnflags = 0;
|
|
|
|
unsigned int psize = 0;
|
|
|
|
int lorder = 0;
|
|
|
|
size_t reclen = 0;
|
|
|
|
char *bval = "";
|
|
|
|
char *bfname = NULL;
|
1995-08-30 20:43:03 -03:00
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "s|siiiiiiss",
|
1996-05-23 19:57:54 -03:00
|
|
|
&file, &flag, &mode,
|
|
|
|
&rnflags, &cachesize, &psize, &lorder,
|
|
|
|
&reclen, &bval, &bfname))
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
if (flag != NULL) {
|
|
|
|
/* XXX need a way 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 {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetString(BsddbError,
|
1995-08-30 20:43:03 -03:00
|
|
|
"Flag should begin with 'r', 'w', 'c' or 'n'");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (flag[1] == 'l') {
|
|
|
|
#if defined(O_EXLOCK) && defined(O_SHLOCK)
|
|
|
|
if (flag[0] == 'r')
|
|
|
|
flags |= O_SHLOCK;
|
|
|
|
else
|
|
|
|
flags |= O_EXLOCK;
|
|
|
|
#else
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetString(BsddbError, "locking not supported on this platform");
|
1995-08-30 20:43:03 -03:00
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
1996-05-23 19:57:54 -03:00
|
|
|
else if (flag[1] != '\0') {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetString(BsddbError,
|
1996-05-23 19:57:54 -03:00
|
|
|
"Flag char 2 should be 'l' or absent");
|
|
|
|
return NULL;
|
|
|
|
}
|
1995-08-30 20:43:03 -03:00
|
|
|
}
|
|
|
|
return newdbrnobject(file, flags, mode,
|
|
|
|
rnflags, cachesize, psize, lorder, bval[0], bfname);
|
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyMethodDef bsddbmodule_methods[] = {
|
|
|
|
{"hashopen", (PyCFunction)bsdhashopen, 1},
|
|
|
|
{"btopen", (PyCFunction)bsdbtopen, 1},
|
|
|
|
{"rnopen", (PyCFunction)bsdrnopen, 1},
|
1995-08-30 20:43:03 -03:00
|
|
|
{0, 0},
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
initbsddb() {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *m, *d;
|
1995-08-30 20:43:03 -03:00
|
|
|
|
1996-09-11 20:18:43 -03:00
|
|
|
Bsddbtype.ob_type = &PyType_Type;
|
1996-07-23 21:51:20 -03:00
|
|
|
m = Py_InitModule("bsddb", bsddbmodule_methods);
|
|
|
|
d = PyModule_GetDict(m);
|
|
|
|
BsddbError = PyString_FromString("bsddb.error");
|
|
|
|
if (BsddbError == NULL || PyDict_SetItemString(d, "error", BsddbError))
|
|
|
|
Py_FatalError("can't define bsddb.error");
|
1995-08-30 20:43:03 -03:00
|
|
|
}
|