Commit e2bdd446 authored by Hallvard Furuseth's avatar Hallvard Furuseth
Browse files

mtest*.c, sample-*.c: Add/note error checks.

parent a6049489
......@@ -17,6 +17,11 @@
#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;
......@@ -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<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);
......@@ -79,30 +84,30 @@ 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(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);
......
......@@ -20,6 +20,11 @@
#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;
......@@ -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<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);
......@@ -79,34 +85,35 @@ 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(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);
......
......@@ -20,6 +20,11 @@
#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;
......@@ -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);
......
......@@ -20,6 +20,11 @@
#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;
......@@ -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<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);
......@@ -90,28 +96,29 @@ int main(int argc,char * argv[])
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;
......@@ -119,38 +126,39 @@ int main(int argc,char * argv[])
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);
......
......@@ -20,6 +20,11 @@
#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;
......@@ -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));