summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDon Anderson <dda@ddanderson.com>2015-11-03 13:36:26 -0500
committerDon Anderson <dda@ddanderson.com>2015-11-03 13:36:26 -0500
commitd46c3bf7b1bed11da473d351847b915bda1bb83f (patch)
tree60472c582d319d2264ee7cc2fbf38352290dc32b
parent2770eb0749ec395c7937154798ef20e30ad58fcb (diff)
downloadmongo-d46c3bf7b1bed11da473d351847b915bda1bb83f.tar.gz
WT-1315. Added C and java examples for join cursors.
-rw-r--r--examples/c/ex_schema.c39
-rw-r--r--examples/java/com/wiredtiger/examples/ex_schema.java43
2 files changed, 78 insertions, 4 deletions
diff --git a/examples/c/ex_schema.c b/examples/c/ex_schema.c
index 8b74500acd3..dabb568129e 100644
--- a/examples/c/ex_schema.c
+++ b/examples/c/ex_schema.c
@@ -49,12 +49,16 @@ typedef struct {
static POP_RECORD pop_data[] = {
{ "AU", 1900, 4000000 },
+ { "AU", 1950, 8267337 },
{ "AU", 2000, 19053186 },
{ "CAN", 1900, 5500000 },
+ { "CAN", 1950, 14011422 },
{ "CAN", 2000, 31099561 },
{ "UK", 1900, 369000000 },
+ { "UK", 1950, 50127000 },
{ "UK", 2000, 59522468 },
{ "USA", 1900, 76212168 },
+ { "USA", 1950, 150697361 },
{ "USA", 2000, 301279593 },
{ "", 0, 0 }
};
@@ -65,7 +69,7 @@ main(void)
{
POP_RECORD *p;
WT_CONNECTION *conn;
- WT_CURSOR *cursor;
+ WT_CURSOR *cursor, *cursor2, *join_cursor;
WT_SESSION *session;
const char *country;
uint64_t recno, population;
@@ -321,6 +325,39 @@ main(void)
/*! [Access only the index] */
ret = cursor->close(cursor);
+ /*! [Join cursors] */
+ /* Open cursors needed by the join. */
+ ret = session->open_cursor(session,
+ "join:table:poptable", NULL, NULL, &join_cursor);
+ ret = session->open_cursor(session,
+ "index:poptable:country", NULL, NULL, &cursor);
+ ret = session->open_cursor(session,
+ "index:poptable:immutable_year", NULL, NULL, &cursor2);
+
+ /* select values WHERE country == "AU" AND year > 1900 */
+ cursor->set_key(cursor, "AU\0\0\0");
+ ret = cursor->search(cursor);
+ ret = session->join(session, join_cursor, cursor,
+ "compare=eq,count=10");
+ cursor2->set_key(cursor2, (uint16_t)1900);
+ ret = cursor2->search(cursor2);
+ ret = session->join(session, join_cursor, cursor2,
+ "compare=gt,count=10,strategy=bloom");
+
+ /* List the values that are joined */
+ while ((ret = join_cursor->next(join_cursor)) == 0) {
+ ret = join_cursor->get_key(join_cursor, &recno);
+ ret = join_cursor->get_value(join_cursor, &country, &year,
+ &population);
+ printf("ID %" PRIu64, recno);
+ printf(": country %s, year %u, population %" PRIu64 "\n",
+ country, year, population);
+ }
+ /*! [Join cursors] */
+ ret = join_cursor->close(join_cursor);
+ ret = cursor2->close(cursor2);
+ ret = cursor->close(cursor);
+
ret = conn->close(conn, NULL);
return (ret);
diff --git a/examples/java/com/wiredtiger/examples/ex_schema.java b/examples/java/com/wiredtiger/examples/ex_schema.java
index 5b849ecf430..ba15db62a14 100644
--- a/examples/java/com/wiredtiger/examples/ex_schema.java
+++ b/examples/java/com/wiredtiger/examples/ex_schema.java
@@ -57,12 +57,16 @@ public class ex_schema {
popData = new ArrayList<PopRecord>();
popData.add(new PopRecord("AU", (short)1900, 4000000 ));
+ popData.add(new PopRecord("AU", (short)1950, 8267337 ));
popData.add(new PopRecord("AU", (short)2000, 19053186 ));
popData.add(new PopRecord("CAN", (short)1900, 5500000 ));
+ popData.add(new PopRecord("CAN", (short)1950, 14011422 ));
popData.add(new PopRecord("CAN", (short)2000, 31099561 ));
popData.add(new PopRecord("UK", (short)1900, 369000000 ));
+ popData.add(new PopRecord("UK", (short)1950, 50127000 ));
popData.add(new PopRecord("UK", (short)2000, 59522468 ));
popData.add(new PopRecord("USA", (short)1900, 76212168 ));
+ popData.add(new PopRecord("USA", (short)1950, 150697361 ));
popData.add(new PopRecord("USA", (short)2000, 301279593 ));
};
/*! [schema declaration] */
@@ -72,7 +76,7 @@ public class ex_schema {
throws WiredTigerException
{
Connection conn;
- Cursor cursor;
+ Cursor cursor, cursor2, join_cursor;
Session session;
String country;
long recno, population;
@@ -206,7 +210,7 @@ public class ex_schema {
* for a particular country.
*/
cursor = session.open_cursor("colgroup:poptable:main", null, null);
- cursor.putKeyLong(2);
+ cursor.putKeyRecord(2);
if ((ret = cursor.search()) == 0) {
country = cursor.getValueString();
year = cursor.getValueShort();
@@ -223,7 +227,7 @@ public class ex_schema {
* population of a particular country.
*/
cursor = session.open_cursor("colgroup:poptable:population", null, null);
- cursor.putKeyLong(2);
+ cursor.putKeyRecord(2);
if ((ret = cursor.search()) == 0) {
population = cursor.getValueLong();
System.out.println("ID 2: population " + population);
@@ -335,6 +339,39 @@ public class ex_schema {
/*! [Access only the index] */
ret = cursor.close();
+ /*! [Join cursors] */
+ /* Open cursors needed by the join. */
+ join_cursor = session.open_cursor(
+ "join:table:poptable", null, null);
+ cursor = session.open_cursor(
+ "index:poptable:country", null, null);
+ cursor2 = session.open_cursor(
+ "index:poptable:immutable_year", null, null);
+
+ /* select values WHERE country == "AU" AND year > 1900 */
+ cursor.putKeyString("AU");
+ ret = cursor.search();
+ session.join(join_cursor, cursor, "compare=eq,count=10");
+ cursor2.putKeyShort((short)1900);
+ ret = cursor2.search();
+ session.join(join_cursor, cursor2,
+ "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);
+ }
+ /*! [Join cursors] */
+ ret = join_cursor.close();
+ ret = cursor2.close();
+ ret = cursor.close();
+
ret = conn.close(null);
return (ret);