|
|
|
@ -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); |
|
|
|
|