2011-01-06 21:45:04 +11:00
|
|
|
/*! @page basic_api Getting Started with the API
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-09-23 22:46:49 +10:00
|
|
|
WiredTiger applications will generally use the following classes to access
|
|
|
|
|
and manage data:
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-09-23 22:46:49 +10:00
|
|
|
- a WT_CONNECTION represents a connection to a database. Most
|
|
|
|
|
applications will only open one connection to a database for each process.
|
|
|
|
|
All methods in WT_CONNECTION are thread safe.
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-01-06 21:45:04 +11:00
|
|
|
- a WT_SESSION represents a context in which database operations are
|
2011-09-23 22:46:49 +10:00
|
|
|
performed. Sessions are opened on a specified connection, and
|
|
|
|
|
applications must open a single session for each thread accessing the
|
|
|
|
|
database.
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-01-06 21:45:04 +11:00
|
|
|
- a WT_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.
|
|
|
|
|
|
2011-09-23 22:46:49 +10:00
|
|
|
Handles and operations are @ref config_strings "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
|
|
|
|
|
key and value are raw byte arrays accessed with WT_ITEM. Keys can be up to
|
|
|
|
|
4GB, but depending on how @ref WT_SESSION::create "page sizes" are
|
|
|
|
|
configured, keys larger than some size will be stored on overflow pages.
|
|
|
|
|
|
|
|
|
|
WiredTiger also supports a @ref schema "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 4GB limit on keys and values
|
|
|
|
|
still applies.
|
2011-01-06 21:45:04 +11:00
|
|
|
|
|
|
|
|
@dontinclude ex_access.c
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-09-23 22:46:49 +10:00
|
|
|
All applications that use WiredTiger will be structured roughly as follows.
|
|
|
|
|
The code below is taken from the complete example program
|
|
|
|
|
@ex_ref{ex_access.c}.
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-01-06 21:45:04 +11:00
|
|
|
@section basic_connection Connecting to a database
|
|
|
|
|
|
|
|
|
|
To access a database, first open a connection with the following code:
|
|
|
|
|
@skip main
|
|
|
|
|
@until Note
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-09-23 22:46:49 +10:00
|
|
|
Here the configuration string @c "create" is passed to ::wiredtiger_open to
|
|
|
|
|
indicate that the database should be created if it does not already exist.
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-01-06 21:45:04 +11:00
|
|
|
Next we open a session handle for the single thread accessing the database.
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-09-23 22:46:49 +10:00
|
|
|
The code block above also shows simple error handling with
|
|
|
|
|
::wiredtiger_strerror (a function that returns a string describing an error
|
|
|
|
|
code passed as its argument). More complex error handling can be
|
|
|
|
|
configured by passing an implementation of WT_ERROR_HANDLER to
|
|
|
|
|
wiredtiger_open or WT_CONNECTION::open_session.
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-01-06 21:45:04 +11:00
|
|
|
@section basic_create_table Creating a Table
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-09-23 22:46:49 +10:00
|
|
|
If the database was created by the ::wiredtiger_open call above, it will be
|
|
|
|
|
empty. We now create a table that we can use to store data:
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-01-06 21:45:04 +11:00
|
|
|
@until ;
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-09-23 22:46:49 +10:00
|
|
|
This call creates a table called @c "access", configured to use strings for
|
|
|
|
|
its key and value columns. We go into more details about what is possible
|
|
|
|
|
later in the section on @ref schema.
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-01-06 21:45:04 +11:00
|
|
|
@section basic_cursors Accessing Data With Cursors
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-09-23 22:46:49 +10:00
|
|
|
Now that we're sure we have a table, we open a cursor to perform some
|
|
|
|
|
operations on it:
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-01-06 21:45:04 +11:00
|
|
|
@until insert
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-09-23 22:46:49 +10:00
|
|
|
Here, the string @c "table:access" specifies that we are opening the cursor
|
|
|
|
|
on the table named @c "access" that we created above.
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-09-23 22:46:49 +10:00
|
|
|
The WT_CURSOR::set_key and WT_CURSOR::set_value calls put the application's
|
|
|
|
|
data into the cursor. The WT_CURSOR::insert call creates a record
|
|
|
|
|
containing that data and inserts it into the table.
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-09-23 22:46:49 +10:00
|
|
|
Now we iterate through all of the records in the table, printing them out
|
|
|
|
|
as we go:
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-01-06 21:45:04 +11:00
|
|
|
@until }
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-09-23 22:46:49 +10:00
|
|
|
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.
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-09-23 22:46:49 +10:00
|
|
|
Because the cursor was positioned in the table after the WT_CURSOR::insert
|
|
|
|
|
call, we had to re-position it using the WT_CURSOR::first call; if we
|
|
|
|
|
weren't using the cursor for the call to WT_CURSOR::insert above, this loop
|
|
|
|
|
would simplify to:
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-01-06 21:45:04 +11:00
|
|
|
@code
|
2010-12-30 18:03:23 +11:00
|
|
|
while ((ret = cursor->next(cursor)) == 0) {
|
|
|
|
|
...
|
|
|
|
|
}
|
2011-01-06 21:45:04 +11:00
|
|
|
@endcode
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-01-06 21:45:04 +11:00
|
|
|
@section basic_close Closing Handles
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-09-23 22:46:49 +10:00
|
|
|
Lastly, we close the connection, which implicitly closes the cursor and
|
|
|
|
|
session handles:
|
2010-12-30 18:03:23 +11:00
|
|
|
|
2011-01-06 21:45:04 +11:00
|
|
|
@skipline close
|
2010-12-30 18:03:23 +11:00
|
|
|
*/
|