diff --git a/libraries/liblmdb/Makefile b/libraries/liblmdb/Makefile index 000f1f9217de117d280d1243b440d7ba95a465c4..520b8fd1d824db98621a8c4e71d3770ae3fc6fcf 100644 --- a/libraries/liblmdb/Makefile +++ b/libraries/liblmdb/Makefile @@ -41,7 +41,7 @@ IHDRS = lmdb.h ILIBS = liblmdb.a liblmdb$(SOEXT) IPROGS = mdb_stat mdb_copy mdb_dump mdb_load IDOCS = mdb_stat.1 mdb_copy.1 mdb_dump.1 mdb_load.1 -PROGS = $(IPROGS) mtest mtest2 mtest3 mtest4 mtest5 +PROGS = $(IPROGS) mtest mtest2 mtest3 mtest4 mtest5 mtest7 all: $(ILIBS) $(PROGS) install: $(ILIBS) $(IPROGS) $(IHDRS) @@ -78,6 +78,7 @@ mtest3: mtest3.o liblmdb.a mtest4: mtest4.o liblmdb.a mtest5: mtest5.o liblmdb.a mtest6: mtest6.o liblmdb.a +mtest7: mtest7.o liblmdb.a mplay: mplay.o liblmdb.a mdb.o: mdb.c lmdb.h midl.h diff --git a/libraries/liblmdb/mtest7.c b/libraries/liblmdb/mtest7.c new file mode 100644 index 0000000000000000000000000000000000000000..5ceac80431f991f5c5abec643e6ef5c138a2a76a --- /dev/null +++ b/libraries/liblmdb/mtest7.c @@ -0,0 +1,71 @@ +/* mtest7.c - memory-mapped database tester/toy */ +/* + * Copyright 2011-2021 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 + * <http://www.OpenLDAP.org/license.html>. + */ +/* Test for mdb_get */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#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 rc; + MDB_env *env; + MDB_dbi dbi; + MDB_val key, data; + MDB_txn *txn; + int key_data = 1234; // Example key + char *expected_data = "example_data"; // Example data + + 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, 0664)); + + E(mdb_txn_begin(env, NULL, 0, &txn)); + E(mdb_dbi_open(txn, NULL, 0, &dbi)); + + // Insert a key-value pair for testing + key.mv_size = sizeof(int); + key.mv_data = &key_data; + data.mv_size = strlen(expected_data); + data.mv_data = expected_data; + E(mdb_put(txn, dbi, &key, &data, 0)); + E(mdb_txn_commit(txn)); + + // Begin a new transaction for reading + E(mdb_txn_begin(env, NULL, MDB_RDONLY, &txn)); + + // Perform the database get operation + rc = mdb_get(txn, dbi, &key, &data); + if (rc == MDB_NOTFOUND) { + printf("Key not found.\n"); + } else { + CHECK(rc == MDB_SUCCESS, "mdb_get"); + CHECK(data.mv_size == strlen(expected_data), "Data size mismatch"); + CHECK(strncmp((char *)data.mv_data, expected_data, data.mv_size) == 0, "Data content mismatch"); + printf("Data found: %.*s\n", (int)data.mv_size, (char *)data.mv_data); + } + + mdb_txn_abort(txn); + mdb_dbi_close(env, dbi); + mdb_env_close(env); + + return 0; +} \ No newline at end of file