WiredTiger(3)
👑 WiredTiger 👑
okay so bestie, welcome to the WiredTiger manual, this is literally the "getting started with the API" page and it's gonna be so good, i promise 🫶
🎀 NAME 🎀
WiredTiger — Getting Started with the API ✨
🌸 THE VIBE CHECK 🌸
so like, WiredTiger applications will generally use the following classes to access and manage data, and honestly? iconic lineup:
- a
WT_CONNECTIONrepresents a connection to a database. Most applications will only open one connection to a database for each process. All methods inWT_CONNECTIONare thread safe — no cap. 💅 - a
WT_SESSIONrepresents 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. one thread, one session, that's the rule. 🧚 - a
WT_CURSORrepresents 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. so many vibes from one little cursor 😭
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). multilingual king. 🌈
By default, WiredTiger works as a traditional key/value store, where the keys and values are raw byte arrays accessed using a WT_ITEM structure. Keys and values may be up to (4GB - 512B) bytes in size, but depending on how WT_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.c. 🍓
🔌 Connecting to a database 🔌
To access a database, first open a connection and create a session handle for the single thread accessing the database:
WT_CONNECTION *conn;
WT_CURSOR *cursor;
WT_SESSION *session;
const char *key, *value;
int ret;
/* Open a connection to the database, creating it if necessary. */
error_check(wiredtiger_open(home, NULL, "create", &conn));
/* Open a session handle for the database. */
error_check(conn->open_session(conn, NULL, NULL, &session));
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 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_EVENT_HANDLER to wiredtiger_open or WT_CONNECTION::open_session. fancy error handling, we love to see it 💜
🛠️ Creating a table 🛠️
Create a table we can use to store data:
error_check(session->create(session, "table:access", "key_format=S,value_format=S"));
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 for more information on tables with other types of key and value columns.) slay.
🔍 Accessing data with cursors 🔍
Now that we have a table, we open a cursor to perform some operations on it:
error_check(session->open_cursor(session, "table:access", NULL, NULL, &cursor));
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 WT_CURSOR::set_key and WT_CURSOR::set_value calls put the application's key and value into the cursor, respectively. The WT_CURSOR::insert call creates a record containing that value and inserts it into the table.
cursor->set_key(cursor, "key1"); /* Insert a record. */
cursor->set_value(cursor, "value1");
error_check(cursor->insert(cursor));
Now we iterate through all of the records in the table, printing them out as we go:
error_check(cursor->reset(cursor)); /* Restart the scan. */
while ((ret = cursor->next(cursor)) == 0) {
error_check(cursor->get_key(cursor, &key));
error_check(cursor->get_value(cursor, &value));
printf("Got record: %s : %s0, key, value);
}
scan_end_check(ret == WT_NOTFOUND); /* Check for end-of-table. */
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. we love a zero-effort glow up ✨
Because the cursor was positioned in the table after the WT_CURSOR::insert call, we had to re-position it using the WT_CURSOR::reset call; if we weren't using the cursor for the call to WT_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:
error_check(conn->close(conn, NULL)); /* Close all handles. */
okay bestie real talk: conn->close closes everything — cursor and session handles included. so don't go reaching for them after, ok? 😭
and that's the whole page!! go build something gorgeous, you've got this 💖
this little tour came to you from Version 3.2.1, dated Tue Aug 27 2019 — that's the WiredTiger(3) manual page, mwah, bye bestie 💅🎀🦋
📦 where this page comes from
- debian bookworm: wiredtiger 3.2.1-1 · GPL-2.0-or-later
- debian trixie: wiredtiger 3.2.1-1 · GPL-2.0-or-later
- ubuntu jammy: wiredtiger 3.2.1-1ubuntu1 · GPL-2.0-or-later
- ubuntu noble: wiredtiger 3.2.1-1ubuntu1 · GPL-2.0-or-later
- ubuntu questing: wiredtiger 3.2.1-1ubuntu4
- ubuntu resolute: wiredtiger 3.2.1-1ubuntu5 · GPL-2.0-or-later
- ubuntu stonking: wiredtiger 3.2.1-1ubuntu5 · GPL-2.0-or-later
- 💖 girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms