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;
	}
}