Version 3.0.0
Join cursors in Java

Join cursors provide a way to iterate over a subset of a table, where the subset is specified by relationships with reference cursors.

A join cursor is created with Session.open_cursor using a "join:table:<name>" URI prefix. Then reference cursors are positioned to keys on indices and joined to the join cursor using Session.join calls. The result is a join cursor that can be iterated to satisfy the join equation.

Here is an example using join cursors:

/* Open cursors needed by the join. */
join_cursor = session.open_cursor(
"join:table:poptable", null, null);
country_cursor = session.open_cursor(
"index:poptable:country", null, null);
year_cursor = session.open_cursor(
"index:poptable:immutable_year", null, null);
/* select values WHERE country == "AU" AND year > 1900 */
country_cursor.putKeyString("AU");
ret = country_cursor.search();
session.join(join_cursor, country_cursor, "compare=eq,count=10");
year_cursor.putKeyShort((short)1900);
ret = year_cursor.search();
session.join(join_cursor, year_cursor,
"compare=gt,count=10,strategy=bloom");
/* List the values that are joined */
while ((ret = join_cursor.next()) == 0) {
recno = join_cursor.getKeyRecord();
country = join_cursor.getValueString();
year = join_cursor.getValueShort();
population = join_cursor.getValueLong();
System.out.print("ID " + recno);
System.out.println( ": country " + country + ", year " + year +
", population " + population);
}

Joins support various comparison operators: "eq", "gt", "ge", "lt", "le". Ranges with lower and upper bounds can also be specified, by joining two cursors on the same index, for example, one with "compare=ge" and another "compare=lt". In addition to joining indices, the main table can be joined so that a range of primary keys can be specified.

By default, a join cursor returns a conjunction, that is, all keys that satisfy all the joined comparisons. By specifying a configuration with "operation=or", a join cursor will return a disjunction, or all keys that satisfy at least one of the joined comparisons. More complex joins can be composed by specifying another join cursor as the reference cursor in a join call.

Here is an example using these concepts to show a conjunction of a disjunction:

/* Open cursors needed by the join. */
join_cursor = session.open_cursor(
"join:table:poptable", null, null);
subjoin_cursor = session.open_cursor(
"join:table:poptable", null, null);
country_cursor = session.open_cursor(
"index:poptable:country", null, null);
country_cursor2 = session.open_cursor(
"index:poptable:country", null, null);
year_cursor = session.open_cursor(
"index:poptable:immutable_year", null, null);
/*
* select values WHERE (country == "AU" OR country == "UK")
* AND year > 1900
*
* First, set up the join representing the country clause.
*/
country_cursor.putKeyString("AU");
ret = country_cursor.search();
ret = session.join(subjoin_cursor, country_cursor,
"operation=or,compare=eq,count=10");
country_cursor2.putKeyString("UK");
ret = country_cursor2.search();
ret = session.join(subjoin_cursor, country_cursor2,
"operation=or,compare=eq,count=10");
/* Join that to the top join, and add the year clause */
ret = session.join(join_cursor, subjoin_cursor, null);
year_cursor.putKeyShort((short)1900);
ret = year_cursor.search();
ret = session.join(join_cursor, year_cursor,
"compare=gt,count=10,strategy=bloom");
/* List the values that are joined */
while ((ret = join_cursor.next()) == 0) {
recno = join_cursor.getKeyRecord();
country = join_cursor.getValueString();
year = join_cursor.getValueShort();
population = join_cursor.getValueLong();
System.out.print("ID " + recno);
System.out.println( ": country " + country + ", year " + year +
", population " + population);
}

All the joins should be done on the join cursor before Cursor.next is called. Calling Cursor.next on a join cursor for the first time populates any bloom filters and performs other initialization. The join cursor's key is the primary key (the key for the main table), and its value is the entire set of values of the main table. A join cursor can be created with a projection by appending "(col1,col2,...)" to the URI if a different set of values is needed.

Keys returned from the join cursor are ordered according to the first reference cursor joined. For example, if an index cursor was joined first, that index determines the order of results. If the join cursor uses disjunctions, then the ordering of all joins determines the order. The first join in a conjunctive join, or all joins in a disjunctive join, are distinctive in that they are iterated internally as the cursor join returns values in order. Any bloom filters specified on the joins that are used for iteration are not useful, and are silently ignored.

When disjunctions are used where the sets of keys overlap on these 'iteration joins', a join cursor will return duplicates. A join cursor never returns duplicates unless "operation=or" is used in a join configuration, or unless the first joined cursor is itself a join cursor that would return duplicates.