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.
```
- db_env_create(&dbenv, 0); ``` - Set the cache size.
```
- dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
`` - Open the environment. The only flag required isDB_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 theDB_INIT_CDBandDB_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 theDB_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. TheTDB_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 theDB_AUTO_COMMITflag. For a database to support transactions it has to be open with either theDB_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); ```
