Skip to content

Instantly share code, notes, and snippets.

@hirokuma
Created November 18, 2017 12:50
Show Gist options
  • Save hirokuma/98b5010f4770267ab730efd3422be982 to your computer and use it in GitHub Desktop.
Save hirokuma/98b5010f4770267ab730efd3422be982 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <assert.h>
#include "lmdb.h"
#define DB_AAA "aaa"
#define DB_BBB "bbb"
static int code = 0;
static MDB_env *env;
static void db_create(void)
{
int rc;
rc = mdb_env_create(&env);
if (rc) {
fprintf(stderr, "fail: mdb_env_create(%d)=%d(%s)\n", code, rc, mdb_strerror(rc));
assert(0);
}
rc = mdb_env_set_maxdbs(env, 1);
if (rc) {
fprintf(stderr, "fail: mdb_env_set_maxdbs(%d)=%d(%s)\n", code, rc, mdb_strerror(rc));
assert(0);
}
mkdir("testdb", 0755);
rc = mdb_env_open(env, "./testdb", 0, 0644);
if (rc) {
fprintf(stderr, "fail: mdb_env_open(%d)=%d(%s)\n", code, rc, mdb_strerror(rc));
assert(0);
}
}
static void db_close(void)
{
mdb_env_close(env);
}
static void db_test1(void)
{
int rc;
MDB_txn *txn;
MDB_dbi dbi;
MDB_val key, data;
printf("%s\n", __func__);
rc = mdb_txn_begin(env, NULL, 0, &txn);
if (rc) {
fprintf(stderr, "fail: mdb_txn_begin(%d)=%d(%s)\n", code, rc, mdb_strerror(rc));
assert(0);
}
rc = mdb_dbi_open(txn, DB_AAA, MDB_CREATE, &dbi);
if (rc) {
fprintf(stderr, "fail: mdb_dbi_open(%d)=%d(%s)\n", code, rc, mdb_strerror(rc));
assert(0);
}
key.mv_data = "aaaaa";
key.mv_size = 5;
data.mv_data = "123";
data.mv_size = 3;
rc = mdb_put(txn, dbi, &key, &data, 0);
if (rc) {
fprintf(stderr, "fail: mdb_put(%d)=%d(%s)\n", code, rc, mdb_strerror(rc));
assert(0);
}
//mdb_dbi_close(env, dbi);
//rc = mdb_dbi_open(txn, DB_BBB, MDB_CREATE, &dbi);
//if (rc) {
// fprintf(stderr, "fail: mdb_dbi_open(%d)=%d(%s)\n", code, rc, mdb_strerror(rc));
// assert(0);
//}
//key.mv_data = "bbbbb";
//key.mv_size = 5;
//data.mv_data = "456";
//data.mv_size = 3;
//rc = mdb_put(txn, dbi, &key, &data, 0);
//if (rc) {
// fprintf(stderr, "fail: mdb_put(%d)=%d(%s)\n", code, rc, mdb_strerror(rc));
// assert(0);
//}
//mdb_dbi_close(env, dbi);
mdb_txn_commit(txn);
}
static void db_test2(void)
{
int rc;
MDB_txn *txn;
MDB_dbi dbi;
printf("%s\n", __func__);
rc = mdb_txn_begin(env, NULL, 0, &txn);
if (rc) {
fprintf(stderr, "fail: mdb_txn_begin(%d)=%d(%s)\n", code, rc, mdb_strerror(rc));
assert(0);
}
rc = mdb_dbi_open(txn, DB_AAA, 0, &dbi);
if (rc) {
fprintf(stderr, "fail: mdb_dbi_open(%d)=%d(%s)\n", code, rc, mdb_strerror(rc));
assert(0);
}
rc = mdb_drop(txn, dbi, 1);
if (rc) {
fprintf(stderr, "fail: mdb_drop(%d)=%d(%s)\n", code, rc, mdb_strerror(rc));
assert(0);
}
mdb_dbi_close(env, dbi);
mdb_txn_commit(txn);
}
int main(void)
{
db_create();
db_test1();
//db_test2();
db_close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment