BerkleyDB & C# – Part 1

Jan 31st, 2010 | By sankarsan | Category: Featured

In my last post I have discussed about the BerkleyDB product family.In this post we will discuss about using BerkleyDB with it’s C# API set.Before getting into the details we will take a quick look into the different access methods provided by Berkley DB.

  • BTree – Here the data is stored in a balanced tree structure.We can put complex as well as simple types as key/values in the database.Duplicate key values also can be stored.
  • Hash – Here the data is stored in a hash table structure, both simple & complex types can be stored as key/values in the database, database can be configured to store duplicate key values as well.
  • Queue – Here data is stored as fixed length records, logical record number is used as the key.This is specially designed for fast inserts at the tail of the queue and retrieval/deletion from the head of the queue.
  • Recno– Like Queue here also the logical record number is used as the key store fixed/variable length records.

BTree database is very suitable when there is some locality reference i.e. query for a given key will be followed by query for one of it’s neighbours.

Hash database is suitable for large dataset.This is because internal information stored by the database in case of BTree is larger than that of Hash.So when the application data grows there will be very little space left in the cache for actual application data in case of BTree databases.

Queue & Recno databases are suitable for cases where we want to use the logical record number as the key.Queue only supports fixed length records whereas Recno support both fixed and variable length records.

The access methods needs to be selected right at the beginning before opening the databases.In the C# API the following classes provides an abstraction of the databases with different access methods:

  • BTreeDatabase
  • HashDatabase
  • QueueDatabase
  • RecnoDatabase

Each of these databases classes have a corresponding configuration class which is used to hold the configuration information for those databases e.g. BTreeDatabase as an accompanying class called BTreeDatabaseConfig.

The code sample below shows how to open a BerkleyDB BTreeDatabase.

BTreeDatabase btdb = null;
BTreeDatabaseConfig btcfg = new BTreeDatabaseConfig();
btcfg.Duplicates = DuplicatesPolicy.SORTED;
btcfg.Creation = CreatePolicy.IF_NEEDED;

try
{
btdb = BTreeDatabase.Open(@"C:\\BTreeSample.db", btcfg);
Console.WriteLine("Database Opened..");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
if (btdb != null) btdb.Close();
}
Console.ReadLine();

Here we have first setup the BTreeDatabaseConfig and then opened the database by passing the filename and config.

The BTreeDatabaseConfig exposes two important properties:

  • Duplicates – This property is of type DuplicatePolicy enumeration.This enumeration supports three values
    • NONE – This means duplicate keys will not be allowed.
    • SORTED – This means duplicate keys will be stored in a sorted fashion
    • UNSORTED – This means duplicate keys will be stored in an unsorted fashion
  • Creation – This property is of type CreatePolicy enumeration.This enumeration supports three values
    • NEVER –It will never create the database.Only existing DB will be opened
    • ALWAYS – It will always try to create the database and throw exception if database already exists. BerkeleyDB.DatabaseException: File exists
    • IF_NEEDED – This will create the database if it does not exists otherwise will open the existing database.

So we have now seen how to configure and open an BerkleyDB database.In the next post we will how insert & retrieve values from it.


Kick It on DotNetKicks.com
Tags:

View Comments
Leave a comment »

  1. [...] BerkleyDB & C# – Part 1 – Coding N Design [...]

Leave Comment

blog comments powered by Disqus