#include #include "permdb.h" typedef struct permdb_object_py { PyObject_HEAD struct permdb_object *permdb; } permdb_object_py; static void permdb_dealloc(permdb_object_py *state) { permdb_free(state->permdb); } PyTypeObject permdb_type = { PyObject_HEAD_INIT(NULL) 0, /*ob_size*/ "permdb.permdb", /*tp_name*/ sizeof(permdb_object_py), /*tp_basicsize*/ 0, /*tp_itemsize*/ (destructor)permdb_dealloc,/*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ 0, /*tp_compare*/ 0, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ 0, /*tp_hash */ 0, /*tp_call*/ 0, /*tp_str*/ 0, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT, /*tp_flags*/ "PermDB state", /*tp_doc*/ }; permdb_object_py * permdb_alloc_py(const char *dbpath) { permdb_object_py *state = PyObject_New(permdb_object_py, &permdb_type); state->permdb = permdb_alloc(dbpath); return state; } static PyObject * data_pread(PyObject *self, PyObject *args) { unsigned long long offset; unsigned int length; permdb_object_py *state; if (!PyArg_ParseTuple(args, "O!KI", &permdb_type, &state, &offset, &length)) { return NULL; } unsigned char *result = read_internal_data(state->permdb, offset, length); if (result == NULL) { return NULL; } PyObject* resultObj = PyString_FromStringAndSize((char *)result, length); free(result); return resultObj; } static PyObject * permdb_alloc_wrapper(PyObject *self, PyObject *args) { const char *dbpath = NULL; if (!PyArg_ParseTuple(args, "s", &dbpath)) { return NULL; } return (PyObject*)permdb_alloc_py(dbpath); } static PyObject * datasize_wrapper(PyObject *self, PyObject *args) { permdb_object_py *state; if (!PyArg_ParseTuple(args, "O!", &permdb_type, &state)) { return NULL; } return PyInt_FromLong(datasize(state->permdb)); } static PyObject * addvalue_wrapper(PyObject *self, PyObject *args) { permdb_object_py *state; const char *key; unsigned int keylength; const char *data; unsigned int datalength; if (!PyArg_ParseTuple(args, "O!s#s#", &permdb_type, &state, &key, &keylength, &data, &datalength)) { return NULL; } int result = addvalue(state->permdb, (unsigned char *) key, keylength, (unsigned char *) data, datalength, 0); if (result < 0) { return NULL; } else if (result == 0) { Py_INCREF(Py_False); return Py_False; } else { Py_INCREF(Py_True); return Py_True; } } static PyObject * getvalue_wrapper(PyObject *self, PyObject *args) { permdb_object_py *state; const char *key; int keylen; if (!PyArg_ParseTuple(args, "O!s#", &permdb_type, &state, &key, &keylen)) { return NULL; } size_t datalen; unsigned char *result = getvalue(state->permdb, (unsigned char *) key, keylen, &datalen); if (result == NULL) { Py_INCREF(Py_None); return Py_None; } PyObject* resultObj = PyString_FromStringAndSize((char *) result, datalen); free(result); return resultObj; } static PyObject * clear_nodecache(PyObject *self, PyObject *args) { permdb_object_py *state; if (!PyArg_ParseTuple(args, "O!", &permdb_type, &state)) { return NULL; } delete_all_nodes_in_cache(state->permdb); Py_INCREF(Py_None); return Py_None; } static PyObject * committree_wrapper(PyObject *self, PyObject *args) { permdb_object_py *state; fprintf(stderr, "starting commit\n"); if (!PyArg_ParseTuple(args, "O!", &permdb_type, &state)) { return NULL; } int result = committree(state->permdb); if (result < 0) { return NULL; } else { Py_INCREF(Py_None); return Py_None; } } static PyMethodDef UtilMethods[] = { {"data_pread", data_pread, METH_VARARGS}, {"alloc", permdb_alloc_wrapper, METH_VARARGS}, {"datasize", datasize_wrapper, METH_VARARGS}, {"addvalue", addvalue_wrapper, METH_VARARGS}, {"getvalue", getvalue_wrapper, METH_VARARGS}, {"committree", committree_wrapper, METH_VARARGS}, {"clear_nodecache", clear_nodecache, METH_VARARGS}, {NULL, NULL} }; void initpermdb() { (void) Py_InitModule("permdb", UtilMethods); }