1 /* sample-bdb.c - BerkeleyDB toy/sample
3 * Do a line-by-line comparison of this and sample-mdb.c
6 * Copyright 2012 Howard Chu, Symas Corp.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
21 int main(int argc,char * argv[])
29 char sval[32], kval[32];
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);
38 memset(&key, 0, sizeof(DBT));
39 memset(&data, 0, sizeof(DBT));
40 key.size = sizeof(int);
42 data.size = sizeof(sval);
45 sprintf(sval, "%03x %d foo bar", 32, 3141592);
46 rc = dbi->put(dbi, txn, &key, &data, 0);
47 rc = txn->commit(txn, 0);
49 fprintf(stderr, "txn->commit: (%d) %s\n", rc, db_strerror(rc));
52 rc = env->txn_begin(env, NULL, &txn, 0);
53 rc = dbi->cursor(dbi, txn, &cursor, 0);
54 key.flags = DB_DBT_USERMEM;
56 key.ulen = sizeof(kval);
57 data.flags = DB_DBT_USERMEM;
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);
65 rc = cursor->c_close(cursor);
68 rc = dbi->close(dbi, 0);
69 rc = env->close(env, 0);