/* mtest.c - memory-mapped database tester/toy */ /* * Copyright 2011-2018 Howard Chu, Symas Corp. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted only as authorized by the OpenLDAP * Public License. * * A copy of this license is available in the file LICENSE in the * top-level directory of the distribution or, alternatively, at * . */ #include #include #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; MDB_env *env; MDB_dbi dbi; MDB_val key, data; MDB_txn *txn; MDB_stat mst; MDB_cursor *cursor, *cur2; MDB_cursor_op op; int count = 20000; char sval[32] = ""; char shortkey[8], longkey[32]; E(mdb_env_create(&env)); E(mdb_env_set_maxreaders(env, 1)); E(mdb_env_set_mapsize(env, 10485760)); E(mdb_env_open(env, "./testdb", 0 /*|MDB_NOSYNC*/, 0664)); E(mdb_txn_begin(env, NULL, 0, &txn)); E(mdb_dbi_open(txn, NULL, 0, &dbi)); key.mv_size = sizeof(shortkey); key.mv_data = shortkey; printf("Adding %d short keys\n", count); sprintf(sval, "short"); data.mv_size = sizeof(sval); data.mv_data = sval; for (i=0;i in each iteration, since MDB_NOOVERWRITE may modify it */ 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); E(mdb_txn_commit(txn)); E(mdb_env_stat(env, &mst)); key.mv_size = sizeof(longkey); key.mv_data = longkey; printf("Adding %d long keys\n", count); E(mdb_txn_begin(env, NULL, 0, &txn)); sprintf(sval, "long"); data.mv_size = sizeof(sval); data.mv_data = sval; j = 0; for (i=0;i in each iteration, since MDB_NOOVERWRITE may modify it */ 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); E(mdb_txn_commit(txn)); E(mdb_env_stat(env, &mst)); printf("Deleting %d short keys\n", count); key.mv_size = sizeof(shortkey); key.mv_data = shortkey; for (i= 0; i