WiredTiger: Cursor operations in Java

| Version 3.1.0

Fork me on GitHub Join my user group
Cursor operations in Java

Common operations in WiredTiger are performed using Cursor handles. A cursor includes:

- a position within a data source
- getter/setters for key and value fields
- encoding of fields to store in the data source
- methods to navigate within and iterate through the data

# Opening a cursor

Cursors are created using the Session.open_cursor method. For example, from the program ex_cursor.java:

cursor = session.open_cursor("table:world", null, null);

Another example from the same program:

cursor = session.open_cursor("table:world(country,population)", null, null);

In addition to traditional data sources, cursors in WiredTiger are used to access projections and even created data sources such as the run-time statistics:

cursor = session.open_cursor("statistics:", null, null);

See Cursors in Java for more information on available cursor types.

# Closing a cursor

Cursors remain open until either Cursor.close is called or the cursor's session is closed, which may either be in Session.close or Connection.close.

# Positioning a cursor

Cursors may be positioned at the beginning of the data source, the end of the data source, at an exact key within the data source, and near a key within the data source.

To invalidate the position of a cursor so that subsequent iterations start from the beginning or end of the data source, use the Cursor.reset method:

public static int
cursor_reset(Cursor cursor)
throws WiredTigerException
{
return (cursor.reset());
}

To move a cursor forward in the data source, use the cursor Cursor.next method:

public static int
cursor_forward_scan(Cursor cursor)
throws WiredTigerException
{
String key, value;
int ret;
while ((ret = cursor.next()) == 0) {
key = cursor.getKeyString();
value = cursor.getValueString();
}
return (ret);
}

If the Cursor.next method is called on a cursor without a position in the data source, it is positioned at the beginning of the data source.

To move a cursor backward in the data source, use the cursor Cursor.prev method:

public static int
cursor_reverse_scan(Cursor cursor)
throws WiredTigerException
{
String key, value;
int ret;
while ((ret = cursor.prev()) == 0) {
key = cursor.getKeyString();
value = cursor.getValueString();
}
return (ret);
}

If the Cursor.prev method is called on a cursor without a position in the data source, it is positioned at the end of the data source.

To position a cursor at a specific location in the data source, use the Cursor.search method:

public static int
cursor_search(Cursor cursor)
throws WiredTigerException
{
String value;
int ret;
cursor.putKeyString("foo");
if ((ret = cursor.search()) != 0)
value = cursor.getValueString();
return (ret);
}

To position a cursor at or near a location in the data source, use the Cursor.search_near method:

public static int
cursor_search_near(Cursor cursor)
throws WiredTigerException
{
String key, value;
SearchStatus exact;
key = "foo";
cursor.putKeyString(key);
exact = cursor.search_near();
if (exact == SearchStatus.SMALLER)
/* Returned key smaller than search key */
key = cursor.getKeyString();
else if (exact == SearchStatus.LARGER)
/* Returned key larger than search key */
key = cursor.getKeyString();
/* Else exact match found, and key already set */
value = cursor.getValueString();
return (0);
}

Cursor positions do not survive transactions: cursors that are open during Session.begin_transaction, Session.commit_transaction or Session.rollback_transaction will lose their position as if Cursor.reset was called.

Cursors can be configured to move to a random position with Cursor.next is called, see Cursor random in Java for details.

# Inserting and updating

To insert new data, and optionally update existing data, using a cursor, use the Cursor.insert method:

public static int
cursor_insert(Cursor cursor)
throws WiredTigerException
{
cursor.putKeyString("foo");
cursor.putValueString("bar");
return (cursor.insert());
}

To update existing data using a cursor, use the Cursor.update method:

public static int
cursor_update(Cursor cursor)
throws WiredTigerException
{
cursor.putKeyString("foo");
cursor.putValueString("newbar");
return (cursor.update());
}

To remove existing data using a cursor, use the Cursor.remove method:

public static int
cursor_remove(Cursor cursor)
throws WiredTigerException
{
cursor.putKeyString("foo");
return (cursor.remove());
}

The Session.open_cursor `overwrite` configuration is `true` by default, causing Cursor.insert, Cursor.remove and Cursor.update to ignore the current state of the record, and these methods will succeed regardless of whether or not the record previously exists.

When an application configures `overwrite` to `false`, Cursor.insert will fail with WT_DUPLICATE_KEY if the record previously exists, and Cursor.update and Cursor.remove will fail with WT_NOTFOUND if the record does not previously exist.

# Cursor position after error

After any cursor handle method failure, the cursor's position is undetermined. For cursor operations that expect a key to be set before the operation begins (including Cursor.search, Cursor.insert, Cursor.update and Cursor.remove), the application's key and value will not be cleared by an error.

Applications that cannot re-position the cursor after failure must duplicate the cursor by calling Session.open_cursor and passing the cursor as the `to_dup` parameter before calling a cursor method that will attempt to re-position the cursor. Cursor duplication is not supported for the backup, config and statistics cursor types.

# Cursor key/value memory scoping

When applications pass a pointer (either to a WT_ITEM or a string), to Cursor.set_key or Cursor.set_value, WiredTiger does not copy the memory referenced by the pointer. For this reason, the application must keep the referenced memory unchanged and valid until the next operation that successfully positions the cursor, modifies the underlying data, or the cursor is reset or closed (discarding its resources). The operations that position the cursor are Cursor.next, Cursor.prev, Cursor.search and Cursor.search_near; the operations that modify the underlying data are Cursor.insert, Cursor.update and Cursor.remove.

If a cursor operation fails (for example, due to a WT_ROLLBACK error), it may be retried without calling Cursor.set_key or Cursor.set_value again. That is, the cursor may still reference the application-supplied memory until the cursor is successfully positioned, underlying data is modified, or the cursor is closed or reset.