+I:Berkeley DB Reference Guide: Access Methods[P4

Berkeley DB Reference Guide: Access Methods



"

Logical join



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

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

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

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



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



GThis secondary index would allow an application to look up a color, andEthen use the data items to look up the stores where the colored fruitCcould be purchased, e.g., by first looking up blue, the dataDitem 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 theBcost of the fruit, a similar lookup could be done on stores+where inexpensive fruit could be purchased:



oThe DB->join function, makes it possible to do lookups based on multipleEcriteria in a single operation, e.g., it would be possible to look upFfruits that were both red and expensive in a single operation. If theIsame fruit appeared as a data item in both the color and expense indices,Dthen that fruit name would be used as the key for retrieval from theIprimary index, and would then return the store where expensive, red fruitcould be purchased.

Example



'Consider the following three databases:



personnel

lastname

jobs



Consider the following query:

=



GWhat is desired are all the records in the primary database (personnel)Afor whom the criteria lastname=smith, job title=manager istrue.

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



HAJÿÿ