1 /* sample-bdb.txt - BerkeleyDB toy/sample
3 * Do a line-by-line comparison of this and sample-mdb.txt
6 * Copyright 2012-2015 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 /* Note: Most error checking omitted for simplicity */
33 #define FLAGS (DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_TXN|DB_INIT_MPOOL|DB_CREATE|DB_THREAD)
34 rc = db_env_create(&env, 0);
35 rc = env->open(env, "./testdb", FLAGS, 0664);
36 rc = db_create(&dbi, env, 0);
37 rc = env->txn_begin(env, NULL, &txn, 0);
38 rc = dbi->open(dbi, txn, "test.bdb", NULL, DB_BTREE, DB_CREATE, 0664);
40 memset(&key, 0, sizeof(DBT));
41 memset(&data, 0, sizeof(DBT));
42 key.size = sizeof(int);
44 data.size = sizeof(sval);
47 sprintf(sval, "%03x %d foo bar", 32, 3141592);
48 rc = dbi->put(dbi, txn, &key, &data, 0);
49 rc = txn->commit(txn, 0);
51 fprintf(stderr, "txn->commit: (%d) %s\n", rc, db_strerror(rc));
54 rc = env->txn_begin(env, NULL, &txn, 0);
55 rc = dbi->cursor(dbi, txn, &cursor, 0);
56 key.flags = DB_DBT_USERMEM;
58 key.ulen = sizeof(kval);
59 data.flags = DB_DBT_USERMEM;
61 data.ulen = sizeof(sval);
62 while ((rc = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) {
63 printf("key: %p %.*s, data: %p %.*s\n",
64 key.data, (int) key.size, (char *) key.data,
65 data.data, (int) data.size, (char *) data.data);
67 rc = cursor->c_close(cursor);
70 rc = dbi->close(dbi, 0);
71 rc = env->close(env, 0);