From b0dc98bec0281cc0987253d9e498cdb117a80100 Mon Sep 17 00:00:00 2001 From: dreamsxin Date: Wed, 16 Aug 2017 03:53:06 +0300 Subject: [PATCH] mdbx: add `tutorial/sample-mdbx.c` and drop obsolete `sample-mdb.txt`. Also this is a testcase for https://github.com/leo-yuriev/libmdbx/issues/11. Change-Id: I95c876b1120c1479b2e740b43f6c60e2ac8b33c1 --- AUTHORS | 1 + Makefile | 5 ++- libmdbx.files | 1 + tutorial/sample-mdb.txt | 66 ------------------------------ tutorial/sample-mdbx.c | 89 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 67 deletions(-) delete mode 100644 tutorial/sample-mdb.txt create mode 100644 tutorial/sample-mdbx.c diff --git a/AUTHORS b/AUTHORS index 0c7ca23f..a2d80146 100644 --- a/AUTHORS +++ b/AUTHORS @@ -6,6 +6,7 @@ Chris Mikkelson Claude Brisson David Barbour David Wilson +dreamsxin Hallvard Furuseth , Heiko Becker Howard Chu , diff --git a/Makefile b/Makefile index 95be9c0d..dccc656a 100644 --- a/Makefile +++ b/Makefile @@ -53,10 +53,13 @@ TEST_OBJ := $(patsubst %.cc,%.o,$(TEST_SRC)) .PHONY: mdbx all install clean check coverage -all: $(LIBRARIES) $(TOOLS) test/test +all: $(LIBRARIES) $(TOOLS) test/test example mdbx: libmdbx.a libmdbx.so +example: mdbx.h tutorial/sample-mdbx.c libmdbx.so + $(CC) $(CFLAGS) -I. tutorial/sample-mdbx.c ./libmdbx.so -o example + tools: $(TOOLS) install: $(LIBRARIES) $(TOOLS) $(HEADERS) diff --git a/libmdbx.files b/libmdbx.files index 153bfe32..a07ea48d 100644 --- a/libmdbx.files +++ b/libmdbx.files @@ -1,3 +1,4 @@ +tutorial/sample-mdbx.c AUTHORS LICENSE Makefile diff --git a/tutorial/sample-mdb.txt b/tutorial/sample-mdb.txt deleted file mode 100644 index 54b56f61..00000000 --- a/tutorial/sample-mdb.txt +++ /dev/null @@ -1,66 +0,0 @@ -/* sample-mdb.txt - MDB toy/sample - * - * Do a line-by-line comparison of this and sample-bdb.txt - */ - -/* - * Copyright 2015-2017 Leonid Yuriev . - * Copyright 2012-2015 Howard Chu, Symas Corp. - * Copyright 2015,2016 Peter-Service R&D LLC. - * 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 "mdbx.h" - -int main(int argc,char * argv[]) -{ - int rc; - MDBX_env *env; - MDBX_dbi dbi; - MDBX_val key, data; - MDBX_txn *txn; - MDBX_cursor *cursor; - char sval[32]; - - /* Note: Most error checking omitted for simplicity */ - - rc = mdbx_env_create(&env); - rc = mdbx_env_open(env, "./testdb", 0, 0664); - rc = mdbx_txn_begin(env, NULL, 0, &txn); - rc = mdbx_dbi_open(txn, NULL, 0, &dbi); - - key.iov_len = sizeof(int); - key.iov_base = sval; - data.iov_len = sizeof(sval); - data.iov_base = sval; - - sprintf(sval, "%03x %d foo bar", 32, 3141592); - rc = mdbx_put(txn, dbi, &key, &data, 0); - rc = mdbx_txn_commit(txn); - if (rc) { - fprintf(stderr, "mdbx_txn_commit: (%d) %s\n", rc, mdbx_strerror(rc)); - goto leave; - } - rc = mdbx_txn_begin(env, NULL, MDBX_RDONLY, &txn); - rc = mdbx_cursor_open(txn, dbi, &cursor); - while ((rc = mdbx_cursor_get(cursor, &key, &data, MDBX_NEXT)) == 0) { - printf("key: %p %.*s, data: %p %.*s\n", - key.iov_base, (int) key.iov_len, (char *) key.iov_base, - data.iov_base, (int) data.iov_len, (char *) data.iov_base); - } - mdbx_cursor_close(cursor); - mdbx_txn_abort(txn); -leave: - mdbx_dbi_close(env, dbi); - mdbx_env_close(env); - return 0; -} diff --git a/tutorial/sample-mdbx.c b/tutorial/sample-mdbx.c new file mode 100644 index 00000000..07d5b7dd --- /dev/null +++ b/tutorial/sample-mdbx.c @@ -0,0 +1,89 @@ +/* sample-mdb.txt - MDB toy/sample + * + * Do a line-by-line comparison of this and sample-bdb.txt + */ + +/* + * Copyright 2017 Ilya Shipitsin . + * Copyright 2015-2017 Leonid Yuriev . + * Copyright 2012-2015 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 "mdbx.h" +#include + +int main(int argc, char *argv[]) { + (void)argc; + (void)argv; + + int rc; + MDBX_env *env; + MDBX_dbi dbi; + MDBX_val key, data; + MDBX_txn *txn; + MDBX_cursor *cursor; + char sval[32]; + + rc = mdbx_env_create(&env); + if (rc != MDBX_SUCCESS) { + fprintf(stderr, "mdbx_env_create: (%d) %s\n", rc, mdbx_strerror(rc)); + return 0; + } + rc = mdbx_env_open(env, "./example-db", + MDBX_NOSUBDIR | MDBX_COALESCE | MDBX_LIFORECLAIM, 0664); + if (rc != MDBX_SUCCESS) { + mdbx_env_close(env); + fprintf(stderr, "mdbx_env_open: (%d) %s\n", rc, mdbx_strerror(rc)); + return 0; + } + + rc = mdbx_txn_begin(env, NULL, 0, &txn); + if (rc != MDBX_SUCCESS) { + fprintf(stderr, "mdbx_txn_begin: (%d) %s\n", rc, mdbx_strerror(rc)); + goto leave; + } + rc = mdbx_dbi_open(txn, NULL, 0, &dbi); + if (rc != MDBX_SUCCESS) { + fprintf(stderr, "mdbx_dbi_open: (%d) %s\n", rc, mdbx_strerror(rc)); + goto leave; + } + + key.iov_len = sizeof(int); + key.iov_base = sval; + data.iov_len = sizeof(sval); + data.iov_base = sval; + + sprintf(sval, "%03x %d foo bar", 32, 3141592); + rc = mdbx_put(txn, dbi, &key, &data, 0); + if (rc != MDBX_SUCCESS) { + fprintf(stderr, "mdbx_put: (%d) %s\n", rc, mdbx_strerror(rc)); + goto leave; + } + rc = mdbx_txn_commit(txn); + if (rc) { + fprintf(stderr, "mdbx_txn_commit: (%d) %s\n", rc, mdbx_strerror(rc)); + goto leave; + } + rc = mdbx_txn_begin(env, NULL, MDBX_RDONLY, &txn); + rc = mdbx_cursor_open(txn, dbi, &cursor); + while ((rc = mdbx_cursor_get(cursor, &key, &data, MDBX_NEXT)) == 0) { + printf("key: %p %.*s, data: %p %.*s\n", key.iov_base, (int)key.iov_len, + (char *)key.iov_base, data.iov_base, (int)data.iov_len, + (char *)data.iov_base); + } + mdbx_cursor_close(cursor); + mdbx_txn_abort(txn); +leave: + mdbx_dbi_close(env, dbi); + mdbx_env_close(env); + return 0; +}