Skip to content

BabuDB usage in Cpp

lkairies edited this page Jun 12, 2014 · 1 revision

How to use BabuDB/cpp

Using BabuDB/cpp as a string-indexed key-value database

babudb is started by supplying the names of one or more key-value indices.

#include "babudb/profiles/string_db.h"

vector<string> indices; indices.push_back("index");
StringDB* db = StringDB::Open("dbs/testdb/testdb", indices);

// now you can use the database and change it atomically
db->Add("index", "key1", "value1");
db->Add("index", "key2", "value2");
db->Remove("index", "key4");
db->Commit();

delete db;

Compaction

From time to time you should call

db->Compact("dbs/testdb/old/testdb_old");

to compact the in-memory overlay indices that are created through the operations in the log into on-disk indices. This operation creates a new on-disk index and renames obsolete data (old on-disk indices and log sections) to the supplied prefix. This data can then be deleted by the application.

Lookups

babudb supports simple key lookups and range scans.

string value = db->Lookup("index", "key1")

// Scan index in lexicographic order
LookupIterator it = Lookup("index", "key1", "key9");

while (it.hasMore()) {
  std::pair<Buffer, Buffer> key_value = *it;
  string key(key_value.first.data, key_value.first.size);
  string value(key_value.second.data, key_value.second.size);
  ++it;
} 

BabuDB/cpp as a LSM-tree toolkit

BabuDB/cpp has been designed to be used as a database toolkit that provides you with indices and their consistency/recovery logic and a database log for persistence. Please refer to the implementation of StringDB (babudb/profiles/string_db.(h|cpp)" for details.