diff options
author | Keith Bostic <keith@wiredtiger.com> | 2014-08-23 15:14:16 -0400 |
---|---|---|
committer | Keith Bostic <keith@wiredtiger.com> | 2014-08-23 15:14:16 -0400 |
commit | 4f60793732fd57d7d4d9d2e2d1bf974dfd249a86 (patch) | |
tree | 5703efdf24b7eec03f41a67b9194a4b03f0f014c /examples | |
parent | d5796de3bb8bdcd20b45edcd4dd962245493f73f (diff) | |
parent | 4a309f6d5c15546d023e25caf3533aaa5dc26066 (diff) | |
download | mongo-4f60793732fd57d7d4d9d2e2d1bf974dfd249a86.tar.gz |
Merge branch 'log-printf' into log-printf-docs
Diffstat (limited to 'examples')
-rw-r--r-- | examples/c/Makefile.am | 2 | ||||
-rw-r--r-- | examples/c/ex_access.c | 14 | ||||
-rw-r--r-- | examples/c/ex_all.c | 36 | ||||
-rw-r--r-- | examples/c/ex_async.c | 4 | ||||
-rw-r--r-- | examples/c/ex_call_center.c | 16 | ||||
-rw-r--r-- | examples/c/ex_config.c | 17 | ||||
-rw-r--r-- | examples/c/ex_config_parse.c | 10 | ||||
-rw-r--r-- | examples/c/ex_cursor.c | 13 | ||||
-rw-r--r-- | examples/c/ex_extending.c | 13 | ||||
-rw-r--r-- | examples/c/ex_file.c | 13 | ||||
-rw-r--r-- | examples/c/ex_hello.c | 15 | ||||
-rw-r--r-- | examples/c/ex_pack.c | 13 | ||||
-rw-r--r-- | examples/c/ex_process.c | 13 | ||||
-rw-r--r-- | examples/c/ex_schema.c | 20 | ||||
-rw-r--r-- | examples/c/ex_scope.c | 16 | ||||
-rw-r--r-- | examples/c/ex_stat.c | 15 | ||||
-rw-r--r-- | examples/c/ex_thread.c | 34 |
17 files changed, 209 insertions, 55 deletions
diff --git a/examples/c/Makefile.am b/examples/c/Makefile.am index cde2e9e1460..fa576c6c517 100644 --- a/examples/c/Makefile.am +++ b/examples/c/Makefile.am @@ -29,4 +29,4 @@ AM_TESTS_ENVIRONMENT = WIREDTIGER_HOME=`mktemp -d WT_TEST.XXXX` ; export WIREDTI TESTS_ENVIRONMENT = $(AM_TESTS_ENVIRONMENT) clean-local: - rm -rf WT_TEST* *.core WiredTiger* *.wt + rm -rf WIREDTIGER_HOME *.core WiredTiger* *.wt diff --git a/examples/c/ex_access.c b/examples/c/ex_access.c index f7da1848811..26c42a449ed 100644 --- a/examples/c/ex_access.c +++ b/examples/c/ex_access.c @@ -28,11 +28,12 @@ * demonstrates how to create and access a simple table. */ #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <wiredtiger.h> -const char *home = NULL; +const char *home; int main(void) { @@ -43,6 +44,17 @@ int main(void) const char *key, *value; 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 = "WIREDTIGER_HOME"; + (void)system("rm -rf WIREDTIGER_HOME && mkdir WIREDTIGER_HOME"); + } else + home = NULL; + + /* Open a connection to the database, creating it if necessary. */ 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", diff --git a/examples/c/ex_all.c b/examples/c/ex_all.c index bf6f6dda9e6..72c678abd9b 100644 --- a/examples/c/ex_all.c +++ b/examples/c/ex_all.c @@ -55,8 +55,8 @@ int pack_ops(WT_SESSION *session); int session_ops(WT_SESSION *session); int transaction_ops(WT_CONNECTION *conn, WT_SESSION *session); -const char *progname; -const char *home = NULL; +const char * const progname = "ex_all"; +const char *home; int cursor_ops(WT_SESSION *session) @@ -166,7 +166,7 @@ cursor_ops(WT_SESSION *session) const char *first; int32_t second; uint16_t third; - cursor->get_key(cursor, &first, &second, &third); + ret = cursor->get_key(cursor, &first, &second, &third); /*! [Get the cursor's composite key] */ } @@ -243,7 +243,7 @@ cursor_ops(WT_SESSION *session) /*! [Search for an exact match] */ } - cursor_search_near(cursor); + ret = cursor_search_near(cursor); { /*! [Insert a new record or overwrite an existing record] */ @@ -629,7 +629,7 @@ session_ops(WT_SESSION *session) cursor->set_key(cursor, "June30"); cursor->set_value(cursor, "value"); ret = cursor->update(cursor); - cursor->close(cursor); + ret = cursor->close(cursor); { /*! [Truncate a range] */ @@ -814,8 +814,8 @@ connection_ops(WT_CONNECTION *conn) /*! [Load an extension] */ #endif - add_collator(conn); - add_extractor(conn); + ret = add_collator(conn); + ret = add_extractor(conn); /*! [Reconfigure a connection] */ ret = conn->reconfigure(conn, "eviction_target=75"); @@ -837,7 +837,7 @@ connection_ops(WT_CONNECTION *conn) ret = conn->open_session(conn, NULL, NULL, &session); /*! [Open a session] */ - session_ops(session); + ret = session_ops(session); } /*! [Configure method configuration] */ @@ -938,7 +938,7 @@ hot_backup(WT_SESSION *session) ret = session->checkpoint(session, "drop=(from=June01),name=June01"); /*! [Hot backup of a checkpoint]*/ - return (0); + return (ret); } int @@ -947,12 +947,22 @@ main(void) WT_CONNECTION *conn; 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 = "WIREDTIGER_HOME"; + (void)system("rm -rf WIREDTIGER_HOME && mkdir WIREDTIGER_HOME"); + } else + home = NULL; + /*! [Open a connection] */ ret = wiredtiger_open(home, NULL, "create,cache_size=500M", &conn); /*! [Open a connection] */ if (ret == 0) - connection_ops(conn); + ret = connection_ops(conn); /* * The connection has been closed. */ @@ -1086,10 +1096,10 @@ main(void) { /*! [Get the WiredTiger library version #2] */ - int major, minor, patch; - (void)wiredtiger_version(&major, &minor, &patch); + int major_v, minor_v, patch; + (void)wiredtiger_version(&major_v, &minor_v, &patch); printf("WiredTiger version is %d, %d (patch %d)\n", - major, minor, patch); + major_v, minor_v, patch); /*! [Get the WiredTiger library version #2] */ } diff --git a/examples/c/ex_async.c b/examples/c/ex_async.c index 1940ebaae3b..0498ad3b8d3 100644 --- a/examples/c/ex_async.c +++ b/examples/c/ex_async.c @@ -41,7 +41,7 @@ #define ATOMIC_ADD(v, val) __sync_add_and_fetch(&(v), val) #endif -const char *home = NULL; +const char * const home = NULL; int global_error = 0; /*! [async example callback implementation] */ @@ -201,7 +201,7 @@ main(void) */ snprintf(k[i], sizeof(k), "key%d", i); op->set_key(op, k[i]); - op->search(op); + ret = op->search(op); /*! [async search] */ } diff --git a/examples/c/ex_call_center.c b/examples/c/ex_call_center.c index 7ae1ca91acf..3ba42c7e2a6 100644 --- a/examples/c/ex_call_center.c +++ b/examples/c/ex_call_center.c @@ -31,11 +31,12 @@ #include <inttypes.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <wiredtiger.h> -const char *home = NULL; +const char *home; /*! [call-center decl] */ /* @@ -92,8 +93,17 @@ int main(void) { 0, 0, 0, 0, NULL, NULL } }; - ret = wiredtiger_open(home, NULL, "create", &conn); - if (ret != 0) { + /* + * 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 = "WIREDTIGER_HOME"; + (void)system("rm -rf WIREDTIGER_HOME && mkdir WIREDTIGER_HOME"); + } else + home = NULL; + + if ((ret = wiredtiger_open(home, NULL, "create", &conn)) != 0) { fprintf(stderr, "Error connecting to %s: %s\n", home, wiredtiger_strerror(ret)); return (1); diff --git a/examples/c/ex_config.c b/examples/c/ex_config.c index 33eca0bc26d..3d5c40367ea 100644 --- a/examples/c/ex_config.c +++ b/examples/c/ex_config.c @@ -30,11 +30,12 @@ */ #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <wiredtiger.h> -const char *home = NULL; +const char *home; int main(void) { @@ -44,6 +45,16 @@ int main(void) WT_CURSOR *cursor; const char *key, *value; + /* + * 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 = "WIREDTIGER_HOME"; + (void)system("rm -rf WIREDTIGER_HOME && mkdir WIREDTIGER_HOME"); + } else + home = NULL; + /*! [configure cache size] */ if ((ret = wiredtiger_open(home, NULL, "create,cache_size=500M", &conn)) != 0) @@ -64,8 +75,8 @@ int main(void) ret = session->open_cursor(session, "config:", NULL, NULL, &cursor); while ((ret = cursor->next(cursor)) == 0) { - cursor->get_key(cursor, &key); - cursor->get_value(cursor, &value); + ret = cursor->get_key(cursor, &key); + ret = cursor->get_value(cursor, &value); printf("configuration value: %s = %s\n", key, value); } diff --git a/examples/c/ex_config_parse.c b/examples/c/ex_config_parse.c index c6adc327c78..df16891e7af 100644 --- a/examples/c/ex_config_parse.c +++ b/examples/c/ex_config_parse.c @@ -34,8 +34,6 @@ #include <wiredtiger.h> -const char *home = NULL; - int main(void) { int ret; @@ -149,18 +147,18 @@ int main(void) "Error creating nested configuration " "parser: %s\n", wiredtiger_strerror(ret)); - parser->close(parser); + ret = parser->close(parser); return (ret); } while ((ret = sub_parser->next( sub_parser, &k, &v)) == 0) printf("\t%.*s\n", (int)k.len, k.str); - sub_parser->close(sub_parser); + ret = sub_parser->close(sub_parser); } } /*! [nested traverse] */ - parser->close(parser); + ret = parser->close(parser); } - return (0); + return (ret); } diff --git a/examples/c/ex_cursor.c b/examples/c/ex_cursor.c index ab77d55154b..1d0e86371eb 100644 --- a/examples/c/ex_cursor.c +++ b/examples/c/ex_cursor.c @@ -29,6 +29,7 @@ */ #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <wiredtiger.h> @@ -42,7 +43,7 @@ int cursor_insert(WT_CURSOR *cursor); int cursor_update(WT_CURSOR *cursor); int cursor_remove(WT_CURSOR *cursor); -const char *home = NULL; +const char *home; /*! [cursor next] */ int @@ -164,6 +165,16 @@ int main(void) 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 = "WIREDTIGER_HOME"; + (void)system("rm -rf WIREDTIGER_HOME && mkdir WIREDTIGER_HOME"); + } else + home = NULL; + /* Open a connection to the database, creating it if necessary. */ if ((ret = wiredtiger_open( home, NULL, "create,statistics=(fast)", &conn)) != 0) diff --git a/examples/c/ex_extending.c b/examples/c/ex_extending.c index d2a538fac2e..3cfa4ad8109 100644 --- a/examples/c/ex_extending.c +++ b/examples/c/ex_extending.c @@ -30,11 +30,12 @@ */ #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <wiredtiger.h> -const char *home = NULL; +const char *home; /*! [case insensitive comparator] */ /* A simple case insensitive comparator. */ @@ -88,6 +89,16 @@ int main(void) WT_CONNECTION *conn; WT_SESSION *session; + /* + * 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 = "WIREDTIGER_HOME"; + (void)system("rm -rf WIREDTIGER_HOME && mkdir WIREDTIGER_HOME"); + } else + home = NULL; + /* 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", diff --git a/examples/c/ex_file.c b/examples/c/ex_file.c index 665eb3687fd..2893dadf7ee 100644 --- a/examples/c/ex_file.c +++ b/examples/c/ex_file.c @@ -28,14 +28,13 @@ * This is an example demonstrating how to configure an individual file. */ -#include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <wiredtiger.h> -const char *home = NULL; +const char *home; int main(void) @@ -44,6 +43,16 @@ main(void) 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 = "WIREDTIGER_HOME"; + (void)system("rm -rf WIREDTIGER_HOME && mkdir WIREDTIGER_HOME"); + } else + home = NULL; + 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", diff --git a/examples/c/ex_hello.c b/examples/c/ex_hello.c index 33b088ba4e3..5e1c159a60e 100644 --- a/examples/c/ex_hello.c +++ b/examples/c/ex_hello.c @@ -30,17 +30,28 @@ */ #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <wiredtiger.h> -const char *home = NULL; +const char *home; int main(void) { - int ret; WT_CONNECTION *conn; 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 = "WIREDTIGER_HOME"; + (void)system("rm -rf WIREDTIGER_HOME && mkdir WIREDTIGER_HOME"); + } else + home = NULL; /* Open a connection to the database, creating it if necessary. */ if ((ret = wiredtiger_open(home, NULL, "create", &conn)) != 0) diff --git a/examples/c/ex_pack.c b/examples/c/ex_pack.c index c2f58e1be6c..9f4b4630e99 100644 --- a/examples/c/ex_pack.c +++ b/examples/c/ex_pack.c @@ -29,11 +29,12 @@ */ #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <wiredtiger.h> -const char *home = NULL; +const char *home; int main(void) { @@ -43,6 +44,16 @@ int main(void) size_t size; int i, j, k, 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 = "WIREDTIGER_HOME"; + (void)system("rm -rf WIREDTIGER_HOME && mkdir WIREDTIGER_HOME"); + } else + home = NULL; + /* 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", diff --git a/examples/c/ex_process.c b/examples/c/ex_process.c index 2677a44bb1b..a603ec204ff 100644 --- a/examples/c/ex_process.c +++ b/examples/c/ex_process.c @@ -30,11 +30,12 @@ */ #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <wiredtiger.h> -const char *home = NULL; +const char *home; int main(void) { @@ -42,6 +43,16 @@ int main(void) WT_CONNECTION *conn; WT_SESSION *session; + /* + * 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 = "WIREDTIGER_HOME"; + (void)system("rm -rf WIREDTIGER_HOME && mkdir WIREDTIGER_HOME"); + } else + home = NULL; + /*! [processes] */ /* Open a connection to the database, creating it if necessary. */ if ((ret = diff --git a/examples/c/ex_schema.c b/examples/c/ex_schema.c index 5c718b95a87..fca4cef03ee 100644 --- a/examples/c/ex_schema.c +++ b/examples/c/ex_schema.c @@ -29,13 +29,14 @@ * tables using a schema. */ +#include <inttypes.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> -#include <inttypes.h> #include <wiredtiger.h> -const char *home = NULL; +const char *home; /*! [schema declaration] */ /* The C struct for the data we are storing in a WiredTiger table. */ @@ -70,10 +71,21 @@ main(void) uint16_t year; int ret; - ret = wiredtiger_open(home, NULL, "create", &conn); - if (ret != 0) + /* + * 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 = "WIREDTIGER_HOME"; + (void)system("rm -rf WIREDTIGER_HOME && mkdir WIREDTIGER_HOME"); + } else + home = NULL; + + if ((ret = wiredtiger_open(home, NULL, "create", &conn)) != 0) { fprintf(stderr, "Error connecting to %s: %s\n", home, wiredtiger_strerror(ret)); + return (ret); + } /* Note: error checking omitted for clarity. */ ret = conn->open_session(conn, NULL, NULL, &session); diff --git a/examples/c/ex_scope.c b/examples/c/ex_scope.c index f2eca0bdb3f..9ad5bb47be2 100644 --- a/examples/c/ex_scope.c +++ b/examples/c/ex_scope.c @@ -29,11 +29,12 @@ */ #include <errno.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <wiredtiger.h> -const char *home = NULL; +const char *home; static int cursor_scope_ops(WT_CURSOR *cursor) @@ -126,13 +127,24 @@ cursor_scope_ops(WT_CURSOR *cursor) return (0); } -int main(void) +int +main(void) { WT_CONNECTION *conn; WT_CURSOR *cursor; WT_SESSION *session; int ret, tret; + /* + * 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 = "WIREDTIGER_HOME"; + (void)system("rm -rf WIREDTIGER_HOME && mkdir WIREDTIGER_HOME"); + } else + home = NULL; + /* Open a connection, create a simple table, open a cursor. */ if ((ret = wiredtiger_open(home, NULL, "create", &conn)) != 0 || (ret = conn->open_session(conn, NULL, NULL, &session)) != 0) { diff --git a/examples/c/ex_stat.c b/examples/c/ex_stat.c index 04efcc3cfc9..dfa419e2998 100644 --- a/examples/c/ex_stat.c +++ b/examples/c/ex_stat.c @@ -28,7 +28,6 @@ * This is an example demonstrating how to query database statistics. */ -#include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -42,7 +41,7 @@ int print_overflow_pages(WT_SESSION *); int get_stat(WT_CURSOR *cursor, int stat_field, uint64_t *valuep); int print_derived_stats(WT_SESSION *); -const char *home = NULL; +const char *home; /*! [statistics display function] */ int @@ -188,6 +187,16 @@ main(void) 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 = "WIREDTIGER_HOME"; + (void)system("rm -rf WIREDTIGER_HOME && mkdir WIREDTIGER_HOME"); + } else + home = NULL; + ret = wiredtiger_open(home, NULL, "create,statistics=(all)", &conn); ret = conn->open_session(conn, NULL, NULL, &session); ret = session->create( @@ -198,7 +207,7 @@ main(void) cursor->set_key(cursor, "key"); cursor->set_value(cursor, "value"); ret = cursor->insert(cursor); - cursor->close(cursor); + ret = cursor->close(cursor); ret = session->checkpoint(session, NULL); diff --git a/examples/c/ex_thread.c b/examples/c/ex_thread.c index 0637c0b4e49..5835067612d 100644 --- a/examples/c/ex_thread.c +++ b/examples/c/ex_thread.c @@ -29,31 +29,33 @@ * table from multiple threads. */ +#include <pthread.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> -#include <pthread.h> #include <wiredtiger.h> +const char *home; + void *scan_thread(void *arg); -const char *home = NULL; #define NUM_THREADS 10 -WT_CONNECTION *conn; - /*! [thread scan] */ void * -scan_thread(void *arg) +scan_thread(void *conn_arg) { + WT_CONNECTION *conn; WT_SESSION *session; WT_CURSOR *cursor; const char *key, *value; int ret; + conn = conn_arg; ret = conn->open_session(conn, NULL, NULL, &session); - ret = session->open_cursor(session, "table:access", - NULL, NULL, &cursor); + ret = session->open_cursor( + session, "table:access", NULL, NULL, &cursor); /* Show all records. */ while ((ret = cursor->next(cursor)) == 0) { @@ -62,8 +64,11 @@ scan_thread(void *arg) printf("Got record: %s : %s\n", key, value); } + if (ret != WT_NOTFOUND) + fprintf(stderr, + "WT_CURSOR.next: %s\n", wiredtiger_strerror(ret)); - return (arg); + return (NULL); } /*! [thread scan] */ @@ -71,11 +76,22 @@ scan_thread(void *arg) 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 = "WIREDTIGER_HOME"; + (void)system("rm -rf WIREDTIGER_HOME && mkdir WIREDTIGER_HOME"); + } else + home = NULL; + if ((ret = wiredtiger_open(home, NULL, "create", &conn)) != 0) fprintf(stderr, "Error connecting to %s: %s\n", @@ -93,7 +109,7 @@ main(void) ret = session->close(session, NULL); for (i = 0; i < NUM_THREADS; i++) - ret = pthread_create(&threads[i], NULL, scan_thread, NULL); + ret = pthread_create(&threads[i], NULL, scan_thread, conn); for (i = 0; i < NUM_THREADS; i++) ret = pthread_join(threads[i], NULL); |