static int DiscoDB_contains(register DiscoDB *self, register PyObject *key) { PyObject *pack = NULL; struct ddb_entry *kentry = ddb_entry_alloc(1); struct ddb_cursor *cursor = NULL; int isfound = 1; if (kentry == NULL) goto Done; pack = Py_BuildValue("(O)", key); if (pack == NULL) goto Done; if (!PyArg_ParseTuple(pack, "s#", &kentry->data, &kentry->length)) goto Done; cursor = ddb_getitem(self->discodb, kentry); if (cursor == NULL) if (ddb_has_error(self->discodb)) goto Done; if (ddb_notfound(cursor)) isfound = 0; Done: Py_CLEAR(pack); DiscoDB_CLEAR(kentry); if (PyErr_Occurred()) return -1; return isfound; }
static PyObject * DiscoDB_getitem(register DiscoDB *self, register PyObject *key) { PyObject *pack = NULL; struct ddb_entry *kentry = ddb_entry_alloc(1); struct ddb_cursor *cursor = NULL; if (kentry == NULL) goto Done; pack = Py_BuildValue("(O)", key); if (pack == NULL) goto Done; if (!PyArg_ParseTuple(pack, "s#", &kentry->data, &kentry->length)) goto Done; cursor = ddb_getitem(self->discodb, kentry); if (cursor == NULL) if (ddb_has_error(self->discodb)) goto Done; if (ddb_notfound(cursor)) PyErr_Format(PyExc_KeyError, "%s", PyBytes_AsString(key)); Done: Py_CLEAR(pack); DiscoDB_CLEAR(kentry); if (PyErr_Occurred()) return NULL; return DiscoDBIter_new(&DiscoDBIterEntryType, self, cursor); }
int main(int argc, char **argv) { if (argc < 3) usage(); struct ddb *db = open_discodb(argv[1]); if (!strcmp(argv[2], "-info")) print_info(db); else if (!strcmp(argv[2], "-keys")) print_cursor(db, ddb_keys(db)); else if (!strcmp(argv[2], "-values")) print_cursor(db, ddb_values(db)); else if (!strcmp(argv[2], "-uvalues")) print_cursor(db, ddb_unique_values(db)); else if (!strcmp(argv[2], "-item")){ if (argc < 4){ fprintf(stderr, "Specify query\n"); exit(1); } struct ddb_entry e; e.data = argv[3]; e.length = strlen(argv[3]); print_cursor(db, ddb_getitem(db, &e)); }else if (!strcmp(argv[2], "-cnf")){ if (argc < 4){ fprintf(stderr, "Specify query\n"); exit(1); } int num_q = 0; struct ddb_query_clause *q = parse_cnf(&argv[3], argc - 3, &num_q); print_cursor(db, ddb_query(db, q, num_q)); free(q[0].terms); free(q); }else usage(); ddb_free(db); return 0; }
static PyObject * DiscoDBIter_iternextitem(DiscoDBIter *self) { int errcode; const struct ddb_entry *nextkey = ddb_next(self->cursor, &errcode); struct ddb_cursor *vcursor; if (errcode) return PyErr_NoMemory(); if (nextkey == NULL) return NULL; vcursor = ddb_getitem(self->owner->discodb, nextkey); if (vcursor == NULL) if (ddb_has_error(self->owner->discodb)) return NULL; return Py_BuildValue("s#N", nextkey->data, nextkey->length, DiscoDBIter_new(&DiscoDBIterEntryType, self->owner, vcursor)); }
struct ddb_cons *ddb_cons_ddb(struct ddb *db) { struct ddb_cons *cons; struct ddb_cursor *keys = NULL, *vals = NULL; const struct ddb_entry *k, *v; int errno = 0; if (!(cons = ddb_cons_new())) goto error; if (!(keys = ddb_keys(db))) goto error; while ((k = ddb_next(keys, &errno))) { if (!(vals = ddb_getitem(db, k))) goto error; while ((v = ddb_next(vals, &errno))) if (ddb_cons_add(cons, k, v)) goto error; if (errno) goto error; ddb_free_cursor(vals); } if (errno) goto error; ddb_free_cursor(keys); return cons; error: if (cons) ddb_cons_free(cons); if (keys) ddb_free_cursor(keys); if (vals) ddb_free_cursor(vals); return NULL; }