Version 3.0.0
WT_CURSOR Struct Reference

A WT_CURSOR handle is the interface to a cursor. More...

Public Member Functions

int close (WT_CURSOR *cursor)
 Close the cursor. More...
 
int reconfigure (WT_CURSOR *cursor, const char *config)
 Reconfigure the cursor. More...
 
Data access
int get_key (WT_CURSOR *cursor,...)
 Get the key for the current record. More...
 
int get_value (WT_CURSOR *cursor,...)
 Get the value for the current record. More...
 
void set_key (WT_CURSOR *cursor,...)
 Set the key for the next operation. More...
 
void set_value (WT_CURSOR *cursor,...)
 Set the value for the next operation. More...
 
Cursor positioning
int compare (WT_CURSOR *cursor, WT_CURSOR *other, int *comparep)
 Return the ordering relationship between two cursors: both cursors must have the same data source and have valid keys. More...
 
int equals (WT_CURSOR *cursor, WT_CURSOR *other, int *equalp)
 Return the ordering relationship between two cursors, testing only for equality: both cursors must have the same data source and have valid keys. More...
 
int next (WT_CURSOR *cursor)
 Return the next record. More...
 
int prev (WT_CURSOR *cursor)
 Return the previous record. More...
 
int reset (WT_CURSOR *cursor)
 Reset the cursor. More...
 
int search (WT_CURSOR *cursor)
 Return the record matching the key. More...
 
int search_near (WT_CURSOR *cursor, int *exactp)
 Return the record matching the key if it exists, or an adjacent record. More...
 
Data modification
int insert (WT_CURSOR *cursor)
 Insert a record and optionally update an existing record. More...
 
int modify (WT_CURSOR *cursor, WT_MODIFY *entries, int nentries)
 Modify an existing record. More...
 
int update (WT_CURSOR *cursor)
 Update an existing record and optionally insert a record. More...
 
int remove (WT_CURSOR *cursor)
 Remove a record. More...
 
int reserve (WT_CURSOR *cursor)
 Reserve an existing record so a subsequent write is less likely to fail due to a conflict between concurrent operations. More...
 

Public Attributes

WT_SESSIONsession
 The session handle for this cursor. More...
 
const char * uri
 The name of the data source for the cursor, matches the uri parameter to WT_SESSION::open_cursor used to open the cursor.
 
const char * key_format
 The format of the data packed into key items. More...
 
const char * value_format
 The format of the data packed into value items. More...
 

Detailed Description

A WT_CURSOR handle is the interface to a cursor.

Cursors allow data to be searched, iterated and modified, implementing the CRUD (create, read, update and delete) operations. Cursors are opened in the context of a session. If a transaction is started, cursors operate in the context of the transaction until the transaction is resolved.

Raw data is represented by key/value pairs of WT_ITEM structures, but cursors can also provide access to fields within the key and value if the formats are described in the WT_SESSION::create method.

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. See WT_SESSION::open_cursor for more information.

Thread safety: A WT_CURSOR handle is not usually shared between threads, see Multithreading for more information.

Examples:
ex_access.c, ex_call_center.c, ex_cursor.c, ex_encrypt.c, ex_extractor.c, ex_file_system.c, ex_log.c, ex_schema.c, ex_stat.c, and ex_thread.c.

Member Function Documentation

◆ close()

int WT_CURSOR::close ( WT_CURSOR cursor)

Close the cursor.

This releases the resources associated with the cursor handle. Cursors are closed implicitly by ending the enclosing connection or closing the session in which they were opened.

error_check(cursor->close(cursor));
Parameters
cursorthe cursor handle
Returns
zero on success and a non-zero error code on failure. See Error handling for details.
Examples:
ex_call_center.c, ex_cursor.c, ex_encrypt.c, ex_extractor.c, ex_file_system.c, ex_log.c, ex_schema.c, and ex_stat.c.

◆ compare()

int WT_CURSOR::compare ( WT_CURSOR cursor,
WT_CURSOR other,
int *  comparep 
)

Return the ordering relationship between two cursors: both cursors must have the same data source and have valid keys.

(When testing only for equality, WT_CURSOR::equals may be faster.)

int compare;
error_check(cursor->compare(cursor, other, &compare));
if (compare == 0) {
/* Cursors reference the same key */
} else if (compare < 0) {
/* Cursor key less than other key */
} else if (compare > 0) {
/* Cursor key greater than other key */
}
Parameters
cursorthe cursor handle
otheranother cursor handle
comparepthe status of the comparison: < 0 if cursor refers to a key that appears before other, 0 if the cursors refer to the same key, and > 0 if cursor refers to a key that appears after other.
Returns
zero on success and a non-zero error code on failure. See Error handling for details.

◆ equals()

int WT_CURSOR::equals ( WT_CURSOR cursor,
WT_CURSOR other,
int *  equalp 
)

Return the ordering relationship between two cursors, testing only for equality: both cursors must have the same data source and have valid keys.

int equal;
error_check(cursor->equals(cursor, other, &equal));
if (equal) {
/* Cursors reference the same key */
} else {
/* Cursors don't reference the same key */
}
Parameters
cursorthe cursor handle
otheranother cursor handle
[out]equalpthe status of the comparison: 1 if the cursors refer to the same key, otherwise 0.
Returns
zero on success and a non-zero error code on failure. See Error handling for details.

◆ get_key()

int WT_CURSOR::get_key ( WT_CURSOR cursor,
  ... 
)

Get the key for the current record.

const char *key; /* Get the cursor's string key. */
error_check(cursor->get_key(cursor, &key));
uint64_t recno; /* Get the cursor's record number key. */
error_check(cursor->get_key(cursor, &recno));
Parameters
cursorthe cursor handle
...pointers to hold key fields corresponding to WT_CURSOR::key_format.
Returns
zero on success and a non-zero error code on failure. See Error handling for details.
Examples:
ex_access.c, ex_cursor.c, ex_encrypt.c, ex_extractor.c, ex_file_system.c, ex_log.c, ex_schema.c, and ex_thread.c.

◆ get_value()

int WT_CURSOR::get_value ( WT_CURSOR cursor,
  ... 
)

Get the value for the current record.

const char *value; /* Get the cursor's string value. */
error_check(cursor->get_value(cursor, &value));
WT_ITEM value; /* Get the cursor's raw value. */
error_check(cursor->get_value(cursor, &value));
Parameters
cursorthe cursor handle
...pointers to hold value fields corresponding to WT_CURSOR::value_format.
Returns
zero on success and a non-zero error code on failure. See Error handling for details.
Examples:
ex_access.c, ex_call_center.c, ex_cursor.c, ex_encrypt.c, ex_extractor.c, ex_log.c, ex_schema.c, ex_stat.c, and ex_thread.c.

◆ insert()

int WT_CURSOR::insert ( WT_CURSOR cursor)

Insert a record and optionally update an existing record.

If the cursor was configured with "overwrite=true" (the default), both the key and value must be set; if the record already exists, the key's value will be updated, otherwise, the record will be inserted.

/* Insert a new record or overwrite an existing record. */
const char *key = "some key", *value = "some value";
error_check(session->open_cursor(
session, "table:mytable", NULL, NULL, &cursor));
cursor->set_key(cursor, key);
cursor->set_value(cursor, value);
error_check(cursor->insert(cursor));

If the cursor was not configured with "overwrite=true", both the key and value must be set and the record must not already exist; the record will be inserted.

/* Insert a new record and fail if the record exists. */
const char *key = "new key", *value = "some value";
error_check(session->open_cursor(
session, "table:mytable", NULL, "overwrite=false", &cursor));
cursor->set_key(cursor, key);
cursor->set_value(cursor, value);
error_check(cursor->insert(cursor));

If a cursor with record number keys was configured with "append=true" (not the default), the value must be set; a new record will be appended and the record number set as the cursor key value.

/* Insert a new record and assign a record number. */
uint64_t recno;
const char *value = "some value";
cursor->set_value(cursor, value);
error_check(cursor->insert(cursor));
error_check(cursor->get_key(cursor, &recno));

The cursor ends with no position, and a subsequent call to the WT_CURSOR::next (WT_CURSOR::prev) method will iterate from the beginning (end) of the table.

If the cursor does not have record number keys or was not configured with "append=true", the cursor ends with no key set and a subsequent call to the WT_CURSOR::get_key method will fail. The cursor ends with no value set and a subsequent call to the WT_CURSOR::get_value method will fail.

Inserting a new record after the current maximum record in a fixed-length bit field column-store (that is, a store with an 'r' type key and 't' type value) may implicitly create the missing records as records with a value of 0.

When loading a large amount of data into a new object, using a cursor with the bulk configuration string enabled and loading the data in sorted order will be much faster than doing out-of-order inserts. See Bulk-load for more information.

The maximum length of a single column stored in a table is not fixed (as it partially depends on the underlying file configuration), but is always a small number of bytes less than 4GB.

Parameters
cursorthe cursor handle
Returns
zero on success and a non-zero error code on failure. See Error handling for details. In particular, if overwrite=false is configured and a record with the specified key already exists, WT_DUPLICATE_KEY is returned. Also, if in_memory is configured for the database and the insert requires more than the configured cache size to complete, WT_CACHE_FULL is returned.
Examples:
ex_access.c, ex_call_center.c, ex_cursor.c, ex_encrypt.c, ex_extractor.c, ex_file_system.c, ex_log.c, ex_schema.c, ex_stat.c, and ex_thread.c.

◆ modify()

int WT_CURSOR::modify ( WT_CURSOR cursor,
WT_MODIFY entries,
int  nentries 
)

Modify an existing record.

Both the key and value must be set and the record must already exist; the record will be updated.

Modification structures are applied in order, and later modifications can update earlier modifications.

The modify method is only supported on raw byte arrays accessed using a WT_ITEM structure, that is, a format type of u.

Calling the WT_CURSOR::modify method outside of snapshot isolation can lead to unexpected results. While read-committed isolation is supported with the WT_CURSOR::modify method, read-uncommitted isolation is not.

WT_MODIFY entries[3];
const char *key = "some key";
/* Position the cursor. */
cursor->set_key(cursor, key);
error_check(cursor->search(cursor));
/* Replace 20 bytes starting at byte offset 5. */
entries[0].data.data = "some data";
entries[0].data.size = strlen(entries[0].data.data);
entries[0].offset = 5;
entries[0].size = 20;
/* Insert data at byte offset 40. */
entries[1].data.data = "and more data";
entries[1].data.size = strlen(entries[1].data.data);
entries[1].offset = 40;
entries[1].size = 0;
/* Replace 2 bytes starting at byte offset 10. */
entries[2].data.data = "and more data";
entries[2].data.size = strlen(entries[2].data.data);
entries[2].offset = 10;
entries[2].size = 2;
error_check(cursor->modify(cursor, entries, 3));

On success, the cursor ends positioned at the modified record; to minimize cursor resources, the WT_CURSOR::reset method should be called as soon as the cursor no longer needs that position.

The maximum length of a single column stored in a table is not fixed (as it partially depends on the underlying file configuration), but is always a small number of bytes less than 4GB.

The WT_CURSOR::modify method stores a change record in cache and writes a change record to the log, instead of the usual complete value. This can reduce cache and logging requirements, but may result in slower reads because the complete value must be assembled during retrieval.

Parameters
cursorthe cursor handle
entriesan array of modification data structures
nentriesthe number of modification data structures
Returns
zero on success and a non-zero error code on failure. See Error handling for details. In particular, if in_memory is configured for the database and the modify requires more than the configured cache size to complete, WT_CACHE_FULL is returned.

◆ next()

int WT_CURSOR::next ( WT_CURSOR cursor)

Return the next record.

error_check(cursor->next(cursor));
Parameters
cursorthe cursor handle
Returns
zero on success and a non-zero error code on failure. See Error handling for details.
Examples:
ex_access.c, ex_cursor.c, ex_encrypt.c, ex_extractor.c, ex_file_system.c, ex_log.c, ex_schema.c, ex_stat.c, and ex_thread.c.

◆ prev()

int WT_CURSOR::prev ( WT_CURSOR cursor)

Return the previous record.

error_check(cursor->prev(cursor));
Parameters
cursorthe cursor handle
Returns
zero on success and a non-zero error code on failure. See Error handling for details.
Examples:
ex_call_center.c, and ex_cursor.c.

◆ reconfigure()

int WT_CURSOR::reconfigure ( WT_CURSOR cursor,
const char *  config 
)

Reconfigure the cursor.

The cursor is reset.

error_check(session->open_cursor(
session, "table:mytable", NULL, "overwrite=false", &cursor));
/* Reconfigure the cursor to overwrite the record. */
error_check(cursor->reconfigure(cursor, "overwrite=true"));
cursor->set_key(cursor, key);
cursor->set_value(cursor, value);
error_check(cursor->insert(cursor));
Parameters
cursorthe cursor handle
configConfiguration string, see Configuration Strings. Permitted values:
NameEffectValues
appendappend the value as a new record, creating a new record number key; valid only for cursors with record number keys.a boolean flag; default false.
overwriteconfigures whether the cursor's insert, update and remove methods check the existing state of the record. If overwrite is false, WT_CURSOR::insert fails with WT_DUPLICATE_KEY if the record exists, WT_CURSOR::update and WT_CURSOR::remove fail with WT_NOTFOUND if the record does not exist.a boolean flag; default true.
Returns
zero on success and a non-zero error code on failure. See Error handling for details.

◆ remove()

int WT_CURSOR::remove ( WT_CURSOR cursor)

Remove a record.

If the cursor was configured with "overwrite=true" (the default), the key must be set; the key's record will be removed if it exists, no error will be returned if the record does not exist.

const char *key = "some key";
error_check(session->open_cursor(
session, "table:mytable", NULL, NULL, &cursor));
cursor->set_key(cursor, key);
error_check(cursor->remove(cursor));

If the cursor was configured with "overwrite=false" (not the default), the key must be set and the key's record must exist; the record will be removed.

Any cursor position does not change: if the cursor was positioned before the WT_CURSOR::remove call, the cursor remains positioned at the removed record; to minimize cursor resources, the WT_CURSOR::reset method should be called as soon as the cursor no longer needs that position. If the cursor was not positioned before the WT_CURSOR::remove call, the cursor ends with no position, and a subsequent call to the WT_CURSOR::next (WT_CURSOR::prev) method will iterate from the beginning (end) of the table.

const char *key = "some key";
error_check(session->open_cursor(
session, "table:mytable", NULL, "overwrite=false", &cursor));
cursor->set_key(cursor, key);
error_check(cursor->remove(cursor));

Removing a record in a fixed-length bit field column-store (that is, a store with an 'r' type key and 't' type value) is identical to setting the record's value to 0.

Parameters
cursorthe cursor handle
Returns
zero on success and a non-zero error code on failure. See Error handling for details. In particular, if overwrite=false is configured and no record with the specified key exists, WT_NOTFOUND is returned.
Examples:
ex_cursor.c, and ex_extractor.c.

◆ reserve()

int WT_CURSOR::reserve ( WT_CURSOR cursor)

Reserve an existing record so a subsequent write is less likely to fail due to a conflict between concurrent operations.

The key must first be set and the record must already exist.

const char *key = "some key";
error_check(session->begin_transaction(session, NULL));
cursor->set_key(cursor, key);
error_check(cursor->reserve(cursor));
error_check(session->commit_transaction(session, NULL));

On success, the cursor ends positioned at the specified record; to minimize cursor resources, the WT_CURSOR::reset method should be called as soon as the cursor no longer needs that position.

Parameters
cursorthe cursor handle
Returns
zero on success and a non-zero error code on failure. See Error handling for details.

◆ reset()

int WT_CURSOR::reset ( WT_CURSOR cursor)

Reset the cursor.

Any resources held by the cursor are released, and the cursor's key and position are no longer valid. Subsequent iterations with WT_CURSOR::next will move to the first record, or with WT_CURSOR::prev will move to the last record.

In the case of a statistics cursor, resetting the cursor refreshes the statistics information returned.

error_check(cursor->reset(cursor));
Parameters
cursorthe cursor handle
Returns
zero on success and a non-zero error code on failure. See Error handling for details.
Examples:
ex_access.c, ex_cursor.c, and ex_log.c.

◆ search()

int WT_CURSOR::search ( WT_CURSOR cursor)

Return the record matching the key.

The key must first be set.

const char *key = "some key";
cursor->set_key(cursor, key);
error_check(cursor->search(cursor));

On success, the cursor ends positioned at the returned record; to minimize cursor resources, the WT_CURSOR::reset method should be called as soon as the record has been retrieved and the cursor no longer needs that position.

Parameters
cursorthe cursor handle
Returns
zero on success and a non-zero error code on failure. See Error handling for details.
Examples:
ex_call_center.c, ex_cursor.c, ex_extractor.c, ex_log.c, ex_schema.c, and ex_stat.c.

◆ search_near()

int WT_CURSOR::search_near ( WT_CURSOR cursor,
int *  exactp 
)

Return the record matching the key if it exists, or an adjacent record.

An adjacent record is either the smallest record larger than the key or the largest record smaller than the key (in other words, a logically adjacent key).

The key must first be set.

An example of a search for an exact or adjacent match:

cursor->set_key(cursor, key);
error_check(cursor->search_near(cursor, &exact));
if (exact == 0) {
/* an exact match */
} else if (exact < 0) {
/* returned smaller key */
} else if (exact > 0) {
/* returned larger key */
}

An example of a forward scan through the table, where all keys greater than or equal to a specified prefix are included in the scan:

cursor->set_key(cursor, key);
error_check(cursor->search_near(cursor, &exact));
if (exact >= 0) {
/* include first key returned in the scan */
}
while ((ret = cursor->next(cursor)) == 0) {
/* the rest of the scan */
}
scan_end_check(ret == WT_NOTFOUND);

An example of a backward scan through the table, where all keys less than a specified prefix are included in the scan:

cursor->set_key(cursor, key);
error_check(cursor->search_near(cursor, &exact));
if (exact < 0) {
/* include first key returned in the scan */
}
while ((ret = cursor->prev(cursor)) == 0) {
/* the rest of the scan */
}
scan_end_check(ret == WT_NOTFOUND);

On success, the cursor ends positioned at the returned record; to minimize cursor resources, the WT_CURSOR::reset method should be called as soon as the record has been retrieved and the cursor no longer needs that position.

Parameters
cursorthe cursor handle
exactpthe status of the search: 0 if an exact match is found, < 0 if a smaller key is returned, > 0 if a larger key is returned
Returns
zero on success and a non-zero error code on failure. See Error handling for details.
Examples:
ex_call_center.c, and ex_cursor.c.

◆ set_key()

void WT_CURSOR::set_key ( WT_CURSOR cursor,
  ... 
)

Set the key for the next operation.

/* Set the cursor's string key. */
const char *key = "another key";
cursor->set_key(cursor, key);
uint64_t recno = 37; /* Set the cursor's record number key. */
cursor->set_key(cursor, recno);
Parameters
cursorthe cursor handle
...key fields corresponding to WT_CURSOR::key_format.

If an error occurs during this operation, a flag will be set in the cursor, and the next operation to access the key will fail. This simplifies error handling in applications.

Examples:
ex_access.c, ex_call_center.c, ex_cursor.c, ex_encrypt.c, ex_extractor.c, ex_file_system.c, ex_log.c, ex_schema.c, ex_stat.c, and ex_thread.c.

◆ set_value()

void WT_CURSOR::set_value ( WT_CURSOR cursor,
  ... 
)

Set the value for the next operation.

/* Set the cursor's string value. */
const char *value = "another value";
cursor->set_value(cursor, value);
WT_ITEM value; /* Set the cursor's raw value. */
value.data = "another value";
value.size = strlen("another value");
cursor->set_value(cursor, &value);
Parameters
cursorthe cursor handle
...value fields corresponding to WT_CURSOR::value_format.

If an error occurs during this operation, a flag will be set in the cursor, and the next operation to access the value will fail. This simplifies error handling in applications.

Examples:
ex_access.c, ex_call_center.c, ex_cursor.c, ex_encrypt.c, ex_extractor.c, ex_file_system.c, ex_log.c, ex_schema.c, ex_stat.c, and ex_thread.c.

◆ update()

int WT_CURSOR::update ( WT_CURSOR cursor)

Update an existing record and optionally insert a record.

If the cursor was configured with "overwrite=true" (the default), both the key and value must be set; if the record already exists, the key's value will be updated, otherwise, the record will be inserted.

const char *key = "some key", *value = "some value";
error_check(session->open_cursor(
session, "table:mytable", NULL, NULL, &cursor));
cursor->set_key(cursor, key);
cursor->set_value(cursor, value);
error_check(cursor->update(cursor));

If the cursor was not configured with "overwrite=true", both the key and value must be set and the record must already exist; the record will be updated.

const char *key = "some key", *value = "some value";
error_check(session->open_cursor(
session, "table:mytable", NULL, "overwrite=false", &cursor));
cursor->set_key(cursor, key);
cursor->set_value(cursor, value);
error_check(cursor->update(cursor));

On success, the cursor ends positioned at the modified record; to minimize cursor resources, the WT_CURSOR::reset method should be called as soon as the cursor no longer needs that position. (The WT_CURSOR::insert method never keeps a cursor position and may be more efficient for that reason.)

The maximum length of a single column stored in a table is not fixed (as it partially depends on the underlying file configuration), but is always a small number of bytes less than 4GB.

Parameters
cursorthe cursor handle
Returns
zero on success and a non-zero error code on failure. See Error handling for details. In particular, if overwrite=false is configured and no record with the specified key exists, WT_NOTFOUND is returned. Also, if in_memory is configured for the database and the update requires more than the configured cache size to complete, WT_CACHE_FULL is returned.
Examples:
ex_cursor.c, and ex_schema.c.

Member Data Documentation

◆ key_format

const char* WT_CURSOR::key_format

The format of the data packed into key items.

See Packing and Unpacking Data for details. If not set, a default value of "u" is assumed, and applications must use WT_ITEM structures to manipulate untyped byte arrays.

◆ session

WT_SESSION* WT_CURSOR::session

The session handle for this cursor.

◆ value_format

const char* WT_CURSOR::value_format

The format of the data packed into value items.

See Packing and Unpacking Data for details. If not set, a default value of "u" is assumed, and applications must use WT_ITEM structures to manipulate untyped byte arrays.