Simple Access Berkeley DB Using STLdb4
Listing 10. Using RAII Transactions with STLdb4
int main( int,char** )
{
Environment::setDefault(new Environment( "/tmp" ));
fh_database db = new Database(
new Transaction(), DB_BTREE, "/tmp/play.db" );
try
{
{
fh_trans tr = new Transaction();
tr->setDefaultDestructionIsAbort( true );
db->setImplicitTransaction( tr );
db["foo"] = "bar";
tr->commit();
tr = 0;
}
// (AA) RAII with transactions
// Don't use setImplicitTransaction() in this block
{
fh_trans tr = new Transaction();
tr->setDefaultDestructionIsAbort( true );
db->set( "foo", "First setting", 0, tr );
Database::iterator diter = db->find("foo",tr);
diter->second = "this is something evil";
throw exception();
tr->commit();
}
}
catch( exception& e )
{
cerr << e.what() << endl;
}
cerr << db["foo"] << endl;
return 0;
}Database environments are convenient for configuring a group of Berkeley databases that will be used together. Using database environments together with the Concurrent Data Store mode with multiple database files allows you to have multiple applications all reading and writing to the database files, and Berkeley DB takes care of locking to make sure that the files don't become corrupt.
The default database environment in STLdb4 is effectively a null environment. New database environments are created using the Environment class. The static Environment::setDefault() method can be used in applications using a single database environment to avoid having to pass the database environment object to the Database constructor.
The code shown in Listing 11 uses a database environment to protect two database files from simultaneous update by multiple running processes. First, a new database environment is created and set to use the Concurrent Data Store mode. This database environment is set to be the default STLdb4 environment. The first Database object is created using the default database environment; the second Database object is created by specifying the database environment explicitly and opening the database file as a separate call.
Listing 11. STLdb4 and Database Environments
#include <iostream>
#include <STLdb4/stldb4.hh>
using namespace STLdb4;
using namespace std;
int main( int argc, char** argv )
{
string dbenvpath = argv[1];
fh_env dbenv = new Environment( dbenvpath );
dbenv->setDefaultOpenFlags(
DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL );
Environment::setDefault( dbenv );
fh_database db = new Database(
DB_BTREE, dbenvpath + "/foo.db" );
db["bar"] = argv[2];
fh_database db2 = new Database( dbenv );
db2->create( DB_BTREE, dbenvpath + "/foo2.db" );
db2["key"] = (string)"value_" + argv[2];
return 0;
}The ordering of elements in the database can be changed with Database::set_bt_compare() using either a function pointer or a Loki functor object. For details on Loki functors, refer to the Modern C++ Design book (see the on-line Resources). As the comparison function is a relatively low-level operation, no implicit conversions happen for this, and you must compare two Dbt values. A collection of functions for numeric comparison, such as getInt32Compare() and string comparison with and without case sensitivity using getCISCompare(), are available in STLdb4. The ordering of a comparison functor can be reversed by passing it to makeReverseCompare() to create a new functor. These operations must be performed before the database is open, so you have to use the open() or create() calls and the non-opening Database constructor as shown in Listing 12.
Increasing the default Berkeley DB cache size using Database::set_cachesize() can improve read-only database performance significantly.
Listing 12. STLdb4 and Database Environments
fh_database db = new Database(); Database::m_bt_compare_functor_t tmpf = getInt32Compare(); db->set_bt_compare( makeReverseCompare( tmpf ) ); db->create( DB_BTREE, "/tmp/play.db" );
A template subclass of Database taking parameters similar to std::map<> would be nice. There would need to be some extra work to allow both the key and value to be (de)serialized on demand perhaps by assuming that both can be Boost serialized.
Resources for this article: /article/9512.
Ben Martin has been working on filesystems for more than ten years. He is currently working toward a PhD at the University of Wollongong, Australia, combining Semantic Filesystems with Formal Concept Analysis to improve human-filesystem interaction.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- RSS Feeds
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- Readers' Choice Awards
- Developer Poll
- What's the tweeting protocol?
- New Products
- Reply to comment | Linux Journal
2 hours 1 min ago - play with linux? i think you mean work-around linux
10 hours 28 min ago - Where is Epistle?
10 hours 33 min ago - You forgot OwnCloud
11 hours 3 min ago - aplikasi free
14 hours 17 min ago - Having a framework
14 hours 21 min ago - Fix my computer
15 hours 1 min ago - go-mtpfs
19 hours 8 min ago - Missed one
19 hours 27 min ago - web Host
19 hours 36 min ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




Comments
STLdb4 looks good but seems a pain to compile
Having read the article STLdb4 looks very good, though trying to get it compiled and installed is a pain.
If anybody else is having problems then I've written my experiences of compiling on Debian Etch here: http://www.richardbishop.net/wpress/?p=23
Nice!
After reading this article, I have been using STLdb4, and am very pleased with it! It could use a few touchups, but since you can
always get the raw BerkeleyDB pointer there's nothing I haven't been
able to accomplish yet. *SO* much nicer to work with than the existing C++ implementation. Good job!