summaryrefslogtreecommitdiff
path: root/c_src/permdbpy.c
blob: da7fc8fd0b6328fe796b0305d058bdb990fc62ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * Copyright (c) 2016, NORDUnet A/S.
 * See LICENSE for licensing information.
 */

#include <Python.h>
#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, int write_enable)
{
        struct permdb_object *permdb;

        permdb = permdb_alloc(dbpath, write_enable);

        if (permdb == NULL) {
                PyErr_SetString(PyExc_RuntimeError, "Cannot allocate permdb object");
                return NULL;
        }

        permdb_object_py *state = PyObject_New(permdb_object_py, &permdb_type);
        state->permdb = permdb;
        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) {
            PyErr_SetString(PyExc_RuntimeError, get_permdb_error(state->permdb));
            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;
        int write_enable = 1;

        if (!PyArg_ParseTuple(args, "s|i", &dbpath, &write_enable)) {
                return NULL;
        }

        return (PyObject*)permdb_alloc_py(dbpath, write_enable);
}

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) {
                PyErr_SetString(PyExc_RuntimeError, get_permdb_error(state->permdb));
                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;

        if (!PyArg_ParseTuple(args, "O!", &permdb_type, &state)) {
                return NULL;
        }

        int result = committree(state->permdb);

        if (result < 0) {
                PyErr_SetString(PyExc_RuntimeError, get_permdb_error(state->permdb));
                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);
}

/* Local Variables: */
/* c-file-style: "BSD" */
/* End: */