void DbPlugin::getNextBar (QDateTime &startDate, Bar &bar) { DBT key, data; DBC *cur; memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); DBBar dbbar; memset(&dbbar, 0, sizeof(DBBar)); data.data = &dbbar; data.ulen = sizeof(DBBar); data.flags = DB_DBT_USERMEM; db->cursor(db, NULL, &cur, 0); QString s = startDate.toString("yyyyMMddhhmmss"); key.data = (char *) s.latin1(); key.size = s.length() + 1; cur->c_get(cur, &key, &data, DB_SET_RANGE); int ret = cur->c_get(cur, &key, &data, DB_NEXT); if (ret) { // char *err = db_strerror(ret); // qDebug("%s %s", s.latin1(), err); cur->c_close(cur); return; } s = (char *) key.data; getBar(dbbar, s, bar); cur->c_close(cur); }
/** * e_dbhash_foreach_key: * @edbh: an #EDbHash * @func: a callback function * @user_data: data to pass to @func * * Calls @func for each database object. **/ void e_dbhash_foreach_key (EDbHash *edbh, EDbHashFunc func, gpointer user_data) { DB *db; DBT dkey; DBT ddata; DBC *dbc; gint db_error = 0; g_return_if_fail (edbh != NULL); g_return_if_fail (edbh->priv != NULL); g_return_if_fail (func != NULL); db = edbh->priv->db; db_error = db->cursor (db, NULL, &dbc, 0); if (db_error != 0) { return; } memset (&dkey, 0, sizeof (DBT)); memset (&ddata, 0, sizeof (DBT)); db_error = dbc->c_get (dbc, &dkey, &ddata, DB_FIRST); while (db_error == 0) { (*func) ((const gchar *) dkey.data, user_data); db_error = dbc->c_get (dbc, &dkey, &ddata, DB_NEXT); } dbc->c_close (dbc); }
int ftfs_bstore_scan_one_page(DB *data_db, struct ftfs_meta_key *meta_key, DB_TXN *txn, struct page *page) { int ret; uint64_t block_num; size_t block_offset; loff_t offset; size_t len; struct ftfs_data_key *data_key; void *buf, *value_buf; DBT key_dbt, value_dbt; DBC *cursor; offset = page_offset(page); block_num = block_get_num_by_position(offset); data_key = kmalloc(DATA_KEY_MAX_LEN, GFP_KERNEL); if (!data_key) return -ENOMEM; value_buf = kmalloc(FTFS_BSTORE_BLOCKSIZE, GFP_KERNEL); if (!value_buf) { ret = -ENOMEM; goto out1; } copy_data_key_from_meta_key(data_key, meta_key, block_num); dbt_init(&key_dbt, data_key, DATA_KEY_MAX_LEN); dbt_init(&value_dbt, value_buf, FTFS_BSTORE_BLOCKSIZE); len = PAGE_CACHE_SIZE; buf = kmap(page); ret = data_db->cursor(data_db, txn, &cursor, DB_CURSOR_FLAGS); if (ret) goto out2; ret = cursor->c_get(cursor, &key_dbt, &value_dbt, DB_SET_RANGE); while (len > 0 && ret) { ssize_t read_size; block_num = block_get_num_by_position(offset); block_offset = block_get_offset_by_position(offset); read_size = min(len, (FTFS_BSTORE_BLOCKSIZE - block_offset)); if (data_key->circle_id == meta_key->circle_id && !strcmp(data_key->path, meta_key->path) && data_key->block_num == block_num) { memcpy(buf, value_buf, read_size); ret = cursor->c_get(cursor, &key_dbt, &value_dbt, DB_NEXT); } else memset(buf, 0, read_size); buf += read_size; offset += read_size; len -= read_size; } cursor->c_close(cursor); out2: kunmap(page); out1: kfree(value_buf); data_key_free(data_key); return ret; }
bdb_lookup(const char *zone, const char *name, void *dbdata, dns_sdblookup_t *l) #endif /* DNS_CLIENTINFO_VERSION */ { int ret; char *type, *rdata; dns_ttl_t ttl; isc_consttextregion_t ttltext; DBC *c; DBT key, data; UNUSED(zone); #ifdef DNS_CLIENTINFO_VERSION UNUSED(methods); UNUSED(clientinfo); #endif /* DNS_CLIENTINFO_VERSION */ if ((ret = ((DB *)dbdata)->cursor((DB *)dbdata, NULL, &c, 0)) != 0) { isc_log_iwrite(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_SDB, ISC_LOG_ERROR, isc_msgcat, ISC_MSGSET_GENERAL, ISC_MSG_FAILED, "DB->cursor: %s", db_strerror(ret)); return ISC_R_FAILURE; } memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); (const char *)key.data = name; key.size = strlen(name); ret = c->c_get(c, &key, &data, DB_SET); while (ret == 0) { ((char *)key.data)[key.size] = 0; ((char *)data.data)[data.size] = 0; ttltext.base = strtok((char *)data.data, " "); ttltext.length = strlen(ttltext.base); dns_ttl_fromtext((isc_textregion_t *)&ttltext, &ttl); type = strtok(NULL, " "); rdata = type + strlen(type) + 1; if (dns_sdb_putrr(l, type, ttl, rdata) != ISC_R_SUCCESS) { isc_log_iwrite(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_SDB, ISC_LOG_ERROR, isc_msgcat, ISC_MSGSET_GENERAL, ISC_MSG_FAILED, "dns_sdb_putrr"); return ISC_R_FAILURE; } ret = c->c_get(c, &key, &data, DB_NEXT_DUP); } c->c_close(c); return ISC_R_SUCCESS; }
void DbPlugin::getHistory (BarData *barData, QDateTime &startDate) { switch (type) { case Index1: getIndexHistory(barData, startDate); barData->createDateList(); return; break; case Spread1: getSpreadHistory(barData, startDate); barData->createDateList(); return; break; case CC1: getCCHistory(barData, startDate); barData->createDateList(); return; break; default: break; } DBT key, data; DBC *cur; memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); DBBar dbbar; memset(&dbbar, 0, sizeof(DBBar)); data.data = &dbbar; data.ulen = sizeof(DBBar); data.flags = DB_DBT_USERMEM; db->cursor(db, NULL, &cur, 0); QString s = startDate.toString("yyyyMMddhhmmss"); key.data = (char *) s.latin1(); key.size = s.length() + 1; cur->c_get(cur, &key, &data, DB_SET_RANGE); while (! cur->c_get(cur, &key, &data, DB_PREV)) { if (barData->count() >= barRange) break; Bar bar; s = (char *) key.data; getBar(dbbar, s, bar); bar.setTickFlag(barData->getBarType()); barData->prepend(bar); } cur->c_close(cur); barData->createDateList(); }
/* Display all the stock quote information in the database. */ int print_stocks(DB *dbp) { DBC *dbc; DBT key, data; #define MAXKEYSIZE 10 #define MAXDATASIZE 20 char keybuf[MAXKEYSIZE + 1], databuf[MAXDATASIZE + 1]; int ret, t_ret; u_int32_t keysize, datasize; if ((ret = dbp->cursor(dbp, NULL, &dbc, 0)) != 0) { dbp->err(dbp, ret, "can't open cursor"); return (ret); } memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); printf("\tSymbol\tPrice\n"); printf("\t======\t=====\n"); for (ret = dbc->c_get(dbc, &key, &data, DB_FIRST); ret == 0; ret = dbc->c_get(dbc, &key, &data, DB_NEXT)) { keysize = key.size > MAXKEYSIZE ? MAXKEYSIZE : key.size; memcpy(keybuf, key.data, keysize); keybuf[keysize] = '\0'; datasize = data.size >= MAXDATASIZE ? MAXDATASIZE : data.size; memcpy(databuf, data.data, datasize); databuf[datasize] = '\0'; printf("\t%s\t%s\n", keybuf, databuf); } printf("\n"); fflush(stdout); if ((t_ret = dbc->c_close(dbc)) != 0 && ret == 0) ret = t_ret; switch (ret) { case 0: case DB_NOTFOUND: return (0); case DB_LOCK_DEADLOCK: return (0); default: return (ret); } }
int store_list(const char *key, int (*add_file)(const char *, void *), void *opaque, struct acrd_txid *txid) { DBT db_key, db_data; int ret; DB_TXN *tid = NULL; DBC *cursor; if (txid) tid = txid->tid; ret = dbp->cursor(dbp, tid, &cursor, DB_DIRTY_READ); if (ret) { eprintf("failed open a cursor\n"); return ACRD_ERR_UNKNOWN; } memset(&db_key, 0, sizeof(db_key)); memset(&db_data, 0, sizeof(db_data)); if (key) { db_key.data = (void *)key; db_key.size = strlen(key) + 1; ret = cursor->c_get(cursor, &db_key, &db_data, DB_SET_RANGE); } else ret = cursor->c_get(cursor, &db_key, &db_data, DB_FIRST); while (ret == 0) { if (key && strncmp(db_key.data, key, strlen(key)) != 0) /* prefix doesn't match */ break; ret = add_file(db_key.data, opaque); if (ret != 0) break; ret = cursor->c_get(cursor, &db_key, &db_data, DB_NEXT); }; if (ret == 0 || ret == DB_NOTFOUND) ret = ACRD_SUCCESS; else { envp->err(envp, ret, "store_list failed\n"); ret = ACRD_ERR_UNKNOWN; } cursor->close(cursor); return ret; }
/** * e_book_backend_db_cache_get_contacts: * @db: DB Handle * @query: an s-expression * * Returns a list of #EContact elements from @cache matching @query. * When done with the list, the caller must unref the contacts and * free the list. * * Returns: A #GList of pointers to #EContact. **/ GList * e_book_backend_db_cache_get_contacts (DB *db, const gchar *query) { DBC *dbc; DBT uid_dbt, vcard_dbt; gint db_error; GList *list = NULL; EBookBackendSExp *sexp = NULL; EContact *contact; if (query) { sexp = e_book_backend_sexp_new (query); if (!sexp) return NULL; } db_error = db->cursor (db, NULL, &dbc, 0); if (db_error != 0) { g_warning ("db->cursor failed with %d", db_error); if (sexp) g_object_unref (sexp); return NULL; } memset (&vcard_dbt, 0 , sizeof (vcard_dbt)); memset (&uid_dbt, 0, sizeof (uid_dbt)); db_error = dbc->c_get (dbc, &uid_dbt, &vcard_dbt, DB_FIRST); while (db_error == 0) { if (vcard_dbt.data && !strncmp (vcard_dbt.data, "BEGIN:VCARD", 11)) { contact = e_contact_new_from_vcard (vcard_dbt.data); if (!sexp || e_book_backend_sexp_match_contact (sexp, contact)) list = g_list_prepend (list, contact); else g_object_unref (contact); } db_error = dbc->c_get (dbc, &uid_dbt, &vcard_dbt, DB_NEXT); } db_error = dbc->c_close (dbc); if (db_error != 0) g_warning ("db->c_close failed with %d", db_error); if (sexp) g_object_unref (sexp); return g_list_reverse (list); }
int dump_db() { DBC *dbc; DBT db_key, db_data; int ret, i; void *store; void **store_index; printf("Going to dump database!\n"); if(key_ops != &ci_str_ops ||val_ops != &ci_str_ops) { ci_debug_printf(1, "can not dump not string databases\n"); return 0; } if ((ret = db->cursor(db, NULL, &dbc, 0)) != 0) { ci_debug_printf(1, "error creating cursor\n"); return 0; } memset(&db_data, 0, sizeof(db_data)); memset(&db_key, 0, sizeof(db_key)); if ((ret = dbc->c_get(dbc, &db_key, &db_data, DB_SET_RANGE)) != 0){ ci_debug_printf(1, "error getting first element of DB : %s\n", db_strerror(ret)); dbc->c_close(dbc); return 0; } do { printf("%s :", (char *)db_key.data ); if (db_data.data) { store = db_data.data; store_index = store; for(i=0; store_index[i]!=0; i++) { store_index[i]+=(long int)store; } for(i=0; store_index[i]!=0; i++) { printf("%s |", (char *)store_index[i]); } } printf("\n"); ret = dbc->c_get(dbc, &db_key, &db_data, DB_NEXT); } while (ret==0); dbc->c_close(dbc); return 1; }
bool Bigram::get_all_items(GArray * items){ g_array_set_size(items, 0); if ( !m_db ) return false; DBC * cursorp = NULL; DBT key, data; int ret; /* Get a cursor */ m_db->cursor(m_db, NULL, &cursorp, 0); /* Initialize our DBTs. */ memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); /* Iterate over the database, retrieving each record in turn. */ while ((ret = cursorp->c_get(cursorp, &key, &data, DB_NEXT)) == 0) { assert(key.size == sizeof(phrase_token_t)); phrase_token_t * token = (phrase_token_t *)key.data; g_array_append_val(items, *token); } assert (ret == DB_NOTFOUND); /* Cursors must be closed */ if (cursorp != NULL) cursorp->c_close(cursorp); return true; }
static int dbiCursorDel(dbiCursor dbc, DBT * key, DBT * data, unsigned int flags) { int rc = EINVAL; int sane = (key->data != NULL && key->size > 0); if (dbc && sane) { DBC * cursor = dbc->cursor; int _printit; rpmdb rdb = dbc->dbi->dbi_rpmdb; rpmswEnter(&rdb->db_delops, 0); /* XXX TODO: insure that cursor is positioned with duplicates */ rc = cursor->c_get(cursor, key, data, DB_SET); /* XXX DB_NOTFOUND can be returned */ _printit = (rc == DB_NOTFOUND ? 0 : _debug); rc = cvtdberr(dbc->dbi, "dbcursor->c_get", rc, _printit); if (rc == 0) { rc = cursor->c_del(cursor, flags); rc = cvtdberr(dbc->dbi, "dbcursor->c_del", rc, _debug); } rpmswExit(&rdb->db_delops, data->size); } return rc; }
/* Silently does nothing if the key/value isn't found. Can't use auto-commit here! */ int db_del_kv(DB *db, DB_TXN *tid, unsigned char *key, u_int32_t key_size, unsigned char *value, u_int32_t value_size) { DBT DBTKey, DBTValue; DBC *cursor; int ret, c_ret; memset(&DBTKey, 0, sizeof(DBT)); DBTKey.data = key; DBTKey.size = key_size; memset(&DBTValue, 0, sizeof(DBT)); DBTValue.data = value; DBTValue.size = value_size; if ((ret = db->cursor(db, tid, &cursor, 0)) != 0) return ret; if ((ret = cursor->c_get(cursor, &DBTKey, &DBTValue, DB_GET_BOTH)) != 0) goto fail; ret = cursor->c_del(cursor, 0); fail: if ((c_ret = cursor->c_close(cursor)) != 0) return c_ret; return ret; }
int db_walk(void *dbh, int (*walk_func)(char *key, void *data, int len)) { DBT key, data; DBC *dbc; int rc = 0; GET_DB(dbh); if (!walk_func) return -EINVAL; if ((rc = db->cursor(db, NULL, &dbc, 0))) { db->close(db, 0); return rc; } memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); while (dbc->c_get(dbc, &key, &data, DB_NEXT) == 0) if ((rc = walk_func(key.data, data.data, data.size))) break; dbc->c_close(dbc); return rc; }
static void scanscan_hwc (void) { int r; int counter=0; for (counter=0; counter<n_experiments; counter++) { long long totalbytes=0; int rowcounter=0; double prevtime = gettime(); DBT k,v; DBC *dbc; r = db->cursor(db, tid, &dbc, 0); assert(r==0); memset(&k, 0, sizeof(k)); memset(&v, 0, sizeof(v)); u_int32_t c_get_flags = DB_NEXT; if (prelockflag && (counter || prelock)) { c_get_flags |= lock_flag; } while (0 == (r = dbc->c_get(dbc, &k, &v, c_get_flags))) { //printf("r=%d\n", r); totalbytes += k.size + v.size; rowcounter++; if (limitcount>0 && rowcounter>=limitcount) break; } assert(r==DB_NOTFOUND); r = dbc->c_close(dbc); assert(r==0); double thistime = gettime(); double tdiff = thistime-prevtime; printf("Scan %lld bytes (%d rows) in %9.6fs at %9fMB/s\n", totalbytes, rowcounter, tdiff, 1e-6*totalbytes/tdiff); } }
int show_koef_db(DB *dbp) { DBT key, data; DBC *cursorp; struct kdb_key *kdbkeyp; struct kdb_data *kdbdatap; int ret; dbp->cursor(dbp, NULL, &cursorp, 0); /* Initialize our DBTs. */ memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); while ((ret = cursorp->c_get(cursorp, &key, &data, DB_NEXT)) == 0) { kdbkeyp = (struct kdb_key *) key.data; kdbdatap = (struct kdb_data *) data.data; fprintf(stdout,"%i : %x : %f\t%f\n", kdbkeyp->asdu, kdbkeyp->ioa2, kdbdatap->koef_u, kdbdatap->koef_i ); } return 0; }
static int pctlmfdb_stat(struct pctldb_st *pctldb, int *dberror){ int status = 0; DB* mfdbp = pctldb->mfdbp; DBC *cursor; DBT key, data; uint32_t memfile_size; *dberror = mfdbp->cursor(mfdbp, NULL, &cursor, 0); if(*dberror != 0) return(-1); memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); pctldb->mf_total_size = 0; while(status == 0){ status = cursor->c_get(cursor, &key, &data, DB_NEXT); if(status == 0){ memfile_size = data.size; pctldb->mf_total_size += memfile_size; } } cursor->c_close(cursor); if(status == DB_NOTFOUND) status = 0; else{ *dberror = status; status = -1; } return(status); }
int userToSubname_deletecol(struct userToSubnameDbFormat *userToSubnameDb,char subname[]) { DBC *cursorp; DBT key, data; int ret; debug("userToSubname_deletecol: deleting \"%s\"", subname); /* Get a cursor */ (*userToSubnameDb).dbp->cursor((*userToSubnameDb).dbp, NULL, &cursorp, 0); //resetter minne memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); /* Iterate over the database, retrieving each record in turn. */ while ((ret = cursorp->c_get(cursorp, &key, &data, DB_NEXT)) == 0) { /* Do interesting things with the DBTs here. */ if (!strcmp(subname, data.data)) { debug("Deleting %s->%s (ret=%i)", (char*)key.data, (char*)data.data, cursorp->c_del(cursorp, 0)); } else { debug("Keeping %s->%s", (char*)key.data, (char*)data.data); } } /* Close the cursor */ if (cursorp != NULL) { cursorp->c_close(cursorp); } return 1; }
void auth_db_clean(int sig) { DBC *dbc; DBT key, data; int ret; time_t now=time(NULL); time_t max=(time_t)conffile_param_int("timeout"); logger(LOG_NOTICE, "cleaning db file\n"); // apparently i should use DB_WRITECURSOR as a flag here // but the version 3 db barfs on me when i do that if((ret = db->cursor(db, NULL, &dbc, 0)) != 0) { db->err(db, ret, "opening cursor"); exit(22); } memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); while((ret = dbc->c_get(dbc, &key, &data, DB_NEXT)) ==0) { time_t then = (time_t)data.data; if(now - then > max) { if((ret = dbc->c_del(dbc, 0)) != 0) { db->err(db, ret, "deleting key"); exit(22); } } } logger(LOG_DEBUG,"Finished cleaning cycle\n"); }
void auth_db_dump() { DBC *dbc; DBT key, data; int ret; FILE *f=fopen(conffile_param("dumpfile"),"w"); if(!f) { logger(LOG_ERR, "Unable to write to dump file %s\n", conffile_param("dumpfile")); return; } chmod(conffile_param("dumpfile"),0640); logger(LOG_NOTICE, "dumping state\n"); if((ret = db->cursor(db, NULL, &dbc, 0)) != 0) { db->err(db, ret, "opening cursor"); exit(22); } memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); while((ret = dbc->c_get(dbc, &key, &data, DB_NEXT)) ==0) { char tbuff[1024]; char *hostname=key.data; time_t *time=data.data; strftime(tbuff, 1023, "%Y-%m-%d %H:%M:%S", localtime(time)); fprintf(f, "%s\t%s\n", hostname, tbuff); } fclose(f); }
int db_get_instances(int node_no, const char *app, const char *key, void (*func)(char *str, size_t size)) { struct databases db; DBC *cursor = NULL; DBT pair[2]; int e, ret = -1; char *match; memset(&pair[0], 0, sizeof(DBT) * 2); if (open_env(&db, PROFILE_DB)) goto out; blaa = &db; db.profile->cursor(db.profile, NULL, &cursor, 0); // FIXME: multiple instance keys? match = make_profile_key(node_no, app, key, NULL); if (!match) goto out; while ((e = cursor->c_get(cursor, &pair[0], &pair[1], DB_NEXT)) == 0) { if (strncmp(match, pair[0].data, strlen(match)) == 0) func(((char *) pair[0].data) + strlen(match), pair[0].size - strlen(match)); } if (e != DB_NOTFOUND) { goto out; } ret = 0; out: blaa = NULL; if (cursor) cursor->c_close(cursor); close_env(&db); return ret; }
int scan_db(DB *dbp) { DBC *cursor; int ret, num_recs = 0; DBT key, data; ret = dbp->cursor(dbp, 0, &cursor, 0); if(ret) { db_error("DB->cursor", ret); return ret; } memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); while( (ret = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) num_recs++; if(ret == DB_NOTFOUND) printf("Scan of %s completed successfully. Scanned %d records\n", dbp->fname, num_recs); else { db_error("DBC->get", ret); return ret; } cursor->c_close(cursor); return 0; }
void DbPlugin::getAllBars (BarData *bars) { DBT key, data; DBC *cur; memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); DBBar dbbar; memset(&dbbar, 0, sizeof(DBBar)); data.data = &dbbar; data.ulen = sizeof(DBBar); data.flags = DB_DBT_USERMEM; db->cursor(db, NULL, &cur, 0); while (! cur->c_get(cur, &key, &data, DB_PREV)) { Bar bar; QString s = (char *) key.data; getBar(dbbar, s, bar); bar.setTickFlag(bars->getBarType()); bars->prependRaw(bar); } cur->c_close(cur); }
static int dbiCursorGet(dbiCursor dbc, DBT * key, DBT * data, unsigned int flags) { int rc = EINVAL; int sane = ((flags == DB_NEXT) || (key->data != NULL && key->size > 0)); if (dbc && sane) { DBC * cursor = dbc->cursor; rpmdb rdb = dbc->dbi->dbi_rpmdb; int _printit; rpmswEnter(&rdb->db_getops, 0); /* XXX db4 does DB_FIRST on uninitialized cursor */ rc = cursor->c_get(cursor, key, data, flags); /* XXX DB_NOTFOUND can be returned */ _printit = (rc == DB_NOTFOUND ? 0 : _debug); rc = cvtdberr(dbc->dbi, "dbcursor->c_get", rc, _printit); /* Remember the last key fetched */ if (rc == 0) { dbc->key = key->data; dbc->keylen = key->size; } else { dbc->key = NULL; dbc->keylen = 0; } rpmswExit(&rdb->db_getops, data->size); } return rc; }
void ReadFromDBAndPutInFile(FILE *outFile) { pwr_tInt32 ret, sts; pwr_tUInt32 nrOfEvents = 0; sEvent *eventp; DBT data, key; DBC *dbcp; WriteColumnNames(outFile); /* Acquire a cursor for the database. */ if ((ret = dataBaseP->cursor(dataBaseP, NULL, &dbcp, 0)) != 0) { printf("error dataBaseP->cursor: %s\n", db_strerror(ret)); exit(1); } /* Initialize the key/data return pair. */ memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); if((ret = dbcp->c_get(dbcp, &key, &data, DB_FIRST)) == 0) { eventp = data.data; sts = Write(eventp, outFile); nrOfEvents++; } while((ret = dbcp->c_get(dbcp, &key, &data, DB_NEXT)) == 0) { eventp = data.data; sts = Write(eventp, outFile); nrOfEvents++; } if(ret != DB_NOTFOUND) { printf("error dbcp->c_get: %s\n", db_strerror(ret)); printf("** Error reading post number %u, terminating\n", nrOfEvents); exit(1); } printf("-- Number of events written to file: %u\n", nrOfEvents); /*Close the cursor*/ if((ret = dbcp->c_close(dbcp)) != 0) { printf("Error dbcp->c_close(): %s\n", db_strerror(ret)); } }
/* estimate average component length for components (approx) */ double se_get_avg_component_len(component_list_entry *comp) { DBT key, data; DBC *dbcursor; component_data_item comp_data; int comp_id; int n, numrecs; int errno; double sum_size, avg_size; /* the component database should already be opened before this */ /* function is called */ if (comp == NULL || comp->comp_db == NULL) return (0.0); /* no data */ /* Acquire a cursor for the database. */ if ((errno = comp->comp_db->cursor(comp->comp_db, NULL, &dbcursor, 0)) != 0) { fprintf(stderr, "se_get_avg_component_len: cursor open error: %s\n", strerror(errno)); return(0.0); } /* Initialize the key/data pair so the flags aren't set. */ memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); /* Walk through the database and get the start and end info for comp */ numrecs = 100; n = 0; while ((errno = dbcursor->c_get(dbcursor, &key, &data, DB_NEXT)) == 0 || numrecs > 0) { numrecs--; n++; if (key.size == sizeof(int)) { memcpy((char *)&comp_data,data.data,data.size); sum_size += (double)(comp_data.end_offset - comp_data.start_offset); } } (void)dbcursor->c_close(dbcursor); if (n > 0) { avg_size = sum_size/(double)n; } return(avg_size); }
static void run_expiry() { DBC *dbcp; int rc; time_t now; unsigned int count = 0; /* Cursor operations can hold several locks and therefore deadlock so don't run expiry if deadlock detection does not work http://www.oracle.com/technology/documentation/berkeley-db/db/ref/lock/notxn.html */ if (db == 0 || deadlock_detect == 0) return; if (time(&now) == (time_t)-1) { syslog(LOG_ERR, "time failed during run_expiry"); return; } muffle_error++; if (rc = db->cursor(db, 0, &dbcp, 0)) log_db_error("db->cursor failed during expiry run", rc); else { DBT key = { 0 }; while ((rc = dbcp->c_get(dbcp, &key, &dbdata, DB_NEXT | DB_RMW)) == 0) { time_t ref_time; double age_max, age; if (triplet_data.pass_count) { ref_time = triplet_data.access_time; age_max = pass_max_idle; } else { ref_time = triplet_data.create_time; age_max = bloc_max_idle; } age = difftime(now, ref_time); if (age > age_max) { if (opt_verbose) syslog(LOG_INFO, "Expiring %s %s after %.0f seconds idle", key.data, triplet_data.pass_count ? "pass" : "block", age); if (rc = dbcp->c_del(dbcp, 0)) log_db_error("dbcp->c_del failed", rc); else count++; } } if (rc == DB_LOCK_DEADLOCK) syslog(LOG_DEBUG, "skipping concurrent expiry avoids " "deadlocks and unnecessary work"); else if (rc != DB_NOTFOUND) log_db_error("dbcp->c_get failed", rc); if (rc = dbcp->c_close(dbcp)) log_db_error("dbcp->c_close failed", rc); } muffle_error--; if (count) syslog(LOG_NOTICE, "Expired %u triplets", count); }
int hdb_dn2id_parent( Operation *op, DB_TXN *txn, EntryInfo *ei, ID *idp ) { struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private; DB *db = bdb->bi_dn2id->bdi_db; DBT key, data; DBC *cursor; int rc = 0; diskNode *d; char *ptr; ID nid; DBTzero(&key); key.size = sizeof(ID); key.data = &nid; key.ulen = sizeof(ID); key.flags = DB_DBT_USERMEM; BDB_ID2DISK( ei->bei_id, &nid ); DBTzero(&data); data.flags = DB_DBT_USERMEM; rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags ); if ( rc ) return rc; data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2); d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx ); data.data = d; rc = cursor->c_get( cursor, &key, &data, DB_SET ); if ( rc == 0 ) { if (d->nrdnlen[0] & 0x80) { rc = LDAP_OTHER; } else { db_recno_t dkids; ptr = (char *) data.data + data.size - sizeof(ID); BDB_DISK2ID( ptr, idp ); ei->bei_nrdn.bv_len = (d->nrdnlen[0] << 8) | d->nrdnlen[1]; ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn ); ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - ei->bei_nrdn.bv_len; ptr = d->nrdn + ei->bei_nrdn.bv_len + 1; ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn ); /* How many children does this node have? */ cursor->c_count( cursor, &dkids, 0 ); ei->bei_dkids = dkids; } } cursor->c_close( cursor ); op->o_tmpfree( d, op->o_tmpmemctx ); return rc; }
bool Bigram::load_db(const char * dbfile){ reset(); /* create in memory db. */ int ret = db_create(&m_db, NULL, 0); assert(ret == 0); ret = m_db->open(m_db, NULL, NULL, NULL, DB_HASH, DB_CREATE, 0600); if ( ret != 0 ) return false; /* load db into memory. */ DB * tmp_db = NULL; ret = db_create(&tmp_db, NULL, 0); assert(ret == 0); if (NULL == tmp_db) return false; ret = tmp_db->open(tmp_db, NULL, dbfile, NULL, DB_HASH, DB_RDONLY, 0600); if ( ret != 0 ) return false; DBC * cursorp = NULL; DBT key, data; /* Get a cursor */ tmp_db->cursor(tmp_db, NULL, &cursorp, 0); if (NULL == cursorp) return false; /* Initialize our DBTs. */ memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); /* Iterate over the database, retrieving each record in turn. */ while ((ret = cursorp->c_get(cursorp, &key, &data, DB_NEXT)) == 0) { int ret = m_db->put(m_db, NULL, &key, &data, 0); assert(ret == 0); } assert (ret == DB_NOTFOUND); /* Cursors must be closed */ if ( cursorp != NULL ) cursorp->c_close(cursorp); if ( tmp_db != NULL ) tmp_db->close(tmp_db, 0); return true; }
static void scanscan_range (void) { int r; double texperiments[n_experiments]; u_int64_t k = 0; char kv[8]; DBT key, val; int counter; for (counter = 0; counter < n_experiments; counter++) { if (1) { //if ((counter&1) == 0) { makekey: // generate a random key in the key range k = (start_range + (random() % (end_range - start_range))) * (1<<6); int i; for (i = 0; i < 8; i++) kv[i] = k >> (56-8*i); } memset(&key, 0, sizeof key); key.data = &kv, key.size = sizeof kv; memset(&val, 0, sizeof val); double tstart = gettime(); DBC *dbc; r = db->cursor(db, tid, &dbc, 0); assert(r==0); // set the cursor to the random key r = dbc->c_get(dbc, &key, &val, DB_SET_RANGE+lock_flag); if (r != 0) { assert(r == DB_NOTFOUND); printf("%s:%d %" PRIu64 "\n", __FUNCTION__, __LINE__, k); goto makekey; } #ifdef TOKUDB // do the range scan long rowcounter = 0; struct extra_count e = {0,0}; while (limitcount > 0 && rowcounter < limitcount) { r = dbc->c_getf_next(dbc, prelockflag ? lock_flag : 0, counttotalbytes, &e); if (r != 0) break; rowcounter++; } #endif r = dbc->c_close(dbc); assert(r==0); texperiments[counter] = gettime() - tstart; printf("%" PRIu64 " %f\n", k, texperiments[counter]); fflush(stdout); }
/* FIXME should this be wrapped in an explicit transaction? */ static void do_dump_triplets() { DB *db; DBC *dbcp; int rc; DBT key = { 0 }, data = { 0 }; rc = get_db(&db, 1); if (rc) { fprintf(stderr, "DBD-%d: failed to open database\n", rc); return; } rc = db->cursor(db, 0, &dbcp, 0); if (rc) fprintf(stderr, "DBD-%d: db->cursor failed: %s\n", rc, db_strerror(rc)); else { while (! exit_requested && (rc = dbcp->c_get(dbcp, &key, &data, DB_NEXT)) == 0) { const char *const start = key.data, *s = start; const struct triplet_data *t = data.data; printf("%d\t", t->crypted); printf("%s\t", db_key_ntop(s)); switch ((enum dbkey_type_enum)*s++) { case DBKEY_T_RAW: s += strlen(s) + 1; break; case DBKEY_T_IP4: s += IPV4_BITS / 8; break; case DBKEY_T_IP6: s += IPV6_BITS / 8; break; } printf("%s\t", s); s += strlen(s) + 1; fwrite(s, 1, key.size - (s - start), stdout); putchar('\t'); write_ctime(&t->create_time); putchar('\t'); write_ctime(&t->access_time); printf("\t%lu\t%lu\n", t->block_count, t->pass_count); } if (rc && rc != DB_NOTFOUND) fprintf(stderr, "DBD-%d: dbcp->c_get failed: %s\n", rc, db_strerror(rc)); rc = dbcp->c_close(dbcp); if (rc) fprintf(stderr, "DBD-%d: dbcp->c_close failed: %s\n", rc, db_strerror(rc)); } }