From 1701892d066c626bbd82c9340d6b442f733c33a1 Mon Sep 17 00:00:00 2001 From: Kristian Amlie Date: Mon, 28 Jul 2014 11:28:38 +0200 Subject: [PATCH 1/4] Add extra tools and CFEngine modifications for LMDB. --- libraries/liblmdb/lmdump.c | 128 +++++++++++++++++++++++++++ libraries/liblmdb/lmmgr.c | 201 +++++++++++++++++++++++++++++++++++++++++++ libraries/liblmdb/mdb_copy.c | 2 +- 3 files changed, 330 insertions(+), 1 deletion(-) create mode 100644 libraries/liblmdb/lmdump.c create mode 100644 libraries/liblmdb/lmmgr.c diff --git a/libraries/liblmdb/lmdump.c b/libraries/liblmdb/lmdump.c new file mode 100644 index 000000000000..0e3ee4bb61c2 --- /dev/null +++ b/libraries/liblmdb/lmdump.c @@ -0,0 +1,128 @@ +/* lmdump.c - Lmdb database dumper + Has three modes : + -a : print keys in ascii form + -A : print keys and values in ascii form + -x : print keys and values in hexadecimal form + -d : print only the size of keys and values + */ + +#define _XOPEN_SOURCE 500 /* srandom(), random() */ +#include +#include +#include +#include +#include "lmdb.h" + +static void print_hex(char *s, int len) +{ + int i = 0; + for (i=0; i +#include +#include "lmdb.h" + +static int report_error(int rc) +{ + printf("err(%d): %s\n", rc, mdb_strerror(rc)); + return rc; +} + +int openconn(const char *dbfile, MDB_env **env, long int maxreaders) +{ +/*init*/ + int rc; + rc = mdb_env_create(env); + if(rc) return report_error(rc); + + rc = mdb_env_open(*env, dbfile, MDB_NOSUBDIR, 0644); + if(rc) return report_error(rc); + return 0; +} + +int gettxn(MDB_env *env, MDB_txn **txn, MDB_dbi *dbi) +{ + int rc; +/*setup txn*/ + rc = mdb_txn_begin(env, NULL, 0, txn); + if(rc) return report_error(rc); + rc = mdb_open(*txn, NULL, 0, dbi); + if(rc) return report_error(rc); + return 0; +} + +int committxn(MDB_txn *txn) +{ +/*commit*/ + int rc; + rc = mdb_txn_commit(txn); + return 0; +} + +int aborttxn(MDB_txn *txn) +{ +/*abort*/ + int rc; + mdb_txn_abort(txn); + return 0; +} + +int closeall(MDB_env *env, MDB_txn *txn, MDB_dbi dbi) +{ +/*end*/ + mdb_close(env, dbi); + mdb_env_close(env); + return 0; +} + +int do_put(char *dbfile, char *mykey, char *myval) +{ + MDB_env *env; + openconn(dbfile, &env, 0); + + MDB_txn *txn; + MDB_dbi dbi; + gettxn(env, &txn, &dbi); + + int rc; + MDB_val key, data; + key.mv_data = mykey; + data.mv_data = myval; + key.mv_size = strlen(mykey) + 1; + data.mv_size = strlen(myval) + 1; + + rc = mdb_put(txn, dbi, &key, &data, MDB_NOOVERWRITE); + if (rc == MDB_SUCCESS) + { + rc = mdb_txn_commit(txn); + } + else + { + mdb_txn_abort(txn); + } + + mdb_env_close(env); + return rc; +} + +int do_del(char *dbfile, char *mykey) +{ + MDB_env *env; + openconn(dbfile, &env, 0); + + MDB_txn *txn; + MDB_dbi dbi; + gettxn(env, &txn, &dbi); + + int rc; + MDB_val key; + key.mv_data = mykey; + key.mv_size = strlen(mykey) + 1; + + rc = mdb_del(txn, dbi, &key, NULL); + if (rc == MDB_SUCCESS) + { + rc = mdb_txn_commit(txn); + } + else + { + mdb_txn_abort(txn); + } + + mdb_env_close(env); + return rc; +} + +int do_init(char *dbfile, unsigned long maxr) +{ + MDB_env *env; + openconn(dbfile, &env, maxr); + mdb_env_close(env); + return 0; +} + +int do_stat(char *dbfile) +{ + int rc; + MDB_env *env; + openconn(dbfile, &env, 0); + + MDB_stat stat; + MDB_envinfo info; + + rc = mdb_env_stat(env, &stat); + rc = mdb_env_info(env, &info); + printf("me_maxreaders=%ld\n", info.me_maxreaders); + mdb_env_close(env); + return 0; +} + +int main(int argc, char *argv[]) +{ + if (argc < 3) + { + goto fail; + } + + if (!strcmp(argv[1], "put")) + { + if (argc == 5) + { + int rc = do_put(argv[2], argv[3], argv[4]); + return rc; + } + } + else if (!strcmp(argv[1], "del")) + { + if (argc == 4) + { + int rc = do_del(argv[2], argv[3]); + return rc; + } + } + else if (!strcmp(argv[1], "init")) + { + if (argc == 3) + { + int rc = do_init(argv[2], 0L); + return rc; + } + else if (argc == 5 && !strcmp(argv[3], "-m")) + { + int rc = do_init(argv[2], atol(argv[4])); + do_stat(argv[2]); + return rc; + } + } + else if (!strcmp(argv[1], "maxr")) + { + if (argc == 3) + { + int rc = do_stat(argv[2]); + return rc; + } + } + +fail: + printf("Usage :\n"); + printf("Add a key and value to a DB file :\n"); + printf("\tlmmgr put dbfile key value\n"); + printf("Remove a key from a DB file :\n"); + printf("\tlmmgr del dbfile key\n"); + printf("Set a new maxreaders value of a DB file :\n"); + printf("\tlmmgr init dbfile [-m maxreaders]\n"); + printf("Give maxreaders value of a DB file :\n"); + printf("\tlmmgr maxr dbfile\n"); + + return 1; +} diff --git a/libraries/liblmdb/mdb_copy.c b/libraries/liblmdb/mdb_copy.c index f37ccbcc213e..bbe43bf601ae 100644 --- a/libraries/liblmdb/mdb_copy.c +++ b/libraries/liblmdb/mdb_copy.c @@ -32,7 +32,7 @@ int main(int argc,char * argv[]) int rc; MDB_env *env; const char *progname = argv[0], *act; - unsigned flags = MDB_RDONLY; + unsigned flags = MDB_NOSUBDIR|MDB_RDONLY; unsigned cpflags = 0; for (; argc > 1 && argv[1][0] == '-'; argc--, argv++) { -- 2.7.0