Version 3.0.0
Getting Started with the API in Java

WiredTiger applications will generally use the following classes to access and manage data:

  • a Connection represents a connection to a database. Most applications will only open one connection to a database for each process. All methods in Connection are thread safe.
  • a Session represents a context in which database operations are performed. Sessions are opened on a specified connection, and applications must open a single session for each thread accessing the database.
  • a Cursor represents a cursor over a collection of data. Cursors are opened in the context of a session (which may have an associated transaction), and can query and update records. In the common case, a cursor is used to access records in a table. However, cursors can be used on subsets of tables (such as a single column or a projection of multiple columns), as an interface to statistics, configuration data or application-specific data sources.

Handles and operations are configured using strings, which keeps the set of methods in the API relatively small and makes the interface very similar regardless of the programming language used in the application. WiredTiger supports the C, C++, Java and Python programming languages (among others).

By default, WiredTiger works as a traditional key/value store, where the keys and values are raw byte arrays. Keys and values may be up to (4GB - 512B) bytes in size, but depending on how Session.create "maximum item sizes" are configured, large key and value items will be stored on overflow pages.

WiredTiger also supports a schema layer so that keys and values types can be chosen from a list, or composite keys or values made up of columns with any combination of types. The size (4GB - 512B) byte limit on keys and values still applies.

All applications that use WiredTiger will be structured roughly as follows. The code below is taken from the complete example program ex_access.java.

Connecting to a database

To access a database, first open a connection and create a session handle for the single thread accessing the database:

Connection conn;
Session s;
Cursor c;
try {
conn = wiredtiger.open("WT_HOME", "create");
s = conn.open_session(null);
} catch (WiredTigerException wte) {
System.err.println("WiredTigerException: " + wte);
return;
}

The configuration string "create" is passed to wiredtiger.open to indicate the database should be created if it does not already exist.

The code block above also shows simple error handling by catching WiredTigerException.

Creating a table

Create a table we can use to store data:

s.create("table:t", "key_format=S,value_format=u");

This call creates a table called "access", configured to use strings for its key and value columns. (See Schema, Columns, Column Groups, Indices and Projections in Java for more information on tables with other types of key and value columns.)

Accessing data with cursors

Now that we have a table, we open a cursor to perform some operations on it:

c = s.open_cursor("table:t", null, null);

Here, the string "table:access" specifies that we are opening the cursor on the table named "access".

Then we insert a new row into the table. The Cursor.set_key and Cursor.set_value calls put the application's key and value into the cursor, respectively. The Cursor.insert call creates a record containing that value and inserts it into the table.

try {
c.putKeyString("foo");
c.putValueByteArray("bar".getBytes());
c.insert();
} catch (WiredTigerPackingException wtpe) {
System.err.println("WiredTigerPackingException: " + wtpe);
} catch (WiredTigerException wte) {
System.err.println("WiredTigerException: " + wte);
}

Now we iterate through all of the records in the table, printing them out as we go:

try {
c.reset();
while (c.next() == 0) {
System.out.println("Got: " + c.getKeyString());
}
} catch (WiredTigerPackingException wtpe) {
System.err.println("WiredTigerPackingException: " + wtpe);
} catch (WiredTigerException wte) {
System.err.println("WiredTigerException: " + wte);
}

Note that the key and value parts of the records are returned as C strings because the table was created that way (even if it was created by a previous run of the example). No data extraction or conversion is required in the application.

Because the cursor was positioned in the table after the Cursor.insert call, we had to re-position it using the Cursor.reset call; if we weren't using the cursor for the call to Cursor.insert above, this loop would simplify to:

while ((ret = cursor->next(cursor)) == 0) {
...
}

Closing handles

Lastly, we close the connection, which implicitly closes the cursor and session handles:

try {
conn.close(null);
} catch (WiredTigerException wte) {
System.err.println("WiredTigerException: " + wte);
}