Guest User

Untitled

a guest
Jul 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #include "db_map.h" // Berkeley DB's STL Map support
  2.  
  3. using namespace dbstl;
  4.  
  5. main()
  6. {
  7. std::string key = "hello";
  8. int value = 1234;
  9. Db *dbp = NULL;
  10. DbEnv *envp = NULL;
  11. typedef db_multimap<char *, int, ElementHolder<int> > strmap_t;
  12.  
  13. envp = new DbEnv( ... );
  14. dbp = new Db(envp, ... );
  15.  
  16. dbstl::dbstl_startup();
  17. dbstl::register_db(dbp);
  18. dbstl::register_db_env(envp);
  19.  
  20. strmap_t *strmap = new strmap_t(dbp, envp);
  21.  
  22. txn = dbstl::begin_txn(0, envp);
  23.  
  24. strmap->insert(key, value);
  25.  
  26. try {
  27. dbstl::commit_txn(envp);
  28. } catch (DbException &e) {
  29. std::cout << "Error on txn commit: " << e.what() << std::endl;
  30. dbstl::abort_txn(envp);
  31. return FAILURE;
  32. }
  33. dbstl::commit_txn(envp);
  34.  
  35. return SUCCESS;
  36. }
  37.  
  38. int
  39. count_records(strmap_t *strmap)
  40. {
  41. int count = 0;
  42. strmap_t::iterator itr;
  43. try {
  44. for (itr = strmap->begin(); itr != strmap->end(); ++itr)
  45. count++;
  46. } catch (DbDeadlockException &de) {
  47. std::cerr << "countRecords: deadlock" << std::endl;
  48. throw de;
  49. } catch (DbException &e) {
  50. std::cerr << "countRecords: error" << std::endl;
  51. std::cerr << e.what() << std::endl;
  52. }
  53. // itr's DB cursor is closed when the itr is destructed
  54.  
  55. return (count);
  56. }
Add Comment
Please, Sign In to add comment