DBCursorPriv *DBPrivOpenCursor(DBPriv *db) { if (!LockCursor(db)) { return NULL; } if (!Lock(db)) { UnlockCursor(db); return NULL; } if (!dpiterinit(db->depot)) { Log(LOG_LEVEL_ERR, "Could not initialize QuickDB iterator. (dpiterinit: %s)", dperrmsg(dpecode)); Unlock(db); UnlockCursor(db); return NULL; } DBCursorPriv *cursor = xcalloc(1, sizeof(DBCursorPriv)); cursor->db = db; Unlock(db); /* Cursor remains locked */ return cursor; }
/* 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 first key of a database. */ datum gdbm_firstkey(GDBM_FILE dbf){ datum key; assert(dbf); memset(&key, 0, sizeof(datum)); if(dbf->depot){ if(dprnum(dbf->depot) < 1){ gdbm_errno = GDBM_EMPTY_DATABASE; key.dptr = NULL; key.dsize = 0; return key; } dpiterinit(dbf->depot); return gdbm_nextkey(dbf, key); } else { if(crrnum(dbf->curia) < 1){ gdbm_errno = GDBM_EMPTY_DATABASE; key.dptr = NULL; key.dsize = 0; return key; } criterinit(dbf->curia); return gdbm_nextkey(dbf, 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; }