head 1.2; access; symbols RPM_4_2_1:1.1.1.5 RPM_4_2:1.1.1.5 RPM_4_1_1:1.1.1.5 RPM_4_1:1.1.1.4 RPM_4_0_5:1.1.1.3 RPM_4_0_4:1.1.1.2 RPM_4_0_3:1.1.1.1 RPM:1.1.1; locks; strict; comment @# @; 1.2 date 2008.01.02.09.55.43; author rse; state dead; branches; next 1.1; commitid z4cpSiAhOCXk5PLs; 1.1 date 2001.07.23.20.45.38; author rse; state Exp; branches 1.1.1.1; next ; 1.1.1.1 date 2001.07.23.20.45.38; author rse; state Exp; branches; next 1.1.1.2; 1.1.1.2 date 2002.01.08.00.30.12; author rse; state Exp; branches; next 1.1.1.3; 1.1.1.3 date 2003.01.18.13.49.03; author rse; state Exp; branches; next 1.1.1.4; 1.1.1.4 date 2001.10.15.03.47.34; author rse; state Exp; branches; next 1.1.1.5; 1.1.1.5 date 2003.01.18.14.05.00; author rse; state Exp; branches; next ; desc @@ 1.2 log @remove the ancient RPM 4.2.1 source tree copy @ text @
|
![]() ![]() ![]() |
Next, we open three databases ("color" and "fruit" and "cats"), in the database environment. Again, our DB database handles are declared to be free-threaded using the DB_THREAD flag, and so may be used by any number of threads we subsequently create.
int main(int argc, char *argv) { extern char *optarg; extern int optind; DB *db_cats, *db_color, *db_fruit; DB_ENV *dbenv; pthread_t ptid; int ch;while ((ch = getopt(argc, argv, "")) != EOF) switch (ch) { case '?': default: usage(); } argc -= optind; argv += optind;
env_dir_create(); env_open(&dbenv);
/* Open database: Key is fruit class; Data is specific type. */ db_open(dbenv, &db_fruit, "fruit", 0);
/* Open database: Key is a color; Data is an integer. */ db_open(dbenv, &db_color, "color", 0);
/* * Open database: * Key is a name; Data is: company name, cat breeds. */ db_open(dbenv, &db_cats, "cats", 1);
return (0); }
void db_open(DB_ENV *dbenv, DB **dbp, char *name, int dups) { DB *db; int ret;
/* Create the database handle. */ if ((ret = db_create(&db, dbenv, 0)) != 0) { dbenv->err(dbenv, ret, "db_create"); exit (1); }
/* Optionally, turn on duplicate data items. */ if (dups && (ret = db->set_flags(db, DB_DUP)) != 0) { dbenv->err(dbenv, ret, "db->set_flags: DB_DUP"); exit (1); }
/* * Open a database in the environment: * create if it doesn't exist * free-threaded handle * read/write owner only */ if ((ret = db->open(db, name, NULL, DB_BTREE, DB_CREATE | DB_THREAD, S_IRUSR | S_IWUSR)) != 0) { dbenv->err(dbenv, ret, "db->open: %s", name); exit (1); }
*dbp = db; }
There is no reason to wrap database opens inside of transactions. All database creates are transaction-protected internally to Berkeley DB, and applications using transaction-protected environments can simply rely on files either being successfully re-created in a recovered environment or not appearing at all.
After running this initial code, we can use the db_stat utility to display information about a database we have created:
prompt> db_stat -h TXNAPP -d color 53162 Btree magic number. 8 Btree version number. Flags: 2 Minimum keys per-page. 8192 Underlying database page size. 1 Number of levels in the tree. 0 Number of unique keys in the tree. 0 Number of data items in the tree. 0 Number of tree internal pages. 0 Number of bytes free in tree internal pages (0% ff). 1 Number of tree leaf pages. 8166 Number of bytes free in tree leaf pages (0.% ff). 0 Number of tree duplicate pages. 0 Number of bytes free in tree duplicate pages (0% ff). 0 Number of tree overflow pages. 0 Number of bytes free in tree overflow pages (0% ff). 0 Number of pages on the free list.
![]() ![]() ![]() |
Copyright Sleepycat Software @ 1.1 log @Initial revision @ text @d1 1 a1 1 @ 1.1.1.1 log @Import: RPM 4.0.3 @ text @@ 1.1.1.2 log @Import: RPM 4.0.4 @ text @d1 1 a1 1 d18 1 a18 1 database environment. Again, our DB database handles are @ 1.1.1.3 log @Import: RPM 4.0.5 @ text @d1 2 a2 2 a3 1 d18 1 a18 1 database environment. Again, our DB database handles are d28 1 d44 1 a44 2 if (db_open(dbenv, &db_fruit, "fruit", 0)) return (1); d47 1 a47 2 if (db_open(dbenv, &db_color, "color", 0)) return (1); d53 1 a53 2 if (db_open(dbenv, &db_cats, "cats", 1)) return (1); d58 1 a58 1 int d67 1 a67 1 return (1); a71 1 (void)db->close(db, 0); d73 1 a73 1 return (1); d82 2 a83 3 if ((ret = db->open(db, NULL, name, NULL, DB_BTREE, DB_CREATE | DB_THREAD | DB_AUTO_COMMIT, S_IRUSR | S_IWUSR)) != 0) { (void)db->close(db, 0); d85 1 a85 1 return (1); a88 1 return (0); d90 7 a96 2
After opening the database, we can use the db_stat utility to display information about a database we have created: a114 23
The database open must be enclosed within a transaction in order to be recoverable. The transaction will ensure that created files are re-created in recovered environments (or do not appear at all). Additional database operations or operations on other databases can be included in the same transaction, of course. In the simple case, where the open is the only operation in the transaction, an application can set the DB_AUTO_COMMIT flag instead of creating and managing its own transaction handle. The DB_AUTO_COMMIT flag will internally wrap the operation in a transaction, simplifying application code.
The previous example is the simplest case of transaction protection for database open. Obviously, additional database operations can be done in the scope of the same transaction. For example, an application maintaining a list of the databases in a database environment in a well-known file might include an update of the list in the same transaction in which the database is created. Or, an application might create both a primary and secondary database in a single transaction.
DB handles that will later be used for transactionally protected operations must be opened within a transaction. Specifying a transaction handle to operations using handles not opened within a transaction will return an error. Similarly, not specifying a transaction handle to operations using handles that were opened within a transaction will also return an error. @ 1.1.1.4 log @Import: RPM 4.1 @ text @d1 2 a2 2 d4 1 d19 1 a19 1 database environment. Again, our DB database handles are a28 1 pthread_t ptid; d44 2 a45 1 db_open(dbenv, &db_fruit, "fruit", 0); d48 2 a49 1 db_open(dbenv, &db_color, "color", 0); d55 2 a56 1 db_open(dbenv, &db_cats, "cats", 1); d61 1 a61 1 void d70 1 a70 1 exit (1); d75 1 d77 1 a77 1 exit (1); d86 3 a88 2 if ((ret = db->open(db, name, NULL, DB_BTREE, DB_CREATE | DB_THREAD, S_IRUSR | S_IWUSR)) != 0) { d90 1 a90 1 exit (1); d94 1 d96 2 a97 7
There is no reason to wrap database opens inside of transactions. All database creates are transaction-protected internally to Berkeley DB, and applications using transaction-protected environments can simply rely on files either being successfully re-created in a recovered environment or not appearing at all.
After running this initial code, we can use the db_stat utility to display information about a database we have created: d116 23 @ 1.1.1.5 log @Import: RPM 4.1.1 @ text @d1 2 a2 2 a3 1 d18 1 a18 1 database environment. Again, our DB database handles are d28 1 d44 1 a44 2 if (db_open(dbenv, &db_fruit, "fruit", 0)) return (1); d47 1 a47 2 if (db_open(dbenv, &db_color, "color", 0)) return (1); d53 1 a53 2 if (db_open(dbenv, &db_cats, "cats", 1)) return (1); d58 1 a58 1 int d67 1 a67 1 return (1); a71 1 (void)db->close(db, 0); d73 1 a73 1 return (1); d82 2 a83 3 if ((ret = db->open(db, NULL, name, NULL, DB_BTREE, DB_CREATE | DB_THREAD | DB_AUTO_COMMIT, S_IRUSR | S_IWUSR)) != 0) { (void)db->close(db, 0); d85 1 a85 1 return (1); a88 1 return (0); d90 7 a96 2
After opening the database, we can use the db_stat utility to display information about a database we have created: a114 23
The database open must be enclosed within a transaction in order to be recoverable. The transaction will ensure that created files are re-created in recovered environments (or do not appear at all). Additional database operations or operations on other databases can be included in the same transaction, of course. In the simple case, where the open is the only operation in the transaction, an application can set the DB_AUTO_COMMIT flag instead of creating and managing its own transaction handle. The DB_AUTO_COMMIT flag will internally wrap the operation in a transaction, simplifying application code.
The previous example is the simplest case of transaction protection for database open. Obviously, additional database operations can be done in the scope of the same transaction. For example, an application maintaining a list of the databases in a database environment in a well-known file might include an update of the list in the same transaction in which the database is created. Or, an application might create both a primary and secondary database in a single transaction.
DB handles that will later be used for transactionally protected operations must be opened within a transaction. Specifying a transaction handle to operations using handles not opened within a transaction will return an error. Similarly, not specifying a transaction handle to operations using handles that were opened within a transaction will also return an error. @