Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//$ sudo apt install iblmdb-dev
//$ gcc -o tst win10bug.c -llmdb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "lmdb.h"
static int test1(MDB_env *env)
{
int retval;
MDB_txn *txn;
MDB_dbi dbi;
MDB_val key;
MDB_val data;
retval = mdb_txn_begin(env, NULL, 0, &txn);
if (retval != 0) {
fprintf(stderr, "ERR(%u): %s\n", __LINE__, mdb_strerror(retval));
goto LABEL_EXIT;
}
retval = mdb_dbi_open(txn, "test1", MDB_CREATE, &dbi);
if (retval != 0) {
fprintf(stderr, "ERR(%u): %s\n", __LINE__, mdb_strerror(retval));
mdb_txn_abort(txn);
goto LABEL_EXIT;
}
key.mv_size = 5;
key.mv_data = "test1";
data.mv_size = 5;
data.mv_data = "TEST1";
retval = mdb_put(txn, dbi, &key, &data, 0);
if (retval == 0) {
fprintf(stderr, "%s(): OK\n", __func__);
} else {
fprintf(stderr, "ERR(%u): %s\n", __LINE__, mdb_strerror(retval));
}
retval = mdb_txn_commit(txn);
if (retval != 0) {
fprintf(stderr, "ERR(%u): %s\n", __LINE__, mdb_strerror(retval));
}
LABEL_EXIT:
return retval;
}
static int test2(MDB_env *env)
{
int retval;
MDB_txn *txn;
MDB_dbi dbi;
MDB_val key;
MDB_val data;
retval = mdb_txn_begin(env, NULL, 0, &txn);
if (retval != 0) {
fprintf(stderr, "ERR(%u): %s\n", __LINE__, mdb_strerror(retval));
goto LABEL_EXIT;
}
retval = mdb_dbi_open(txn, "test1", 0, &dbi);
if (retval != 0) {
fprintf(stderr, "ERR(%u): %s\n", __LINE__, mdb_strerror(retval));
mdb_txn_abort(txn);
goto LABEL_EXIT;
}
key.mv_size = 5;
key.mv_data = "test1";
retval = mdb_get(txn, dbi, &key, &data);
mdb_txn_commit(txn);
if (retval == 0) {
fprintf(stderr, "%s(): OK\n", __func__);
char *keyname = (char *)malloc(key.mv_size + 1);
memcpy(keyname, key.mv_data, key.mv_size);
keyname[key.mv_size] = '\0';
char *dataname = (char *)malloc(data.mv_size + 1);
memcpy(dataname, data.mv_data, data.mv_size);
dataname[data.mv_size] = '\0';
printf(" key(%lu)=%s, data(%lu)=%s\n", key.mv_size, keyname, data.mv_size, dataname);
free(dataname);
free(keyname);
} else {
fprintf(stderr, "ERR(%u): %s\n", __LINE__, mdb_strerror(retval));
}
LABEL_EXIT:
return retval;
}
int main(void)
{
int retval;
MDB_env *env = NULL;
retval = mdb_env_create(&env);
if (retval != 0) {
fprintf(stderr, "ERR(%u): %s\n", __LINE__, mdb_strerror(retval));
goto LABEL_EXIT;
}
retval = mdb_env_set_maxdbs(env, 1);
if (retval != 0) {
fprintf(stderr, "ERR(%u): %s\n", __LINE__, mdb_strerror(retval));
goto LABEL_EXIT;
}
mkdir("testdb", 0755);
retval = mdb_env_open(env, "testdb", 0, 0644);
if (retval != 0) {
fprintf(stderr, "ERR(%u): %s\n", __LINE__, mdb_strerror(retval));
goto LABEL_EXIT;
}
retval = test1(env);
if (retval != 0) {
goto LABEL_EXIT;
}
//mdb_env_sync(env, 1);
retval = test2(env);
if (retval != 0) {
goto LABEL_EXIT;
}
LABEL_EXIT:
mdb_env_close(env);
return retval;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.