]> git.sur5r.net Git - openldap/blob - libraries/liblmdb/sample-mdb.c
Merge remote-tracking branch 'origin/mdb.master' into OPENLDAP_REL_ENG_2_5
[openldap] / libraries / liblmdb / sample-mdb.c
1 /* sample-mdb.c - MDB toy/sample
2  *
3  * Do a line-by-line comparison of this and sample-bdb.c
4  */
5 /*
6  * Copyright 2012 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
20 int 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         rc = mdb_env_create(&env);
31         rc = mdb_env_open(env, "./testdb", 0, 0664);
32         rc = mdb_txn_begin(env, NULL, 0, &txn);
33         rc = mdb_open(txn, NULL, 0, &dbi);
34
35         key.mv_size = sizeof(int);
36         key.mv_data = sval;
37         data.mv_size = sizeof(sval);
38         data.mv_data = sval;
39
40         sprintf(sval, "%03x %d foo bar", 32, 3141592);
41         rc = mdb_put(txn, dbi, &key, &data, 0);
42         rc = mdb_txn_commit(txn);
43         if (rc) {
44                 fprintf(stderr, "mdb_txn_commit: (%d) %s\n", rc, mdb_strerror(rc));
45                 goto leave;
46         }
47         rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
48         rc = mdb_cursor_open(txn, dbi, &cursor);
49         while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
50                 printf("key: %p %.*s, data: %p %.*s\n",
51                         key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
52                         data.mv_data, (int) data.mv_size, (char *) data.mv_data);
53         }
54         mdb_cursor_close(cursor);
55         mdb_txn_abort(txn);
56 leave:
57         mdb_close(env, dbi);
58         mdb_env_close(env);
59         return 0;
60 }