### WiredTiger Database Setup and Operations Source: https://source.wiredtiger.com/develop/ex_cursor_8c-example Illustrates the initial setup for a WiredTiger database, including opening a connection, creating a session, creating tables with specific schemas, and opening cursors for different tables and statistics. ```C int main(int argc, char *argv[]) { WT_CONNECTION *conn; WT_CURSOR *cursor; WT_SESSION *session; const char *home; /* home = example_setup(argc, argv); */ /* Open a connection to the database, creating it if necessary. */ /* error_check(wiredtiger_open(home, NULL, "create,statistics=(fast)", &conn)); */ /* Open a session for the current thread's work. */ /* error_check(conn->open_session(conn, NULL, NULL, &session)); */ /* Create a table:world with specified key and value formats. */ /* error_check(session->create(session, "table:world", "key_format=r,value_format=5sii,columns=(id,country,population,area)")); */ /* error_check(session->open_cursor(session, "table:world", NULL, NULL, &cursor)); */ /* Open cursors for specific table configurations. */ /* error_check(session->open_cursor(session, "table:world(country,population)", NULL, NULL, &cursor)); */ /* error_check(session->open_cursor(session, "statistics:", NULL, NULL, &cursor)); */ /* Create a simple string table:map. */ /* error_check(session->create(session, "table:map", "key_format=S,value_format=S")); */ /* error_check(session->open_cursor(session, "table:map", NULL, NULL, &cursor)); */ /* Perform various cursor operations. */ /* error_check(cursor_insert(cursor)); */ /* error_check(cursor_reset(cursor)); */ /* error_check(cursor_forward_scan(cursor)); */ /* error_check(cursor_reset(cursor)); */ /* error_check(cursor_reverse_scan(cursor)); */ /* error_check(cursor_search_near(cursor)); */ /* error_check(cursor_update(cursor)); */ /* error_check(cursor_remove(cursor)); */ /* error_check(cursor_insert(cursor)); */ /* Close the cursor. */ /* error_check(cursor->close(cursor)); */ /* Create a version cursor. */ /* error_check(session->open_cursor(session, "file:map.wt", NULL, "debug=(dump_version=(enabled=true))", &cursor)); */ /* error_check(version_cursor_dump(cursor)); */ return (0); } ``` -------------------------------- ### Setup WiredTiger Connection and Create Table Source: https://source.wiredtiger.com/develop/ex_col_store_8c-example Initializes a WiredTiger connection, opens a session, and creates a weather data table with specified formats and column groups. Handles potential errors during setup. ```C int main(int argc, char *argv[]) { [WT_CONNECTION](struct_w_t___c_o_n_n_e_c_t_i_o_n.html) *conn; [WT_CURSOR](struct_w_t___c_u_r_s_o_r.html) *cursor; [WT_SESSION](struct_w_t___s_e_s_s_i_o_n.html) *session; WEATHER weather_data[NUM_ENTRIES]; char countries[][NUM_COUNTRIES - 1] = {"AUS", "GBR", "USA", "NZD", "IND", "CHI", "RUS"}; int max_temp_result, min_temp_result, ret; uint16_t ending_time, starting_time; home = example_setup(argc, argv); /* Establishing a connection. */ error_check([wiredtiger_open](group__wt.html#gacbe8d118f978f5bfc8ccb4c77c9e8813)(home, NULL, "create,statistics=(fast)", &conn)); /* Establishing a session. */ error_check(conn->[open_session](struct_w_t___c_o_n_n_e_c_t_i_o_n.html#adad5965cd4a60f65b5ac01f7ca6d1fc0)(conn, NULL, NULL, &session)); /* Create a table with columns and colgroups. */ error_check(session->[create](struct_w_t___s_e_s_s_i_o_n.html#a358ca4141d59c345f401c58501276bbb)(session, TABLE_NAME, "key_format=r,value_format=" WT_UNCHECKED_STRING( HHHHBBBB5S5S) ",columns=(id,hour,pressure,loc_lat, "loc_long,temp,humidity," "wind,feels_like_temp,day,country),colgroups=(day_time,temperature," "humidity_pressure," "wind,feels_like_temp,location)")); /* Create the colgroups */ error_check(session->[create](struct_w_t___s_e_s_s_i_o_n.html#a358ca4141d59c345f401c58501276bbb)(session, "colgroup:weather:day_time", "columns=(hour,day)")); ``` -------------------------------- ### WiredTiger Incremental Backup C Example Source: https://source.wiredtiger.com/develop/ex_backup_8c-example Demonstrates the WiredTiger C API for performing incremental backups. Includes setup, data insertion, full backups, incremental backups, and verification using command-line tools. ```c /* * Public Domain 2014-present MongoDB, Inc. * Public Domain 2008-2014 WiredTiger, Inc. * * This is free and unencumbered software released into the public domain. * * Anyone is free to copy, modify, publish, use, compile, sell, or * distribute this software, either in source code form or as a compiled * binary, for any purpose, commercial or non-commercial, and by any * means. * * In jurisdictions that recognize copyright laws, the author or authors * of this software dedicate any and all copyright interest in the * software to the public domain. We make this dedication for the benefit * of the public at large and to the detriment of our heirs and * successors. We intend this dedication to be an overt act of * relinquishment in perpetuity of all present and future rights to this * software under copyright law. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * ex_backup.c * demonstrates how to use incremental backup. */ #include static const char *const home = "WT_BLOCK"; static const char *const home_full = "WT_BLOCK_LOG_FULL"; static const char *const home_incr = "WT_BLOCK_LOG_INCR"; static const char *const logpath = "logpath"; #define WTLOG "WiredTigerLog" #define WTLOGLEN strlen(WTLOG) static const char *const full_out = "./backup_block_full"; static const char *const incr_out = "./backup_block_incr"; static const char *const uri = "table:main"; static const char *const uri2 = "table:extra"; typedef struct __filelist { const char *name; bool exist; } FILELIST; static FILELIST *last_flist = NULL; static size_t filelist_count = 0; #define FLIST_INIT 16 #define CONN_CONFIG "create,cache_size=100MB,log=(enabled=true,path=logpath,file_max=100K)" #define MAX_ITERATIONS 5 #define MAX_KEYS 10000 static int compare_backups(int i) { int ret; char buf[1024], msg[32]; /* * We run 'wt dump' on both the full backup directory and the incremental backup directory for * this iteration. Since running 'wt' runs recovery and makes both directories "live", we need a * new directory for each iteration. * * If i == 0, we're comparing against the main, original directory with the final incremental * directory. */ if (i == 0) testutil_system("../../wt -R -h %s dump main > %s.%d", home, full_out, i); else testutil_system("../../wt -R -h %s.%d dump main > %s.%d", home_full, i, full_out, i); /* * Now run dump on the incremental directory. */ testutil_system("../../wt -R -h %s.%d dump main > %s.%d", home_incr, i, incr_out, i); /* * Compare the files. */ (void)snprintf(buf, sizeof(buf), "cmp %s.%d %s.%d", full_out, i, incr_out, i); ret = system(buf); if (i == 0) (void)snprintf(msg, sizeof(msg), "%s", "MAIN"); else (void)snprintf(msg, sizeof(msg), "%d", i); printf("Iteration %s: Tables %s.%d and %s.%d %s\n", msg, full_out, i, incr_out, i, ret == 0 ? "identical" : "differ"); if (ret != 0) exit(1); /* * If they compare successfully, clean up. */ if (i != 0) { testutil_system( "rm -rf %s.%d %s.%d %s.%d %s.%d", home_full, i, home_incr, i, full_out, i, incr_out, i); } return (ret); } /* * Set up all the directories needed for the test. We have a full backup directory for each * iteration and an incremental backup for each iteration. That way we can compare the full and * incremental each time through. */ static void setup_directories(void) { int i; for (i = 0; i < MAX_ITERATIONS; i++) { /* * For incremental backups we need 0-N. The 0 incremental directory will compare with the * original at the end. */ testutil_system("rm -rf %s.%d && mkdir -p %s.%d/%s", home_incr, i, home_incr, i, logpath); if (i == 0) continue; /* * For full backups we need 1-N. */ testutil_system("rm -rf %s.%d && mkdir -p %s.%d/%s", home_full, i, home_full, i, logpath); } } static void add_work(WT_SESSION *session, int iter, int iterj) { WT_CURSOR *cursor, *cursor2; int i; char k[64], v[64]; error_check(session->open_cursor(session, uri, NULL, NULL, &cursor)); /* * Add some data to the main table. */ for (i = 0; i < MAX_KEYS; i++) { snprintf(k, sizeof(k), "key-%d-%d-%d", iter, iterj, i); snprintf(v, sizeof(v), "value-%d-%d-%d", iter, iterj, i); cursor->set_key(cursor, k); cursor->set_value(cursor, v); error_check(cursor->insert(cursor)); } cursor->close(cursor); /* * Add some data to the second table. */ error_check(session->open_cursor(session, uri2, NULL, NULL, &cursor2)); for (i = 0; i < MAX_KEYS; i++) { snprintf(k, sizeof(k), "key2-%d-%d-%d", iter, iterj, i); snprintf(v, sizeof(v), "value2-%d-%d-%d", iter, iterj, i); cursor2->set_key(cursor2, k); cursor2->set_value(cursor2, v); error_check(cursor2->insert(cursor2)); } cursor2->close(cursor2); } static void backup_test(void) { WT_CONNECTION *conn; WT_SESSION *session; WT_CURSOR *cursor; int i, ret; const char *backup_dir; /* * Create the initial database. */ testutil_work_dir_init(home); error_check(wiredtiger_open(home, NULL, CONN_CONFIG, &conn)); error_check(conn->open_session(conn, NULL, NULL, &session)); /* * Create the tables. */ error_check(session->create(session, uri, "key_format=S,value_format=S")); error_check(session->create(session, uri2, "key_format=S,value_format=S")); /* * Add some initial data. */ add_work(session, 0, 0); /* * Set up the directories for full and incremental backups. */ setup_directories(); /* * Perform the backups. */ for (i = 0; i < MAX_ITERATIONS; i++) { /* * Perform a full backup. */ if (i > 0) { backup_dir = home_full; error_check(session->checkpoint(session, "backup=")); error_check(conn->backup_prepare(conn, backup_dir, "incremental=false")); error_check(conn->backup_copy(conn, backup_dir, "incremental=false")); error_check(conn->backup_complete(conn, backup_dir, "incremental=false")); } /* * Perform an incremental backup. */ backup_dir = home_incr; error_check(session->checkpoint(session, "backup=")); error_check(conn->backup_prepare(conn, backup_dir, "incremental=true,from_log=0")); error_check(conn->backup_copy(conn, backup_dir, "incremental=true,from_log=0")); error_check(conn->backup_complete(conn, backup_dir, "incremental=true,from_log=0")); /* * Add more data for the next iteration. */ add_work(session, i + 1, 0); /* * Compare the backups. */ compare_backups(i); } /* * Clean up. */ error_check(session->close(session, NULL)); error_check(conn->close(conn, NULL)); testutil_cleanup_dir(home); for (i = 0; i < MAX_ITERATIONS; i++) { testutil_cleanup_dir(home_full); testutil_cleanup_dir(home_incr); } } int main(int argc, char *argv[]) { testutil_work_dir_init(NULL); backup_test(); return (0); } ``` -------------------------------- ### WiredTiger Cursor Operations: Setup and Insert Source: https://source.wiredtiger.com/develop/ex_log_8c-example Demonstrates opening a cursor for a table, setting key-value pairs, and inserting them into the database. ```APIDOC WT_SESSION.open_cursor(WT_SESSION *session, const char *uri, WT_CURSOR *session_cursor, const char *config, WT_CURSOR **cursor) - Opens a cursor for a table or index. - Parameters: - session: The WT_SESSION structure. - uri: The URI of the table or index. - session_cursor: A cursor from the same session (NULL for new). - config: Configuration string for the cursor. - cursor: Pointer to a WT_CURSOR structure. - Returns: 0 on success, or an error code. WT_CURSOR.set_key(WT_CURSOR *cursor, ...) - Sets the key for a cursor operation. - Parameters: - cursor: The WT_CURSOR structure. - ...: Key components (type depends on table configuration). - Returns: 0 on success, or an error code. WT_CURSOR.set_value(WT_CURSOR *cursor, ...) - Sets the value for a cursor operation. - Parameters: - cursor: The WT_CURSOR structure. - ...: Value components (type depends on table configuration). - Returns: 0 on success, or an error code. WT_CURSOR.insert(WT_CURSOR *cursor) - Inserts the key/value pair into the table. - Parameters: - cursor: The WT_CURSOR structure. - Returns: 0 on success, or an error code. ``` -------------------------------- ### Main Function Setup and Backup Loop Source: https://source.wiredtiger.com/develop/ex_backup_8c-example The main function initializes WiredTiger, sets up directories, creates tables, adds initial data, and performs a series of incremental backups. It includes checkpoints and data comparisons between backup iterations. ```c int main(int argc, char *argv[]) { struct stat sb; [WT_CONNECTION](struct_w_t___c_o_n_n_e_c_t_i_o_n.html) *wt_conn; [WT_CURSOR](struct_w_t___c_u_r_s_o_r.html) *backup_cur; [WT_SESSION](struct_w_t___s_e_s_s_i_o_n.html) *session; int i, j, ret; char cmd_buf[256], *idstr; (void)argc; /* Unused variable */ (void)testutil_set_progname(argv); testutil_system("rm -rf %s && mkdir -p %s/%s", home, home, logpath); error_check([wiredtiger_open](group__wt.html#gacbe8d118f978f5bfc8ccb4c77c9e8813)(home, NULL, CONN_CONFIG, &wt_conn)); setup_directories(); error_check(wt_conn->open_session(wt_conn, NULL, NULL, &session)); error_check(session->[create](struct_w_t___s_e_s_s_i_o_n.html#a358ca4141d59c345f401c58501276bbb)(session, uri, "key_format=S,value_format=S")); error_check(session->[create](struct_w_t___s_e_s_s_i_o_n.html#a358ca4141d59c345f401c58501276bbb)(session, uri2, "key_format=S,value_format=S")); printf("Adding initial data\n"); add_work(session, 0, 0); printf("Taking initial backup\n"); take_full_backup(session, 0); error_check(session->[checkpoint](struct_w_t___s_e_s_s_i_o_n.html#a6550c9079198955c5071583941c85bbf)(session, NULL)); for (i = 1; i < MAX_ITERATIONS; i++) { printf("Iteration %d: adding data\n", i); /* For each iteration we may add work and checkpoint multiple times. */ for (j = 0; j < i; j++) { add_work(session, i, j); error_check(session->[checkpoint](struct_w_t___s_e_s_s_i_o_n.html#a6550c9079198955c5071583941c85bbf)(session, NULL)); } /* * The full backup here is only needed for testing and comparison purposes. A normal * incremental backup procedure would not include this. */ printf("Iteration %d: taking full backup\n", i); take_full_backup(session, i); /* * Taking the incremental backup also calls truncate to remove the log files, if the copies * were successful. See that function for details on that call. */ printf("Iteration %d: taking incremental backup\n", i); take_incr_backup(session, i); printf("Iteration %d: dumping and comparing data\n", i); error_check(compare_backups(i)); } printf("Close and reopen the connection\n"); /* * Close and reopen the connection to illustrate the durability of ID information. */ error_check(wt_conn->close(wt_conn, NULL)); error_check([wiredtiger_open](group__wt.html#gacbe8d118f978f5bfc8ccb4c77c9e8813)(home, NULL, CONN_CONFIG, &wt_conn)); error_check(wt_conn->open_session(wt_conn, NULL, NULL, &session)); printf("Verify query after reopen\n"); error_check(session->[open_cursor](struct_w_t___s_e_s_s_i_o_n.html#afb5b4a69c2c5cafe411b2b04fdc1c75d)(session, "backup:query_id", NULL, NULL, &backup_cur)); while ((ret = backup_cur->[next](struct_w_t___c_u_r_s_o_r.html#a0503f16bd8f3d05aa3552f229b3a8e1b)(backup_cur)) == 0) { error_check(backup_cur->[get_key](struct_w_t___c_u_r_s_o_r.html#af19f6f9d9c7fc248ab38879032620b2f)(backup_cur, &idstr)); printf("Existing incremental ID string: %s\n", idstr); } error_check(backup_cur->[close](struct_w_t___c_u_r_s_o_r.html#aeea071f192cab12245a50fbe71c3460b)(backup_cur)); /* * We should have an entry for i-1 and i-2. Use the older one. */ (void)snprintf( cmd_buf, sizeof(cmd_buf), "incremental=(src_id=\"ID%d\",this_id=\"ID%d\")", i - 2, i); error_check(session->[open_cursor](struct_w_t___s_e_s_s_i_o_n.html#afb5b4a69c2c5cafe411b2b04fdc1c75d)(session, "backup:", NULL, cmd_buf, &backup_cur)); error_check(backup_cur->[close](struct_w_t___c_u_r_s_o_r.html#aeea071f192cab12245a50fbe71c3460b)(backup_cur)); /* * After we're done, release resources. Test the force stop setting. */ (void)snprintf(cmd_buf, sizeof(cmd_buf), "incremental=(force_stop=true)"); return 0; } ``` -------------------------------- ### Create and Open WiredTiger Database (C) Source: https://source.wiredtiger.com/develop/ex_hello_8c-example This C code snippet demonstrates the basic steps to create and open a WiredTiger database. It includes opening a connection, creating a session, and closing the connection. It relies on the `wiredtiger_open` function and the `test_util.h` header for setup. ```c /* * ex_hello.c * This is an example demonstrating how to create and connect to a * database. */ #include static const char *home; int main(int argc, char *argv[]) { [WT_CONNECTION](struct_w_t___c_o_n_n_e_c_t_i_o_n.html) *conn; [WT_SESSION](struct_w_t___s_e_s_s_i_o_n.html) *session; home = example_setup(argc, argv); /* Open a connection to the database, creating it if necessary. */ error_check([wiredtiger_open](group__wt.html#gacbe8d118f978f5bfc8ccb4c77c9e8813)(home, NULL, "create", &conn)); /* Open a session for the current thread's work. */ error_check(conn->[open_session](struct_w_t___c_o_n_n_e_c_t_i_o_n.html#adad5965cd4a60f65b5ac01f7ca6d1fc0)(conn, NULL, NULL, &session)); /* Do some work... * Note: closing the connection implicitly closes open session(s). */ error_check(conn->[close](struct_w_t___c_o_n_n_e_c_t_i_o_n.html#af535c517df851eeac8ebf3594d40b545)(conn, NULL)); return (EXIT_SUCCESS); } ``` -------------------------------- ### WiredTiger Connection and Session Management Source: https://source.wiredtiger.com/develop/ex_schema_8c-example Demonstrates the fundamental steps of opening a WiredTiger database connection, creating a session, and subsequently closing them. This is the initial setup required for any database operations. ```APIDOC wiredtiger_open(const char *home, WT_EVENT_HANDLER *event_handler, const char *config, WT_CONNECTION **connectionp) Opens a connection to a database. WT_CONNECTION::open_session(WT_CONNECTION *connection, WT_EVENT_HANDLER *event_handler, const char *config, WT_SESSION **sessionp) Opens a session on a WiredTiger database connection. WT_CONNECTION::close(WT_CONNECTION *conn, const char *config) Closes a WiredTiger database connection. Example Usage: WT_CONNECTION *conn; WT_SESSION *session; // ... wiredtiger_open ... // ... conn->open_session ... // ... perform operations ... // ... session->close ... // ... conn->close ... ``` -------------------------------- ### Main Function: WiredTiger Setup and Operation Source: https://source.wiredtiger.com/develop/ex_file_system_8c-example The main entry point of the program. It sets up the WiredTiger environment, including creating a test directory, configuring the custom file system extension, opening a connection and session, creating a table, and performing basic cursor operations. ```c static const char *home; int main(void) { WT_CONNECTION *conn; WT_CURSOR *cursor; WT_SESSION *session; const char *key, *open_config, *uri; int i; int ret = 0; char kbuf[64]; /* * Create a clean test directory for this run of the test program if the environment variable * isn't already set (as is done by make check). */ if (getenv("WIREDTIGER_HOME") == NULL) { home = "WT_HOME"; ret = system("rm -rf WT_HOME && mkdir WT_HOME"); } else home = NULL; /* * Setup a configuration string that will load our custom file system. Use the special local * extension to indicate that the entry point is in the same executable. Also enable early load * for this extension, since WiredTiger needs to be able to find it before doing any file * operations. Finally, pass in two pieces of configuration information to our initialization * function as the "config" value. */ open_config = "create,log=(enabled=true),extensions=(local={entry=demo_file_system_create,early_load=true," "config={config_string=\"demo-file-system\",config_value=37}})"; /* Open a connection to the database, creating it if necessary. */ if ((ret = wiredtiger_open(home, NULL, open_config, &conn)) != 0) { fprintf(stderr, "Error connecting to %s: %s\n", home == NULL ? "." : home, wiredtiger_strerror(ret)); return (EXIT_FAILURE); } if ((ret = conn->open_session(conn, NULL, NULL, &session)) != 0) { fprintf(stderr, "WT_CONNECTION.open_session: %s\n", wiredtiger_strerror(ret)); return (EXIT_FAILURE); } uri = "table:fs"; if ((ret = session->create(session, uri, "key_format=S,value_format=S")) != 0) { fprintf(stderr, "WT_SESSION.create: %s: %s\n", uri, wiredtiger_strerror(ret)); return (EXIT_FAILURE); } if ((ret = session->open_cursor(session, uri, NULL, NULL, &cursor)) != 0) { fprintf(stderr, "WT_SESSION.open_cursor: %s: %s\n", uri, wiredtiger_strerror(ret)); return (EXIT_FAILURE); } for (i = 0; i < 1000; ++i) { (void)snprintf(kbuf, sizeof(kbuf), "%010d KEY -----", i); cursor->set_key(cursor, kbuf); /* ... rest of the loop ... */ } /* ... rest of the main function ... */ return (0); } ``` -------------------------------- ### WiredTiger API Initialization and Table Creation Source: https://source.wiredtiger.com/develop/ex_call_center_8c-example Demonstrates the initial setup of the WiredTiger environment, opening a connection, creating a session, and defining tables with column groups and indexes. ```C int main(int argc, char *argv[]) { int count, exact; WT_CONNECTION *conn; WT_SESSION *session; WT_CURSOR *cursor; CUSTOMER cust, *custp, cust_sample[] = {{0, "Professor Oak", "LeafGreen Avenue", "123-456-7890"}, {0, "Lorelei", "Sevii Islands", "098-765-4321"}, {0, NULL, NULL, NULL}}; CALL call, *callp, call_sample[] = {{0, 32, 1, 2, "billing", "unavailable"}, {0, 33, 1, 2, "billing", "available"}, {0, 34, 1, 2, "reminder", "unavailable"}, {0, 35, 1, 2, "reminder", "available"}, {0, 0, 0, 0, NULL, NULL}}; home = example_setup(argc, argv); error_check(wiredtiger_open(home, NULL, "create", &conn)); error_check(conn->open_session(conn, NULL, NULL, &session)); /* * Create the customers table, give names and types to the columns. The columns will be stored * in two groups: "main" and "address", created below. */ error_check(session->create(session, "table:customers", "key_format=r,value_format=SSS,columns=(id,name,address,phone),colgroups=(main,address)")); /* Create the main column group with value columns except address. */ error_check(session->create(session, "colgroup:customers:main", "columns=(name,phone)")); /* Create the address column group with just the address. */ error_check(session->create(session, "colgroup:customers:address", "columns=(address)")); /* Create an index on the customer table by phone number. */ error_check(session->create(session, "index:customers:phone", "columns=(phone)")); /* Populate the customers table with some data. */ ``` -------------------------------- ### WiredTiger Cursor Operations Source: https://source.wiredtiger.com/develop/ex_access_8c-example Documentation for common WiredTiger cursor operations including setting keys, getting values, inserting records, and navigating through data. ```APIDOC WT_CURSOR::set_key void set_key(WT_CURSOR *cursor, ...) Sets the key for the next operation. Parameters: cursor: The cursor handle. ...: Key components to set. WT_CURSOR::set_value void set_value(WT_CURSOR *cursor, ...) Sets the value for the next operation. Parameters: cursor: The cursor handle. ...: Value components to set. WT_CURSOR::insert int insert(WT_CURSOR *cursor) Inserts a record using the key and value previously set on the cursor. Optionally updates an existing record if the key already exists. Parameters: cursor: The cursor handle. Returns: 0 on success, or a negative error value on failure. WT_CURSOR::get_value int get_value(WT_CURSOR *cursor, ...) Retrieves the value for the current record pointed to by the cursor. Parameters: cursor: The cursor handle. ...: Output parameters to receive the value components. Returns: 0 on success, or a negative error value on failure. WT_CURSOR::next int next(WT_CURSOR *cursor) Advances the cursor to the next record in the dataset. Parameters: cursor: The cursor handle. Returns: 0 on success, WT_NOTFOUND if no more records, or a negative error value. WT_CURSOR::reset int reset(WT_CURSOR *cursor) Resets the cursor to the beginning of the dataset. Parameters: cursor: The cursor handle. Returns: 0 on success, or a negative error value on failure. ``` -------------------------------- ### wtperf Configuration Examples Source: https://source.wiredtiger.com/develop/wtperf Examples of configuring the `wtperf` utility for database population and workload execution, including connection and table settings. ```configuration conn_config="cache_size=500MB" table_config="type=file" icount=500000 run_time=120 populate_threads=1 threads=((count=8,reads=1)) ``` ```configuration conn_config="cache_size=20G,statistics=(fast,clear),statistics_log=(wait=600)" report_interval=5 ``` ```configuration threads=((count=2,inserts=1),(count=2,inserts=1,reads=1,updates=1)) ``` ```configuration sample_interval=10 ``` -------------------------------- ### WiredTiger Config Parsing and File System Setup Source: https://source.wiredtiger.com/develop/ex_file_system_8c-example This snippet demonstrates the core logic for parsing configuration items, handling specific key types like 'config_string' and 'config_value', and printing them. It also shows error handling for unexpected configurations and the subsequent setup of a custom file system by assigning function pointers for operations like directory listing, existence checks, opening files, and removal. Finally, it sets this custom file system on a WiredTiger connection. ```c if (byte_string_match("config_string", k.str(struct_w_t___c_o_n_f_i_g___i_t_e_m.html#aa0ce7d30a32600e16824966c638ee45f), k.len(struct_w_t___c_o_n_f_i_g___i_t_e_m.html#adff0f6e5a3f781f0228015e8336f1a14))) { printf( "\t" "key %.*s=\"%.*s\"\n", (int)k.len(struct_w_t___c_o_n_f_i_g___i_t_e_m.html#adff0f6e5a3f781f0228015e8336f1a14), k.str(struct_w_t___c_o_n_f_i_g___i_t_e_m.html#aa0ce7d30a32600e16824966c638ee45f), (int)v.len(struct_w_t___c_o_n_f_i_g___i_t_e_m.html#adff0f6e5a3f781f0228015e8336f1a14), v.str(struct_w_t___c_o_n_f_i_g___i_t_e_m.html#aa0ce7d30a32600e16824966c638ee45f)); continue; } if (byte_string_match("config_value", k.str(struct_w_t___c_o_n_f_i_g___i_t_e_m.html#aa0ce7d30a32600e16824966c638ee45f), k.len(struct_w_t___c_o_n_f_i_g___i_t_e_m.html#adff0f6e5a3f781f0228015e8336f1a14))) { printf( "\t" "key %.*s=%%" PRId64 "\n", (int)k.len(struct_w_t___c_o_n_f_i_g___i_t_e_m.html#adff0f6e5a3f781f0228015e8336f1a14), k.str(struct_w_t___c_o_n_f_i_g___i_t_e_m.html#aa0ce7d30a32600e16824966c638ee45f), v.val(struct_w_t___c_o_n_f_i_g___i_t_e_m.html#a4aefab7843f434e5a5cc18203e9fec5f)); continue; } ret = EINVAL; (void)wtext->err_printf(struct_w_t___e_x_t_e_n_s_i_o_n___a_p_i.html#a6d58298e356dbf58ac854c3d1af99678)(wtext, NULL, "WT_CONFIG_PARSER.next: unexpected configuration " "information: %.*s=%.*s: %s", (int)k.len(struct_w_t___c_o_n_f_i_g___i_t_e_m.html#adff0f6e5a3f781f0228015e8336f1a14), k.str(struct_w_t___c_o_n_f_i_g___i_t_e_m.html#aa0ce7d30a32600e16824966c638ee45f), (int)v.len(struct_w_t___c_o_n_f_i_g___i_t_e_m.html#adff0f6e5a3f781f0228015e8336f1a14), v.str(struct_w_t___c_o_n_f_i_g___i_t_e_m.html#adff0f6e5a3f781f0228015e8336f1a14), wtext->strerror(struct_w_t___e_x_t_e_n_s_i_o_n___a_p_i.html#a3fd4b5255e2f82139a846d66d67be565)(wtext, NULL, ret)); goto err; } /* Check for expected parser termination and close the parser. */ if (ret != WT_NOTFOUND) { (void)wtext->err_printf(struct_w_t___e_x_t_e_n_s_i_o_n___a_p_i.html#a6d58298e356dbf58ac854c3d1af99678)( wtext, NULL, "WT_CONFIG_PARSER.next: config: %s", wtext->strerror(struct_w_t___e_x_t_e_n_s_i_o_n___a_p_i.html#a3fd4b5255e2f82139a846d66d67be565)(wtext, NULL, ret)); goto err; } if ((ret = config_parser->close(struct_w_t___c_o_n_f_i_g___p_a_r_s_e_r.html#a44eea5e27a4da10006ab444ee7cf00f3)(config_parser)) != 0) { (void)wtext->err_printf(struct_w_t___e_x_t_e_n_s_i_o_n___a_p_i.html#a6d58298e356dbf58ac854c3d1af99678)( wtext, NULL, "WT_CONFIG_PARSER.close: config: %s", wtext->strerror(struct_w_t___e_x_t_e_n_s_i_o_n___a_p_i.html#a3fd4b5255e2f82139a846d66d67be565)(wtext, NULL, ret)); goto err; } allocate_file_system_lock(&demo_fs->lock); /* Initialize the in-memory jump table. */ file_system->fs_directory_list = demo_fs_directory_list; file_system->fs_directory_list_free = demo_fs_directory_list_free; file_system->fs_exist = demo_fs_exist; file_system->fs_open_file = demo_fs_open; file_system->fs_remove = demo_fs_remove; file_system->fs_rename = demo_fs_rename; file_system->fs_size = demo_fs_size; file_system->terminate = demo_fs_terminate; if ((ret = conn->set_file_system(struct_w_t___c_o_n_n_e_c_t_i_o_n.html#a7c9d5b40133a6ba1cbc4a6b1d8e1e86b)(conn, file_system, NULL)) != 0) { (void)wtext->err_printf(struct_w_t___e_x_t_e_n_s_i_o_n___a_p_i.html#a6d58298e356dbf58ac854c3d1af99678)( wtext, NULL, "WT_CONNECTION.set_file_system: %s", wtext->strerror(struct_w_t___e_x_t_e_n_s_i_o_n___a_p_i.html#a3fd4b5255e2f82139a846d66d67be565)(wtext, NULL, ret)); goto err; } return (0); err: free(demo_fs); /* An error installing the file system is fatal. */ exit(1); } ``` -------------------------------- ### Main WiredTiger Example Source: https://source.wiredtiger.com/develop/ex_stat_8c-example Sets up the WiredTiger environment, opens a connection and session, creates a table, inserts a key-value pair, checkpoints the database, prints derived statistics, and closes the connection. ```c int main(int argc, char *argv[]) { WT_CONNECTION *conn; WT_CURSOR *cursor; WT_SESSION *session; home = example_setup(argc, argv); error_check(wiredtiger_open(home, NULL, "create,statistics=(all)", &conn)); error_check(conn->open_session(conn, NULL, NULL, &session)); error_check(session->create(session, "table:access", "key_format=S,value_format=S,columns=(k,v)")); error_check(session->open_cursor(session, "table:access", NULL, NULL, &cursor)); cursor->set_key(cursor, "key"); cursor->set_value(cursor, "value"); error_check(cursor->insert(cursor)); error_check(cursor->close(cursor)); error_check(session->checkpoint(session, NULL)); print_database_stats(session); print_file_stats(session); print_session_stats(session); print_overflow_pages(session); print_derived_stats(session); error_check(conn->close(conn, NULL)); return (EXIT_SUCCESS); } ``` -------------------------------- ### WiredTiger Logging and Log Cursors Example (C) Source: https://source.wiredtiger.com/develop/ex_log_8c-example Demonstrates the usage of WiredTiger's logging features and log cursors. This example shows how to open a database connection with logging enabled, create a table, and process log records. It includes functions for setting up the environment and printing log record details. ```C /* * Public Domain 2014-present MongoDB, Inc. * Public Domain 2008-2014 WiredTiger, Inc. * * This is free and unencumbered software released into the public domain. * * Anyone is free to copy, modify, publish, use, compile, sell, or * distribute this software, either in source code form or as a compiled * binary, for any purpose, commercial or non-commercial, and by any * means. * * In jurisdictions that recognize copyright laws, the author or authors * of this software dedicate any and all copyright interest in the * software to the public domain. We make this dedication for the benefit * of the public at large and to the detriment of our heirs and * successors. We intend this dedication to be an overt act of * relinquishment in perpetuity of all present and future rights to this * software under copyright law. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * ex_log.c * demonstrates how to use logging and log cursors. */ #include static const char *home1 = "WT_HOME_LOG_1"; static const char *home2 = "WT_HOME_LOG_2"; static const char *const uri = "table:logtest"; #define CONN_CONFIG "create,cache_size=100MB,log=(enabled=true,remove=false)" #define MAX_KEYS 10 static void setup_copy(WT_CONNECTION **wt_connp, WT_SESSION **sessionp) { error_check(wiredtiger_open(home2, NULL, CONN_CONFIG, wt_connp)); error_check((*wt_connp)->open_session(*wt_connp, NULL, NULL, sessionp)); error_check((*sessionp)->create(*sessionp, uri, "key_format=S,value_format=S")); } static void compare_tables(WT_SESSION *session, WT_SESSION *sess_copy) { WT_CURSOR *cursor, *curs_copy; int ret; const char *key, *key_copy, *value, *value_copy; error_check(session->open_cursor(session, uri, NULL, NULL, &cursor)); error_check(sess_copy->open_cursor(sess_copy, uri, NULL, NULL, &curs_copy)); while ((ret = cursor->next(cursor)) == 0) { error_check(curs_copy->next(curs_copy)); error_check(cursor->get_key(cursor, &key)); error_check(cursor->get_value(cursor, &value)); error_check(curs_copy->get_key(curs_copy, &key_copy)); error_check(curs_copy->get_value(curs_copy, &value_copy)); if (strcmp(key, key_copy) != 0 || strcmp(value, value_copy) != 0) { fprintf(stderr, "Mismatched: key %s, key_copy %s value %s value_copy %s\n", key, key_copy, value, value_copy); exit(1); } } scan_end_check(ret == WT_NOTFOUND); error_check(cursor->close(cursor)); ret = curs_copy->next(curs_copy); scan_end_check(ret == WT_NOTFOUND); error_check(curs_copy->close(curs_copy)); } static void print_record(uint32_t log_file, uint32_t log_offset, uint32_t opcount, uint32_t rectype, uint32_t optype, uint64_t txnid, uint32_t fileid, WT_ITEM *key, WT_ITEM *value) { printf("LSN [%%" PRIu32 "].[" PRIu32 "].%%" PRIu32 ": record type %%" PRIu32 " optype %%" PRIu32 " txnid %%" PRIu64 " fileid %%" PRIu32 ", key: ", log_file, log_offset, opcount, rectype, optype, txnid, fileid); if (key != NULL) printf("%.*s", (int)key->size, (char *)key->data); printf(", value: "); if (value != NULL) printf("%.*s", (int)value->size, (char *)value->data); printf("\n"); } ```