sample-mdb.txt 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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-2021 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. int main(int argc,char * argv[])
  20. {
  21. int rc;
  22. MDB_env *env;
  23. MDB_dbi dbi;
  24. MDB_val key, data;
  25. MDB_txn *txn;
  26. MDB_cursor *cursor;
  27. char sval[32];
  28. /* Note: Most error checking omitted for simplicity */
  29. rc = mdb_env_create(&env);
  30. rc = mdb_env_open(env, "./testdb", 0, 0664);
  31. rc = mdb_txn_begin(env, NULL, 0, &txn);
  32. rc = mdb_dbi_open(txn, NULL, 0, &dbi);
  33. key.mv_size = sizeof(int);
  34. key.mv_data = sval;
  35. data.mv_size = sizeof(sval);
  36. data.mv_data = sval;
  37. sprintf(sval, "%03x %d foo bar", 32, 3141592);
  38. rc = mdb_put(txn, dbi, &key, &data, 0);
  39. rc = mdb_txn_commit(txn);
  40. if (rc) {
  41. fprintf(stderr, "mdb_txn_commit: (%d) %s\n", rc, mdb_strerror(rc));
  42. goto leave;
  43. }
  44. rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
  45. rc = mdb_cursor_open(txn, dbi, &cursor);
  46. while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
  47. printf("key: %p %.*s, data: %p %.*s\n",
  48. key.mv_data, (int) key.mv_size, (char *) key.mv_data,
  49. data.mv_data, (int) data.mv_size, (char *) data.mv_data);
  50. }
  51. mdb_cursor_close(cursor);
  52. mdb_txn_abort(txn);
  53. leave:
  54. mdb_dbi_close(env, dbi);
  55. mdb_env_close(env);
  56. return 0;
  57. }