summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMichael Cahill <michael.cahill@wiredtiger.com>2012-08-13 12:37:08 +1000
committerMichael Cahill <michael.cahill@wiredtiger.com>2012-08-13 12:37:08 +1000
commit6bae50301c05708b37ce1b90a7eee65b0af11696 (patch)
treebd396dbe25667c5d9adf8e050a8d23358b9f7f73 /examples
parentddf4996a071e719a3f8ddccafbf762bdf5bfb320 (diff)
parentb3236229a726ecd7bdf232b60466ff28d0aabf93 (diff)
downloadmongo-6bae50301c05708b37ce1b90a7eee65b0af11696.tar.gz
Merge branch 'develop' into lsm.
Diffstat (limited to 'examples')
-rw-r--r--examples/c/Makefile.am3
-rw-r--r--examples/c/ex_all.c75
-rw-r--r--examples/c/ex_transaction.c67
3 files changed, 73 insertions, 72 deletions
diff --git a/examples/c/Makefile.am b/examples/c/Makefile.am
index b4eae787749..2bca8422a91 100644
--- a/examples/c/Makefile.am
+++ b/examples/c/Makefile.am
@@ -13,8 +13,7 @@ noinst_PROGRAMS = \
ex_process \
ex_schema \
ex_stat \
- ex_thread \
- ex_transaction
+ ex_thread
# The examples can be run with no arguments as simple smoke tests
TESTS = $(noinst_PROGRAMS)
diff --git a/examples/c/ex_all.c b/examples/c/ex_all.c
index 41bf0a19f3f..ce19525775e 100644
--- a/examples/c/ex_all.c
+++ b/examples/c/ex_all.c
@@ -54,6 +54,7 @@ int cursor_search_near(WT_CURSOR *cursor);
int hot_backup(WT_SESSION *session);
int pack_ops(WT_SESSION *session);
int session_ops(WT_SESSION *session);
+int transaction_ops(WT_CONNECTION *conn, WT_SESSION *session);
const char *progname;
@@ -381,7 +382,9 @@ session_ops(WT_SESSION *session)
{
int ret;
- cursor_ops(session);
+ /*! [Reconfigure a session] */
+ ret = session->reconfigure(session, "isolation=snapshot");
+ /*! [Reconfigure a session] */
/*! [Create a table] */
ret = session->create(session,
@@ -393,8 +396,6 @@ session_ops(WT_SESSION *session)
"table:mytable", "key_format=r,value_format=S,cache_resident=true");
/*! [Create a cache-resident object] */
- checkpoint_ops(session);
-
/*! [Drop a table] */
ret = session->drop(session, "table:mytable", NULL);
/*! [Drop a table] */
@@ -465,6 +466,74 @@ session_ops(WT_SESSION *session)
return (ret);
}
+int
+transaction_ops(WT_CONNECTION *conn, WT_SESSION *session)
+{
+ WT_CURSOR *cursor;
+ int ret;
+
+ /*! [simple transaction] */
+ ret = session->begin_transaction(session, NULL);
+
+ ret =
+ session->open_cursor(session, "table:mytable", NULL, NULL, &cursor);
+ cursor->set_key(cursor, "some-key");
+ cursor->set_value(cursor, "some-value");
+ ret = cursor->update(cursor);
+
+ /* Resolving the transaction closes the open cursors. */
+ ret = session->commit_transaction(session, NULL);
+ /*! [simple transaction] */
+
+ /*! [simple rollback transaction] */
+ ret = session->begin_transaction(session, NULL);
+
+ ret =
+ session->open_cursor(session, "table:mytable", NULL, NULL, &cursor);
+ cursor->set_key(cursor, "some-key");
+ cursor->set_value(cursor, "some-value");
+ ret = cursor->update(cursor);
+
+ /* Resolving the transaction closes the open cursors. */
+ switch (ret) {
+ case 0:
+ ret = session->commit_transaction(session, NULL);
+ break;
+ case WT_DEADLOCK: /* Conflict */
+ default: /* Other error */
+ ret = session->rollback_transaction(session, NULL);
+ break;
+ }
+ /*! [simple rollback transaction] */
+
+ /*! [simple transaction isolation] */
+ /* A single transaction configured for snapshot isolation. */
+ ret = session->begin_transaction(session, "isolation=snapshot");
+
+ ret =
+ session->open_cursor(session, "table:mytable", NULL, NULL, &cursor);
+ cursor->set_key(cursor, "some-key");
+ cursor->set_value(cursor, "some-value");
+ ret = cursor->update(cursor);
+
+ /* Resolving the transaction closes the open cursors. */
+ ret = session->commit_transaction(session, NULL);
+ /*! [simple transaction isolation] */
+
+ /*! [session isolation configuration] */
+ /* Open a session configured for read-uncommitted isolation. */
+ ret = conn->open_session(
+ conn, NULL, "isolation=read_uncommitted", &session);
+ /*! [session isolation configuration] */
+
+ /*! [session isolation re-configuration] */
+ /* Re-configure a session for snapshot isolation. */
+ ret = session->reconfigure(session, "isolation=snapshot");
+ /*! [session isolation re-configuration] */
+
+ return (ret);
+}
+
/*! [WT_DATA_SOURCE create] */
static int
my_create(WT_DATA_SOURCE *dsrc, WT_SESSION *session,
diff --git a/examples/c/ex_transaction.c b/examples/c/ex_transaction.c
deleted file mode 100644
index 86254bb4a26..00000000000
--- a/examples/c/ex_transaction.c
+++ /dev/null
@@ -1,67 +0,0 @@
-/*-
- * Copyright (c) 2008-2012 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_transaction.c
- * This is an example demonstrating how to use transactions.
- */
-
-#include <stdio.h>
-#include <string.h>
-
-#include <wiredtiger.h>
-
-const char *home = "WT_TEST";
-
-int main(void)
-{
- int ret;
- WT_CONNECTION *conn;
- WT_SESSION *session;
-
- /* Open a connection to the database, creating it if necessary. */
- if ((ret =
- wiredtiger_open(home, NULL, "create,transactional", &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));
-
- /*! [transaction] */
- session->begin_transaction(session, NULL);
- /* ... */
- session->commit_transaction(session, NULL);
- /*! [transaction] */
-
- /* 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);
-}