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):


1. int
2. main(argc, argv)
3. int argc;
4. char *argv[];
5. {
6. DB_ENV *dbenv, *rm;
7. DB *db;
8. DBC *dbc;
9. DB_TXN *txn;
10. const char *home = ".";

Data Store Environment

  • Create a Data Store object and configure it.

```

  1. db_env_create(&dbenv, 0); ``` - Set the cache size.

```

  1. dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0); `` - Open the environment. The only flag required isDB_INIT_MPOOL`.

```

  1. dbenv->open(dbenv, home, DB_INIT_MPOOL | DB_CREATE, 0644); ``` - Create the database handle.

```

  1. db_create(&db, dbenv, 0); ``` - Open the database, no flags are required.

```

  1. db->open(db, NULL, NULL, "database", DB_BTREE, DB_CREATE, 0644); ``` - Begin a cursor.

```

  1. db->cursor(db, NULL, &dbc, 0); ``` - Close the cursor.

```

  1. dbc->close(dbc);
  2. db->close(db, 0);
  3. dbenv->close(dbenv, 0); ```

Concurrent Data Store Environment

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

```

  1. db_env_create(&dbenv, 0); ``` - Set the cache size.

```

  1. dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0); `` - Open the environment. The environment requires theDB_INIT_CDBandDB_INIT_MPOOL` flags.

```

  1. dbenv->open(dbenv, home, DB_INIT_CDB | DB_INIT_MPOOL | DB_CREATE, 0644); ``` - Create the database handle.

```

  1. db_create(&db, dbenv, 0); ``` - Begin a Concurrent Data Store transaction.

```

  1. dbenv->cdsgroup_begin(dbenv, &txn); ``` - Open the database. A Concurrent Data Store transaction is required.

```

  1. db->open(db, txn, NULL, "database", DB_BTREE, DB_CREATE, 0644); `` - Begin a cursor. A transaction and theDB_WRITECURSOR` flag is required if the cursor will insert values.

```

  1. db->cursor(db, txn, &dbc, DB_WRITECURSOR); ``` - Close the cursor.

```

  1. dbc->close(dbc); ``` - Commit the transaction.

```

  1. txn->commit(txn, 0);
  2. db->close(db, 0);
  3. dbenv->close(dbenv, 0); ```

Transactional Data Store Environment

  • Create a Transactional environment object and configure it.

```

  1. db_env_create(&dbenv, 0); ``` - Set the cache size.

```

  1. dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0); `` - Open the environment. TheTDB_INIT_LOCK,DB_INIT_LOG,DB_INIT_MPOOL,DB_INIT_TXN` flags required by it specify that the environment will have transactional support.

```

  1. dbenv->open(dbenv, home,
  2. TDB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL |
  3. DB_INIT_TXN | DB_CREATE, 0644); ``` - Create the database handle.

```

  1. db_create(&db, dbenv, 0); `` - Open the database with theDB_AUTO_COMMITflag. For a database to support transactions it has to be open with either theDB_AUTO_COMMIT` flag, or a transaction.

```

  1. db->open(db, NULL, NULL, "database", DB_BTREE,
  2. DB_AUTO_COMMIT | DB_CREATE, 0644); ``` - Begin a transaction.

```

  1. dbenv->txn_begin(dbenv, NULL, &txn, 0); ``` - Begin a transactional cursor.

```

  1. db->cursor(db, txn, &dbc, 0); ``` - Close the cursor.

```

  1. dbc->close(dbc); ``` - Commit and close the transaction.

```

  1. txn->commit(txn, 0);
  2. db->close(db, 0);
  3. dbenv->close(dbenv, 0); ```