const std::string WiredTigerEngine::Stats() { std::string info; int major_v, minor_v, patch; (void) wiredtiger_version(&major_v, &minor_v, &patch); info.append("WiredTiger version:").append(stringfromll(major_v)).append(".").append(stringfromll(minor_v)).append( ".").append(stringfromll(patch)).append("\r\n"); info.append("WiredTiger Init Options:").append(m_cfg.init_options).append("\r\n"); info.append("WiredTiger Table Init Options:").append(m_cfg.init_table_options).append("\r\n"); ContextHolder& holder = m_context.GetValue(); WT_SESSION* session = holder.session; if (NULL == session) { return info; } WT_CURSOR *cursor; int ret; /*! [statistics database function] */ if ((ret = session->open_cursor(session, "statistics:", NULL, NULL, &cursor)) == 0) { print_cursor(cursor, info); cursor->close(cursor); } if ((ret = session->open_cursor(session, "statistics:table:ardb", NULL, NULL, &cursor)) == 0) { print_cursor(cursor, info); cursor->close(cursor); } return info; }
/* * verify_metadata -- * Verify all the tables expected are in the metadata. We expect all but * the "corrupt" table name. */ static void verify_metadata(WT_CONNECTION *conn, TABLE_INFO *tables) { TABLE_INFO *t; WT_CURSOR *cursor; int ret; const char *kv; /* * Open a metadata cursor. */ testutil_check(conn->open_session(conn, NULL, NULL, &wt_session)); testutil_check(wt_session->open_cursor( wt_session, "metadata:", NULL, NULL, &cursor)); reset_verified(tables); /* * We have to walk the cursor and walk the tables to match up that * the expected tables are in the metadata. It is not efficient, but * the list of tables is small. Walk the cursor once and the array * of tables each time. */ while ((ret = cursor->next(cursor)) == 0) { testutil_check(cursor->get_key(cursor, &kv)); for (t = tables; t->name != NULL; t++) { if (strcmp(t->name, kv) == 0) { testutil_assert(t->verified == false); t->verified = true; break; } } } testutil_assert(ret == WT_NOTFOUND); testutil_check(cursor->close(cursor)); /* * Any tables that were salvaged, make sure we can read the data. * The corrupt table should never be salvaged. */ for (t = tables; t->name != NULL; t++) { if (strcmp(t->name, CORRUPT) == 0 && !test_out_of_sync) testutil_assert(t->verified == false); else if (t->verified != true) printf("%s not seen in metadata\n", t->name); else { testutil_check(wt_session->open_cursor( wt_session, t->name, NULL, NULL, &cursor)); while ((ret = cursor->next(cursor)) == 0) { testutil_check(cursor->get_value(cursor, &kv)); testutil_assert(strcmp(kv, VALUE) == 0); } testutil_assert(ret == WT_NOTFOUND); testutil_check(cursor->close(cursor)); printf("%s metadata salvaged and data verified\n", t->name); } } }
/* * wts_stats -- * Dump the run's statistics. */ void wts_stats(void) { WT_CONNECTION *conn; WT_CURSOR *cursor; WT_SESSION *session; FILE *fp; const char *pval, *desc; uint64_t v; int ret; track("stat", 0ULL, NULL); conn = g.wts_conn; if ((ret = conn->open_session(conn, NULL, NULL, &session)) != 0) die(ret, "connection.open_session"); if ((fp = fopen("__stats", "w")) == NULL) die(errno, "fopen: __stats"); /* Connection statistics. */ if ((ret = session->open_cursor(session, "statistics:", NULL, NULL, &cursor)) != 0) die(ret, "session.open_cursor"); while ((ret = cursor->next(cursor)) == 0 && (ret = cursor->get_value(cursor, &desc, &pval, &v)) == 0) if (fprintf(fp, "%s=%s\n", desc, pval) < 0) die(errno, "fprintf"); if (ret != WT_NOTFOUND) die(ret, "cursor.next"); if ((ret = cursor->close(cursor)) != 0) die(ret, "cursor.close"); /* File statistics. */ if ((ret = session->open_cursor(session, "statistics:" WT_TABLENAME, NULL, NULL, &cursor)) != 0) die(ret, "session.open_cursor"); while ((ret = cursor->next(cursor)) == 0 && (ret = cursor->get_value(cursor, &desc, &pval, &v)) == 0) if (fprintf(fp, "%s=%s\n", desc, pval) < 0) die(errno, "fprintf"); if (ret != WT_NOTFOUND) die(ret, "cursor.next"); if ((ret = cursor->close(cursor)) != 0) die(ret, "cursor.close"); if ((ret = fclose(fp)) != 0) die(ret, "fclose"); if ((ret = session->close(session, NULL)) != 0) die(ret, "session.close"); }
/* * __curds_close -- * WT_CURSOR.close method for the data-source cursor type. */ static int __curds_close(WT_CURSOR *cursor) { WT_CURSOR *source; WT_DECL_RET; WT_SESSION_IMPL *session; source = ((WT_CURSOR_DATA_SOURCE *)cursor)->source; CURSOR_API_CALL(cursor, session, close, NULL); if (source != NULL) ret = source->close(source); /* * The key/value formats are in allocated memory, which isn't standard * behavior. */ __wt_free(session, cursor->key_format); __wt_free(session, cursor->value_format); WT_TRET(__wt_cursor_close(cursor)); err: API_END_RET(session, ret); }
/* * simple_walk_log -- * A simple walk of the write-ahead log. * We wrote text messages into the log. Print them. * This verifies we're decrypting properly. */ static void simple_walk_log(WT_SESSION *session) { WT_CURSOR *cursor; WT_ITEM logrec_key, logrec_value; uint64_t txnid; uint32_t fileid, log_file, log_offset, opcount, optype, rectype; int found, ret; error_check(session->open_cursor(session, "log:", NULL, NULL, &cursor)); found = 0; while ((ret = cursor->next(cursor)) == 0) { error_check(cursor->get_key( cursor, &log_file, &log_offset, &opcount)); error_check(cursor->get_value(cursor, &txnid, &rectype, &optype, &fileid, &logrec_key, &logrec_value)); if (rectype == WT_LOGREC_MESSAGE) { found = 1; printf("Application Log Record: %s\n", (char *)logrec_value.data); } } scan_end_check(ret == WT_NOTFOUND); error_check(cursor->close(cursor)); if (found == 0) { fprintf(stderr, "Did not find log messages.\n"); exit(EXIT_FAILURE); } }
/* * 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++; }
/* * simple_walk_log -- * A simple walk of the log. */ static int simple_walk_log(WT_SESSION *session) { WT_CURSOR *cursor; WT_LSN lsn; WT_ITEM logrec_key, logrec_value; uint64_t txnid; uint32_t fileid, opcount, optype, rectype; int ret; /*! [log cursor open] */ ret = session->open_cursor(session, "log:", NULL, NULL, &cursor); /*! [log cursor open] */ while ((ret = cursor->next(cursor)) == 0) { /*! [log cursor get_key] */ ret = cursor->get_key(cursor, &lsn.file, &lsn.offset, &opcount); /*! [log cursor get_key] */ /*! [log cursor get_value] */ ret = cursor->get_value(cursor, &txnid, &rectype, &optype, &fileid, &logrec_key, &logrec_value); /*! [log cursor get_value] */ print_record(&lsn, opcount, rectype, optype, txnid, fileid, &logrec_key, &logrec_value); } if (ret == WT_NOTFOUND) ret = 0; ret = cursor->close(cursor); return (ret); }
/* * __wt_metadata_search -- * Return a copied row from the metadata. * The caller is responsible for freeing the allocated memory. */ int __wt_metadata_search( WT_SESSION_IMPL *session, const char *key, char **valuep) { WT_CURSOR *cursor; WT_DECL_RET; const char *value; *valuep = NULL; WT_RET(__wt_verbose(session, WT_VERB_METADATA, "Search: key: %s, tracking: %s, %s" "turtle", key, WT_META_TRACKING(session) ? "true" : "false", __metadata_turtle(key) ? "" : "not ")); if (__metadata_turtle(key)) return (__wt_turtle_read(session, key, valuep)); WT_RET(__wt_metadata_cursor(session, NULL, &cursor)); cursor->set_key(cursor, key); WT_ERR(cursor->search(cursor)); WT_ERR(cursor->get_value(cursor, &value)); WT_ERR(__wt_strdup(session, value, valuep)); err: WT_TRET(cursor->close(cursor)); return (ret); }
int WiredTigerEngine::Get(const Slice& key, std::string* value, const Options& options) { WT_SESSION* session = GetContextHolder().session; if (NULL == session) { return -1; } WT_CURSOR *cursor = create_wiredtiger_cursor(session); if (NULL == cursor) { return -1; } WT_ITEM key_item, value_item; key_item.data = key.data(); key_item.size = key.size(); cursor->set_key(cursor, &key_item); int ret = 0; if ((ret = cursor->search(cursor)) == 0) { ret = cursor->get_value(cursor, &value_item); if (0 == ret) { if (NULL != value) { value->assign((const char*) value_item.data, value_item.size); } } else { CHECK_WT_RETURN(ret); } } cursor->close(cursor); return ret; }
void WiredTigerOperationStats::fetchStats(WT_SESSION* session, const std::string& uri, const std::string& config) { invariant(session); WT_CURSOR* c = nullptr; const char* cursorConfig = config.empty() ? nullptr : config.c_str(); int ret = session->open_cursor(session, uri.c_str(), nullptr, cursorConfig, &c); uassert(ErrorCodes::CursorNotFound, "Unable to open statistics cursor", ret == 0); invariant(c); ON_BLOCK_EXIT([&] { c->close(c); }); const char* desc; uint64_t value; uint64_t key; while (c->next(c) == 0 && c->get_key(c, &key) == 0) { fassert(51035, c->get_value(c, &desc, nullptr, &value) == 0); #if defined(__s390x__) _stats[key >> 32] = WiredTigerUtil::castStatisticsValue<long long>(value); #else _stats[key] = WiredTigerUtil::castStatisticsValue<long long>(value); #endif // __s390x__ } // Reset the statistics so that the next fetch gives the recent values. invariantWTOK(c->reset(c)); }
/* * __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; WT_RET(__wt_verbose(session, WT_VERB_METADATA, "Insert: key: %s, value: %s, tracking: %s, %s" "turtle", key, value, WT_META_TRACKING(session) ? "true" : "false", __metadata_turtle(key) ? "" : "not ")); 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); }
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); }
int wt_id2entry_delete( Operation *op, WT_SESSION *session, Entry *e ) { int rc; WT_CURSOR *cursor = NULL; rc = session->open_cursor(session, WT_TABLE_ID2ENTRY, NULL, NULL, &cursor); if ( rc ) { Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(wt_id2entry_delete) ": open_cursor failed: %s (%d)\n", wiredtiger_strerror(rc), rc, 0 ); goto done; } cursor->set_key(cursor, e->e_id); rc = cursor->remove(cursor); if ( rc ) { Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(wt_id2entry_delete) ": remove failed: %s (%d)\n", wiredtiger_strerror(rc), rc, 0 ); goto done; } done: if(cursor){ cursor->close(cursor); } return rc; }
/* * __clsm_close_cursors -- * Close any btree cursors that are not needed. */ static int __clsm_close_cursors(WT_CURSOR_LSM *clsm, u_int start, u_int end) { WT_BLOOM *bloom; WT_CURSOR *c; u_int i; if (clsm->cursors == NULL || clsm->nchunks == 0) return (0); /* * Walk the cursors, closing any we don't need. Note that the exit * condition here is special, don't use WT_FORALL_CURSORS, and be * careful with unsigned integer wrapping. */ for (i = start; i < end; i++) { if ((c = (clsm)->cursors[i]) != NULL) { clsm->cursors[i] = NULL; WT_RET(c->close(c)); } if ((bloom = clsm->blooms[i]) != NULL) { clsm->blooms[i] = NULL; WT_RET(__wt_bloom_close(bloom)); } } return (0); }
/* * __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; WT_RET(__wt_verbose(session, WT_VERB_METADATA, "Update: key: %s, value: %s, tracking: %s, %s" "turtle", key, value, WT_META_TRACKING(session) ? "true" : "false", __metadata_turtle(key) ? "" : "not ")); 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); }
/* * Create a table and open a bulk cursor on it. */ void op_bulk(void *arg) { TEST_OPTS *opts; TEST_PER_THREAD_OPTS *args; WT_CURSOR *c; WT_SESSION *session; int ret; args = (TEST_PER_THREAD_OPTS *)arg; opts = args->testopts; testutil_check( opts->conn->open_session(opts->conn, NULL, NULL, &session)); if ((ret = session->create(session, opts->uri, DEFAULT_TABLE_SCHEMA)) != 0) if (ret != EEXIST && ret != EBUSY) testutil_die(ret, "session.create"); if (ret == 0) { __wt_yield(); if ((ret = session->open_cursor(session, opts->uri, NULL, "bulk,checkpoint_wait=false", &c)) == 0) { testutil_check(c->close(c)); } else if (ret != ENOENT && ret != EBUSY && ret != EINVAL) testutil_die(ret, "session.open_cursor bulk"); } testutil_check(session->close(session, NULL)); args->thread_counter++; }
/* * __wt_metadata_remove -- * Remove a row from the metadata. */ int __wt_metadata_remove(WT_SESSION_IMPL *session, const char *key) { WT_CURSOR *cursor; WT_DECL_RET; WT_RET(__wt_verbose(session, WT_VERB_METADATA, "Remove: key: %s, tracking: %s, %s" "turtle", key, WT_META_TRACKING(session) ? "true" : "false", __metadata_turtle(key) ? "" : "not ")); if (__metadata_turtle(key)) WT_RET_MSG(session, EINVAL, "%s: remove not supported on the turtle file", key); WT_RET(__wt_metadata_cursor(session, NULL, &cursor)); cursor->set_key(cursor, key); WT_ERR(cursor->search(cursor)); if (WT_META_TRACKING(session)) WT_ERR(__wt_meta_track_update(session, key)); WT_ERR(cursor->remove(cursor)); err: WT_TRET(cursor->close(cursor)); return (ret); }
/* * cursor_insert -- * Insert some data into a table. */ static void cursor_insert(const char *uri, uint64_t i) { WT_CURSOR *cursor; WT_ITEM vu; char keybuf[100], valuebuf[100]; bool recno; memset(&vu, 0, sizeof(vu)); /* Open a cursor. */ testutil_check(wt_session->open_cursor( wt_session, uri, NULL, NULL, &cursor)); /* Operations change based on the key/value formats. */ recno = strcmp(cursor->key_format, "r") == 0; if (recno) cursor->set_key(cursor, i); else { testutil_check(__wt_snprintf(keybuf, sizeof(keybuf), "%s-%" PRIu64, KEY, i)); cursor->set_key(cursor, keybuf); } strcpy(valuebuf, VALUE); cursor->set_value(cursor, valuebuf); testutil_check(cursor->insert(cursor)); testutil_check(cursor->close(cursor)); }
void obj_bulk(void) { WT_CURSOR *c; WT_SESSION *session; int ret; if ((ret = conn->open_session(conn, NULL, NULL, &session)) != 0) testutil_die(ret, "conn.session"); if ((ret = session->create(session, uri, config)) != 0) if (ret != EEXIST && ret != EBUSY) testutil_die(ret, "session.create"); if (ret == 0) { __wt_yield(); if ((ret = session->open_cursor( session, uri, NULL, "bulk", &c)) == 0) { if ((ret = c->close(c)) != 0) testutil_die(ret, "cursor.close"); } else if (ret != ENOENT && ret != EBUSY && ret != EINVAL) testutil_die(ret, "session.open_cursor bulk"); } if ((ret = session->close(session, NULL)) != 0) testutil_die(ret, "session.close"); }
static void write_and_read_new(WT_SESSION *session) { WT_CURSOR *logc; WT_ITEM logrec_key, logrec_value; uint64_t txnid; uint32_t fileid, log_file, log_offset, opcount, optype, rectype; bool saw_msg; /* * Write a log record and force it to disk so we can read it. */ printf("Write log_printf record and verify.\n"); testutil_check(session->log_printf(session, "Test Log Record")); testutil_check(session->log_flush(session, "sync=on")); testutil_check( session->open_cursor(session, "log:", NULL, NULL, &logc)); testutil_check( session->open_cursor(session, "log:", NULL, NULL, &logc)); saw_msg = false; while (logc->next(logc) == 0) { /* * We don't really need to get the key, but in case we want * the LSN for some message, get it. */ testutil_check(logc->get_key( logc, &log_file, &log_offset, &opcount)); testutil_check(logc->get_value(logc, &txnid, &rectype, &optype, &fileid, &logrec_key, &logrec_value)); /* * We should never see a record from us in log file 2. We wrote * a record there, but then the record in log file 1 was * truncated to be a partial record, ending the log there. * So everything after that, including everything in log * file 2, is invalid until we get to log file 3 which is where * the post-recovery records will be written. * The one exception in log file two is the system record for * the previous log file's LSN. Although it is written by the * system, we do walk it when using a cursor. */ if (log_file == 2 && rectype != WT_LOGREC_SYSTEM) testutil_die(EINVAL, "Found LSN in Log 2"); #if 0 printf("LSN [%" PRIu32 "][%" PRIu32 "].%" PRIu32 ": record type %" PRIu32 " optype %" PRIu32 " txnid %" PRIu64 " fileid %" PRIu32 "\n", log_file, log_offset, opcount, rectype, optype, txnid, fileid); #endif if (rectype == WT_LOGREC_MESSAGE) { saw_msg = true; printf("Application Record: %s\n", (char *)logrec_value.data); break; } } testutil_check(logc->close(logc)); if (!saw_msg) testutil_die(EINVAL, "Did not traverse log printf record"); }
int backup(WT_SESSION *session) { char buf[1024]; /*! [backup]*/ WT_CURSOR *cursor; const char *filename; int ret; /* Create the backup directory. */ ret = mkdir("/path/database.backup", 077); /* Open the backup data source. */ ret = session->open_cursor(session, "backup:", NULL, NULL, &cursor); /* Copy the list of files. */ while ( (ret = cursor->next(cursor)) == 0 && (ret = cursor->get_key(cursor, &filename)) == 0) { (void)snprintf(buf, sizeof(buf), "cp /path/database/%s /path/database.backup/%s", filename, filename); ret = system(buf); } if (ret == WT_NOTFOUND) ret = 0; if (ret != 0) fprintf(stderr, "%s: cursor next(backup:) failed: %s\n", progname, session->strerror(session, ret)); ret = cursor->close(cursor); /*! [backup]*/ /*! [incremental backup]*/ /* Open the backup data source for incremental backup. */ ret = session->open_cursor( session, "backup:", NULL, "target=(\"log:\")", &cursor); /*! [incremental backup]*/ ret = cursor->close(cursor); /*! [backup of a checkpoint]*/ ret = session->checkpoint(session, "drop=(from=June01),name=June01"); /*! [backup of a checkpoint]*/ return (ret); }
/* * stats * Dump the database/file statistics. */ void stats(void) { FILE *fp; WT_CURSOR *cursor; WT_SESSION *session; uint64_t v; int ret; const char *desc, *pval; char name[64]; testutil_check(conn->open_session(conn, NULL, NULL, &session)); if ((fp = fopen(FNAME_STAT, "w")) == NULL) testutil_die(errno, "fopen " FNAME_STAT); /* Connection statistics. */ testutil_check(session->open_cursor( session, "statistics:", NULL, NULL, &cursor)); while ((ret = cursor->next(cursor)) == 0 && (ret = cursor->get_value(cursor, &desc, &pval, &v)) == 0) (void)fprintf(fp, "%s=%s\n", desc, pval); if (ret != WT_NOTFOUND) testutil_die(ret, "cursor.next"); testutil_check(cursor->close(cursor)); /* File statistics. */ if (!multiple_files) { testutil_check(__wt_snprintf( name, sizeof(name), "statistics:" FNAME, 0)); testutil_check(session->open_cursor( session, name, NULL, NULL, &cursor)); while ((ret = cursor->next(cursor)) == 0 && (ret = cursor->get_value(cursor, &desc, &pval, &v)) == 0) (void)fprintf(fp, "%s=%s\n", desc, pval); if (ret != WT_NOTFOUND) testutil_die(ret, "cursor.next"); testutil_check(cursor->close(cursor)); testutil_check(session->close(session, NULL)); } (void)fclose(fp); }
/* * __backup_all -- * Backup all objects in the database. */ static int __backup_all(WT_SESSION_IMPL *session, WT_CURSOR_BACKUP *cb) { WT_CONFIG_ITEM cval; WT_CURSOR *cursor; WT_DECL_RET; const char *key, *value; cursor = NULL; /* * Open a cursor on the metadata file and copy all of the entries to * the hot backup file. */ WT_ERR(__wt_metadata_cursor(session, NULL, &cursor)); while ((ret = cursor->next(cursor)) == 0) { WT_ERR(cursor->get_key(cursor, &key)); WT_ERR(cursor->get_value(cursor, &value)); WT_ERR_TEST((fprintf( cb->bfp, "%s\n%s\n", key, value) < 0), __wt_errno()); /* * While reading the metadata file, check there are no "sources" * or "types" which can't support hot backup. This checks for * a data source that's non-standard, which can't be backed up, * but is also sanity checking: if there's an entry backed by * anything other than a file or lsm entry, we're confused. */ if ((ret = __wt_config_getones( session, value, "type", &cval)) == 0 && !WT_PREFIX_MATCH_LEN(cval.str, cval.len, "file") && !WT_PREFIX_MATCH_LEN(cval.str, cval.len, "lsm")) WT_ERR_MSG(session, ENOTSUP, "hot backup is not supported for objects of " "type %.*s", (int)cval.len, cval.str); WT_ERR_NOTFOUND_OK(ret); if ((ret =__wt_config_getones( session, value, "source", &cval)) == 0 && !WT_PREFIX_MATCH_LEN(cval.str, cval.len, "file:") && !WT_PREFIX_MATCH_LEN(cval.str, cval.len, "lsm:")) WT_ERR_MSG(session, ENOTSUP, "hot backup is not supported for objects of " "source %.*s", (int)cval.len, cval.str); WT_ERR_NOTFOUND_OK(ret); } WT_ERR_NOTFOUND_OK(ret); /* Build a list of the file objects that need to be copied. */ WT_WITH_DHANDLE_LOCK(session, ret = __wt_meta_btree_apply( session, __backup_list_all_append, NULL)); err: if (cursor != NULL) WT_TRET(cursor->close(cursor)); return (ret); }
void WiredTigerSession::closeAllCursors() { invariant(_session); for (CursorCache::iterator i = _cursors.begin(); i != _cursors.end(); ++i) { WT_CURSOR* cursor = i->_cursor; if (cursor) { invariantWTOK(cursor->close(cursor)); } } _cursors.clear(); }
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); }
int main(int argc, char *argv[]) { WT_CONNECTION *conn; WT_CURSOR *cursor; WT_SESSION *session; 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)); error_check(session->create(session, "table:world", "key_format=r,value_format=5sii," "columns=(id,country,population,area)")); /*! [open cursor #1] */ error_check(session->open_cursor( session, "table:world", NULL, NULL, &cursor)); /*! [open cursor #1] */ /*! [open cursor #2] */ error_check(session->open_cursor(session, "table:world(country,population)", NULL, NULL, &cursor)); /*! [open cursor #2] */ /*! [open cursor #3] */ error_check(session->open_cursor( session, "statistics:", NULL, NULL, &cursor)); /*! [open cursor #3] */ /* Create a simple string table to illustrate basic operations. */ error_check(session->create( session, "table:map", "key_format=S,value_format=S")); error_check(session->open_cursor( session, "table:map", NULL, NULL, &cursor)); 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->close(cursor)); /* Note: closing the connection implicitly closes open session(s). */ error_check(conn->close(conn, NULL)); return (EXIT_SUCCESS); }
static void backup(WT_SESSION *session) { char buf[1024]; /*! [backup]*/ WT_CURSOR *cursor; const char *filename; int ret; /* Create the backup directory. */ error_check(mkdir("/path/database.backup", 077)); /* Open the backup data source. */ error_check(session->open_cursor( session, "backup:", NULL, NULL, &cursor)); /* Copy the list of files. */ while ((ret = cursor->next(cursor)) == 0) { error_check(cursor->get_key(cursor, &filename)); (void)snprintf(buf, sizeof(buf), "cp /path/database/%s /path/database.backup/%s", filename, filename); error_check(system(buf)); } scan_end_check(ret == WT_NOTFOUND); error_check(cursor->close(cursor)); /*! [backup]*/ /*! [incremental backup]*/ /* Open the backup data source for incremental backup. */ error_check(session->open_cursor( session, "backup:", NULL, "target=(\"log:\")", &cursor)); /*! [incremental backup]*/ error_check(cursor->close(cursor)); /*! [backup of a checkpoint]*/ error_check(session->checkpoint( session, "drop=(from=June01),name=June01")); /*! [backup of a checkpoint]*/ }
int main(void) { WT_CONNECTION *conn; WT_CURSOR *cursor; WT_SESSION *session; int ret; /* Open a connection to the database, creating it if necessary. */ if ((ret = wiredtiger_open(home, NULL, "create", &conn)) != 0) fprintf(stderr, "Error connecting to %s: %s\n", home, wiredtiger_strerror(ret)); /* Open a session for the current thread's work. */ if ((ret = conn->open_session(conn, NULL, NULL, &session)) != 0) fprintf(stderr, "Error opening a session on %s: %s\n", home, wiredtiger_strerror(ret)); ret = session->create(session, "table:world", "key_format=r,value_format=5sii," "columns=(id,country,population,area)"); /*! [open cursor #1] */ ret = session->open_cursor(session, "table:world", NULL, NULL, &cursor); /*! [open cursor #1] */ /*! [open cursor #2] */ ret = session->open_cursor(session, "table:world(country,population)", NULL, NULL, &cursor); /*! [open cursor #2] */ /*! [open cursor #3] */ ret = session->open_cursor(session, "statistics:", NULL, NULL, &cursor); /*! [open cursor #3] */ /* Create a simple string table to illustrate basic operations. */ ret = session->create(session, "table:map", "key_format=S,value_format=S"); ret = session->open_cursor(session, "table:map", NULL, NULL, &cursor); ret = cursor_insert(cursor); ret = cursor_reset(cursor); ret = cursor_forward_scan(cursor); ret = cursor_reset(cursor); ret = cursor_reverse_scan(cursor); ret = cursor_update(cursor); ret = cursor_remove(cursor); ret = cursor->close(cursor); /* Note: closing the connection implicitly closes open session(s). */ if ((ret = conn->close(conn, NULL)) != 0) fprintf(stderr, "Error connecting to %s: %s\n", home, wiredtiger_strerror(ret)); return (ret); }
int wt_id2entry( BackendDB *be, WT_SESSION *session, ID id, Entry **ep ){ int rc; WT_CURSOR *cursor = NULL; WT_ITEM item; EntryHeader eh; int eoff; Entry *e = NULL; rc = session->open_cursor(session, WT_TABLE_ID2ENTRY"(entry)", NULL, NULL, &cursor); if ( rc ) { Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(wt_id2entry) ": open_cursor failed: %s (%d)\n", wiredtiger_strerror(rc), rc, 0 ); goto done; } cursor->set_key(cursor, id); rc = cursor->search(cursor); if ( rc ) { goto done; } cursor->get_value(cursor, &item); rc = wt_entry_header( &item, &eh ); eoff = eh.data - (char *)item.data; eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + item.size; eh.bv.bv_val = ch_malloc( eh.bv.bv_len ); memset(eh.bv.bv_val, 0xff, eh.bv.bv_len); eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval ); memcpy(eh.data, item.data, item.size); eh.data += eoff; rc = entry_decode( &eh, &e ); if ( rc ) { Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(wt_id2entry) ": entry decode error: %s (%d)\n", rc, 0, 0 ); goto done; } e->e_id = id; *ep = e; done: if(cursor){ cursor->close(cursor); } return rc; }
/* * Create a guaranteed unique table and open and close a bulk cursor on it. */ void op_bulk_unique(void *arg) { TEST_OPTS *opts; TEST_PER_THREAD_OPTS *args; WT_CURSOR *c; WT_RAND_STATE rnd; WT_SESSION *session; int ret; char new_uri[64]; args = (TEST_PER_THREAD_OPTS *)arg; opts = args->testopts; __wt_random_init_seed(NULL, &rnd); testutil_check( opts->conn->open_session(opts->conn, NULL, NULL, &session)); /* Generate a unique object name. */ testutil_check(__wt_snprintf( new_uri, sizeof(new_uri), "%s.%" PRIu64, opts->uri, __wt_atomic_add64(&opts->unique_id, 1))); testutil_check(session->create(session, new_uri, DEFAULT_TABLE_SCHEMA)); __wt_yield(); /* * Opening a bulk cursor may have raced with a forced checkpoint * which created a checkpoint of the empty file, and triggers an EINVAL. */ if ((ret = session->open_cursor( session, new_uri, NULL, "bulk,checkpoint_wait=false", &c)) == 0) { testutil_check(c->close(c)); } else if (ret != EINVAL && ret != EBUSY) testutil_die(ret, "session.open_cursor bulk unique: %s", new_uri); while ((ret = session->drop(session, new_uri, __wt_random(&rnd) & 1 ? "force,checkpoint_wait=false" : "checkpoint_wait=false")) != 0) if (ret != EBUSY) testutil_die(ret, "session.drop: %s", new_uri); else /* * The EBUSY is expected when we run with * checkpoint_wait set to false, so we increment the * counter while in this loop to avoid false positives. */ args->thread_counter++; testutil_check(session->close(session, NULL)); args->thread_counter++; }