summaryrefslogtreecommitdiff
path: root/libs/libmdbx/src/example
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2019-12-06 17:15:42 +0300
committerGeorge Hazan <ghazan@miranda.im>2019-12-06 17:15:42 +0300
commit5077fc56fbde196cdf821e044b607d39a34ce258 (patch)
tree9d46e9d8ddc22696e48d5073ef046ce3aebae85f /libs/libmdbx/src/example
parent3bb16e798cb51d5764aacbefd4edf26f52d8c4f0 (diff)
libmdbx: upgrade to 0.4.0
Diffstat (limited to 'libs/libmdbx/src/example')
-rw-r--r--libs/libmdbx/src/example/CMakeLists.txt6
-rw-r--r--libs/libmdbx/src/example/README.md1
-rw-r--r--libs/libmdbx/src/example/example-mdbx.c112
-rw-r--r--libs/libmdbx/src/example/sample-bdb.txt77
4 files changed, 196 insertions, 0 deletions
diff --git a/libs/libmdbx/src/example/CMakeLists.txt b/libs/libmdbx/src/example/CMakeLists.txt
new file mode 100644
index 0000000000..d3e56e82e9
--- /dev/null
+++ b/libs/libmdbx/src/example/CMakeLists.txt
@@ -0,0 +1,6 @@
+set(TARGET mdbx_example)
+project(${TARGET})
+
+add_executable(${TARGET} example-mdbx.c)
+
+target_link_libraries(${TARGET} mdbx)
diff --git a/libs/libmdbx/src/example/README.md b/libs/libmdbx/src/example/README.md
new file mode 100644
index 0000000000..b819cf4a40
--- /dev/null
+++ b/libs/libmdbx/src/example/README.md
@@ -0,0 +1 @@
+See [example-mdbx.c](example-mdbx.c) as an example of using _libmdbx_, and do a line-by-line comparison of it with the [sample-bdb.txt](sample-bdb.txt) file.
diff --git a/libs/libmdbx/src/example/example-mdbx.c b/libs/libmdbx/src/example/example-mdbx.c
new file mode 100644
index 0000000000..1d25ef6fc8
--- /dev/null
+++ b/libs/libmdbx/src/example/example-mdbx.c
@@ -0,0 +1,112 @@
+/* MDBX usage examle
+ *
+ * Do a line-by-line comparison of this and sample-bdb.txt
+ */
+
+/*
+ * Copyright 2015-2019 Leonid Yuriev <leo@yuriev.ru>.
+ * Copyright 2017 Ilya Shipitsin <chipitsine@gmail.com>.
+ * 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
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+
+#include "mdbx.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(int argc, char *argv[]) {
+ (void)argc;
+ (void)argv;
+
+ int rc;
+ MDBX_env *env = NULL;
+ MDBX_dbi dbi = 0;
+ MDBX_val key, data;
+ MDBX_txn *txn = NULL;
+ MDBX_cursor *cursor = NULL;
+ char sval[32];
+
+ rc = mdbx_env_create(&env);
+ if (rc != MDBX_SUCCESS) {
+ fprintf(stderr, "mdbx_env_create: (%d) %s\n", rc, mdbx_strerror(rc));
+ goto bailout;
+ }
+ rc = mdbx_env_open(env, "./example-db",
+ MDBX_NOSUBDIR | MDBX_COALESCE | MDBX_LIFORECLAIM, 0664);
+ if (rc != MDBX_SUCCESS) {
+ fprintf(stderr, "mdbx_env_open: (%d) %s\n", rc, mdbx_strerror(rc));
+ goto bailout;
+ }
+
+ 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 bailout;
+ }
+ 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 bailout;
+ }
+
+ 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 bailout;
+ }
+ rc = mdbx_txn_commit(txn);
+ if (rc) {
+ fprintf(stderr, "mdbx_txn_commit: (%d) %s\n", rc, mdbx_strerror(rc));
+ goto bailout;
+ }
+ txn = NULL;
+
+ rc = mdbx_txn_begin(env, NULL, MDBX_RDONLY, &txn);
+ if (rc != MDBX_SUCCESS) {
+ fprintf(stderr, "mdbx_txn_begin: (%d) %s\n", rc, mdbx_strerror(rc));
+ goto bailout;
+ }
+ rc = mdbx_cursor_open(txn, dbi, &cursor);
+ if (rc != MDBX_SUCCESS) {
+ fprintf(stderr, "mdbx_cursor_open: (%d) %s\n", rc, mdbx_strerror(rc));
+ goto bailout;
+ }
+
+ int found = 0;
+ 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);
+ found += 1;
+ }
+ if (rc != MDBX_NOTFOUND || found == 0) {
+ fprintf(stderr, "mdbx_cursor_get: (%d) %s\n", rc, mdbx_strerror(rc));
+ goto bailout;
+ } else {
+ rc = MDBX_SUCCESS;
+ }
+bailout:
+ if (cursor)
+ mdbx_cursor_close(cursor);
+ if (txn)
+ mdbx_txn_abort(txn);
+ if (dbi)
+ mdbx_dbi_close(env, dbi);
+ if (env)
+ mdbx_env_close(env);
+ return (rc != MDBX_SUCCESS) ? EXIT_FAILURE : EXIT_SUCCESS;
+}
diff --git a/libs/libmdbx/src/example/sample-bdb.txt b/libs/libmdbx/src/example/sample-bdb.txt
new file mode 100644
index 0000000000..5c89540afa
--- /dev/null
+++ b/libs/libmdbx/src/example/sample-bdb.txt
@@ -0,0 +1,77 @@
+/* BerkeleyDB toy/sample
+ *
+ * Do a line-by-line comparison of this and example-mdbx.c
+ */
+
+/*
+ * Copyright 2015-2019 Leonid Yuriev <leo@yuriev.ru>.
+ * 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
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <db.h>
+
+int main(int argc,char * argv[])
+{
+ int rc;
+ DB_ENV *env;
+ DB *dbi;
+ DBT key, data;
+ DB_TXN *txn;
+ DBC *cursor;
+ char sval[32], kval[32];
+
+ /* Note: Most error checking omitted for simplicity */
+
+#define FLAGS (DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_TXN|DB_INIT_MPOOL|DB_CREATE|DB_THREAD)
+ rc = db_env_create(&env, 0);
+ rc = env->open(env, "./testdb", FLAGS, 0664);
+ rc = db_create(&dbi, env, 0);
+ rc = env->txn_begin(env, NULL, &txn, 0);
+ rc = dbi->open(dbi, txn, "test.bdb", NULL, DB_BTREE, DB_CREATE, 0664);
+
+ memset(&key, 0, sizeof(DBT));
+ memset(&data, 0, sizeof(DBT));
+ key.size = sizeof(int);
+ key.data = sval;
+ data.size = sizeof(sval);
+ data.data = sval;
+
+ sprintf(sval, "%03x %d foo bar", 32, 3141592);
+ rc = dbi->put(dbi, txn, &key, &data, 0);
+ rc = txn->commit(txn, 0);
+ if (rc) {
+ fprintf(stderr, "txn->commit: (%d) %s\n", rc, db_strerror(rc));
+ goto leave;
+ }
+ rc = env->txn_begin(env, NULL, &txn, 0);
+ rc = dbi->cursor(dbi, txn, &cursor, 0);
+ key.flags = DB_DBT_USERMEM;
+ key.data = kval;
+ key.ulen = sizeof(kval);
+ data.flags = DB_DBT_USERMEM;
+ data.data = sval;
+ data.ulen = sizeof(sval);
+ while ((rc = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) {
+ printf("key: %p %.*s, data: %p %.*s\n",
+ key.data, (int) key.size, (char *) key.data,
+ data.data, (int) data.size, (char *) data.data);
+ }
+ rc = cursor->c_close(cursor);
+ rc = txn->abort(txn);
+leave:
+ rc = dbi->close(dbi, 0);
+ rc = env->close(env, 0);
+ return rc;
+}