Library Version 11.2.5.3

Database Joins
Prev | Chapter 10. Secondary Databases | Next

## Database Joins

If you have two or more secondary databases associated with a primary database, then you can retrieve primary records based on the intersection of multiple secondary entries. You do this using a `JoinCursor`.

Throughout this document we have presented a class that stores inventory information on grocery That class is fairly simple with a limited number of data members, few of which would be interesting from a query perspective. But suppose, instead, that we were storing information on something with many more characteristics that can be queried, such as an automobile. In that case, you may be storing information such as color, number of doors, fuel mileage, automobile type, number of passengers, make, model, and year, to name just a few.

In this case, you would still likely be using some unique value to key your primary entries (in the United States, the automobile's VIN would be ideal for this purpose). You would then create a class that identifies all the characteristics of the automobiles in your inventory. You would also have to create some mechanism by which you would move instances of this class in and out of Java `byte` arrays. We described the concepts and mechanisms by which you can perform these activities in Database Records.

To query this data, you might then create multiple secondary databases, one for each of the characteristics that you want to query. For example, you might create a secondary for color, another for number of doors, another for number of passengers, and so forth. Of course, you will need a unique key creator for each such secondary database. You do all of this using the concepts and techniques described throughout this chapter.

Once you have created this primary database and all interesting secondaries, what you have is the ability to retrieve automobile records based on a single characteristic. You can, for example, find all the automobiles that are red. Or you can find all the automobiles that have four doors. Or all the automobiles that are minivans.

The next most natural step, then, is to form compound queries, or joins. For example, you might want to find all the automobiles that are red, and that were built by Toyota, and that are minivans. You can do this using a `JoinCursor` class instance.

### Using Join Cursors

To use a join cursor:

- Open two or more secondary cursors. These cursors for secondary databases that are associated with the same primary database.

- Position each such cursor to the secondary key value in which you are interested. For example, to build on the previous description, the cursor for the color database is positioned to the `red` records while the cursor for the model database is positioned to the `minivan` records, and the cursor for the make database is positioned to `Toyota`.

- Create an array of secondary cursors, and place in it each of the cursors that are participating in your join query.

- Obtain a join cursor. You do this using the `Database.join()` method. You must pass this method the array of secondary cursors that you opened and positioned in the previous steps.

- Iterate over the set of matching records using `JoinCursor.getNext()` until `OperationStatus` is not `SUCCESS`.

- Close your join cursor.

- If you are done with them, close all your secondary cursors.

For example:

    package db.GettingStarted;

    import com.sleepycat.db.Database;
    import com.sleepycat.db.DatabaseEntry;
    import com.sleepycat.db.DatabaseException;
    import com.sleepycat.db.JoinCursor;
    import com.sleepycat.db.LockMode;
    import com.sleepycat.db.OperationStatus;
    import com.sleepycat.db.SecondaryCursor;
    import com.sleepycat.db.SecondaryDatabase;

    ...

    // Database and secondary database opens omitted for brevity.
    // Assume a primary database handle:
    //   automotiveDB
    // Assume 3 secondary database handles:
    //   automotiveColorDB  -- index based on automobile color
    //   automotiveTypeDB  -- index based on automobile type
    //   automotiveMakeDB   -- index based on the manufacturer
    Database automotiveDB = null;
    SecondaryDatabase automotiveColorDB = null;
    SecondaryDatabase automotiveTypeDB = null;
    SecondaryDatabase automotiveMakeDB = null;

    // Query strings:
    String theColor = "red";
    String theType = "minivan";
    String theMake = "Toyota";

    // Secondary cursors used for the query:
    SecondaryCursor colorSecCursor = null;
    SecondaryCursor typeSecCursor = null;
    SecondaryCursor makeSecCursor = null;

    // The join cursor
    JoinCursor joinCursor = null;

    // These are needed for our queries
    DatabaseEntry foundKey = new DatabaseEntry();
    DatabaseEntry foundData = new DatabaseEntry();

    // All cursor operations are enclosed in a try block to ensure that they
    // get closed in the event of an exception.

    try {
        // Database entries used for the query:
        DatabaseEntry color = new DatabaseEntry(theColor.getBytes("UTF-8"));
        DatabaseEntry type = new DatabaseEntry(theType.getBytes("UTF-8"));
        DatabaseEntry make = new DatabaseEntry(theMake.getBytes("UTF-8"));

        colorSecCursor = automotiveColorDB.openSecondaryCursor(null, null);
        typeSecCursor = automotiveTypeDB.openSecondaryCursor(null, null);
        makeSecCursor = automotiveMakeDB.openSecondaryCursor(null, null);

        // Position all our secondary cursors to our query values.
        OperationStatus colorRet =
            colorSecCursor.getSearchKey(color, foundData, LockMode.DEFAULT);
        OperationStatus typeRet =
            typeSecCursor.getSearchKey(type, foundData, LockMode.DEFAULT);
        OperationStatus makeRet =
            makeSecCursor.getSearchKey(make, foundData, LockMode.DEFAULT);

        // If all our searches returned successfully, we can proceed
        if (colorRet == OperationStatus.SUCCESS &&
            typeRet == OperationStatus.SUCCESS &&
            makeRet == OperationStatus.SUCCESS) {

            // Get a secondary cursor array and populate it with our
            // positioned cursors
            SecondaryCursor[] cursorArray = {colorSecCursor,
                                             typeSecCursor,
                                             makeSecCursor};

            // Create the join cursor
            joinCursor = automotiveDB.join(cursorArray, null);

            // Now iterate over the results, handling each in turn
            while (joinCursor.getNext(foundKey, foundData, LockMode.DEFAULT) ==
                            OperationStatus.SUCCESS) {

                // Do something with the key and data retrieved in
                // foundKey and foundData
            }
        }
    } catch (DatabaseException dbe) {
        // Error reporting goes here
    } catch (Exception e) {
        // Error reporting goes here
    } finally {
        try {
            // Make sure to close out all our cursors
            if (colorSecCursor != null) {
                colorSecCursor.close();
            }
            if (typeSecCursor != null) {
                typeSecCursor.close();
            }
            if (makeSecCursor != null) {
                makeSecCursor.close();
            }
            if (joinCursor != null) {
                joinCursor.close();
            }
        } catch (DatabaseException dbe) {
            // Error reporting goes here
        }
    }

### JoinCursor Properties

You can set `JoinCursor` properties using the `JoinConfig` class. Currently there is just one property that you can set:

- `JoinConfig.setNoSort()`

  Specifies whether automatic sorting of input cursors is disabled. The cursors are sorted from the one that refers to the least number of data items to the one that refers to the most.

  If the data is structured so that cursors with many data items also share many common elements, higher performance will result from listing those cursors before cursors with fewer data items. Turning off sorting permits applications to specify cursors in the proper order given this scenario.

  The default value is `false` (automatic cursor sorting is performed).

  For example:

      // All database and environments omitted
      JoinConfig config = new JoinConfig();
      config.setNoSort(true);
      JoinCursor joinCursor = myDb.join(cursorArray, config);

Prev | Up | Next
Using Secondary Cursors | Home | Secondary Database Example