Example #1
0
/* Initialize the database. */
void BulkExample::initDb(int dups, int sflag, int pagesize) {

	DbTxn *txnp;
	int ret;

	txnp = NULL;
	ret = 0;

	dbp = new Db(dbenv, 0);

	dbp->set_error_stream(&cerr);
	dbp->set_errpfx(progname);

	try{
		if ((ret = dbp->set_bt_compare(compare_int)) != 0)
			throwException(dbenv, NULL, ret, "DB->set_bt_compare");

		if ((ret = dbp->set_pagesize(pagesize)) != 0)
			throwException(dbenv, NULL, ret, "DB->set_pagesize");

		if (dups && (ret = dbp->set_flags(DB_DUP)) != 0)
			throwException(dbenv, NULL, ret, "DB->set_flags");

		if ((ret = dbenv->txn_begin(NULL, &txnp, 0)) != 0)
			throwException(dbenv, NULL, ret, "DB_ENV->txn_begin");

		if ((ret = dbp->open(txnp, DATABASE, "primary", DB_BTREE,
		    DB_CREATE, 0664)) != 0)
			throwException(dbenv, txnp, ret, "DB->open");

		if (sflag) {
			sdbp = new Db(dbenv, 0);

			if ((ret = sdbp->set_flags(DB_DUPSORT)) != 0)
				throwException(dbenv, txnp,
				    ret, "DB->set_flags");

			if ((ret = sdbp->open(txnp, DATABASE, "secondary",
			    DB_BTREE, DB_CREATE, 0664)) != 0)
				throwException(dbenv, txnp, ret, "DB->open");

			if ((ret =  dbp->associate(
			    txnp, sdbp, get_first_str, 0)) != 0)
				throwException(dbenv, txnp,
				    ret, "DB->associate");
		}

		ret = txnp->commit(0);
		txnp = NULL;
		if (ret != 0)
			throwException(dbenv, NULL, ret, "DB_TXN->commit");
	} catch(DbException &dbe) {
		cerr << "initDb " << dbe.what() << endl;
		if (txnp != NULL)
			(void)txnp->abort();
		throw dbe;
	}
}
Example #2
0
//
// Initialize the database to the specified number of accounts, branches,
// history records, and tellers.
//
void
TpcbExample::populate(int accounts, int branches, int history, int tellers)
{
	Db *dbp;

	int err;
	u_int32_t balance, idnum;
	u_int32_t end_anum, end_bnum, end_tnum;
	u_int32_t start_anum, start_bnum, start_tnum;

	idnum = BEGID;
	balance = 500000;

	dbp = new Db(this, 0);
	dbp->set_h_nelem((unsigned int)accounts);

	if ((err = dbp->open(NULL, "account", NULL, DB_HASH,
			     DB_CREATE, 0644)) != 0) {
		DbException except("Account file create failed", err);
		throw except;
	}

	start_anum = idnum;
	populateTable(dbp, idnum, balance, accounts, "account");
	idnum += accounts;
	end_anum = idnum - 1;
	if ((err = dbp->close(0)) != 0) {
		DbException except("Account file close failed", err);
		throw except;
	}
	delete dbp;
	if (verbose)
		cout << "Populated accounts: "
		     << (long)start_anum << " - " << (long)end_anum << "\n";

	dbp = new Db(this, 0);
	//
	// Since the number of branches is very small, we want to use very
	// small pages and only 1 key per page.  This is the poor-man's way
	// of getting key locking instead of page locking.
	//
	dbp->set_h_ffactor(1);
	dbp->set_h_nelem((unsigned int)branches);
	dbp->set_pagesize(512);

	if ((err = dbp->open(NULL, "branch", NULL, DB_HASH,
			     DB_CREATE, 0644)) != 0) {
		DbException except("Branch file create failed", err);
		throw except;
	}
	start_bnum = idnum;
	populateTable(dbp, idnum, balance, branches, "branch");
	idnum += branches;
	end_bnum = idnum - 1;
	if ((err = dbp->close(0)) != 0) {
		DbException except("Close of branch file failed", err);
		throw except;
	}
	delete dbp;

	if (verbose)
		cout << "Populated branches: "
		     << (long)start_bnum << " - " << (long)end_bnum << "\n";

	dbp = new Db(this, 0);
	//
	// In the case of tellers, we also want small pages, but we'll let
	// the fill factor dynamically adjust itself.
	//
	dbp->set_h_ffactor(0);
	dbp->set_h_nelem((unsigned int)tellers);
	dbp->set_pagesize(512);

	if ((err = dbp->open(NULL, "teller", NULL, DB_HASH,
			     DB_CREATE, 0644)) != 0) {
		DbException except("Teller file create failed", err);
		throw except;
	}

	start_tnum = idnum;
	populateTable(dbp, idnum, balance, tellers, "teller");
	idnum += tellers;
	end_tnum = idnum - 1;
	if ((err = dbp->close(0)) != 0) {
		DbException except("Close of teller file failed", err);
		throw except;
	}
	delete dbp;
	if (verbose)
		cout << "Populated tellers: "
		     << (long)start_tnum << " - " << (long)end_tnum << "\n";

	dbp = new Db(this, 0);
	dbp->set_re_len(HISTORY_LEN);
	if ((err = dbp->open(NULL, "history", NULL, DB_RECNO,
			     DB_CREATE, 0644)) != 0) {
		DbException except("Create of history file failed", err);
		throw except;
	}

	populateHistory(dbp, history, accounts, branches, tellers);
	if ((err = dbp->close(0)) != 0) {
		DbException except("Close of history file failed", err);
		throw except;
	}
	delete dbp;
}
Example #3
0
//
// Initialize the database to the specified number of accounts, branches,
// history records, and tellers.
//
void
StlTpcbExample::populate(int accounts, int branches, int history, int tellers)
{
	Db *dbp;
	DefrecMap *accounts_map, *branches_map, *tellers_map;
	HistrecVector *history_vector;

	int err, oflags;
	u_int32_t balance, idnum;
	u_int32_t end_anum, end_bnum, end_tnum;
	u_int32_t start_anum, start_bnum, start_tnum;

	idnum = BEGID;
	balance = 500000;
	oflags = DB_CREATE;

	dbp = new Db(this, DB_CXX_NO_EXCEPTIONS);
	dbp->set_h_nelem((unsigned int)accounts);

	if ((err = dbp->open(NULL, "account", NULL,
	    DB_HASH, oflags, 0644)) != 0) {
		DbException except("Account file create failed", err);
		throw except;
	}

	dbstl::register_db(dbp);
	accounts_map = new DefrecMap(dbp, this);
	start_anum = idnum;
	populateTable(accounts_map, idnum, balance, accounts, "account");
	idnum += accounts;
	end_anum = idnum - 1;
	// Automatically closes the underlying database.
	delete accounts_map;
	dbstl::close_db(dbp);
	delete dbp;
	if (verbose)
		cout << "Populated accounts: "
		     << (long)start_anum << " - " << (long)end_anum << "\n";

	dbp = new Db(this, DB_CXX_NO_EXCEPTIONS);
	//
	// Since the number of branches is very small, we want to use very
	// small pages and only 1 key per page.  This is the poor-man's way
	// of getting key locking instead of page locking.
	//
	dbp->set_h_ffactor(1);
	dbp->set_h_nelem((unsigned int)branches);
	dbp->set_pagesize(512);

	if ((err = dbp->open(NULL, 
	    "branch", NULL, DB_HASH, oflags, 0644)) != 0) {
		DbException except("Branch file create failed", err);
		throw except;
	}
	dbstl::register_db(dbp);
	branches_map = new DefrecMap(dbp, this);
	start_bnum = idnum;
	populateTable(branches_map, idnum, balance, branches, "branch");
	idnum += branches;
	end_bnum = idnum - 1;
	delete branches_map;
	dbstl::close_db(dbp);
	delete dbp;

	if (verbose)
		cout << "Populated branches: "
		     << (long)start_bnum << " - " << (long)end_bnum << "\n";

	dbp = new Db(this, DB_CXX_NO_EXCEPTIONS);
	//
	// In the case of tellers, we also want small pages, but we'll let
	// the fill factor dynamically adjust itself.
	//
	dbp->set_h_ffactor(0);
	dbp->set_h_nelem((unsigned int)tellers);
	dbp->set_pagesize(512);

	if ((err = dbp->open(NULL,
	    "teller", NULL, DB_HASH, oflags, 0644)) != 0) {
		DbException except("Teller file create failed", err);
		throw except;
	}

	dbstl::register_db(dbp);
	tellers_map = new DefrecMap(dbp, this);
	start_tnum = idnum;
	populateTable(tellers_map, idnum, balance, tellers, "teller");
	idnum += tellers;
	end_tnum = idnum - 1;
	delete tellers_map;
	dbstl::close_db(dbp);
	delete dbp;
	if (verbose)
		cout << "Populated tellers: "
		     << (long)start_tnum << " - " << (long)end_tnum << "\n";

	dbp = new Db(this, DB_CXX_NO_EXCEPTIONS);
	dbp->set_re_len(HISTORY_LEN);
	if ((err = dbp->open(NULL,
	    "history", NULL, DB_RECNO, oflags, 0644)) != 0) {
		DbException except("Create of history file failed", err);
		throw except;
	}

	dbstl::register_db(dbp);
	history_vector = new HistrecVector(dbp, this);
	populateHistory(history_vector, history, accounts, branches, tellers);
	delete history_vector;
	dbstl::close_db(dbp);
	delete dbp;
}