Getting started with Berkeley DB
01 Jan 2015In today’s post, I’m just going to gloss over some top level details in developing applications that use Berkeley DB.
What is it?
The first line of the Wikipedia article for Berkeley DB sums the whole story up pretty well, I think:
Berkeley DB (BDB) is a software library that provides a high-performance embedded database for key/value data.
Pretty simple. Berkeley DB is going to offer you an in-process database to manage data in your applications in a little more organised approach.
Getting installed
I’m using Debian Linux, more specifically the testing release. I’m sure the installation process will translate for other Debian releases and/or other Linux distributions with their respective package managers.
If you’ve already got a standard development/build environment running, you won’t need the build-essential
package listed below.
Building applications
Once you’ve finished writing an application using this library, you’ll need to link the Berkeley DB library against your application. This is all pretty simple as well:
The key piece being the -ldb
linker library switch.
Some simple operations
The following blocks of code are heavily based off of the information contained in this pdf. That pdf has a heap of information in it well worth the read if you’re going to do something serious with Berkeley DB.
First thing you need to do, is to initialize the database structure that you’ll use to conduct all of your operations.
db_create
will allocate and fill out the structure of the DB
typed pointer. It just sets it up ready for use. Once you’ve created the database handle, it’s time to actually open up a database (or create one).
open
is going to try and find the requested database; (in my case test.db
) and open it up. Failing that, it’ll create it (because of DB_CREATE
). The format parameter is quite interesting. In the sample above, DB_BTREE
has been specified. Looking at the DB->open() documentation:
The currently supported Berkeley DB file formats (or access methods) are Btree, Hash, Queue, and Recno. The Btree format is a representation of a sorted, balanced tree structure. The Hash format is an extensible, dynamic hashing scheme. The Queue format supports fast access to fixed-length records accessed sequentially or by logical record number. The Recno format supports fixed- or variable-length records, accessed sequentially or by logical record number, and optionally backed by a flat text file.
This gives you the flexability to use the format that suits your application best.
Once the database is open, writing data into it is as easy as specifying a key and value. There are some further data structures that need to be filled out, but the code is pretty easy to follow:
As the data
member of the DBT
struct is typed out as void *
, you can store any information you’d like in there. Of course, you must specify the size
so the system knows how much to write.
Reading values back out into native variables is just as easy:
Finally, you’ll want to close your database once you’re done: