Skip to content
Permalink
Browse files

allow passing on kvdata and delete_on_close to PyKV_open()

  • Loading branch information...
jacobgorm committed Jan 25, 2019
1 parent 48bb378 commit f157b45e41d96913c1cb05f01abc53bbf644918f
Showing with 11 additions and 27 deletions.
  1. +5 −7 kv.c
  2. +1 −1 kv.h
  3. +5 −19 pykv.c
12 kv.c
@@ -57,7 +57,7 @@ int kv_global_init(void) {

#define BUFFER_MAX (4<<20)

int kv_init(struct kv *kv, const char *prefix, const char *kvinfo) {
int kv_init(struct kv *kv, const char *kvinfo, int delete_on_close) {

memset(kv, 0, sizeof(*kv));

@@ -86,14 +86,16 @@ int kv_init(struct kv *kv, const char *prefix, const char *kvinfo) {
for (c = line + 8; *c != '\0' && *c != '\n'; ++c);
*c = '\0';
kv->kvdata = strdup(line + 7);
if (!delete_on_close) {
kv->saved = 1;
}
} else if (!strncmp(line, "fallback=", 9)) {
fallback = strdup(line + 9);
} else if (!strncmp(line, "cache=", 6)) {
cache = strdup(line + 6);
}
}
free(dup);
kv->saved = 1;
}
if (!have_key) {
RAND_bytes(kv->crypto_key, CRYPTO_KEY_SIZE);
@@ -106,13 +108,9 @@ int kv_init(struct kv *kv, const char *prefix, const char *kvinfo) {
}

char kvdata[256];
if (prefix) {
sprintf(kvdata, "%s/kvdata-", prefix);
} else {
strcpy(kvdata, "kvdata-");
}
if (!kv->kvdata) {
uint8_t kvdata_random[16];
strcpy(kvdata, "kvdata-");
RAND_bytes(kvdata_random, sizeof(kvdata_random));
hex(kvdata + strlen(kvdata), kvdata_random, sizeof(kvdata_random));
kv->kvdata = strdup(kvdata);
2 kv.h
@@ -26,7 +26,7 @@ struct kv {
};

int kv_global_init(void);
int kv_init(struct kv *kv, const char *prefix, const char *kvinfo);
int kv_init(struct kv *kv, const char *kvinfo, int delete_on_close);
int kv_insert(struct kv *kv, uint64_t key, const uint8_t *value, size_t size);
int kv_find(struct kv *kv, uint8_t **rptr, size_t *rsize, uint64_t key);
int kv_flush(struct kv *kv);
24 pykv.c
@@ -61,28 +61,15 @@ static struct PyModuleDef pykvmodule = {
PyKVMethods
};


static PyObject *PyKV_init(PyKV * self, PyObject * args) {
char *prefix;
if (!PyArg_ParseTuple(args, "s", &prefix)) {
PyErr_SetString(PyKVError, "wrong init args");
return NULL;
}
if (kv_init(&self->kv, prefix[0] ? prefix : NULL, NULL) < 0) {
PyErr_SetString(PyKVError, "failed to init pykv");
return NULL;
}
Py_RETURN_NONE;
}

static PyObject *PyKV_open(PyKV * self, PyObject * args) {
char *prefix, *kvinfo;
if (!PyArg_ParseTuple(args, "ss", &prefix, &kvinfo)) {
static PyObject *PyKV_open(PyKV *self, PyObject *args) {
char *kvinfo;
int delete_on_close;
if (!PyArg_ParseTuple(args, "sp", &kvinfo, &delete_on_close)) {
PyErr_SetString(PyKVError, "wrong open args");
return NULL;
}

if (kv_init(&self->kv, prefix, kvinfo) < 0) {
if (kv_init(&self->kv, kvinfo, delete_on_close) < 0) {
PyErr_SetString(PyKVError, "failed to init pykv");
return NULL;
}
@@ -103,7 +90,6 @@ static PyObject *PyKV_save(PyKV * self, PyObject * args) {
}

static PyMethodDef PyKV_methods[] = {
{ "init", (PyCFunction) PyKV_init, METH_VARARGS, "init"},
{ "open", (PyCFunction) PyKV_open, METH_VARARGS, "open"},
{ "save", (PyCFunction) PyKV_save, METH_VARARGS, "save"},
{ "insert", (PyCFunction) PyKV_insert, METH_VARARGS, "Insert key and value" },

0 comments on commit f157b45

Please sign in to comment.
You can’t perform that action at this time.