int DB->associate(DB *primary, DB_TXN *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 returns a non-zero error value on failure and 0 on success.
The 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:
If the callback function needs to allocate memory for the data field rather than simply pointing into the primary key or datum, the flags field of the returned DBT should be set to DB_DBT_APPMALLOC, which indicates that Berkeley DB should free the memory when it is done with it.
If any key/data pair in the primary yields a null secondary key and should be left out of the secondary index, the callback function may optionally return DB_DONOTINDEX. Otherwise, the callback function should return 0 in case of success or an error outside of the Berkeley DB name space in case of failure; the error code will be returned from the Berkeley DB call that initiated the callback.
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.
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 return one of the following non-zero errors:
Copyright (c) 1996-2005 Sleepycat Software, Inc. - All rights reserved.