/* thread the read function */ static void *threadread(void *targ){ TCFDB *fdb = ((TARGREAD *)targ)->fdb; int rnum = ((TARGREAD *)targ)->rnum; bool wb = ((TARGREAD *)targ)->wb; bool rnd = ((TARGREAD *)targ)->rnd; int id = ((TARGREAD *)targ)->id; bool err = false; int base = id * rnum; for(int i = 1; i <= rnum && !err; i++){ uint64_t kid = base + (rnd ? myrandnd(i) + 1 : i); int vsiz; if(wb){ char vbuf[RECBUFSIZ]; int vsiz = tcfdbget4(fdb, kid, vbuf, RECBUFSIZ); if(vsiz < 0 && (!rnd || tcfdbecode(fdb) != TCENOREC)){ eprint(fdb, __LINE__, "tcfdbget4"); err = true; } } else { char *vbuf = tcfdbget(fdb, kid, &vsiz); if(!vbuf && (!rnd || tcfdbecode(fdb) != TCENOREC)){ eprint(fdb, __LINE__, "tcfdbget"); err = true; } tcfree(vbuf); } if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } return err ? "error" : NULL; }
/* FDBIterValuesType.tp_iternext */ static PyObject * FDBIterValues_tp_iternext(DBIter *self) { FDB *fdb = (FDB *)self->db; long long key; void *value; int value_size; PyObject *pyvalue; if (fdb->changed) { return set_error(Error, "FDB changed during iteration"); } key = uint64_to_int64(tcfdbiternext(fdb->fdb)); if (key == -1) { return NULL; } else if (!key) { if (tcfdbecode(fdb->fdb) == TCENOREC) { return set_stopiteration_error(); } return set_fdb_error(fdb->fdb, 0); } value = tcfdbget(fdb->fdb, key, &value_size); pyvalue = void_to_bytes(value, value_size); tcfree(value); return pyvalue; }
/* FDBIterItemsType.tp_iternext */ static PyObject * FDBIterItems_tp_iternext(DBIter *self) { FDB *fdb = (FDB *)self->db; long long key; void *value; int value_size; PyObject *pykey, *pyvalue, *pyresult = NULL; if (fdb->changed) { return set_error(Error, "FDB changed during iteration"); } key = uint64_to_int64(tcfdbiternext(fdb->fdb)); if (key == -1) { return NULL; } else if (!key) { if (tcfdbecode(fdb->fdb) == TCENOREC) { return set_stopiteration_error(); } return set_fdb_error(fdb->fdb, 0); } value = tcfdbget(fdb->fdb, key, &value_size); pykey = PyLong_FromLongLong(key); pyvalue = void_to_bytes(value, value_size); if (pykey && pyvalue) { pyresult = PyTuple_Pack(2, pykey, pyvalue); } Py_XDECREF(pykey); Py_XDECREF(pyvalue); tcfree(value); return pyresult; }
/* perform list command */ static int proclist(const char *path, int omode, int max, bool pv, bool px, const char *rlstr, const char *rustr, const char *ristr){ TCFDB *fdb = tcfdbnew(); if(g_dbgfd >= 0) tcfdbsetdbgfd(fdb, g_dbgfd); if(!tcfdbopen(fdb, path, FDBOREADER | omode)){ printerr(fdb); tcfdbdel(fdb); return 1; } bool err = false; if(rlstr || ristr){ TCLIST *keys = ristr ? tcfdbrange5(fdb, ristr, max) : tcfdbrange3(fdb, rlstr, rustr, max); for(int i = 0; i < tclistnum(keys); i++){ int ksiz; const char *kbuf = tclistval(keys, i, &ksiz); printf("%s", kbuf); if(pv){ int vsiz; char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz); if(vbuf){ putchar('\t'); printdata(vbuf, vsiz, px); tcfree(vbuf); } } putchar('\n'); } tclistdel(keys); } else { if(!tcfdbiterinit(fdb)){ printerr(fdb); err = true; } int cnt = 0; uint64_t id; while((id = tcfdbiternext(fdb)) > 0){ printf("%llu", (unsigned long long)id); if(pv){ int vsiz; char *vbuf = tcfdbget(fdb, id, &vsiz); if(vbuf){ putchar('\t'); printdata(vbuf, vsiz, px); tcfree(vbuf); } } putchar('\n'); if(max >= 0 && ++cnt >= max) break; } } if(!tcfdbclose(fdb)){ if(!err) printerr(fdb); err = true; } tcfdbdel(fdb); return err ? 1 : 0; }
/* FDB_tp_as_mapping.mp_subscript */ static PyObject * FDB_GetItem(FDB *self, PyObject *pykey) { long long key; void *value; int value_size; PyObject *pyvalue; key = PyLong_AsLongLong(pykey); if (key == -1 && PyErr_Occurred()) { return NULL; } value = tcfdbget(self->fdb, key, &value_size); if (!value) { return set_fdb_error(self->fdb, key); } pyvalue = void_to_bytes(value, value_size); tcfree(value); return pyvalue; }
/* FDB_tp_as_sequence.sq_contains */ static int FDB_Contains(FDB *self, PyObject *pykey) { long long key; int value_size; void *value; key = PyLong_AsLongLong(pykey); if (key == -1 && PyErr_Occurred()) { return -1; } value = tcfdbget(self->fdb, key, &value_size); if (!value) { if (tcfdbecode(self->fdb) == TCENOREC) { return 0; } set_fdb_error(self->fdb, 0); return -1; } tcfree(value); return 1; }