xref: /netbsd-src/external/bsd/openldap/dist/libraries/liblmdb/sample-mdb.txt (revision 154bfe8e089c1a0a4e9ed8414f08d3da90949162)
1/* sample-mdb.txt - MDB toy/sample
2 *
3 * Do a line-by-line comparison of this and sample-bdb.txt
4 */
5/*
6 * Copyright 2012-2020 Howard Chu, Symas Corp.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
16 */
17#include <stdio.h>
18#include "lmdb.h"
19
20int main(int argc,char * argv[])
21{
22	int rc;
23	MDB_env *env;
24	MDB_dbi dbi;
25	MDB_val key, data;
26	MDB_txn *txn;
27	MDB_cursor *cursor;
28	char sval[32];
29
30	/* Note: Most error checking omitted for simplicity */
31
32	rc = mdb_env_create(&env);
33	rc = mdb_env_open(env, "./testdb", 0, 0664);
34	rc = mdb_txn_begin(env, NULL, 0, &txn);
35	rc = mdb_dbi_open(txn, NULL, 0, &dbi);
36
37	key.mv_size = sizeof(int);
38	key.mv_data = sval;
39	data.mv_size = sizeof(sval);
40	data.mv_data = sval;
41
42	sprintf(sval, "%03x %d foo bar", 32, 3141592);
43	rc = mdb_put(txn, dbi, &key, &data, 0);
44	rc = mdb_txn_commit(txn);
45	if (rc) {
46		fprintf(stderr, "mdb_txn_commit: (%d) %s\n", rc, mdb_strerror(rc));
47		goto leave;
48	}
49	rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
50	rc = mdb_cursor_open(txn, dbi, &cursor);
51	while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
52		printf("key: %p %.*s, data: %p %.*s\n",
53			key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
54			data.mv_data, (int) data.mv_size, (char *) data.mv_data);
55	}
56	mdb_cursor_close(cursor);
57	mdb_txn_abort(txn);
58leave:
59	mdb_dbi_close(env, dbi);
60	mdb_env_close(env);
61	return 0;
62}
63