int TpcbExample::populateHistory(DB *dbp, int nrecs, u_int32_t accounts, u_int32_t branches, u_int32_t tellers) { DBT kdbt, ddbt; Histrec hrec; memset(&hrec.pad[0], 1, sizeof(hrec.pad)); hrec.amount = 10; db_recno_t key; memset(&kdbt, 0, sizeof(kdbt)); memset(&ddbt, 0, sizeof(ddbt)); kdbt.data = &key; kdbt.size = sizeof(u_int32_t); ddbt.data = &hrec; ddbt.size = sizeof(hrec); for (int i = 1; i <= nrecs; i++) { hrec.aid = randomId(ACCOUNT, accounts, branches, tellers); hrec.bid = randomId(BRANCH, accounts, branches, tellers); hrec.tid = randomId(TELLER, accounts, branches, tellers); int err; key = (db_recno_t)i; if ((err = dbp->put(dbp, NULL, &kdbt, &ddbt, DB_APPEND)) != 0) { _snprintf(msgString, ERR_STRING_MAX, "Failure initializing history file: %s.", db_strerror(err)); return (1); } } return (0); }
ZString WebSession::generateId(){ string tmpid = randomId(20); QDir dir(SESSION_PATH); QList<QFileInfo> flist = dir.entryInfoList(); for(int i = 0; i < flist.size(); ++i){ if(flist[i].isFile()){ if(flist[i].fileName() == ZString(tmpid).QS()){ tmpid = randomId(20); i = -1; } } } return ZString(tmpid); }
// // XXX Figure out the appropriate way to pick out IDs. // int TpcbExample::txn(DB *adb, DB *bdb, DB *tdb, DB *hdb, int accounts, int branches, int tellers) { DBC *acurs, *bcurs, *tcurs; DB_TXN *t; DBT d_dbt, d_histdbt, k_dbt, k_histdbt; db_recno_t key; Defrec rec; Histrec hrec; int account, branch, teller, ret; memset(&d_dbt, 0, sizeof(d_dbt)); memset(&d_histdbt, 0, sizeof(d_histdbt)); memset(&k_dbt, 0, sizeof(k_dbt)); memset(&k_histdbt, 0, sizeof(k_histdbt)); k_histdbt.data = &key; k_histdbt.size = sizeof(key); // !!! // This is sample code -- we could move a lot of this into the driver // to make it faster. // account = randomId(ACCOUNT, accounts, branches, tellers); branch = randomId(BRANCH, accounts, branches, tellers); teller = randomId(TELLER, accounts, branches, tellers); k_dbt.size = sizeof(int); d_dbt.flags |= DB_DBT_USERMEM; d_dbt.data = &rec; d_dbt.ulen = sizeof(rec); hrec.aid = account; hrec.bid = branch; hrec.tid = teller; hrec.amount = 10; // Request 0 bytes since we're just positioning. d_histdbt.flags |= DB_DBT_PARTIAL; // START PER-TRANSACTION TIMING. // // Technically, TPCB requires a limit on response time, you only get // to count transactions that complete within 2 seconds. That's not // an issue for this sample application -- regardless, here's where // the transaction begins. if (dbenv->txn_begin(dbenv, NULL, &t, 0) != 0) goto err; if (adb->cursor(adb, t, &acurs, 0) != 0 || bdb->cursor(bdb, t, &bcurs, 0) != 0 || tdb->cursor(tdb, t, &tcurs, 0) != 0) goto err; // Account record k_dbt.data = &account; if (acurs->get(acurs, &k_dbt, &d_dbt, DB_SET) != 0) goto err; rec.balance += 10; if (acurs->put(acurs, &k_dbt, &d_dbt, DB_CURRENT) != 0) goto err; // Branch record k_dbt.data = &branch; if (bcurs->get(bcurs, &k_dbt, &d_dbt, DB_SET) != 0) goto err; rec.balance += 10; if (bcurs->put(bcurs, &k_dbt, &d_dbt, DB_CURRENT) != 0) goto err; // Teller record k_dbt.data = &teller; if (tcurs->get(tcurs, &k_dbt, &d_dbt, DB_SET) != 0) goto err; rec.balance += 10; if (tcurs->put(tcurs, &k_dbt, &d_dbt, DB_CURRENT) != 0) goto err; // History record d_histdbt.flags = 0; d_histdbt.data = &hrec; d_histdbt.ulen = sizeof(hrec); if (hdb->put(hdb, t, &k_histdbt, &d_histdbt, DB_APPEND) != 0) goto err; if (acurs->close(acurs) != 0 || bcurs->close(bcurs) != 0 || tcurs->close(tcurs) != 0) goto err; ret = t->commit(t, 0); t = NULL; if (ret != 0) goto err; // END PER-TRANSACTION TIMING. return (0); err: if (acurs != NULL) (void)acurs->close(acurs); if (bcurs != NULL) (void)bcurs->close(bcurs); if (tcurs != NULL) (void)tcurs->close(tcurs); if (t != NULL) (void)t->abort(t); return (-1); }