]> git.sur5r.net Git - openldap/blob - libraries/liblmdb/sample-bdb.c
Note changes for LMDB 0.9.11
[openldap] / libraries / liblmdb / sample-bdb.c
1 /* sample-bdb.c - BerkeleyDB toy/sample
2  *
3  * Do a line-by-line comparison of this and sample-mdb.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 <string.h>
19 #include <db.h>
20
21 int main(int argc,char * argv[])
22 {
23         int rc;
24         DB_ENV *env;
25         DB *dbi;
26         DBT key, data;
27         DB_TXN *txn;
28         DBC *cursor;
29         char sval[32], kval[32];
30
31 #define FLAGS (DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_TXN|DB_INIT_MPOOL|DB_CREATE|DB_THREAD)
32         rc = db_env_create(&env, 0);
33         rc = env->open(env, "./testdb", FLAGS, 0664);
34         rc = db_create(&dbi, env, 0);
35         rc = env->txn_begin(env, NULL, &txn, 0);
36         rc = dbi->open(dbi, txn, "test.bdb", NULL, DB_BTREE, DB_CREATE, 0664);
37
38         memset(&key, 0, sizeof(DBT));
39         memset(&data, 0, sizeof(DBT));
40         key.size = sizeof(int);
41         key.data = sval;
42         data.size = sizeof(sval);
43         data.data = sval;
44
45         sprintf(sval, "%03x %d foo bar", 32, 3141592);
46         rc = dbi->put(dbi, txn, &key, &data, 0);
47         rc = txn->commit(txn, 0);
48         if (rc) {
49                 fprintf(stderr, "txn->commit: (%d) %s\n", rc, db_strerror(rc));
50                 goto leave;
51         }
52         rc = env->txn_begin(env, NULL, &txn, 0);
53         rc = dbi->cursor(dbi, txn, &cursor, 0);
54         key.flags = DB_DBT_USERMEM;
55         key.data = kval;
56         key.ulen = sizeof(kval);
57         data.flags = DB_DBT_USERMEM;
58         data.data = sval;
59         data.ulen = sizeof(sval);
60         while ((rc = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) {
61                 printf("key: %p %.*s, data: %p %.*s\n",
62                         key.data,  (int) key.size,  (char *) key.data,
63                         data.data, (int) data.size, (char *) data.data);
64         }
65         rc = cursor->c_close(cursor);
66         rc = txn->abort(txn);
67 leave:
68         rc = dbi->close(dbi, 0);
69         rc = env->close(env, 0);
70         return rc;
71 }