bool DBPrivAdvanceCursor(DBCursorPriv *cursor, void **key, int *ksize, void **value, int *vsize) { if (!Lock(cursor->db)) { return false; } free(cursor->curkey); free(cursor->curval); cursor->curkey = NULL; cursor->curval = NULL; *key = dpiternext(cursor->db->depot, ksize); if (*key == NULL) { /* Reached the end of database */ Unlock(cursor->db); return false; } *value = dpget(cursor->db->depot, *key, *ksize, 0, -1, vsize); // keep pointers for later free cursor->curkey = *key; cursor->curkey_size = *ksize; cursor->curval = *value; Unlock(cursor->db); return true; }
/* perform export command */ int doexport(const char *name, int bin){ DEPOT *depot; char *kbuf, *vbuf, *tmp; int err, ksiz, vsiz; /* open a database */ if(!(depot = dpopen(name, DP_OREADER, -1))){ pdperror(name); return 1; } /* initialize the iterator */ dpiterinit(depot); /* loop for each key */ err = FALSE; while((kbuf = dpiternext(depot, &ksiz)) != NULL){ /* retrieve a value with a key */ if(!(vbuf = dpget(depot, kbuf, ksiz, 0, -1, &vsiz))){ pdperror(name); free(kbuf); err = TRUE; break; } /* output data */ if(bin){ tmp = cbbaseencode(kbuf, ksiz); printf("%s\t", tmp); free(tmp); tmp = cbbaseencode(vbuf, vsiz); printf("%s\n", tmp); free(tmp); } else { printf("%s\t%s\n", kbuf, vbuf); } /* free resources */ free(vbuf); free(kbuf); } /* check whether all records were retrieved */ if(dpecode != DP_ENOITEM){ pdperror(name); err = TRUE; } /* close the database */ if(!dpclose(depot)){ pdperror(name); return 1; } return err ? 1 : 0; }
static VALUE minidb_each(VALUE self) { DEPOT * db; char *p; int len; VALUE key; Data_Get_Struct(self, DEPOT, db); dpiterinit(db); for(;;) { p = dpiternext(db, &len); if (!p) break; key = rb_tainted_str_new(p, len); rb_yield_values(2, key, minidb_get(self, key)); } return self; }
/* Get the next key of a database. */ datum gdbm_nextkey(GDBM_FILE dbf, datum key){ char *kbuf; int ksiz; assert(dbf); if(dbf->depot){ if(!(kbuf = dpiternext(dbf->depot, &ksiz))){ gdbm_errno = gdbm_geterrno(dpecode); key.dptr = NULL; key.dsize = 0; return key; } } else { if(!(kbuf = criternext(dbf->curia, &ksiz))){ gdbm_errno = gdbm_geterrno(dpecode); key.dptr = NULL; key.dsize = 0; return key; } } key.dptr = kbuf; key.dsize = ksiz; return key; }
int main(int argc, char *argv[]) { DEPOT *dbmap = NULL; char *str_prefix = "dbbm_key_prefix_"; char *key, keystr[24]; unsigned long i; int rc = 0; PDUMMY_DATA datap; if (argc < 3) { fprintf(stderr, "Usage : <prog> <dbpath> <datasize>\n"); exit(1); } dbmap = dpopen(argv[1], DP_OWRITER|DP_OCREAT|DP_OSPARSE|DP_ONOLCK, -1); if (!dbmap) { fprintf(stderr, "Unable to open the dbbm file \n"); exit(1); } datap = (PDUMMY_DATA) malloc(sizeof(DUMMY_DATA)); if (!datap) { fprintf(stderr, "Malloc error %s\n", strerror(errno)); exit(1); } unsigned long datasize = strtoul(argv[2], NULL, 10); // push data for (i = 0; i < datasize; i++) { asprintf(keystr, "%s%lu", str_prefix, i); datap->x = i; datap->y = i+1; datap->z = i+2; if (!dpput(dbmap, keystr, strlen(keystr), (char *) datap, sizeof(DUMMY_DATA), DP_DOVER)) { fprintf(stderr, "Unable to insert to qdbm\n"); }; } if(!dpclose(dbmap)){ fprintf(stderr, "dpclose: %s\n", dperrmsg(dpecode)); return 1; } //read data dbmap = dpopen(argv[1], DP_OREADER, -1); if (!dbmap) { fprintf(stderr, "Unable to open the dbbm file \n"); exit(1); } fprintf(stdout, "Starting read of the database sequentially\n"); if(!dpiterinit(dbmap)){ fprintf(stderr, "dpiterinit: %s\n", dperrmsg(dpecode)); } /* scan with the iterator */ while ((key = dpiternext(dbmap, NULL)) != NULL) { if (!(datap = (PDUMMY_DATA) dpget(dbmap, key, -1, 0, sizeof(DUMMY_DATA), NULL))) { fprintf(stderr, "Value is not found for key %s\n", key); fprintf(stderr, "dpget: %s\n", dperrmsg(dpecode)); free(key); break; } /* fprintf(stdout, "Data read for dbm : x=%lu y=%lu z=%lu\n", datap->x, datap->y, datap->z);*/ free(datap); } fprintf(stdout, "End of the database reached\n"); if(!dpclose(dbmap)){ fprintf(stderr, "dpclose: %s\n", dperrmsg(dpecode)); return 1; } return 0; }