Db::associate |
#include <db_cxx.h>int Db::associate(DbTxn *txnid, Db *secondary, int (*callback)(Db *secondary, const Dbt *key, const Dbt *data, Dbt *result), u_int32_t flags);
The Db::associate function is used to declare one database a secondary index for a primary database. After a secondary database has been "associated" with a primary database, all updates to the primary will be automatically reflected in the secondary and all reads from the secondary will return corresponding data from the primary. Note that as primary keys must be unique for secondary indices to work, the primary database must be configured without support for duplicate data items. See Secondary indices for more information.
The Db::associate method either returns a non-zero error value or throws an exception that encapsulates a non-zero error value on failure, and returns 0 on success.
ParametersThe callback parameter may be NULL if both the primary and secondary database handles were opened with the DB_RDONLY flag.
The callback takes four arguments:
The callback optionally returns some special values:
When multiple secondary keys are returned, keys may not be repeated. In other words, there must be no repeated record numbers in the array for Recno and Queue databases, and keys must not compare equally using the secondary database's comparison function for Btree and Hash databases. If keys are repeated, operations may fail and the secondary may become inconsistent with the primary.
The DB_DBT_APPMALLOC flag may be set for any Dbt in the array of returned Dbt's to indicate that Berkeley DB should free the memory referenced by that particular Dbt's data field when it is done with it.
The DB_DBT_APPMALLOC flag may be combined with DB_DBT_MULTIPLE in the result Dbt's flag field to indicate that Berkeley DB should free the array once it is done with all of the returned keys.
If the callback function returns DB_DONOTINDEX for any key/data pairs in the primary database, the secondary index will not contain any reference to those key/data pairs, and such operations as cursor iterations and range queries will reflect only the corresponding subset of the database. If this is not desirable, the application should ensure that the callback function is well-defined for all possible values and never returns DB_DONOTINDEX.
Returning DB_DONOTINDEX is equivalent to setting DB_DBT_MULTIPLE on the result Dbt and setting the size field to zero.
Berkeley DB is not re-entrant. Callback functions should not attempt to make library calls (for example, to release locks or close open handles). Re-entering Berkeley DB is not guaranteed to work correctly, and the results are undefined.
If the secondary database has been opened in an environment configured with transactions, each put necessary for its creation will be done in the context of a transaction created for the purpose.
Care should be taken not to use a newly-populated secondary database in another thread of control until the Db::associate call has returned successfully in the first thread.
If transactions are not being used, care should be taken not to modify a primary database being used to populate a secondary database, in another thread of control, until the Db::associate call has returned successfully in the first thread. If transactions are being used, Berkeley DB will perform appropriate locking and the application need not do any special operation ordering.
This flag can be used to optimize updates when the secondary key in a primary record will never be changed after the primary record is inserted. For immutable secondary keys, a best effort is made to avoid calling the secondary callback function when primary records are updated. This optimization may reduce the overhead of update operations significantly if the callback function is expensive.
Be sure to specify this flag only if the secondary key in the primary record is never changed. If this rule is violated, the secondary index will become corrupted, that is, it will become out of sync with the primary.
The Db::associate method may fail and throw DbException, encapsulating one of the following non-zero errors, or return one of the following non-zero errors:
Copyright (c) 1996,2008 Oracle. All rights reserved.