ex_t db_foreach(void *vhandle, db_foreach_t hook, void *userdata) { int ret = 0; dbh_t *handle = vhandle; VILLA *dbp = handle->dbp; dbv_t dbv_key, dbv_data; int ksiz, dsiz; char *key, *data; ret = vlcurfirst(dbp); if (ret) { while ((key = vlcurkey(dbp, &ksiz))) { data = vlcurval(dbp, &dsiz); if (data) { /* switch to "dbv_t *" variables */ dbv_key.leng = ksiz; dbv_key.data = xmalloc(dbv_key.leng+1); memcpy(dbv_key.data, key, ksiz); ((char *)dbv_key.data)[dbv_key.leng] = '\0'; dbv_data.data = data; dbv_data.leng = dsiz; /* read count */ /* call user function */ ret = hook(&dbv_key, &dbv_data, userdata); xfree(dbv_key.data); if (ret != 0) break; free(data); /* not xfree() as allocated by dpget() */ } free(key); /* not xfree() as allocated by dpiternext() */ vlcurnext(dbp); } } else { print_error(__FILE__, __LINE__, "(qdbm) vlcurfirst err: %d, %s", dpecode, dperrmsg(dpecode)); exit(EX_ERROR); } return EX_OK; }
std::set<ValueKey> JsonDb::Transaction::Walk() { std::set<ValueKey> keys; // initialize the iterator if(!vlcurfirst(db.get())) throw std::runtime_error("Failed to initialize database iterator"); CharPtr key; while((key = CharPtr(vlcurkey(db.get(), NULL))) != NULL) { keys.insert(*(ValueKey const *)key.get()); vlcurnext(db.get()); } return keys; }