Library Version 18.1.40

Quick Start Guide
Prev | Chapter 10. Berkeley DB Data Store and Concurrent Data Store Applications | Next

## Quick Start Guide

Here is an example program for creating/configuring a Berkeley DB environment (using Data Store, Concurrent Data Store and Transactional Data Store environments):

    int
    main(argc, argv)
        int argc;
        char *argv[];
    {
        DB_ENV *dbenv, *rm;
        DB *db;
        DBC *dbc;
        DB_TXN *txn;
        const char *home = ".";

### Data Store Environment

- Create a Data Store object and configure it.

      db_env_create(&dbenv, 0);

- Set the cache size.

      dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);

- Open the environment. The only flag required is `DB_INIT_MPOOL`.

      dbenv->open(dbenv, home, DB_INIT_MPOOL | DB_CREATE, 0644);

- Create the database handle.

      db_create(&db, dbenv, 0);

- Open the database, no flags are required.

      db->open(db, NULL, NULL, "database", DB_BTREE, DB_CREATE, 0644);

- Begin a cursor.

      db->cursor(db, NULL, &dbc, 0);

- Close the cursor.

      dbc->close(dbc);
      db->close(db, 0);
      dbenv->close(dbenv, 0);

### Concurrent Data Store Environment

- Create a Concurrent Data Store environment object and configure it.

      db_env_create(&dbenv, 0);

- Set the cache size.

      dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);

- Open the environment. The environment requires the `DB_INIT_CDB` and `DB_INIT_MPOOL` flags.

      dbenv->open(dbenv, home, DB_INIT_CDB | DB_INIT_MPOOL | DB_CREATE, 0644);

- Create the database handle.

      db_create(&db, dbenv, 0);

- Begin a Concurrent Data Store transaction.

      dbenv->cdsgroup_begin(dbenv, &txn);

- Open the database. A Concurrent Data Store transaction is required.

      db->open(db, txn, NULL, "database", DB_BTREE, DB_CREATE, 0644);

- Begin a cursor. A transaction and the `DB_WRITECURSOR` flag is required if the cursor will insert values.

      db->cursor(db, txn, &dbc, DB_WRITECURSOR);

- Close the cursor.

      dbc->close(dbc);

- Commit the transaction.

      txn->commit(txn, 0);
      db->close(db, 0);
      dbenv->close(dbenv, 0);

### Transactional Data Store Environment

- Create a Transactional environment object and configure it.

      db_env_create(&dbenv, 0);

- Set the cache size.

      dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);

- Open the environment. The `TDB_INIT_LOCK`, `DB_INIT_LOG`, `DB_INIT_MPOOL`, `DB_INIT_TXN` flags required by it specify that the environment will have transactional support.

      dbenv->open(dbenv, home,
      TDB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL |
      DB_INIT_TXN | DB_CREATE, 0644);

- Create the database handle.

      db_create(&db, dbenv, 0);

- Open the database with the `DB_AUTO_COMMIT` flag. For a database to support transactions it has to be open with either the `DB_AUTO_COMMIT` flag, or a transaction.

      db->open(db, NULL, NULL, "database", DB_BTREE,
      DB_AUTO_COMMIT | DB_CREATE, 0644);

- Begin a transaction.

      dbenv->txn_begin(dbenv, NULL, &txn, 0);

- Begin a transactional cursor.

      db->cursor(db, txn, &dbc, 0);

- Close the cursor.

      dbc->close(dbc);

- Commit and close the transaction.

      txn->commit(txn, 0);
      db->close(db, 0);
      dbenv->close(dbenv, 0);

Prev | Up | Next
Architecting Data Store and Concurrent Data Store applications | Home | Chapter 11. Berkeley DB Transactional Data Store Applications