/* * Open and close cursor on a table. */ void op_cursor(void *arg) { TEST_OPTS *opts; TEST_PER_THREAD_OPTS *args; WT_CURSOR *cursor; WT_SESSION *session; int i, ret; args = (TEST_PER_THREAD_OPTS *)arg; opts = args->testopts; testutil_check( opts->conn->open_session(opts->conn, NULL, NULL, &session)); if ((ret = session->open_cursor( session, opts->uri, NULL, NULL, &cursor)) != 0) { if (ret != ENOENT && ret != EBUSY) testutil_die(ret, "session.open_cursor"); } else { /* Put some data in if asked to. */ if (args->testopts->do_data_ops) { for (i = 0; i < 1000; i++) { cursor->set_key(cursor, i); cursor->set_value(cursor, "abcdef"); cursor->insert(cursor); } } testutil_check(cursor->close(cursor)); } testutil_check(session->close(session, NULL)); args->thread_counter++; }
bool indexInsert(size_t indexId, fstring key, llong recId) override { assert(started == m_status); assert(indexId < m_indices.size()); WT_ITEM item; WT_SESSION* ses = m_session.ses; const Schema& schema = m_sconf.getIndexSchema(indexId); WT_CURSOR* cur = m_indices[indexId].insert; WtWritableIndex::setKeyVal(schema, cur, key, recId, &item, &m_wrtBuf); int err = cur->insert(cur); m_sizeDiff += sizeof(llong) + key.size(); if (schema.m_isUnique) { if (WT_DUPLICATE_KEY == err) { return false; } if (err) { THROW_STD(invalid_argument , "ERROR: wiredtiger insert unique index: %s", ses->strerror(ses, err)); } } else { if (WT_DUPLICATE_KEY == err) { assert(0); // assert in debug return true; // ignore in release } if (err) { THROW_STD(invalid_argument , "ERROR: wiredtiger insert multi index: %s", ses->strerror(ses, err)); } } return true; }
int main(void) { WT_CONNECTION *conn; WT_CURSOR *cursor; WT_SESSION *session; int ret; ret = wiredtiger_open(home, NULL, "create,statistics=(all)", &conn); ret = conn->open_session(conn, NULL, NULL, &session); ret = session->create( session, "table:access", "key_format=S,value_format=S"); ret = session->open_cursor( session, "table:access", NULL, NULL, &cursor); cursor->set_key(cursor, "key"); cursor->set_value(cursor, "value"); ret = cursor->insert(cursor); cursor->close(cursor); ret = session->checkpoint(session, NULL); ret = print_database_stats(session); ret = print_file_stats(session); ret = print_overflow_pages(session); ret = print_derived_stats(session); return (conn->close(conn, NULL) == 0 ? ret : EXIT_FAILURE); }
static void op(WT_SESSION *session, WT_RAND_STATE *rnd, WT_CURSOR **cpp) { WT_CURSOR *cursor; WT_DECL_RET; u_int i, key; char buf[128]; bool readonly; /* Close any open cursor in the slot we're about to reuse. */ if (*cpp != NULL) { testutil_check((*cpp)->close(*cpp)); *cpp = NULL; } cursor = NULL; readonly = __wt_random(rnd) % 2 == 0; /* Loop to open an object handle. */ for (i = __wt_random(rnd) % uris; !done; __wt_yield()) { /* Use a checkpoint handle for 50% of reads. */ ret = session->open_cursor(session, uri_list[i], NULL, readonly && (i % 2 == 0) ? "checkpoint=WiredTigerCheckpoint" : NULL, &cursor); if (ret != EBUSY) { testutil_check(ret); break; } (void)__wt_atomic_add64(&worker_busy, 1); } if (cursor == NULL) return; /* Operate on some number of key/value pairs. */ for (key = 1; !done && key < MAXKEY; key += __wt_random(rnd) % 37, __wt_yield()) { testutil_check( __wt_snprintf(buf, sizeof(buf), "key:%020u", key)); cursor->set_key(cursor, buf); if (readonly) testutil_check(cursor->search(cursor)); else { cursor->set_value(cursor, buf); testutil_check(cursor->insert(cursor)); } } /* Close the cursor half the time, otherwise cache it. */ if (__wt_random(rnd) % 2 == 0) testutil_check(cursor->close(cursor)); else *cpp = cursor; (void)__wt_atomic_add64(&worker, 1); }
TEST_F(WiredTigerRecoveryUnitTestFixture, WriteOnADocumentBeingPreparedTriggersWTRollback) { // Prepare but don't commit a transaction ru1->beginUnitOfWork(clientAndCtx1.second.get()); WT_CURSOR* cursor; getCursor(ru1, &cursor); cursor->set_key(cursor, "key"); cursor->set_value(cursor, "value"); invariantWTOK(cursor->insert(cursor)); ru1->setPrepareTimestamp({1, 1}); ru1->prepareUnitOfWork(); // Another transaction with write triggers WT_ROLLBACK ru2->beginUnitOfWork(clientAndCtx2.second.get()); getCursor(ru2, &cursor); cursor->set_key(cursor, "key"); cursor->set_value(cursor, "value2"); int ret = cursor->insert(cursor); ASSERT_EQ(WT_ROLLBACK, ret); ru1->abortUnitOfWork(); ru2->abortUnitOfWork(); }
int WiredTigerEngine::Put(const Slice& key, const Slice& value, const Options& options) { ContextHolder& holder = GetContextHolder(); WT_SESSION* session = holder.session; if (NULL == session) { return -1; } // if (holder.trasc_ref > 0) // { // PutOperation* op = new PutOperation; // op->key.assign((const char*) key.data(), key.size()); // op->value.assign((const char*) value.data(), value.size()); // m_write_queue.Push(op); // holder.readonly_transc = false; // return 0; // } int ret = 0; WT_CURSOR *cursor = holder.batch == NULL ? create_wiredtiger_cursor(session) : holder.batch; if (NULL == cursor) { return -1; } WT_ITEM key_item, value_item; key_item.data = key.data(); key_item.size = key.size(); value_item.data = value.data(); value_item.size = value.size(); cursor->set_key(cursor, &key_item); cursor->set_value(cursor, &value_item); ret = cursor->insert(cursor); if (0 != ret) { ERROR_LOG("Error write data for reason: %s", wiredtiger_strerror(ret)); ret = -1; } if (holder.batch == NULL) { ret = cursor->close(cursor); CHECK_WT_RETURN(ret); } else { holder.IncBatchWriteCount(); if (holder.batch_write_count >= m_cfg.batch_commit_watermark) { holder.RestartBatchWrite(); } } return ret; }
int main(void) { /*! [access example connection] */ WT_CONNECTION *conn; WT_CURSOR *cursor; WT_SESSION *session; const char *key, *value; int ret; if ((ret = wiredtiger_open(home, NULL, "create", &conn)) != 0 || (ret = conn->open_session(conn, NULL, NULL, &session)) != 0) { fprintf(stderr, "Error connecting to %s: %s\n", home, wiredtiger_strerror(ret)); return (ret); } /*! [access example connection] */ /*! [access example table create] */ ret = session->create(session, "table:access", "key_format=S,value_format=S"); /*! [access example table create] */ /*! [access example cursor open] */ ret = session->open_cursor(session, "table:access", NULL, NULL, &cursor); /*! [access example cursor open] */ /*! [access example cursor insert] */ cursor->set_key(cursor, "key1"); /* Insert a record. */ cursor->set_value(cursor, "value1"); ret = cursor->insert(cursor); /*! [access example cursor insert] */ /*! [access example cursor list] */ ret = cursor->reset(cursor); /* Restart the scan. */ while ((ret = cursor->next(cursor)) == 0) { ret = cursor->get_key(cursor, &key); ret = cursor->get_value(cursor, &value); printf("Got record: %s : %s\n", key, value); } /*! [access example cursor list] */ /*! [access example close] */ ret = conn->close(conn, NULL); /*! [access example close] */ return (ret); }
StatusWith<RecordId> WiredTigerRecordStore::insertRecord(OperationContext* txn, const char* data, int len, bool enforceQuota) { if (_isCapped && len > _cappedMaxSize) { return StatusWith<RecordId>(ErrorCodes::BadValue, "object to insert exceeds cappedMaxSize"); } RecordId loc; if (_useOplogHack) { StatusWith<RecordId> status = extractAndCheckLocForOplog(data, len); if (!status.isOK()) return status; loc = status.getValue(); if (loc > _oplog_highestSeen) { stdx::lock_guard<stdx::mutex> lk(_uncommittedDiskLocsMutex); if (loc > _oplog_highestSeen) { _oplog_highestSeen = loc; } } } else if (_isCapped) { stdx::lock_guard<stdx::mutex> lk(_uncommittedDiskLocsMutex); loc = _nextId(); _addUncommitedDiskLoc_inlock(txn, loc); } else { loc = _nextId(); } WiredTigerCursor curwrap(_uri, _tableId, true, txn); curwrap.assertInActiveTxn(); WT_CURSOR* c = curwrap.get(); invariant(c); c->set_key(c, _makeKey(loc)); WiredTigerItem value(data, len); c->set_value(c, value.Get()); int ret = WT_OP_CHECK(c->insert(c)); if (ret) { return StatusWith<RecordId>(wtRCToStatus(ret, "WiredTigerRecordStore::insertRecord")); } _changeNumRecords(txn, 1); _increaseDataSize(txn, len); cappedDeleteAsNeeded(txn, loc); return StatusWith<RecordId>(loc); }
void load(SHARED_CONFIG *cfg, const char *name) { WT_CONNECTION *conn; WT_CURSOR *cursor; WT_ITEM *value, _value; WT_SESSION *session; size_t len; uint64_t keyno; char keybuf[64], valuebuf[64]; conn = cfg->conn; file_create(cfg, name); testutil_check(conn->open_session(conn, NULL, NULL, &session)); testutil_check( session->open_cursor(session, name, NULL, "bulk", &cursor)); value = &_value; for (keyno = 1; keyno <= cfg->nkeys; ++keyno) { if (cfg->ftype == ROW) { testutil_check(__wt_snprintf( keybuf, sizeof(keybuf), "%016" PRIu64, keyno)); cursor->set_key(cursor, keybuf); } else cursor->set_key(cursor, (uint32_t)keyno); value->data = valuebuf; if (cfg->ftype == FIX) cursor->set_value(cursor, 0x01); else { testutil_check(__wt_snprintf_len_set( valuebuf, sizeof(valuebuf), &len, "%37" PRIu64, keyno)); value->size = (uint32_t)len; cursor->set_value(cursor, value); } testutil_check(cursor->insert(cursor)); } /* Setup the starting key range for the workload phase. */ cfg->key_range = cfg->nkeys; testutil_check(cursor->close(cursor)); testutil_check(session->checkpoint(session, NULL)); testutil_check(session->close(session, NULL)); }
/* perform write command */ int dovlcswrite(char *name, int rnum, int bulk, int rnd){ WT_CURSOR *c; WT_ITEM value; int i, err; char buf[RECBUFSIZ]; if(showprgr) printf("<Write Test of var-length Column Store>\n name=%s rnum=%d\n\n", name, rnum); /* open a database */ if(setup(name, "r", "u", bulk ? "bulk" : NULL, &c) != 0) { fprintf(stderr, "create failed\n"); (void)teardown(); return 1; } err = FALSE; value.data = buf; value.size = 8; /* loop for each record */ for(i = 1; i <= rnum; i++){ /* store a record */ sprintf(buf, "%08d", i); c->set_key(c, (uint64_t)(rnd ? myrand() % rnum + 1 : i)); c->set_value(c, &value); if((err = c->insert(c)) != 0 && err != WT_DUPLICATE_KEY) { fprintf(stderr, "insert failed\n"); break; } /* print progression */ if(showprgr && rnum > 250 && i % (rnum / 250) == 0){ putchar('.'); fflush(stdout); if(i == rnum || i % (rnum / 10) == 0){ printf(" (%08d)\n", i); fflush(stdout); } } } /* close the database */ if(teardown() != 0) { fprintf(stderr, "close failed\n"); return 1; } if(showprgr && !err) printf("ok\n\n"); return err ? 1 : 0; }
static int wt_id2entry_put( Operation *op, WT_SESSION *session, Entry *e, const char *config ) { struct berval bv; WT_CURSOR *cursor = NULL; WT_ITEM item; int rc; rc = entry_encode( e, &bv ); if(rc != LDAP_SUCCESS){ return -1; } item.size = bv.bv_len; item.data = bv.bv_val; rc = session->open_cursor(session, WT_TABLE_ID2ENTRY, NULL, config, &cursor); if ( rc ) { Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(wt_id2entry_put) ": open_cursor failed: %s (%d)\n", wiredtiger_strerror(rc), rc, 0 ); goto done; } cursor->set_key(cursor, e->e_id); cursor->set_value(cursor, e->e_ndn, &item); rc = cursor->insert(cursor); if ( rc ) { Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(wt_id2entry_put) ": insert failed: %s (%d)\n", wiredtiger_strerror(rc), rc, 0 ); goto done; } done: ch_free( bv.bv_val ); if(cursor){ cursor->close(cursor); } return rc; }
void load(const char *name) { WT_CURSOR *cursor; WT_ITEM *key, _key, *value, _value; WT_SESSION *session; size_t len; uint64_t keyno; char keybuf[64], valuebuf[64]; file_create(name); testutil_check(conn->open_session(conn, NULL, NULL, &session)); testutil_check( session->open_cursor(session, name, NULL, "bulk", &cursor)); key = &_key; value = &_value; for (keyno = 1; keyno <= nkeys; ++keyno) { if (ftype == ROW) { testutil_check(__wt_snprintf_len_set( keybuf, sizeof(keybuf), &len, "%017" PRIu64, keyno)); key->data = keybuf; key->size = (uint32_t)len; cursor->set_key(cursor, key); } else cursor->set_key(cursor, keyno); if (ftype == FIX) cursor->set_value(cursor, 0x01); else { testutil_check(__wt_snprintf_len_set( valuebuf, sizeof(valuebuf), &len, "%37" PRIu64, keyno)); value->data = valuebuf; value->size = (uint32_t)len; cursor->set_value(cursor, value); } testutil_check(cursor->insert(cursor)); } testutil_check(session->close(session, NULL)); }
void load(const char *name) { WT_CURSOR *cursor; WT_ITEM *key, _key, *value, _value; WT_SESSION *session; char keybuf[64], valuebuf[64]; u_int keyno; int ret; file_create(name); if ((ret = conn->open_session(conn, NULL, NULL, &session)) != 0) testutil_die(ret, "conn.session"); if ((ret = session->open_cursor(session, name, NULL, "bulk", &cursor)) != 0) testutil_die(ret, "cursor.open"); key = &_key; value = &_value; for (keyno = 1; keyno <= nkeys; ++keyno) { if (ftype == ROW) { key->data = keybuf; key->size = (uint32_t) snprintf(keybuf, sizeof(keybuf), "%017u", keyno); cursor->set_key(cursor, key); } else cursor->set_key(cursor, (uint32_t)keyno); value->data = valuebuf; if (ftype == FIX) cursor->set_value(cursor, 0x01); else { value->size = (uint32_t) snprintf(valuebuf, sizeof(valuebuf), "%37u", keyno); cursor->set_value(cursor, value); } if ((ret = cursor->insert(cursor)) != 0) testutil_die(ret, "cursor.insert"); } if ((ret = session->close(session, NULL)) != 0) testutil_die(ret, "session.close"); }
/*! [thread main] */ int main(void) { WT_CONNECTION *conn; WT_SESSION *session; WT_CURSOR *cursor; pthread_t threads[NUM_THREADS]; int i, ret; /* * 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; if ((ret = wiredtiger_open(home, NULL, "create", &conn)) != 0) fprintf(stderr, "Error connecting to %s: %s\n", home == NULL ? "." : home, wiredtiger_strerror(ret)); /* Note: further error checking omitted for clarity. */ ret = conn->open_session(conn, NULL, NULL, &session); ret = session->create(session, "table:access", "key_format=S,value_format=S"); ret = session->open_cursor(session, "table:access", NULL, "overwrite", &cursor); cursor->set_key(cursor, "key1"); cursor->set_value(cursor, "value1"); ret = cursor->insert(cursor); ret = session->close(session, NULL); for (i = 0; i < NUM_THREADS; i++) ret = pthread_create(&threads[i], NULL, scan_thread, conn); for (i = 0; i < NUM_THREADS; i++) ret = pthread_join(threads[i], NULL); ret = conn->close(conn, NULL); return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE); }
int main(void) { WT_CONNECTION *conn; WT_CURSOR *cursor; WT_SESSION *session; int ret; /* * 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; ret = wiredtiger_open(home, NULL, "create,statistics=(all)", &conn); ret = conn->open_session(conn, NULL, NULL, &session); ret = session->create( session, "table:access", "key_format=S,value_format=S"); ret = session->open_cursor( session, "table:access", NULL, NULL, &cursor); cursor->set_key(cursor, "key"); cursor->set_value(cursor, "value"); ret = cursor->insert(cursor); ret = cursor->close(cursor); ret = session->checkpoint(session, NULL); ret = print_database_stats(session); ret = print_file_stats(session); ret = print_overflow_pages(session); ret = print_derived_stats(session); return (conn->close(conn, NULL) == 0 ? ret : EXIT_FAILURE); }
static void add_work(WT_SESSION *session, int iter) { WT_CURSOR *cursor; int i; char k[32], v[32]; error_check(session->open_cursor(session, uri, NULL, NULL, &cursor)); /* * Perform some operations with individual auto-commit transactions. */ for (i = 0; i < MAX_KEYS; i++) { (void)snprintf(k, sizeof(k), "key.%d.%d", iter, i); (void)snprintf(v, sizeof(v), "value.%d.%d", iter, i); cursor->set_key(cursor, k); cursor->set_value(cursor, v); error_check(cursor->insert(cursor)); } error_check(cursor->close(cursor)); }
/* * __wt_metadata_update -- * Update a row in the metadata. */ int __wt_metadata_update( WT_SESSION_IMPL *session, const char *key, const char *value) { WT_CURSOR *cursor; WT_DECL_RET; if (__metadata_turtle(key)) return (__wt_turtle_update(session, key, value)); if (WT_META_TRACKING(session)) WT_RET(__wt_meta_track_update(session, key)); WT_RET(__wt_metadata_cursor(session, "overwrite", &cursor)); cursor->set_key(cursor, key); cursor->set_value(cursor, value); WT_ERR(cursor->insert(cursor)); err: WT_TRET(cursor->close(cursor)); return (ret); }
/* * __wt_metadata_insert -- * Insert a row into the metadata. */ int __wt_metadata_insert( WT_SESSION_IMPL *session, const char *key, const char *value) { WT_CURSOR *cursor; WT_DECL_RET; if (__metadata_turtle(key)) WT_RET_MSG(session, EINVAL, "%s: insert not supported on the turtle file", key); WT_RET(__wt_metadata_cursor(session, NULL, &cursor)); cursor->set_key(cursor, key); cursor->set_value(cursor, value); WT_ERR(cursor->insert(cursor)); if (WT_META_TRACKING(session)) WT_ERR(__wt_meta_track_insert(session, key)); err: WT_TRET(cursor->close(cursor)); return (ret); }
/* * __wt_bloom_finalize -- * Writes the Bloom filter to stable storage. After calling finalize, only * read operations can be performed on the bloom filter. */ int __wt_bloom_finalize(WT_BLOOM *bloom) { WT_CURSOR *c; WT_DECL_RET; WT_ITEM values; WT_SESSION *wt_session; uint64_t i; wt_session = (WT_SESSION *)bloom->session; WT_CLEAR(values); /* * Create a bit table to store the bloom filter in. * TODO: should this call __wt_schema_create directly? */ WT_RET(wt_session->create(wt_session, bloom->uri, bloom->config)); WT_RET(wt_session->open_cursor( wt_session, bloom->uri, NULL, "bulk=bitmap", &c)); /* Add the entries from the array into the table. */ for (i = 0; i < bloom->m; i += values.size) { /* Adjust bits to bytes for string offset */ values.data = bloom->bitstring + (i >> 3); /* * Shave off some bytes for pure paranoia, in case WiredTiger * reserves some special sizes. Choose a value so that if * we do multiple inserts, it will be on an byte boundary. */ values.size = (uint32_t)WT_MIN(bloom->m - i, UINT32_MAX - 127); c->set_value(c, &values); WT_ERR(c->insert(c)); } err: WT_TRET(c->close(c)); __wt_free(bloom->session, bloom->bitstring); bloom->bitstring = NULL; return (ret); }
static void uri_init(void) { WT_CURSOR *cursor; WT_SESSION *session; u_int i, key; char buf[128]; for (i = 0; i < uris; ++i) if (uri_list[i] == NULL) { testutil_check( __wt_snprintf(buf, sizeof(buf), "table:%u", i)); uri_list[i] = dstrdup(buf); } testutil_check(conn->open_session(conn, NULL, NULL, &session)); /* Initialize the file contents. */ for (i = 0; i < uris; ++i) { testutil_check(__wt_snprintf(buf, sizeof(buf), "key_format=S,value_format=S," "allocation_size=4K,leaf_page_max=32KB,")); testutil_check(session->create(session, uri_list[i], buf)); testutil_check(session->open_cursor( session, uri_list[i], NULL, NULL, &cursor)); for (key = 1; key < MAXKEY; ++key) { testutil_check(__wt_snprintf( buf, sizeof(buf), "key:%020u", key)); cursor->set_key(cursor, buf); cursor->set_value(cursor, buf); testutil_check(cursor->insert(cursor)); } testutil_check(cursor->close(cursor)); } /* Create a checkpoint we can use for readonly handles. */ testutil_check(session->checkpoint(session, NULL)); testutil_check(session->close(session, NULL)); }
TEST_F(WiredTigerRecoveryUnitTestFixture, LocalReadOnADocumentBeingPreparedWithoutIgnoringPreparedTriggersPrepareConflict) { // Prepare but don't commit a transaction ru1->beginUnitOfWork(clientAndCtx1.second.get()); WT_CURSOR* cursor; getCursor(ru1, &cursor); cursor->set_key(cursor, "key"); cursor->set_value(cursor, "value"); invariantWTOK(cursor->insert(cursor)); ru1->setPrepareTimestamp({1, 1}); ru1->prepareUnitOfWork(); // The transaction read default enforces prepare conflicts and triggers a WT_PREPARE_CONFLICT. ru2->beginUnitOfWork(clientAndCtx2.second.get()); getCursor(ru2, &cursor); cursor->set_key(cursor, "key"); int ret = cursor->search(cursor); ASSERT_EQ(WT_PREPARE_CONFLICT, ret); ru1->abortUnitOfWork(); ru2->abortUnitOfWork(); }
void populate(TEST_OPTS *opts) { WT_CURSOR *maincur; WT_RAND_STATE rnd; WT_SESSION *session; uint32_t key; int balance, i, flag, post; __wt_random_init_seed(NULL, &rnd); testutil_check(opts->conn->open_session( opts->conn, NULL, NULL, &session)); testutil_check(session->open_cursor(session, opts->uri, NULL, NULL, &maincur)); for (i = 0; i < N_INSERT; i++) { testutil_check(session->begin_transaction(session, NULL)); key = (__wt_random(&rnd) % (N_RECORDS)); maincur->set_key(maincur, key); if (__wt_random(&rnd) % 11 == 0) post = 54321; else post = i % 100000; if (__wt_random(&rnd) % 4 == 0) { balance = -100; flag = 1; } else { balance = 100 * (i + 1); flag = 0; } maincur->set_value(maincur, post, balance, flag, key); testutil_check(maincur->insert(maincur)); testutil_check(session->commit_transaction(session, NULL)); } testutil_check(maincur->close(maincur)); testutil_check(session->close(session, NULL)); }
/* * A thread dedicated to appending records into a table. Works with fixed * length column stores and variable length column stores. * One thread (the first thread created by an application) checks for a * terminating condition after each insert. */ WT_THREAD_RET thread_append(void *arg) { TEST_OPTS *opts; WT_CONNECTION *conn; WT_CURSOR *cursor; WT_SESSION *session; uint64_t id, recno; char buf[64]; opts = (TEST_OPTS *)arg; conn = opts->conn; id = __wt_atomic_fetch_addv64(&opts->next_threadid, 1); testutil_check(conn->open_session(conn, NULL, NULL, &session)); testutil_check( session->open_cursor(session, opts->uri, NULL, "append", &cursor)); buf[0] = '\2'; for (recno = 1; opts->running; ++recno) { if (opts->table_type == TABLE_FIX) cursor->set_value(cursor, buf[0]); else { testutil_check(__wt_snprintf(buf, sizeof(buf), "%" PRIu64 " VALUE ------", recno)); cursor->set_value(cursor, buf); } testutil_check(cursor->insert(cursor)); if (id == 0) { testutil_check( cursor->get_key(cursor, &opts->max_inserted_id)); if (opts->max_inserted_id >= opts->nrecords) opts->running = false; } } return (WT_THREAD_RET_VALUE); }
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_join_cursor_stats(session); print_overflow_pages(session); print_derived_stats(session); error_check(conn->close(conn, NULL)); return (EXIT_SUCCESS); }
TEST_F(WiredTigerRecoveryUnitTestFixture, LocalReadOnADocumentBeingPreparedDoesntTriggerPrepareConflict) { // Prepare but don't commit a transaction ru1->beginUnitOfWork(clientAndCtx1.second.get()); WT_CURSOR* cursor; getCursor(ru1, &cursor); cursor->set_key(cursor, "key"); cursor->set_value(cursor, "value"); invariantWTOK(cursor->insert(cursor)); ru1->setPrepareTimestamp({1, 1}); ru1->prepareUnitOfWork(); // A transaction that chooses to ignore prepare conflicts does not see the record instead of // returning a prepare conflict. ru2->beginUnitOfWork(clientAndCtx2.second.get()); ru2->setIgnorePrepared(true); getCursor(ru2, &cursor); cursor->set_key(cursor, "key"); int ret = cursor->search(cursor); ASSERT_EQ(WT_NOTFOUND, ret); ru1->abortUnitOfWork(); ru2->abortUnitOfWork(); }
int util_write(WT_SESSION *session, int argc, char *argv[]) { WT_CURSOR *cursor; uint64_t recno; int append, ch, overwrite, rkey, ret; const char *uri; char config[100]; append = overwrite = ret = 0; while ((ch = util_getopt(argc, argv, "ao")) != EOF) switch (ch) { case 'a': append = 1; break; case 'o': overwrite = 1; break; case '?': default: return (usage()); } argc -= util_optind; argv += util_optind; /* * The remaining arguments are a uri followed by a list of values (if * append is set), or key/value pairs (if append is not set). */ if (append) { if (argc < 2) return (usage()); } else if (argc < 3 || ((argc - 1) % 2 != 0)) return (usage()); if ((uri = util_name(*argv, "table", UTIL_FILE_OK | UTIL_TABLE_OK)) == NULL) return (1); /* Open the object. */ (void)snprintf(config, sizeof(config), "%s,%s", append ? "append=true" : "", overwrite ? "overwrite=true" : ""); if ((ret = session->open_cursor( session, uri, NULL, config, &cursor)) != 0) return (util_err(ret, "%s: session.open", uri)); /* * A simple search only makes sense if the key format is a string or a * record number, and the value format is a single string. */ if (strcmp(cursor->key_format, "r") != 0 && strcmp(cursor->key_format, "S") != 0) { fprintf(stderr, "%s: write command only possible when the key format is " "a record number or string\n", progname); return (1); } rkey = strcmp(cursor->key_format, "r") == 0 ? 1 : 0; if (strcmp(cursor->value_format, "S") != 0) { fprintf(stderr, "%s: write command only possible when the value format is " "a string\n", progname); return (1); } /* Run through the values or key/value pairs. */ while (*++argv != NULL) { if (!append) { if (rkey) { if (util_str2recno(*argv, &recno)) return (1); cursor->set_key(cursor, recno); } else cursor->set_key(cursor, *argv); ++argv; } cursor->set_value(cursor, *argv); if ((ret = cursor->insert(cursor)) != 0) return (util_cerr(uri, "search", ret)); } return (0); }
int cursor_ops(WT_SESSION *session) { WT_CURSOR *cursor; int ret; /*! [Open a cursor] */ ret = session->open_cursor( session, "table:mytable", NULL, NULL, &cursor); /*! [Open a cursor] */ /*! [Open a cursor on the metadata] */ ret = session->open_cursor( session, "metadata:", NULL, NULL, &cursor); /*! [Open a cursor on the metadata] */ { WT_CURSOR *duplicate; const char *key = "some key"; /*! [Duplicate a cursor] */ ret = session->open_cursor( session, "table:mytable", NULL, NULL, &cursor); cursor->set_key(cursor, key); ret = cursor->search(cursor); /* Duplicate the cursor. */ ret = session->open_cursor(session, NULL, cursor, NULL, &duplicate); /*! [Duplicate a cursor] */ } { WT_CURSOR *overwrite_cursor; const char *key = "some key", *value = "some value"; /*! [Reconfigure a cursor] */ ret = session->open_cursor( session, "table:mytable", NULL, NULL, &cursor); cursor->set_key(cursor, key); /* Reconfigure the cursor to overwrite the record. */ ret = session->open_cursor( session, NULL, cursor, "overwrite", &overwrite_cursor); ret = cursor->close(cursor); overwrite_cursor->set_value(overwrite_cursor, value); ret = overwrite_cursor->insert(cursor); /*! [Reconfigure a cursor] */ } { /*! [boolean configuration string example] */ ret = session->open_cursor(session, "table:mytable", NULL, "overwrite", &cursor); ret = session->open_cursor(session, "table:mytable", NULL, "overwrite=true", &cursor); ret = session->open_cursor(session, "table:mytable", NULL, "overwrite=1", &cursor); /*! [boolean configuration string example] */ } { /*! [open a named checkpoint] */ ret = session->open_cursor(session, "table:mytable", NULL, "checkpoint=midnight", &cursor); /*! [open a named checkpoint] */ } { /*! [open the default checkpoint] */ ret = session->open_cursor(session, "table:mytable", NULL, "checkpoint=WiredTigerCheckpoint", &cursor); /*! [open the default checkpoint] */ } { /*! [Get the cursor's string key] */ const char *key; /* Get the cursor's string key. */ ret = cursor->get_key(cursor, &key); /*! [Get the cursor's string key] */ } { /*! [Set the cursor's string key] */ /* Set the cursor's string key. */ const char *key = "another key"; cursor->set_key(cursor, key); /*! [Set the cursor's string key] */ } { /*! [Get the cursor's record number key] */ uint64_t recno; /* Get the cursor's record number key. */ ret = cursor->get_key(cursor, &recno); /*! [Get the cursor's record number key] */ } { /*! [Set the cursor's record number key] */ uint64_t recno = 37; /* Set the cursor's record number key. */ cursor->set_key(cursor, recno); /*! [Set the cursor's record number key] */ } { /*! [Get the cursor's composite key] */ /* Get the cursor's "SiH" format composite key. */ const char *first; int32_t second; uint16_t third; cursor->get_key(cursor, &first, &second, &third); /*! [Get the cursor's composite key] */ } { /*! [Set the cursor's composite key] */ /* Set the cursor's "SiH" format composite key. */ cursor->set_key(cursor, "first", (int32_t)5, (uint16_t)7); /*! [Set the cursor's composite key] */ } { /*! [Get the cursor's string value] */ const char *value; /* Get the cursor's string value. */ ret = cursor->get_value(cursor, &value); /*! [Get the cursor's string value] */ } { /*! [Set the cursor's string value] */ /* Set the cursor's string value. */ const char *value = "another value"; cursor->set_value(cursor, value); /*! [Set the cursor's string value] */ } { /*! [Get the cursor's raw value] */ WT_ITEM value; /* Get the cursor's raw value. */ ret = cursor->get_value(cursor, &value); /*! [Get the cursor's raw value] */ } { /*! [Set the cursor's raw value] */ WT_ITEM value; /* Set the cursor's raw value. */ value.data = "another value"; value.size = strlen("another value"); cursor->set_value(cursor, &value); /*! [Set the cursor's raw value] */ } /*! [Return the next record] */ ret = cursor->next(cursor); /*! [Return the next record] */ /*! [Return the previous record] */ ret = cursor->prev(cursor); /*! [Return the previous record] */ /*! [Reset the cursor] */ ret = cursor->reset(cursor); /*! [Reset the cursor] */ { WT_CURSOR *other = NULL; /*! [Cursor comparison] */ int compare; ret = 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 */ } /*! [Cursor comparison] */ } { /*! [Search for an exact match] */ const char *key = "some key"; cursor->set_key(cursor, key); ret = cursor->search(cursor); /*! [Search for an exact match] */ } cursor_search_near(cursor); { /*! [Insert a new record or overwrite an existing record] */ /* Insert a new record or overwrite an existing record. */ const char *key = "some key", *value = "some value"; ret = session->open_cursor( session, "table:mytable", NULL, NULL, &cursor); cursor->set_key(cursor, key); cursor->set_value(cursor, value); ret = cursor->insert(cursor); /*! [Insert a new record or overwrite an existing record] */ } { /*! [Insert a new record and fail if the record exists] */ /* Insert a new record and fail if the record exists. */ const char *key = "some key", *value = "some value"; ret = session->open_cursor( session, "table:mytable", NULL, "overwrite=false", &cursor); cursor->set_key(cursor, key); cursor->set_value(cursor, value); ret = cursor->insert(cursor); /*! [Insert a new record and fail if the record exists] */ } { /*! [Insert a new record and assign a record number] */ /* Insert a new record and assign a record number. */ uint64_t recno; const char *value = "some value"; ret = session->open_cursor( session, "table:mytable", NULL, "append", &cursor); cursor->set_value(cursor, value); ret = cursor->insert(cursor); if (ret == 0) ret = cursor->get_key(cursor, &recno); /*! [Insert a new record and assign a record number] */ } { /*! [Update an existing record or insert a new record] */ const char *key = "some key", *value = "some value"; ret = session->open_cursor( session, "table:mytable", NULL, NULL, &cursor); cursor->set_key(cursor, key); cursor->set_value(cursor, value); ret = cursor->update(cursor); /*! [Update an existing record or insert a new record] */ } { /*! [Update an existing record and fail if DNE] */ const char *key = "some key", *value = "some value"; ret = session->open_cursor( session, "table:mytable", NULL, "overwrite=false", &cursor); cursor->set_key(cursor, key); cursor->set_value(cursor, value); ret = cursor->update(cursor); /*! [Update an existing record and fail if DNE] */ } { /*! [Remove a record] */ const char *key = "some key"; ret = session->open_cursor( session, "table:mytable", NULL, NULL, &cursor); cursor->set_key(cursor, key); ret = cursor->remove(cursor); /*! [Remove a record] */ } { /*! [Remove a record and fail if DNE] */ const char *key = "some key"; ret = session->open_cursor( session, "table:mytable", NULL, "overwrite=false", &cursor); cursor->set_key(cursor, key); ret = cursor->remove(cursor); /*! [Remove a record and fail if DNE] */ } { /*! [Display an error] */ const char *key = "non-existent key"; cursor->set_key(cursor, key); if ((ret = cursor->remove(cursor)) != 0) { fprintf(stderr, "cursor.remove: %s\n", wiredtiger_strerror(ret)); return (ret); } /*! [Display an error] */ } /*! [Close the cursor] */ ret = cursor->close(cursor); /*! [Close the cursor] */ return (ret); }
int main(int argc, char *argv[]) { TEST_OPTS *opts, _opts; WT_CURSOR *balancecur, *flagcur, *joincur, *postcur; WT_CURSOR *maincur; WT_SESSION *session; int balance, count, flag, key, key2, post, ret; char balanceuri[256]; char cfg[128]; char flaguri[256]; char joinuri[256]; char posturi[256]; const char *tablename; opts = &_opts; memset(opts, 0, sizeof(*opts)); testutil_check(testutil_parse_opts(argc, argv, opts)); testutil_make_work_dir(opts->home); testutil_progress(opts, "start"); testutil_check(wiredtiger_open(opts->home, NULL, "create,cache_size=250M", &opts->conn)); testutil_progress(opts, "wiredtiger_open"); testutil_check( opts->conn->open_session(opts->conn, NULL, NULL, &session)); testutil_progress(opts, "sessions opened"); /* * Note: repeated primary key 'id' as 'id2'. This makes * it easier to dump an index and know which record we're * looking at. */ testutil_check(session->create(session, opts->uri, "key_format=i,value_format=iiii," "columns=(id,post,balance,flag,id2)")); tablename = strchr(opts->uri, ':'); testutil_assert(tablename != NULL); tablename++; testutil_check(__wt_snprintf( posturi, sizeof(posturi), "index:%s:post", tablename)); testutil_check(__wt_snprintf( balanceuri, sizeof(balanceuri), "index:%s:balance", tablename)); testutil_check(__wt_snprintf( flaguri, sizeof(flaguri), "index:%s:flag", tablename)); testutil_check(__wt_snprintf( joinuri, sizeof(joinuri), "join:%s", opts->uri)); testutil_check(session->create(session, posturi, "columns=(post)")); testutil_check(session->create(session, balanceuri, "columns=(balance)")); testutil_check(session->create(session, flaguri, "columns=(flag)")); testutil_progress(opts, "setup complete"); /* * Insert a single record with all items we are search for, * this makes our logic easier. */ testutil_check(session->open_cursor(session, opts->uri, NULL, NULL, &maincur)); maincur->set_key(maincur, N_RECORDS); maincur->set_value(maincur, 54321, 0, "", 0, N_RECORDS); testutil_check(maincur->insert(maincur)); testutil_check(maincur->close(maincur)); testutil_check(session->close(session, NULL)); testutil_progress(opts, "populate start"); populate(opts); testutil_progress(opts, "populate end"); testutil_check(opts->conn->open_session( opts->conn, NULL, NULL, &session)); testutil_check(session->open_cursor(session, posturi, NULL, NULL, &postcur)); testutil_check(session->open_cursor(session, balanceuri, NULL, NULL, &balancecur)); testutil_check(session->open_cursor(session, flaguri, NULL, NULL, &flagcur)); testutil_check(session->open_cursor(session, joinuri, NULL, NULL, &joincur)); postcur->set_key(postcur, 54321); testutil_check(postcur->search(postcur)); testutil_check(session->join(session, joincur, postcur, "compare=eq")); balancecur->set_key(balancecur, 0); testutil_check(balancecur->search(balancecur)); testutil_check(__wt_snprintf(cfg, sizeof(cfg), "compare=lt,strategy=bloom,count=%d", N_RECORDS / 100)); testutil_check(session->join(session, joincur, balancecur, cfg)); flagcur->set_key(flagcur, 0); testutil_check(flagcur->search(flagcur)); testutil_check(__wt_snprintf(cfg, sizeof(cfg), "compare=eq,strategy=bloom,count=%d", N_RECORDS / 100)); testutil_check(session->join(session, joincur, flagcur, cfg)); /* Expect no values returned */ count = 0; while ((ret = joincur->next(joincur)) == 0) { /* * The values may already have been changed, but * print them for informational purposes. */ testutil_check(joincur->get_key(joincur, &key)); testutil_check(joincur->get_value(joincur, &post, &balance, &flag, &key2)); fprintf(stderr, "FAIL: " "key=%d/%d, postal_code=%d, balance=%d, flag=%d\n", key, key2, post, balance, flag); count++; } testutil_assert(ret == WT_NOTFOUND); testutil_assert(count == 0); testutil_progress(opts, "cleanup starting"); testutil_cleanup(opts); return (EXIT_SUCCESS); }
/* * build -- * Build a row- or column-store page in a file. */ void build(int ikey, int ivalue, int cnt) { WT_CONNECTION *conn; WT_CURSOR *cursor; WT_ITEM key, value; WT_SESSION *session; char config[256], kbuf[64], vbuf[64]; int new_slvg; /* * Disable logging: we're modifying files directly, we don't want to * run recovery. */ CHECK(wiredtiger_open( NULL, NULL, "create,log=(enabled=false)", &conn) == 0); CHECK(conn->open_session(conn, NULL, NULL, &session) == 0); CHECK(session->drop(session, "file:" LOAD, "force") == 0); switch (page_type) { case WT_PAGE_COL_FIX: (void)snprintf(config, sizeof(config), "key_format=r,value_format=7t," "allocation_size=%d," "internal_page_max=%d,internal_item_max=%d," "leaf_page_max=%d,leaf_item_max=%d", PSIZE, PSIZE, OSIZE, PSIZE, OSIZE); break; case WT_PAGE_COL_VAR: (void)snprintf(config, sizeof(config), "key_format=r," "allocation_size=%d," "internal_page_max=%d,internal_item_max=%d," "leaf_page_max=%d,leaf_item_max=%d", PSIZE, PSIZE, OSIZE, PSIZE, OSIZE); break; case WT_PAGE_ROW_LEAF: (void)snprintf(config, sizeof(config), "key_format=u," "allocation_size=%d," "internal_page_max=%d,internal_item_max=%d," "leaf_page_max=%d,leaf_item_max=%d", PSIZE, PSIZE, OSIZE, PSIZE, OSIZE); break; default: assert(0); } CHECK(session->create(session, "file:" LOAD, config) == 0); CHECK(session->open_cursor( session, "file:" LOAD, NULL, "bulk,append", &cursor) == 0); for (; cnt > 0; --cnt, ++ikey, ++ivalue) { switch (page_type) { /* Build the key. */ case WT_PAGE_COL_FIX: case WT_PAGE_COL_VAR: break; case WT_PAGE_ROW_LEAF: snprintf(kbuf, sizeof(kbuf), "%010d KEY------", ikey); key.data = kbuf; key.size = 20; cursor->set_key(cursor, &key); break; } switch (page_type) { /* Build the value. */ case WT_PAGE_COL_FIX: cursor->set_value(cursor, ivalue & 0x7f); break; case WT_PAGE_COL_VAR: case WT_PAGE_ROW_LEAF: snprintf(vbuf, sizeof(vbuf), "%010d VALUE----", value_unique ? ivalue : 37); value.data = vbuf; value.size = 20; cursor->set_value(cursor, &value); } CHECK(cursor->insert(cursor) == 0); } /* * The first time through this routine we create the salvage file and * then remove it (all we want is the appropriate schema entry, we're * creating the salvage file itself by hand). */ new_slvg = !file_exists(SLVG); if (new_slvg) { CHECK(session->drop(session, "file:" SLVG, "force") == 0); CHECK(session->create(session, "file:" SLVG, config) == 0); } CHECK(conn->close(conn, 0) == 0); if (new_slvg) (void)remove(SLVG); }
/* * Child process creates the database and table, and then writes data into * the table until it is killed by the parent. */ static void fill_db(void) { FILE *fp; WT_CONNECTION *conn; WT_CURSOR *cursor; WT_ITEM data; WT_RAND_STATE rnd; WT_SESSION *session; uint64_t i; int ret; uint8_t buf[MAX_VAL]; __wt_random_init(&rnd); memset(buf, 0, sizeof(buf)); /* * Initialize the first 25% to random values. Leave a bunch of data * space at the end to emphasize zero data. */ for (i = 0; i < MAX_VAL/4; i++) buf[i] = (uint8_t)__wt_random(&rnd); /* * Run in the home directory so that the records file is in there too. */ chdir(home); if ((ret = wiredtiger_open(NULL, NULL, ENV_CONFIG, &conn)) != 0) testutil_die(ret, "wiredtiger_open"); if ((ret = conn->open_session(conn, NULL, NULL, &session)) != 0) testutil_die(ret, "WT_CONNECTION:open_session"); if ((ret = session->create(session, uri, "key_format=Q,value_format=u")) != 0) testutil_die(ret, "WT_SESSION.create: %s", uri); if ((ret = session->open_cursor(session, uri, NULL, NULL, &cursor)) != 0) testutil_die(ret, "WT_SESSION.open_cursor: %s", uri); /* * Keep a separate file with the records we wrote for checking. */ (void)unlink(RECORDS_FILE); if ((fp = fopen(RECORDS_FILE, "w")) == NULL) testutil_die(errno, "fopen"); /* * Set to no buffering. */ setvbuf(fp, NULL, _IONBF, 0); /* * Write data into the table until we are killed by the parent. * The data in the buffer is already set to random content. */ data.data = buf; for (i = 0;; ++i) { data.size = __wt_random(&rnd) % MAX_VAL; cursor->set_key(cursor, i); cursor->set_value(cursor, &data); if ((ret = cursor->insert(cursor)) != 0) testutil_die(ret, "WT_CURSOR.insert"); /* * Save the key separately for checking later. */ if (fprintf(fp, "%" PRIu64 "\n", i) == -1) testutil_die(errno, "fprintf"); if (i % 5000) __wt_yield(); } }