mirror of
https://github.com/isar/libmdbx.git
synced 2024-10-30 11:29:19 +08:00
mdbx: fix tutorial/sample-mdbx.c
Fix/refine b0dc98bec0
Change-Id: Ie5b1cf9453261c5e1cdfc4880ce9dafdcc4545f7
This commit is contained in:
parent
b0dc98bec0
commit
276e3578da
2
Makefile
2
Makefile
@ -135,7 +135,7 @@ endif
|
|||||||
ci-rule = ( CC=$$(which $1); if [ -n "$$CC" ]; then \
|
ci-rule = ( CC=$$(which $1); if [ -n "$$CC" ]; then \
|
||||||
echo -n "probe by $2 ($$(readlink -f $$(which $$CC))): " && \
|
echo -n "probe by $2 ($$(readlink -f $$(which $$CC))): " && \
|
||||||
$(MAKE) clean >$1.log 2>$1.err && \
|
$(MAKE) clean >$1.log 2>$1.err && \
|
||||||
$(MAKE) CC=$$(readlink -f $$CC) XCFLAGS="-UNDEBUG -DMDBX_DEBUG=2" check 1>$1.log 2>$1.err && echo "OK" \
|
$(MAKE) CC=$$(readlink -f $$CC) XCFLAGS="-UNDEBUG -DMDBX_DEBUG=2 -DLIBMDBX_EXPORTS=1" check 1>$1.log 2>$1.err && echo "OK" \
|
||||||
|| ( echo "FAILED"; cat $1.err >&2; exit 1 ); \
|
|| ( echo "FAILED"; cat $1.err >&2; exit 1 ); \
|
||||||
else echo "no $2 ($1) for probe"; fi; )
|
else echo "no $2 ($1) for probe"; fi; )
|
||||||
ci:
|
ci:
|
||||||
|
@ -20,17 +20,18 @@
|
|||||||
|
|
||||||
#include "mdbx.h"
|
#include "mdbx.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
(void)argc;
|
(void)argc;
|
||||||
(void)argv;
|
(void)argv;
|
||||||
|
|
||||||
int rc;
|
int rc;
|
||||||
MDBX_env *env;
|
MDBX_env *env = NULL;
|
||||||
MDBX_dbi dbi;
|
MDBX_dbi dbi = 0;
|
||||||
MDBX_val key, data;
|
MDBX_val key, data;
|
||||||
MDBX_txn *txn;
|
MDBX_txn *txn = NULL;
|
||||||
MDBX_cursor *cursor;
|
MDBX_cursor *cursor = NULL;
|
||||||
char sval[32];
|
char sval[32];
|
||||||
|
|
||||||
rc = mdbx_env_create(&env);
|
rc = mdbx_env_create(&env);
|
||||||
@ -41,20 +42,19 @@ int main(int argc, char *argv[]) {
|
|||||||
rc = mdbx_env_open(env, "./example-db",
|
rc = mdbx_env_open(env, "./example-db",
|
||||||
MDBX_NOSUBDIR | MDBX_COALESCE | MDBX_LIFORECLAIM, 0664);
|
MDBX_NOSUBDIR | MDBX_COALESCE | MDBX_LIFORECLAIM, 0664);
|
||||||
if (rc != MDBX_SUCCESS) {
|
if (rc != MDBX_SUCCESS) {
|
||||||
mdbx_env_close(env);
|
|
||||||
fprintf(stderr, "mdbx_env_open: (%d) %s\n", rc, mdbx_strerror(rc));
|
fprintf(stderr, "mdbx_env_open: (%d) %s\n", rc, mdbx_strerror(rc));
|
||||||
return 0;
|
goto bailout;
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = mdbx_txn_begin(env, NULL, 0, &txn);
|
rc = mdbx_txn_begin(env, NULL, 0, &txn);
|
||||||
if (rc != MDBX_SUCCESS) {
|
if (rc != MDBX_SUCCESS) {
|
||||||
fprintf(stderr, "mdbx_txn_begin: (%d) %s\n", rc, mdbx_strerror(rc));
|
fprintf(stderr, "mdbx_txn_begin: (%d) %s\n", rc, mdbx_strerror(rc));
|
||||||
goto leave;
|
goto bailout;
|
||||||
}
|
}
|
||||||
rc = mdbx_dbi_open(txn, NULL, 0, &dbi);
|
rc = mdbx_dbi_open(txn, NULL, 0, &dbi);
|
||||||
if (rc != MDBX_SUCCESS) {
|
if (rc != MDBX_SUCCESS) {
|
||||||
fprintf(stderr, "mdbx_dbi_open: (%d) %s\n", rc, mdbx_strerror(rc));
|
fprintf(stderr, "mdbx_dbi_open: (%d) %s\n", rc, mdbx_strerror(rc));
|
||||||
goto leave;
|
goto bailout;
|
||||||
}
|
}
|
||||||
|
|
||||||
key.iov_len = sizeof(int);
|
key.iov_len = sizeof(int);
|
||||||
@ -66,24 +66,47 @@ int main(int argc, char *argv[]) {
|
|||||||
rc = mdbx_put(txn, dbi, &key, &data, 0);
|
rc = mdbx_put(txn, dbi, &key, &data, 0);
|
||||||
if (rc != MDBX_SUCCESS) {
|
if (rc != MDBX_SUCCESS) {
|
||||||
fprintf(stderr, "mdbx_put: (%d) %s\n", rc, mdbx_strerror(rc));
|
fprintf(stderr, "mdbx_put: (%d) %s\n", rc, mdbx_strerror(rc));
|
||||||
goto leave;
|
goto bailout;
|
||||||
}
|
}
|
||||||
rc = mdbx_txn_commit(txn);
|
rc = mdbx_txn_commit(txn);
|
||||||
if (rc) {
|
if (rc) {
|
||||||
fprintf(stderr, "mdbx_txn_commit: (%d) %s\n", rc, mdbx_strerror(rc));
|
fprintf(stderr, "mdbx_txn_commit: (%d) %s\n", rc, mdbx_strerror(rc));
|
||||||
goto leave;
|
goto bailout;
|
||||||
}
|
}
|
||||||
|
txn = NULL;
|
||||||
|
|
||||||
rc = mdbx_txn_begin(env, NULL, MDBX_RDONLY, &txn);
|
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);
|
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) {
|
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,
|
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 *)key.iov_base, data.iov_base, (int)data.iov_len,
|
||||||
(char *)data.iov_base);
|
(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);
|
mdbx_cursor_close(cursor);
|
||||||
|
if (txn)
|
||||||
mdbx_txn_abort(txn);
|
mdbx_txn_abort(txn);
|
||||||
leave:
|
if (dbi)
|
||||||
mdbx_dbi_close(env, dbi);
|
mdbx_dbi_close(env, dbi);
|
||||||
|
if (env)
|
||||||
mdbx_env_close(env);
|
mdbx_env_close(env);
|
||||||
return 0;
|
return (rc != MDBX_SUCCESS) ? EXIT_FAILURE : EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user