#include <time.h>
#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;
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;
printf("Adding %d values\n", count);
for (i=0;i<count;i++) {
sprintf(sval, "%03x %d foo bar", values[i], values[i]);
- rc = mdb_put(txn, dbi, &key, &data, MDB_NOOVERWRITE);
- if (rc) {
+ if (RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NOOVERWRITE))) {
j++;
data.mv_size = sizeof(sval);
data.mv_data = sval;
}
}
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);
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(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);
(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);
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);
#include <time.h>
#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;
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;
printf("Adding %d values\n", count);
for (i=0;i<count;i++) {
sprintf(sval, "%03x %d foo bar", values[i], values[i]);
- rc = mdb_put(txn, dbi, &key, &data, MDB_NOOVERWRITE);
- if (rc) j++;
+ if (RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NOOVERWRITE)))
+ 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);
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(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);
#include <time.h>
#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;
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;
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);
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);
#include <time.h>
#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;
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;
strcpy(kval, "001");
for (i=0;i<count;i++) {
sprintf(sval, "%07x", 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));
/* there should be one full page of dups now.
*/
- 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);
data.mv_data = sval;
sprintf(sval, "%07x", values[3]+1);
- rc = mdb_txn_begin(env, NULL, 0, &txn);
- rc = mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA);
+ E(mdb_txn_begin(env, NULL, 0, &txn));
+ (void)RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA));
mdb_txn_abort(txn);
sprintf(sval, "%07x", values[255]+1);
- rc = mdb_txn_begin(env, NULL, 0, &txn);
- rc = mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA);
+ E(mdb_txn_begin(env, NULL, 0, &txn));
+ (void)RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA));
mdb_txn_abort(txn);
sprintf(sval, "%07x", values[500]+1);
- rc = mdb_txn_begin(env, NULL, 0, &txn);
- rc = mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA);
- rc = mdb_txn_commit(txn);
+ E(mdb_txn_begin(env, NULL, 0, &txn));
+ (void)RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA));
+ E(mdb_txn_commit(txn));
/* Try MDB_NEXT_MULTIPLE */
- rc = mdb_txn_begin(env, NULL, 0, &txn);
- rc = mdb_cursor_open(txn, dbi, &cursor);
+ E(mdb_txn_begin(env, NULL, 0, &txn));
+ E(mdb_cursor_open(txn, dbi, &cursor));
while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT_MULTIPLE)) == 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_txn_abort(txn);
j=0;
for (i= count - 1; 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);
#include <time.h>
#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;
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;
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);
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);
#include <time.h>
#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[])
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);
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);
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
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);
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);