static void print_cursor(struct ddb *db, struct ddb_cursor *cur) { if (!cur){ const char *err; ddb_error(db, &err); fprintf(stderr, "Query failed: %s\n", err); exit(1); } if (ddb_notfound(cur)){ fprintf(stderr, "Not found\n"); exit(1); } int errno, i = 0; const struct ddb_entry *e; while ((e = ddb_next(cur, &errno))){ printf("%.*s\n", e->length, e->data); ++i; } if (errno){ fprintf(stderr, "Cursor failed: out of memory\n"); exit(1); } ddb_free_cursor(cur); }
struct ddb_view *ddb_view_cons_finalize(const struct ddb_view_cons *cons, struct ddb *db) { uint32_t n = ddb_map_num_items(cons->map); struct ddb_cursor *cursor = ddb_unique_values(db); struct ddb_view *view = malloc(4 + n * sizeof(valueid_t)); const struct ddb_entry *value; valueid_t id = 0; int err = 0; if (!(cursor && view)) goto err; view->num_values = 0; while (n && (value = ddb_next(cursor, &err))){ ++id; if (ddb_map_lookup_str(cons->map, value)){ view->values[view->num_values++] = id; --n; } } if (!err){ ddb_free_cursor(cursor); return view; } err: ddb_view_free(view); ddb_free_cursor(cursor); return NULL; }
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; }
static PyObject * DiscoDBIter_iternextentry(DiscoDBIter *self) { int errcode; const struct ddb_entry *next = ddb_next(self->cursor, &errcode); if (errcode) return PyErr_NoMemory(); if (next == NULL) return NULL; return Py_BuildValue("s#", next->data, next->length); }
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)); }