libmdbx/tutorial/sample-mdb.txt

67 lines
1.8 KiB
Plaintext
Raw Normal View History

2015-01-07 21:49:50 +08:00
/* sample-mdb.txt - MDB toy/sample
*
* Do a line-by-line comparison of this and sample-bdb.txt
*/
2015-01-07 21:49:50 +08:00
/*
* Copyright 2015-2017 Leonid Yuriev <leo@yuriev.ru>.
* Copyright 2012-2015 Howard Chu, Symas Corp.
* Copyright 2015,2016 Peter-Service R&D LLC.
2015-01-07 21:49:50 +08:00
* 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>.
*/
2015-01-07 21:49:50 +08:00
#include <stdio.h>
2017-05-24 06:42:10 +08:00
#include "mdbx.h"
2015-01-07 21:49:50 +08:00
int main(int argc,char * argv[])
{
int rc;
2017-05-24 06:42:10 +08:00
MDBX_env *env;
MDBX_dbi dbi;
2017-05-24 02:05:54 +08:00
MDBX_val key, data;
2017-05-24 02:36:09 +08:00
MDBX_txn *txn;
2017-05-24 06:42:10 +08:00
MDBX_cursor *cursor;
2015-01-07 21:49:50 +08:00
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);
2015-01-07 21:49:50 +08:00
2017-05-24 02:05:54 +08:00
key.iov_len = sizeof(int);
key.iov_base = sval;
data.iov_len = sizeof(sval);
data.iov_base = sval;
2015-01-07 21:49:50 +08:00
sprintf(sval, "%03x %d foo bar", 32, 3141592);
rc = mdbx_put(txn, dbi, &key, &data, 0);
rc = mdbx_txn_commit(txn);
2015-01-07 21:49:50 +08:00
if (rc) {
fprintf(stderr, "mdbx_txn_commit: (%d) %s\n", rc, mdbx_strerror(rc));
2015-01-07 21:49:50 +08:00
goto leave;
}
2017-05-24 06:42:10 +08:00
rc = mdbx_txn_begin(env, NULL, MDBX_RDONLY, &txn);
rc = mdbx_cursor_open(txn, dbi, &cursor);
2017-05-24 06:42:10 +08:00
while ((rc = mdbx_cursor_get(cursor, &key, &data, MDBX_NEXT)) == 0) {
2015-01-07 21:49:50 +08:00
printf("key: %p %.*s, data: %p %.*s\n",
2017-05-24 02:05:54 +08:00
key.iov_base, (int) key.iov_len, (char *) key.iov_base,
data.iov_base, (int) data.iov_len, (char *) data.iov_base);
2015-01-07 21:49:50 +08:00
}
mdbx_cursor_close(cursor);
mdbx_txn_abort(txn);
2015-01-07 21:49:50 +08:00
leave:
mdbx_dbi_close(env, dbi);
mdbx_env_close(env);
2015-01-07 21:49:50 +08:00
return 0;
}