public
Last active

  • Download Gist
db-stl-example.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include "db_map.h" // Berkeley DB's STL Map support
 
using namespace dbstl;
 
main()
{
std::string key = "hello";
int value = 1234;
Db *dbp = NULL;
DbEnv *envp = NULL;
typedef db_multimap<char *, int, ElementHolder<int> > strmap_t;
 
envp = new DbEnv( ... );
dbp = new Db(envp, ... );
 
dbstl::dbstl_startup();
dbstl::register_db(dbp);
dbstl::register_db_env(envp);
 
strmap_t *strmap = new strmap_t(dbp, envp);
 
txn = dbstl::begin_txn(0, envp);
 
strmap->insert(key, value);
 
try {
dbstl::commit_txn(envp);
} catch (DbException &e) {
std::cout << "Error on txn commit: " << e.what() << std::endl;
dbstl::abort_txn(envp);
return FAILURE;
}
dbstl::commit_txn(envp);
 
return SUCCESS;
}
 
int
count_records(strmap_t *strmap)
{
int count = 0;
strmap_t::iterator itr;
try {
for (itr = strmap->begin(); itr != strmap->end(); ++itr)
count++;
} catch (DbDeadlockException &de) {
std::cerr << "countRecords: deadlock" << std::endl;
throw de;
} catch (DbException &e) {
std::cerr << "countRecords: error" << std::endl;
std::cerr << e.what() << std::endl;
}
// itr's DB cursor is closed when the itr is destructed
 
return (count);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.