static PyObject * DiscoDB_keys(DiscoDB *self) { struct ddb_cursor *cursor = ddb_keys(self->discodb); if (cursor == NULL) if (ddb_has_error(self->discodb)) 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; }
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; }