From e2bdd44624a525d4847c22d7ebf1ea4d154ed734 Mon Sep 17 00:00:00 2001 From: Hallvard Furuseth Date: Wed, 19 Feb 2014 21:25:40 +0100 Subject: [PATCH] mtest*.c, sample-*.c: Add/note error checks. --- libraries/liblmdb/mtest.c | 75 ++++++++++++++++++---------------- libraries/liblmdb/mtest2.c | 45 +++++++++++--------- libraries/liblmdb/mtest3.c | 45 +++++++++++--------- libraries/liblmdb/mtest4.c | 64 ++++++++++++++++------------- libraries/liblmdb/mtest5.c | 47 ++++++++++++--------- libraries/liblmdb/mtest6.c | 47 ++++++++++++--------- libraries/liblmdb/sample-bdb.c | 2 + libraries/liblmdb/sample-mdb.c | 2 + 8 files changed, 186 insertions(+), 141 deletions(-) diff --git a/libraries/liblmdb/mtest.c b/libraries/liblmdb/mtest.c index f9a96b2..aad0c70 100644 --- a/libraries/liblmdb/mtest.c +++ b/libraries/liblmdb/mtest.c @@ -17,6 +17,11 @@ #include #include "lmdb.h" +#define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr) +#define RES(err, expr) ((rc = expr) == (err) || (CHECK(!rc, #expr), 0)) +#define CHECK(test, msg) ((test) ? (void)0 : ((void)fprintf(stderr, \ + "%s:%d: %s: %s\n", __FILE__, __LINE__, msg, mdb_strerror(rc)), abort())) + int main(int argc,char * argv[]) { int i = 0, j = 0, rc; @@ -39,11 +44,11 @@ int main(int argc,char * argv[]) values[i] = random()%1024; } - rc = mdb_env_create(&env); - rc = mdb_env_set_mapsize(env, 10485760); - rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP /*|MDB_NOSYNC*/, 0664); - rc = mdb_txn_begin(env, NULL, 0, &txn); - rc = mdb_open(txn, NULL, 0, &dbi); + E(mdb_env_create(&env)); + E(mdb_env_set_mapsize(env, 10485760)); + E(mdb_env_open(env, "./testdb", MDB_FIXEDMAP /*|MDB_NOSYNC*/, 0664)); + E(mdb_txn_begin(env, NULL, 0, &txn)); + E(mdb_open(txn, NULL, 0, &dbi)); key.mv_size = sizeof(int); key.mv_data = sval; @@ -53,24 +58,24 @@ int main(int argc,char * argv[]) printf("Adding %d values\n", count); for (i=0;i -1; i-= (random()%5)) { j++; txn=NULL; - rc = mdb_txn_begin(env, NULL, 0, &txn); + E(mdb_txn_begin(env, NULL, 0, &txn)); sprintf(sval, "%03x ", values[i]); - rc = mdb_del(txn, dbi, &key, NULL); - if (rc) { + if (RES(MDB_NOTFOUND, mdb_del(txn, dbi, &key, NULL))) { j--; mdb_txn_abort(txn); } else { - rc = mdb_txn_commit(txn); + E(mdb_txn_commit(txn)); } } free(values); printf("Deleted %d values\n", j); - rc = mdb_env_stat(env, &mst); - rc = mdb_txn_begin(env, NULL, 1, &txn); - rc = mdb_cursor_open(txn, dbi, &cursor); + E(mdb_env_stat(env, &mst)); + E(mdb_txn_begin(env, NULL, 1, &txn)); + E(mdb_cursor_open(txn, dbi, &cursor)); printf("Cursor next\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } + CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); printf("Cursor last\n"); - rc = mdb_cursor_get(cursor, &key, &data, MDB_LAST); + E(mdb_cursor_get(cursor, &key, &data, MDB_LAST)); printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); @@ -112,12 +117,13 @@ int main(int argc,char * argv[]) (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } + CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); printf("Cursor last/prev\n"); - rc = mdb_cursor_get(cursor, &key, &data, MDB_LAST); + E(mdb_cursor_get(cursor, &key, &data, MDB_LAST)); printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); - rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV); + E(mdb_cursor_get(cursor, &key, &data, MDB_PREV)); printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); @@ -125,43 +131,42 @@ int main(int argc,char * argv[]) mdb_txn_abort(txn); printf("Deleting with cursor\n"); - rc = mdb_txn_begin(env, NULL, 0, &txn); - rc = mdb_cursor_open(txn, dbi, &cur2); + E(mdb_txn_begin(env, NULL, 0, &txn)); + E(mdb_cursor_open(txn, dbi, &cur2)); for (i=0; i<50; i++) { - rc = mdb_cursor_get(cur2, &key, &data, MDB_NEXT); - if (rc) + if (RES(MDB_NOTFOUND, mdb_cursor_get(cur2, &key, &data, MDB_NEXT))) break; printf("key: %p %.*s, data: %p %.*s\n", key.mv_data, (int) key.mv_size, (char *) key.mv_data, data.mv_data, (int) data.mv_size, (char *) data.mv_data); - rc = mdb_del(txn, dbi, &key, NULL); + E(mdb_del(txn, dbi, &key, NULL)); } printf("Restarting cursor in txn\n"); - rc = mdb_cursor_get(cur2, &key, &data, MDB_FIRST); + E(mdb_cursor_get(cur2, &key, &data, MDB_FIRST)); printf("key: %p %.*s, data: %p %.*s\n", key.mv_data, (int) key.mv_size, (char *) key.mv_data, data.mv_data, (int) data.mv_size, (char *) data.mv_data); for (i=0; i<32; i++) { - rc = mdb_cursor_get(cur2, &key, &data, MDB_NEXT); - if (rc) break; + if (RES(MDB_NOTFOUND, mdb_cursor_get(cur2, &key, &data, MDB_NEXT))) + break; printf("key: %p %.*s, data: %p %.*s\n", key.mv_data, (int) key.mv_size, (char *) key.mv_data, data.mv_data, (int) data.mv_size, (char *) data.mv_data); } mdb_cursor_close(cur2); - rc = mdb_txn_commit(txn); + E(mdb_txn_commit(txn)); printf("Restarting cursor outside txn\n"); - rc = mdb_txn_begin(env, NULL, 0, &txn); - rc = mdb_cursor_open(txn, dbi, &cursor); - rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST); + E(mdb_txn_begin(env, NULL, 0, &txn)); + E(mdb_cursor_open(txn, dbi, &cursor)); + E(mdb_cursor_get(cursor, &key, &data, MDB_FIRST)); printf("key: %p %.*s, data: %p %.*s\n", key.mv_data, (int) key.mv_size, (char *) key.mv_data, data.mv_data, (int) data.mv_size, (char *) data.mv_data); for (i=0; i<32; i++) { - rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT); - if (rc) break; + if (RES(MDB_NOTFOUND, mdb_cursor_get(cursor, &key, &data, MDB_NEXT))) + break; printf("key: %p %.*s, data: %p %.*s\n", key.mv_data, (int) key.mv_size, (char *) key.mv_data, data.mv_data, (int) data.mv_size, (char *) data.mv_data); diff --git a/libraries/liblmdb/mtest2.c b/libraries/liblmdb/mtest2.c index bfa29f9..ebda852 100644 --- a/libraries/liblmdb/mtest2.c +++ b/libraries/liblmdb/mtest2.c @@ -20,6 +20,11 @@ #include #include "lmdb.h" +#define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr) +#define RES(err, expr) ((rc = expr) == (err) || (CHECK(!rc, #expr), 0)) +#define CHECK(test, msg) ((test) ? (void)0 : ((void)fprintf(stderr, \ + "%s:%d: %s: %s\n", __FILE__, __LINE__, msg, mdb_strerror(rc)), abort())) + int main(int argc,char * argv[]) { int i = 0, j = 0, rc; @@ -42,12 +47,12 @@ int main(int argc,char * argv[]) values[i] = random()%1024; } - rc = mdb_env_create(&env); - rc = mdb_env_set_mapsize(env, 10485760); - rc = mdb_env_set_maxdbs(env, 4); - rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664); - rc = mdb_txn_begin(env, NULL, 0, &txn); - rc = mdb_open(txn, "id1", MDB_CREATE, &dbi); + E(mdb_env_create(&env)); + E(mdb_env_set_mapsize(env, 10485760)); + E(mdb_env_set_maxdbs(env, 4)); + E(mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664)); + E(mdb_txn_begin(env, NULL, 0, &txn)); + E(mdb_open(txn, "id1", MDB_CREATE, &dbi)); key.mv_size = sizeof(int); key.mv_data = sval; @@ -57,20 +62,21 @@ int main(int argc,char * argv[]) printf("Adding %d values\n", count); for (i=0;i -1; i-= (random()%5)) { j++; txn=NULL; - rc = mdb_txn_begin(env, NULL, 0, &txn); + E(mdb_txn_begin(env, NULL, 0, &txn)); sprintf(sval, "%03x ", values[i]); - rc = mdb_del(txn, dbi, &key, NULL); - if (rc) { + if (RES(MDB_NOTFOUND, mdb_del(txn, dbi, &key, NULL))) { j--; mdb_txn_abort(txn); } else { - rc = mdb_txn_commit(txn); + E(mdb_txn_commit(txn)); } } free(values); printf("Deleted %d values\n", j); - rc = mdb_env_stat(env, &mst); - rc = mdb_txn_begin(env, NULL, 1, &txn); - rc = mdb_cursor_open(txn, dbi, &cursor); + E(mdb_env_stat(env, &mst)); + E(mdb_txn_begin(env, NULL, 1, &txn)); + E(mdb_cursor_open(txn, dbi, &cursor)); printf("Cursor next\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } + CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); printf("Cursor prev\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) { printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } + CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); mdb_cursor_close(cursor); mdb_close(env, dbi); diff --git a/libraries/liblmdb/mtest3.c b/libraries/liblmdb/mtest3.c index c189eaa..95b1749 100644 --- a/libraries/liblmdb/mtest3.c +++ b/libraries/liblmdb/mtest3.c @@ -20,6 +20,11 @@ #include #include "lmdb.h" +#define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr) +#define RES(err, expr) ((rc = expr) == (err) || (CHECK(!rc, #expr), 0)) +#define CHECK(test, msg) ((test) ? (void)0 : ((void)fprintf(stderr, \ + "%s:%d: %s: %s\n", __FILE__, __LINE__, msg, mdb_strerror(rc)), abort())) + int main(int argc,char * argv[]) { int i = 0, j = 0, rc; @@ -45,12 +50,12 @@ int main(int argc,char * argv[]) values[i] = random()%1024; } - rc = mdb_env_create(&env); - rc = mdb_env_set_mapsize(env, 10485760); - rc = mdb_env_set_maxdbs(env, 4); - rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664); - rc = mdb_txn_begin(env, NULL, 0, &txn); - rc = mdb_open(txn, "id2", MDB_CREATE|MDB_DUPSORT, &dbi); + E(mdb_env_create(&env)); + E(mdb_env_set_mapsize(env, 10485760)); + E(mdb_env_set_maxdbs(env, 4)); + E(mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664)); + E(mdb_txn_begin(env, NULL, 0, &txn)); + E(mdb_open(txn, "id2", MDB_CREATE|MDB_DUPSORT, &dbi)); key.mv_size = sizeof(int); key.mv_data = kval; @@ -62,20 +67,21 @@ int main(int argc,char * argv[]) if (!(i & 0x0f)) sprintf(kval, "%03x", values[i]); sprintf(sval, "%03x %d foo bar", values[i], values[i]); - rc = mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA); - if (rc) j++; + if (RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA))) + j++; } if (j) printf("%d duplicates skipped\n", j); - rc = mdb_txn_commit(txn); - rc = mdb_env_stat(env, &mst); + E(mdb_txn_commit(txn)); + E(mdb_env_stat(env, &mst)); - rc = mdb_txn_begin(env, NULL, 1, &txn); - rc = mdb_cursor_open(txn, dbi, &cursor); + E(mdb_txn_begin(env, NULL, 1, &txn)); + E(mdb_cursor_open(txn, dbi, &cursor)); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %p %.*s, data: %p %.*s\n", key.mv_data, (int) key.mv_size, (char *) key.mv_data, data.mv_data, (int) data.mv_size, (char *) data.mv_data); } + CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); mdb_cursor_close(cursor); mdb_txn_abort(txn); @@ -84,39 +90,40 @@ int main(int argc,char * argv[]) for (i= count - 1; i > -1; i-= (random()%5)) { j++; txn=NULL; - rc = mdb_txn_begin(env, NULL, 0, &txn); + E(mdb_txn_begin(env, NULL, 0, &txn)); sprintf(kval, "%03x", values[i & ~0x0f]); sprintf(sval, "%03x %d foo bar", values[i], values[i]); key.mv_size = sizeof(int); key.mv_data = kval; data.mv_size = sizeof(sval); data.mv_data = sval; - rc = mdb_del(txn, dbi, &key, &data); - if (rc) { + if (RES(MDB_NOTFOUND, mdb_del(txn, dbi, &key, &data))) { j--; mdb_txn_abort(txn); } else { - rc = mdb_txn_commit(txn); + E(mdb_txn_commit(txn)); } } free(values); printf("Deleted %d values\n", j); - rc = mdb_env_stat(env, &mst); - rc = mdb_txn_begin(env, NULL, 1, &txn); - rc = mdb_cursor_open(txn, dbi, &cursor); + E(mdb_env_stat(env, &mst)); + E(mdb_txn_begin(env, NULL, 1, &txn)); + E(mdb_cursor_open(txn, dbi, &cursor)); printf("Cursor next\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } + CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); printf("Cursor prev\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) { printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } + CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); mdb_cursor_close(cursor); mdb_close(env, dbi); diff --git a/libraries/liblmdb/mtest4.c b/libraries/liblmdb/mtest4.c index e4da8db..37f95ba 100644 --- a/libraries/liblmdb/mtest4.c +++ b/libraries/liblmdb/mtest4.c @@ -20,6 +20,11 @@ #include #include "lmdb.h" +#define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr) +#define RES(err, expr) ((rc = expr) == (err) || (CHECK(!rc, #expr), 0)) +#define CHECK(test, msg) ((test) ? (void)0 : ((void)fprintf(stderr, \ + "%s:%d: %s: %s\n", __FILE__, __LINE__, msg, mdb_strerror(rc)), abort())) + int main(int argc,char * argv[]) { int i = 0, j = 0, rc; @@ -43,12 +48,12 @@ int main(int argc,char * argv[]) values[i] = i*5; } - rc = mdb_env_create(&env); - rc = mdb_env_set_mapsize(env, 10485760); - rc = mdb_env_set_maxdbs(env, 4); - rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664); - rc = mdb_txn_begin(env, NULL, 0, &txn); - rc = mdb_open(txn, "id4", MDB_CREATE|MDB_DUPSORT|MDB_DUPFIXED, &dbi); + E(mdb_env_create(&env)); + E(mdb_env_set_mapsize(env, 10485760)); + E(mdb_env_set_maxdbs(env, 4)); + E(mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664)); + E(mdb_txn_begin(env, NULL, 0, &txn)); + E(mdb_open(txn, "id4", MDB_CREATE|MDB_DUPSORT|MDB_DUPFIXED, &dbi)); key.mv_size = sizeof(int); key.mv_data = kval; @@ -59,22 +64,23 @@ int main(int argc,char * argv[]) strcpy(kval, "001"); for (i=0;i -1; i-= (random()%3)) { j++; txn=NULL; - rc = mdb_txn_begin(env, NULL, 0, &txn); + E(mdb_txn_begin(env, NULL, 0, &txn)); sprintf(sval, "%07x", values[i]); key.mv_size = sizeof(int); key.mv_data = kval; data.mv_size = sizeof(sval); data.mv_data = sval; - rc = mdb_del(txn, dbi, &key, &data); - if (rc) { + if (RES(MDB_NOTFOUND, mdb_del(txn, dbi, &key, &data))) { j--; mdb_txn_abort(txn); } else { - rc = mdb_txn_commit(txn); + E(mdb_txn_commit(txn)); } } free(values); printf("Deleted %d values\n", j); - rc = mdb_env_stat(env, &mst); - rc = mdb_txn_begin(env, NULL, 1, &txn); - rc = mdb_cursor_open(txn, dbi, &cursor); + E(mdb_env_stat(env, &mst)); + E(mdb_txn_begin(env, NULL, 1, &txn)); + E(mdb_cursor_open(txn, dbi, &cursor)); printf("Cursor next\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } + CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); printf("Cursor prev\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) { printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } + CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); mdb_cursor_close(cursor); mdb_close(env, dbi); diff --git a/libraries/liblmdb/mtest5.c b/libraries/liblmdb/mtest5.c index bc472fa..4edfea0 100644 --- a/libraries/liblmdb/mtest5.c +++ b/libraries/liblmdb/mtest5.c @@ -20,6 +20,11 @@ #include #include "lmdb.h" +#define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr) +#define RES(err, expr) ((rc = expr) == (err) || (CHECK(!rc, #expr), 0)) +#define CHECK(test, msg) ((test) ? (void)0 : ((void)fprintf(stderr, \ + "%s:%d: %s: %s\n", __FILE__, __LINE__, msg, mdb_strerror(rc)), abort())) + int main(int argc,char * argv[]) { int i = 0, j = 0, rc; @@ -45,13 +50,13 @@ int main(int argc,char * argv[]) values[i] = random()%1024; } - rc = mdb_env_create(&env); - rc = mdb_env_set_mapsize(env, 10485760); - rc = mdb_env_set_maxdbs(env, 4); - rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664); - rc = mdb_txn_begin(env, NULL, 0, &txn); - rc = mdb_open(txn, "id2", MDB_CREATE|MDB_DUPSORT, &dbi); - rc = mdb_cursor_open(txn, dbi, &cursor); + E(mdb_env_create(&env)); + E(mdb_env_set_mapsize(env, 10485760)); + E(mdb_env_set_maxdbs(env, 4)); + E(mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664)); + E(mdb_txn_begin(env, NULL, 0, &txn)); + E(mdb_open(txn, "id2", MDB_CREATE|MDB_DUPSORT, &dbi)); + E(mdb_cursor_open(txn, dbi, &cursor)); key.mv_size = sizeof(int); key.mv_data = kval; @@ -63,21 +68,22 @@ int main(int argc,char * argv[]) if (!(i & 0x0f)) sprintf(kval, "%03x", values[i]); sprintf(sval, "%03x %d foo bar", values[i], values[i]); - rc = mdb_cursor_put(cursor, &key, &data, MDB_NODUPDATA); - if (rc) j++; + if (RES(MDB_KEYEXIST, mdb_cursor_put(cursor, &key, &data, MDB_NODUPDATA))) + j++; } if (j) printf("%d duplicates skipped\n", j); mdb_cursor_close(cursor); - rc = mdb_txn_commit(txn); - rc = mdb_env_stat(env, &mst); + E(mdb_txn_commit(txn)); + E(mdb_env_stat(env, &mst)); - rc = mdb_txn_begin(env, NULL, 1, &txn); - rc = mdb_cursor_open(txn, dbi, &cursor); + E(mdb_txn_begin(env, NULL, 1, &txn)); + E(mdb_cursor_open(txn, dbi, &cursor)); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %p %.*s, data: %p %.*s\n", key.mv_data, (int) key.mv_size, (char *) key.mv_data, data.mv_data, (int) data.mv_size, (char *) data.mv_data); } + CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); mdb_cursor_close(cursor); mdb_txn_abort(txn); @@ -86,39 +92,40 @@ int main(int argc,char * argv[]) for (i= count - 1; i > -1; i-= (random()%5)) { j++; txn=NULL; - rc = mdb_txn_begin(env, NULL, 0, &txn); + E(mdb_txn_begin(env, NULL, 0, &txn)); sprintf(kval, "%03x", values[i & ~0x0f]); sprintf(sval, "%03x %d foo bar", values[i], values[i]); key.mv_size = sizeof(int); key.mv_data = kval; data.mv_size = sizeof(sval); data.mv_data = sval; - rc = mdb_del(txn, dbi, &key, &data); - if (rc) { + if (RES(MDB_NOTFOUND, mdb_del(txn, dbi, &key, &data))) { j--; mdb_txn_abort(txn); } else { - rc = mdb_txn_commit(txn); + E(mdb_txn_commit(txn)); } } free(values); printf("Deleted %d values\n", j); - rc = mdb_env_stat(env, &mst); - rc = mdb_txn_begin(env, NULL, 1, &txn); - rc = mdb_cursor_open(txn, dbi, &cursor); + E(mdb_env_stat(env, &mst)); + E(mdb_txn_begin(env, NULL, 1, &txn)); + E(mdb_cursor_open(txn, dbi, &cursor)); printf("Cursor next\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } + CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); printf("Cursor prev\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) { printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } + CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); mdb_cursor_close(cursor); mdb_close(env, dbi); diff --git a/libraries/liblmdb/mtest6.c b/libraries/liblmdb/mtest6.c index 1dd246b..8d32e88 100644 --- a/libraries/liblmdb/mtest6.c +++ b/libraries/liblmdb/mtest6.c @@ -20,6 +20,11 @@ #include #include "lmdb.h" +#define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr) +#define RES(err, expr) ((rc = expr) == (err) || (CHECK(!rc, #expr), 0)) +#define CHECK(test, msg) ((test) ? (void)0 : ((void)fprintf(stderr, \ + "%s:%d: %s: %s\n", __FILE__, __LINE__, msg, mdb_strerror(rc)), abort())) + char dkbuf[1024]; int main(int argc,char * argv[]) @@ -38,14 +43,14 @@ int main(int argc,char * argv[]) srandom(time(NULL)); - rc = mdb_env_create(&env); - rc = mdb_env_set_mapsize(env, 10485760); - rc = mdb_env_set_maxdbs(env, 4); - rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664); - rc = mdb_txn_begin(env, NULL, 0, &txn); - rc = mdb_open(txn, "id6", MDB_CREATE|MDB_INTEGERKEY, &dbi); - rc = mdb_cursor_open(txn, dbi, &cursor); - rc = mdb_stat(txn, dbi, &mst); + E(mdb_env_create(&env)); + E(mdb_env_set_mapsize(env, 10485760)); + E(mdb_env_set_maxdbs(env, 4)); + E(mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664)); + E(mdb_txn_begin(env, NULL, 0, &txn)); + E(mdb_open(txn, "id6", MDB_CREATE|MDB_INTEGERKEY, &dbi)); + E(mdb_cursor_open(txn, dbi, &cursor)); + E(mdb_stat(txn, dbi, &mst)); sval = calloc(1, mst.ms_psize / 4); key.mv_size = sizeof(long); @@ -57,27 +62,28 @@ int main(int argc,char * argv[]) for (i=0;i<12;i++) { kval = i*5; sprintf(sval, "%08x", kval); - rc = mdb_cursor_put(cursor, &key, &data, MDB_NOOVERWRITE); + (void)RES(MDB_KEYEXIST, mdb_cursor_put(cursor, &key, &data, MDB_NOOVERWRITE)); } printf("Adding 12 more values, should yield 3 splits\n"); for (i=0;i<12;i++) { kval = i*5+4; sprintf(sval, "%08x", kval); - rc = mdb_cursor_put(cursor, &key, &data, MDB_NOOVERWRITE); + (void)RES(MDB_KEYEXIST, mdb_cursor_put(cursor, &key, &data, MDB_NOOVERWRITE)); } printf("Adding 12 more values, should yield 3 splits\n"); for (i=0;i<12;i++) { kval = i*5+1; sprintf(sval, "%08x", kval); - rc = mdb_cursor_put(cursor, &key, &data, MDB_NOOVERWRITE); + (void)RES(MDB_KEYEXIST, mdb_cursor_put(cursor, &key, &data, MDB_NOOVERWRITE)); } - rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST); + E(mdb_cursor_get(cursor, &key, &data, MDB_FIRST)); do { printf("key: %p %s, data: %p %.*s\n", key.mv_data, mdb_dkey(&key, dkbuf), data.mv_data, (int) data.mv_size, (char *) data.mv_data); } while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0); + CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); mdb_cursor_close(cursor); mdb_txn_commit(txn); @@ -87,41 +93,42 @@ int main(int argc,char * argv[]) for (i= count - 1; i > -1; i-= (random()%5)) { j++; txn=NULL; - rc = mdb_txn_begin(env, NULL, 0, &txn); + E(mdb_txn_begin(env, NULL, 0, &txn)); sprintf(kval, "%03x", values[i & ~0x0f]); sprintf(sval, "%03x %d foo bar", values[i], values[i]); key.mv_size = sizeof(int); key.mv_data = kval; data.mv_size = sizeof(sval); data.mv_data = sval; - rc = mdb_del(txn, dbi, &key, &data); - if (rc) { + if (RES(MDB_NOTFOUND, mdb_del(txn, dbi, &key, &data))) { j--; mdb_txn_abort(txn); } else { - rc = mdb_txn_commit(txn); + E(mdb_txn_commit(txn)); } } free(values); printf("Deleted %d values\n", j); - rc = mdb_env_stat(env, &mst); - rc = mdb_txn_begin(env, NULL, 1, &txn); - rc = mdb_cursor_open(txn, dbi, &cursor); + E(mdb_env_stat(env, &mst)); + E(mdb_txn_begin(env, NULL, 1, &txn)); + E(mdb_cursor_open(txn, dbi, &cursor)); printf("Cursor next\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } + CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); printf("Cursor prev\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) { printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } + CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); mdb_cursor_close(cursor); - mdb_close(txn, dbi); + mdb_close(env, dbi); mdb_txn_abort(txn); #endif diff --git a/libraries/liblmdb/sample-bdb.c b/libraries/liblmdb/sample-bdb.c index 2c11bb3..f827324 100644 --- a/libraries/liblmdb/sample-bdb.c +++ b/libraries/liblmdb/sample-bdb.c @@ -28,6 +28,8 @@ int main(int argc,char * argv[]) DBC *cursor; char sval[32], kval[32]; + /* Note: Most error checking omitted for simplicity */ + #define FLAGS (DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_TXN|DB_INIT_MPOOL|DB_CREATE|DB_THREAD) rc = db_env_create(&env, 0); rc = env->open(env, "./testdb", FLAGS, 0664); diff --git a/libraries/liblmdb/sample-mdb.c b/libraries/liblmdb/sample-mdb.c index 0b10f47..a74f556 100644 --- a/libraries/liblmdb/sample-mdb.c +++ b/libraries/liblmdb/sample-mdb.c @@ -27,6 +27,8 @@ int main(int argc,char * argv[]) MDB_cursor *cursor; char sval[32]; + /* Note: Most error checking omitted for simplicity */ + rc = mdb_env_create(&env); rc = mdb_env_open(env, "./testdb", 0, 0664); rc = mdb_txn_begin(env, NULL, 0, &txn);