Skip to content

Instantly share code, notes, and snippets.

@berkeley-db
Created January 23, 2011 19:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save berkeley-db/792346 to your computer and use it in GitHub Desktop.
Save berkeley-db/792346 to your computer and use it in GitHub Desktop.
#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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment