/* Flush any data in memory to disk */ void db_flush(void *vhandle) { dbh_t *handle = vhandle; VILLA * dbp = handle->dbp; if (!vlsync(dbp)) print_error(__FILE__, __LINE__, "(qdbm) vlsync err: %d, %s", dpecode, dperrmsg(dpecode)); }
static VALUE minidb_init(VALUE self, VALUE path) { DEPOT *db; SafeStringValue(path); db = dpopen(RSTRING_PTR(path), DP_OREADER|DP_OWRITER|DP_OCREAT, 0); if (!db) { rb_raise(rb_eRuntimeError, "dpopen - %s", dperrmsg(dpecode)); } DATA_PTR(self) = db; return self; }
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; }
/* Initialize database. Returns: pointer to database handle on success, NULL otherwise. */ void *db_open(void * dummy, bfpath *bfp, dbmode_t open_mode) { dbh_t *handle; int open_flags; VILLA *dbp; UNUSED(dummy); if (open_mode & DS_WRITE) open_flags = VL_OWRITER; else open_flags = VL_OREADER; handle = dbh_init(bfp); if (handle == NULL) return NULL; dbp = handle->dbp = vlopen(handle->name, open_flags, cmpkey); if ((dbp == NULL) && (open_mode & DS_WRITE)) { dbp = handle->dbp = vlopen(handle->name, open_flags|VL_OCREAT, cmpkey); if (dbp != NULL) handle->created = true; } if (dbp == NULL) goto open_err; if (DEBUG_DATABASE(1)) fprintf(dbgout, "(qdbm) vlopen( %s, %d )\n", handle->name, open_mode); return handle; open_err: print_error(__FILE__, __LINE__, "(qdbm) vlopen(%s, %d), err: %d, %s", handle->name, open_flags, dpecode, dperrmsg(dpecode)); dbh_free(handle); return NULL; }
int db_set_dbvalue(void *vhandle, const dbv_t *token, const dbv_t *val) { int ret; dbh_t *handle = vhandle; VILLA *dbp = handle->dbp; ret = vlput(dbp, token->data, token->leng, val->data, val->leng, VL_DOVER); if (ret == 0) { print_error(__FILE__, __LINE__, "(qdbm) db_set_dbvalue( '%.*s' ) err: %d, %s", CLAMP_INT_MAX(token->leng), (char *)token->data, dpecode, dperrmsg(dpecode)); exit(EX_ERROR); } db_optimize(dbp, handle->name); return 0; }
int db_delete(void *vhandle, const dbv_t *token) { int ret; dbh_t *handle = vhandle; VILLA *dbp; dbp = handle->dbp; ret = vlout(dbp, token->data, token->leng); if (ret == 0) { print_error(__FILE__, __LINE__, "(qdbm) vlout('%.*s'), err: %d, %s", CLAMP_INT_MAX(token->leng), (char *)token->data, dpecode, dperrmsg(dpecode)); exit(EX_ERROR); } ret = ret ^ 1; /* ok is 1 in qdbm and 0 in bogofilter */ return ret; /* 0 if ok */ }
bool DBPrivWrite(DBPriv *db, const void *key, int key_size, const void *value, int value_size) { if (!Lock(db)) { return false; } if (!dpput(db->depot, key, key_size, value, value_size, DP_DOVER)) { char *db_name = dpname(db->depot); Log(LOG_LEVEL_ERR, "Could not write key to DB '%s'. (dpput: %s)", db_name, dperrmsg(dpecode)); free(db_name); Unlock(db); return false; } Unlock(db); return true; }
bool DBPrivRead(DBPriv *db, const void *key, int key_size, void *dest, int dest_size) { if (!Lock(db)) { return false; } if (dpgetwb(db->depot, key, key_size, 0, dest_size, dest) == -1) { // FIXME: distinguish between "entry not found" and "failure to read" Log(LOG_LEVEL_DEBUG, "QDBM DBPrivRead: Could not read '%s', (dpgetwb: %s)", (const char *)key, dperrmsg(dpecode)); Unlock(db); return false; } Unlock(db); return true; }
DBPriv *DBPrivOpenDB(const char *filename, ARG_UNUSED dbid id) { DBPriv *db = xcalloc(1, sizeof(DBPriv)); pthread_mutex_init(&db->lock, NULL); pthread_mutex_init(&db->cursor_lock, NULL); db->depot = dpopen(filename, DP_OWRITER | DP_OCREAT, -1); if ((db->depot == NULL) && (dpecode == DP_EBROKEN)) { Log(LOG_LEVEL_ERR, "Database '%s' is broken, trying to repair...", filename); if (dprepair(filename)) { Log(LOG_LEVEL_INFO, "Successfully repaired database '%s'", filename); } else { Log(LOG_LEVEL_ERR, "Failed to repair database '%s', recreating...", filename); return DB_PRIV_DATABASE_BROKEN; } db->depot = dpopen(filename, DP_OWRITER | DP_OCREAT, -1); } if (db->depot == NULL) { Log(LOG_LEVEL_ERR, "dpopen: Opening database '%s' failed. (dpopen: %s)", filename, dperrmsg(dpecode)); pthread_mutex_destroy(&db->cursor_lock); pthread_mutex_destroy(&db->lock); free(db); return NULL; } return db; }
void DBPrivCloseDB(DBPriv *db) { int ret; if ((ret = pthread_mutex_destroy(&db->lock)) != 0) { errno = ret; Log(LOG_LEVEL_ERR, "Lock is still active during QDBM database handle close. (pthread_mutex_destroy: %s)", GetErrorStr()); } if ((ret = pthread_mutex_destroy(&db->cursor_lock)) != 0) { errno = ret; Log(LOG_LEVEL_ERR, "Cursor lock is still active during QDBM database handle close. (pthread_mutex_destroy: %s)", GetErrorStr()); } if (!dpclose(db->depot)) { Log(LOG_LEVEL_ERR, "Unable to close QDBM database. (dpclose: %s)", dperrmsg(dpecode)); } free(db); }
/* Close files and clean up. */ void db_close(void *vhandle) { dbh_t *handle = vhandle; VILLA *dbp; if (handle == NULL) return; if (DEBUG_DATABASE(1)) fprintf(dbgout, "(qdbm) vlclose(%s)\n", handle->name); dbp = handle->dbp; db_optimize(dbp, handle->name); if (!vlclose(dbp)) print_error(__FILE__, __LINE__, "(qdbm) vlclose for %s err: %d, %s", handle->name, dpecode, dperrmsg(dpecode)); handle->dbp = NULL; dbh_free(handle); }
JsonDb::Transaction::Transaction(std::string const &filename, ValuePointer const &_null_element) { /* The null element */ null_element = _null_element; /* open the database */ db = StorageDbPointer(vlopen(filename.c_str(), VL_OWRITER | VL_OCREAT, VL_CMPINT), CloseDatabase); /* Start the transaction */ vltranbegin(db.get()); if(db.get() == NULL) throw std::runtime_error((boost::format("Failed to open database: %s") % dperrmsg(dpecode)).str().c_str()); ValuePointer root = Retrieve(root_key); if(root.get() == NULL) { root = ValuePointer(new ValueObject(root_key)); Store(root->GetKey(), root); } ValuePointer next_id_value = Retrieve(next_id_key); if(next_id_value.get() == NULL) { // Initialize values next_id = initial_next_id; start_next_id = next_id; } else { // Retrieve values from database next_id = next_id_value->GetValueInt(); start_next_id = next_id; } // std::cout << "Start transaction, next id: " << next_id << std::endl; }
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; }
int main(void) { VILLA *villa; int size; char *s; printf("compareFunc(\"a\", \"b\") = %i\n", compareFunc("a", 1, "b", 1)); printf("compareFunc(\"b\", \"a\") = %i\n", compareFunc("b", 1, "a", 1)); printf("compareFunc(\"a\", \"a\") = %i\n", compareFunc("a", 1, "a", 1)); printf("compareFunc(\"b\", \"b\") = %i\n", compareFunc("b", 1, "b", 1)); remove("test.qdbm"); if(!(villa = vlopen("test.qdbm", VL_OWRITER | VL_OCREAT, compareFunc))) { printf("vlopen failed %s\n", dperrmsg(dpecode)); return 1; } if(!vltranbegin(villa)) { printf("vltranbegin failed %s\n", dperrmsg(dpecode)); return 1; } if(!vlput(villa, "a", 1, "1", 1, DP_DOVER)) { printf("vlput failed %s\n", dperrmsg(dpecode)); return 1; } if(!vlput(villa, "b", 1, "2", 1, DP_DOVER)) { printf("vlput failed %s\n", dperrmsg(dpecode)); return 1; } if(!vltrancommit(villa)) { printf("vltrancommit failed %s\n", dperrmsg(dpecode)); return 1; } if(!vlclose(villa)) { printf("vlclose failed %s\n", dperrmsg(dpecode)); return 1; } if(!(villa = vlopen("test.qdbm", VL_OWRITER | VL_OCREAT, compareFunc))) { printf("vlopen failed %s\n", dperrmsg(dpecode)); return 1; } s = vlget(villa, "a", 1, &size); printf("a = '%s'\n", s ? s : "NULL"); s = vlget(villa, "b", 1, &size); printf("b = '%s'\n", s ? s : "NULL"); if(!vlclose(villa)) { printf("vlclose failed %s\n", dperrmsg(dpecode)); return 1; } return 0; }
/* print an error message */ void pdperror(const char *name){ fprintf(stderr, "%s: %s: %s\n", progname, name, dperrmsg(dpecode)); }
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; }
const char *db_str_err(int e) { return dperrmsg(e); }