summaryrefslogtreecommitdiff
path: root/examples/c
diff options
context:
space:
mode:
authorAlex Gorrod <alexander.gorrod@mongodb.com>2017-04-14 03:25:28 +1000
committerAlex Gorrod <alexander.gorrod@mongodb.com>2017-04-14 03:25:28 +1000
commitf5c08e2b5f02805b062888d45c9eca19af175f7e (patch)
tree0b43098fab6f6059c04c89e9b85337d5f625c5f2 /examples/c
parentd48181f6f4db08761ed7b80b0332908b272ad0d0 (diff)
parentcb16839cfbdf338af95bed43ca40979ae6e32f54 (diff)
downloadmongodb-3.2.13.tar.gz
Merge branch 'mongodb-3.4' into mongodb-3.2mongodb-3.2.13
Diffstat (limited to 'examples/c')
-rw-r--r--examples/c/Makefile.am1
-rw-r--r--examples/c/ex_all.c4
-rw-r--r--examples/c/ex_async.c6
-rw-r--r--examples/c/ex_backup.c28
-rw-r--r--examples/c/ex_encrypt.c4
-rw-r--r--examples/c/ex_file_system.c13
-rw-r--r--examples/c/ex_log.c12
-rw-r--r--examples/c/ex_scope.c217
-rw-r--r--examples/c/ex_sync.c20
9 files changed, 46 insertions, 259 deletions
diff --git a/examples/c/Makefile.am b/examples/c/Makefile.am
index d5305eec5c8..20936661e06 100644
--- a/examples/c/Makefile.am
+++ b/examples/c/Makefile.am
@@ -20,7 +20,6 @@ noinst_PROGRAMS = \
ex_pack \
ex_process \
ex_schema \
- ex_scope \
ex_stat \
ex_sync \
ex_thread
diff --git a/examples/c/ex_all.c b/examples/c/ex_all.c
index 8a1533011b2..82620673fe1 100644
--- a/examples/c/ex_all.c
+++ b/examples/c/ex_all.c
@@ -848,8 +848,8 @@ my_compare(WT_COLLATOR *collator, WT_SESSION *session,
p1 = (const char *)value1->data;
p2 = (const char *)value2->data;
- while (*p1 != '\0' && *p1 == *p2)
- p1++, p2++;
+ for (; *p1 != '\0' && *p1 == *p2; ++p1, ++p2)
+ ;
*cmp = (int)*p2 - (int)*p1;
return (0);
diff --git a/examples/c/ex_async.c b/examples/c/ex_async.c
index f7531a5c3d8..5cfafca0418 100644
--- a/examples/c/ex_async.c
+++ b/examples/c/ex_async.c
@@ -170,12 +170,12 @@ main(void)
* an asynchronous insert.
*/
/*! [async set the operation's string key] */
- snprintf(k[i], sizeof(k), "key%d", i);
+ (void)snprintf(k[i], sizeof(k), "key%d", i);
op->set_key(op, k[i]);
/*! [async set the operation's string key] */
/*! [async set the operation's string value] */
- snprintf(v[i], sizeof(v), "value%d", i);
+ (void)snprintf(v[i], sizeof(v), "value%d", i);
op->set_value(op, v[i]);
/*! [async set the operation's string value] */
@@ -218,7 +218,7 @@ main(void)
* Set the operation's string key and value, and then do
* an asynchronous search.
*/
- snprintf(k[i], sizeof(k), "key%d", i);
+ (void)snprintf(k[i], sizeof(k), "key%d", i);
op->set_key(op, k[i]);
ret = op->search(op);
/*! [async search] */
diff --git a/examples/c/ex_backup.c b/examples/c/ex_backup.c
index 0697cbb3458..83cc9b22ecc 100644
--- a/examples/c/ex_backup.c
+++ b/examples/c/ex_backup.c
@@ -96,7 +96,7 @@ compare_backups(int i)
if (i == 0)
(void)strncpy(msg, "MAIN", sizeof(msg));
else
- snprintf(msg, sizeof(msg), "%d", i);
+ (void)snprintf(msg, sizeof(msg), "%d", i);
printf(
"Iteration %s: Tables %s.%d and %s.%d %s\n",
msg, full_out, i, incr_out, i, ret == 0 ? "identical" : "differ");
@@ -131,8 +131,8 @@ setup_directories(void)
* For incremental backups we need 0-N. The 0 incremental
* directory will compare with the original at the end.
*/
- snprintf(buf, sizeof(buf), "rm -rf %s.%d && mkdir %s.%d",
- home_incr, i, home_incr, i);
+ (void)snprintf(buf, sizeof(buf),
+ "rm -rf %s.%d && mkdir %s.%d", home_incr, i, home_incr, i);
if ((ret = system(buf)) != 0) {
fprintf(stderr, "%s: failed ret %d\n", buf, ret);
return (ret);
@@ -142,8 +142,8 @@ setup_directories(void)
/*
* For full backups we need 1-N.
*/
- snprintf(buf, sizeof(buf), "rm -rf %s.%d && mkdir %s.%d",
- home_full, i, home_full, i);
+ (void)snprintf(buf, sizeof(buf),
+ "rm -rf %s.%d && mkdir %s.%d", home_full, i, home_full, i);
if ((ret = system(buf)) != 0) {
fprintf(stderr, "%s: failed ret %d\n", buf, ret);
return (ret);
@@ -164,8 +164,8 @@ add_work(WT_SESSION *session, int iter)
* Perform some operations with individual auto-commit transactions.
*/
for (i = 0; i < MAX_KEYS; i++) {
- snprintf(k, sizeof(k), "key.%d.%d", iter, i);
- snprintf(v, sizeof(v), "value.%d.%d", iter, 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);
ret = cursor->insert(cursor);
@@ -187,7 +187,7 @@ take_full_backup(WT_SESSION *session, int i)
* directories. Otherwise only into the appropriate full directory.
*/
if (i != 0) {
- snprintf(h, sizeof(h), "%s.%d", home_full, i);
+ (void)snprintf(h, sizeof(h), "%s.%d", home_full, i);
hdir = h;
} else
hdir = home_incr;
@@ -200,14 +200,15 @@ take_full_backup(WT_SESSION *session, int i)
* Take a full backup into each incremental directory.
*/
for (j = 0; j < MAX_ITERATIONS; j++) {
- snprintf(h, sizeof(h), "%s.%d", home_incr, j);
+ (void)snprintf(h, sizeof(h),
+ "%s.%d", home_incr, j);
(void)snprintf(buf, sizeof(buf),
"cp %s/%s %s/%s",
home, filename, h, filename);
ret = system(buf);
}
else {
- snprintf(h, sizeof(h), "%s.%d", home_full, i);
+ (void)snprintf(h, sizeof(h), "%s.%d", home_full, i);
(void)snprintf(buf, sizeof(buf), "cp %s/%s %s/%s",
home, filename, hdir, filename);
ret = system(buf);
@@ -237,12 +238,12 @@ take_incr_backup(WT_SESSION *session, int i)
* Copy into the 0 incremental directory and then each of the
* incremental directories for this iteration and later.
*/
- snprintf(h, sizeof(h), "%s.0", home_incr);
+ (void)snprintf(h, sizeof(h), "%s.0", home_incr);
(void)snprintf(buf, sizeof(buf), "cp %s/%s %s/%s",
home, filename, h, filename);
ret = system(buf);
for (j = i; j < MAX_ITERATIONS; j++) {
- snprintf(h, sizeof(h), "%s.%d", home_incr, j);
+ (void)snprintf(h, sizeof(h), "%s.%d", home_incr, j);
(void)snprintf(buf, sizeof(buf), "cp %s/%s %s/%s",
home, filename, h, filename);
ret = system(buf);
@@ -270,7 +271,8 @@ main(void)
int i, ret;
char cmd_buf[256];
- snprintf(cmd_buf, sizeof(cmd_buf), "rm -rf %s && mkdir %s", home, home);
+ (void)snprintf(cmd_buf, sizeof(cmd_buf),
+ "rm -rf %s && mkdir %s", home, home);
if ((ret = system(cmd_buf)) != 0) {
fprintf(stderr, "%s: failed ret %d\n", cmd_buf, ret);
return (EXIT_FAILURE);
diff --git a/examples/c/ex_encrypt.c b/examples/c/ex_encrypt.c
index 00dc66fc24d..1520bd286cd 100644
--- a/examples/c/ex_encrypt.c
+++ b/examples/c/ex_encrypt.c
@@ -507,12 +507,12 @@ main(void)
* we decrypt on read.
*/
for (i = 0; i < MAX_KEYS; i++) {
- snprintf(keybuf, sizeof(keybuf), "key%d", i);
+ (void)snprintf(keybuf, sizeof(keybuf), "key%d", i);
c1->set_key(c1, keybuf);
c2->set_key(c2, keybuf);
nc->set_key(nc, keybuf);
- snprintf(valbuf, sizeof(valbuf), "value%d", i);
+ (void)snprintf(valbuf, sizeof(valbuf), "value%d", i);
c1->set_value(c1, valbuf);
c2->set_value(c2, valbuf);
nc->set_value(nc, valbuf);
diff --git a/examples/c/ex_file_system.c b/examples/c/ex_file_system.c
index 56869171558..e807ac54d3b 100644
--- a/examples/c/ex_file_system.c
+++ b/examples/c/ex_file_system.c
@@ -399,6 +399,7 @@ demo_fs_directory_list(WT_FILE_SYSTEM *file_system,
uint32_t allocated, count;
int ret = 0;
char *name, **entries;
+ void *p;
(void)session; /* Unused */
@@ -424,14 +425,16 @@ demo_fs_directory_list(WT_FILE_SYSTEM *file_system,
* matter if the list is a bit longer than necessary.
*/
if (count >= allocated) {
- entries = realloc(
- entries, (allocated + 10) * sizeof(char *));
- if (entries == NULL) {
+ p = realloc(
+ entries, (allocated + 10) * sizeof(*entries));
+ if (p == NULL) {
ret = ENOMEM;
goto err;
}
- memset(entries + allocated * sizeof(char *),
- 0, 10 * sizeof(char *));
+
+ entries = p;
+ memset(entries + allocated * sizeof(*entries),
+ 0, 10 * sizeof(*entries));
allocated += 10;
}
entries[count++] = strdup(name);
diff --git a/examples/c/ex_log.c b/examples/c/ex_log.c
index fdbc39412ae..0d8fbf97233 100644
--- a/examples/c/ex_log.c
+++ b/examples/c/ex_log.c
@@ -291,8 +291,8 @@ main(void)
char cmd_buf[256], k[16], v[16];
count_min = 0;
- snprintf(cmd_buf, sizeof(cmd_buf), "rm -rf %s %s && mkdir %s %s",
- home1, home2, home1, home2);
+ (void)snprintf(cmd_buf, sizeof(cmd_buf),
+ "rm -rf %s %s && mkdir %s %s", home1, home2, home1, home2);
if ((ret = system(cmd_buf)) != 0) {
fprintf(stderr, "%s: failed ret %d\n", cmd_buf, ret);
return (EXIT_FAILURE);
@@ -312,8 +312,8 @@ main(void)
* Perform some operations with individual auto-commit transactions.
*/
for (record_count = 0, i = 0; i < MAX_KEYS; i++, record_count++) {
- snprintf(k, sizeof(k), "key%d", i);
- snprintf(v, sizeof(v), "value%d", i);
+ (void)snprintf(k, sizeof(k), "key%d", i);
+ (void)snprintf(v, sizeof(v), "value%d", i);
cursor->set_key(cursor, k);
cursor->set_value(cursor, v);
ret = cursor->insert(cursor);
@@ -324,8 +324,8 @@ main(void)
* Perform some operations within a single transaction.
*/
for (i = MAX_KEYS; i < MAX_KEYS+5; i++, record_count++) {
- snprintf(k, sizeof(k), "key%d", i);
- snprintf(v, sizeof(v), "value%d", i);
+ (void)snprintf(k, sizeof(k), "key%d", i);
+ (void)snprintf(v, sizeof(v), "value%d", i);
cursor->set_key(cursor, k);
cursor->set_value(cursor, v);
ret = cursor->insert(cursor);
diff --git a/examples/c/ex_scope.c b/examples/c/ex_scope.c
deleted file mode 100644
index 795ad85d57b..00000000000
--- a/examples/c/ex_scope.c
+++ /dev/null
@@ -1,217 +0,0 @@
-/*-
- * Public Domain 2014-2016 MongoDB, Inc.
- * Public Domain 2008-2014 WiredTiger, Inc.
- *
- * This is free and unencumbered software released into the public domain.
- *
- * Anyone is free to copy, modify, publish, use, compile, sell, or
- * distribute this software, either in source code form or as a compiled
- * binary, for any purpose, commercial or non-commercial, and by any
- * means.
- *
- * In jurisdictions that recognize copyright laws, the author or authors
- * of this software dedicate any and all copyright interest in the
- * software to the public domain. We make this dedication for the benefit
- * of the public at large and to the detriment of our heirs and
- * successors. We intend this dedication to be an overt act of
- * relinquishment in perpetuity of all present and future rights to this
- * software under copyright law.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
- * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
- * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
- * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
- *
- * ex_scope.c
- * demonstrates the scope of buffers holding cursor keys and values.
- */
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <wiredtiger.h>
-
-#ifdef _WIN32
-/* snprintf is not supported on <= VS2013 */
-#define snprintf _snprintf
-#endif
-
-static const char *home;
-
-static int
-cursor_scope_ops(WT_CURSOR *cursor)
-{
- struct {
- const char *op;
- const char *key;
- const char *value;
- int (*apply)(WT_CURSOR *);
- } *op, ops[] = {
- { "insert", "key1", "value1", cursor->insert, },
- { "update", "key1", "value2", cursor->update, },
- { "search", "key1", "value2", cursor->search, },
- { "remove", "key1", "value2", cursor->remove, },
- { NULL, NULL, NULL, NULL }
- };
- WT_SESSION *session;
- const char *key, *value;
- char keybuf[10], valuebuf[10];
- int ret;
-
- session = cursor->session;
-
- for (op = ops; op->key != NULL; op++) {
- key = value = NULL;
-
- /*! [cursor scope operation] */
- (void)snprintf(keybuf, sizeof(keybuf), "%s", op->key);
- cursor->set_key(cursor, keybuf);
- (void)snprintf(valuebuf, sizeof(valuebuf), "%s", op->value);
- cursor->set_value(cursor, valuebuf);
-
- /*
- * The application must keep key and value memory valid until
- * the next operation that positions the cursor, modifies the
- * data, or resets or closes the cursor.
- *
- * Modifying either the key or value buffers is not permitted.
- */
-
- /* Apply the operation (insert, update, search or remove). */
- if ((ret = op->apply(cursor)) != 0) {
- fprintf(stderr,
- "%s: error performing the operation: %s\n",
- op->op, session->strerror(session, ret));
- return (ret);
- }
-
- /*
- * The cursor no longer references application memory, so
- * application buffers can be safely overwritten.
- */
- strcpy(keybuf, "no key");
- strcpy(valuebuf, "no value");
-
- /*
- * Check that get_key/value behave as expected after the
- * operation.
- */
- if (op->apply == cursor->insert) {
- /*
- * WT_CURSOR::insert no longer references application
- * memory, but as it does not position the cursor, it
- * doesn't reference memory owned by the cursor, either.
- */
- printf("ex_scope: "
- "expect two WiredTiger error messages:\n");
- if ((ret = cursor->get_key(cursor, &key)) == 0 ||
- (ret = cursor->get_value(cursor, &value)) == 0) {
- fprintf(stderr,
- "%s: error in get_key/value: %s\n",
- op->op, session->strerror(session, ret));
- return (ret);
- }
- continue;
- }
- if (op->apply == cursor->remove) {
- /*
- * WT_CURSOR::remove no longer references application
- * memory; as it does not position the cursor, it will
- * reference key memory owned by the cursor, but has no
- * value.
- */
- printf("ex_scope: "
- "expect one WiredTiger error message:\n");
- if ((ret = cursor->get_key(cursor, &key)) != 0 ||
- (ret = cursor->get_value(cursor, &value)) == 0) {
- fprintf(stderr,
- "%s: error in get_key/value: %s\n",
- op->op, session->strerror(session, ret));
- return (ret);
- }
- } else /* search, update */{
- /*
- * WT_CURSOR::search and WT_CURSOR::update no longer
- * reference application memory; as they position the
- * cursor, they will reference key/value memory owned
- * by the cursor.
- */
- if ((ret = cursor->get_key(cursor, &key)) != 0 ||
- (ret = cursor->get_value(cursor, &value)) != 0) {
- fprintf(stderr,
- "%s: error in get_key/value: %s\n",
- op->op, session->strerror(session, ret));
- return (ret);
- }
- }
-
- /*
- * Modifying the memory referenced by either key or value is
- * not permitted.
- *
- * Check that the cursor's key and value are what we expect.
- */
- if (key == keybuf ||
- (op->apply != cursor->remove && value == valuebuf)) {
- fprintf(stderr,
- "%s: cursor points at application memory!\n",
- op->op);
- return (EINVAL);
- }
-
- if (strcmp(key, op->key) != 0 ||
- (op->apply != cursor->remove &&
- strcmp(value, op->value) != 0)) {
- fprintf(stderr,
- "%s: unexpected key / value!\n", op->op);
- return (EINVAL);
- }
- /*! [cursor scope operation] */
- }
-
- return (0);
-}
-
-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;
-
- /* 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) {
- fprintf(stderr, "Error connecting to %s: %s\n",
- home == NULL ? "." : home, wiredtiger_strerror(ret));
- return (EXIT_FAILURE);
- }
-
- ret = session->create(session,
- "table:scope", "key_format=S,value_format=S,columns=(k,v)");
-
- ret = session->open_cursor(session,
- "table:scope", NULL, NULL, &cursor);
-
- ret = cursor_scope_ops(cursor);
-
- /* Close the connection and clean up. */
- ret = conn->close(conn, NULL);
-
- return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
-}
diff --git a/examples/c/ex_sync.c b/examples/c/ex_sync.c
index 2c610b1e570..b2d74b52f7f 100644
--- a/examples/c/ex_sync.c
+++ b/examples/c/ex_sync.c
@@ -59,8 +59,8 @@ main(void)
char cmd_buf[256], k[16], v[16];
const char *conf;
- snprintf(cmd_buf, sizeof(cmd_buf), "rm -rf %s && mkdir %s",
- home, home);
+ (void)snprintf(cmd_buf, sizeof(cmd_buf),
+ "rm -rf %s && mkdir %s", home, home);
if ((ret = system(cmd_buf)) != 0) {
fprintf(stderr, "%s: failed ret %d\n", cmd_buf, ret);
return (EXIT_FAILURE);
@@ -98,8 +98,8 @@ main(void)
ret = session->commit_transaction(session, conf);
ret = session->begin_transaction(session, NULL);
}
- snprintf(k, sizeof(k), "key%d", i);
- snprintf(v, sizeof(v), "value%d", i);
+ (void)snprintf(k, sizeof(k), "key%d", i);
+ (void)snprintf(v, sizeof(v), "value%d", i);
cursor->set_key(cursor, k);
cursor->set_value(cursor, v);
ret = cursor->insert(cursor);
@@ -113,8 +113,8 @@ main(void)
* Perform some operations within a single transaction.
*/
for (i = MAX_KEYS; i < MAX_KEYS+5; i++, record_count++) {
- snprintf(k, sizeof(k), "key%d", i);
- snprintf(v, sizeof(v), "value%d", i);
+ (void)snprintf(k, sizeof(k), "key%d", i);
+ (void)snprintf(v, sizeof(v), "value%d", i);
cursor->set_key(cursor, k);
cursor->set_value(cursor, v);
ret = cursor->insert(cursor);
@@ -129,8 +129,8 @@ main(void)
* Demonstrate using log_flush to force the log to disk.
*/
for (i = 0; i < MAX_KEYS; i++, record_count++) {
- snprintf(k, sizeof(k), "key%d", record_count);
- snprintf(v, sizeof(v), "value%d", record_count);
+ (void)snprintf(k, sizeof(k), "key%d", record_count);
+ (void)snprintf(v, sizeof(v), "value%d", record_count);
cursor->set_key(cursor, k);
cursor->set_value(cursor, v);
ret = cursor->insert(cursor);
@@ -138,8 +138,8 @@ main(void)
ret = session->log_flush(session, "sync=on");
for (i = 0; i < MAX_KEYS; i++, record_count++) {
- snprintf(k, sizeof(k), "key%d", record_count);
- snprintf(v, sizeof(v), "value%d", record_count);
+ (void)snprintf(k, sizeof(k), "key%d", record_count);
+ (void)snprintf(v, sizeof(v), "value%d", record_count);
cursor->set_key(cursor, k);
cursor->set_value(cursor, v);
ret = cursor->insert(cursor);