Berkeley DB Reference Guide: Access Methods
Полезная информация

Berkeley DB Reference Guide: Access Methods

Logical join

The DB->join function provides logical join functionality. While not strictly a cursor function, in that it is not a method off a cursor handle, it is more related to the cursor functions than to the standard DB functions.

The DB->join function requires that your data be organized as a primary database which contains the primary key and primary data field, and a set of secondary databases. Each of the secondary databases is indexed by a different secondary key, and, for each key in a secondary database, there is a set of duplicate data items that match the primary keys in the primary database.

What the DB->join function does is review a list of secondary keys, and, when it finds a data item that appears as a data item for all of the secondary keys, it uses that data items as a lookup into the primary database, and returns the associated data item.

For example, consider a database that lists types of fruit as the key item, and the store where you can buy them as the data item:

A secondary index might have a key color, and, as the data items, the list of fruits of different colors.

This secondary index would allow an application to look up a color, and then use the data items to look up the stores where the colored fruit could be purchased, e.g., by first looking up blue, the data item blueberry could be used as the lookup key in the primary database, returning Farmer's Market.

If there were a another secondary index that had as its key the cost of the fruit, a similar lookup could be done on stores where inexpensive fruit could be purchased:

The DB->join function, makes it possible to do lookups based on multiple criteria in a single operation, e.g., it would be possible to look up fruits that were both red and expensive in a single operation. If the same fruit appeared as a data item in both the color and expense indices, then that fruit name would be used as the key for retrieval from the primary index, and would then return the store where expensive, red fruit could be purchased.


Consider the following three databases:


Consider the following query:

What is desired are all the records in the primary database (personnel) for whom the criteria lastname=smith, job title=manager is true.

Assume that all databases have been properly opened and have the handles: pers_db, name_db, job_db. Assume that we have an active transaction referenced by the handle txn.