Tkrzw
Tkrzw: a set of implementations of DBM

Introduction

DBM (Database Manager) is a concept of libraries to store an associative array on a permanent storage. In other words, DBM allows an application program to store key-value pairs in a file and reuse them later. Each of keys and values is a string or a sequence of bytes. The key of each record must be unique within the database and a value is associated to it. You can retrieve a stored record with its key very quickly. Thanks to simple structure of DBM, its performance can be extremely high.

Tkrzw is a C++ library implementing DBM with various algorithms. It features high degrees of performance, concurrency, scalability and durability. The following classes are the most important.

  • tkrzw::DBM – Datatabase manager interface.
  • tkrzw::HashDBM – File database manager implementation based on hash table.
  • tkrzw::TreeDBM – File database manager implementation based on B+ tree.
  • tkrzw::SkipDBM – File database manager implementation based on skip list.
  • tkrzw::TinyDBM – On-memory database manager implementation based on hash table.
  • tkrzw::BabyDBM – On-memory database manager implementation based on B+ tree.
  • tkrzw::CacheDBM – On-memory database manager implementations with LRU deletion.
  • tkrzw::StdHashDBM – On-memory hash database manager implementation using std::unordered_map.
  • tkrzw::StdTreeDBM – On-memory tree database manager implementation using std::map.
  • tkrzw::PolyDBM – Polymorphic datatabase manager adapter for all DBM classes.
  • tkrzw::ShardDBM – Sharding datatabase manager adapter based on PolyDBM.
  • tkrzw::FileIndex – File secondary index implementation with TreeDBM.
  • tkrzw::MemIndex – On-memory secondary index implementation with BabyDBM.
  • tkrzw::StdIndex – On-memory secondary index implementation with std::map.

All database classes share the same interface so that applications can use any of them with the common API. All classes are thread-safe so that multiple threads can access the same database simultaneously. Basically, you can store records with the "Set" method, retrieve records with the "Get" method, and remove records with the "Remove" method. Iterator is also supported to retrieve each and every record in the database. See the homepage for details.

#include "tkrzw_dbm_hash.h"
// Main routine.
int main(int argc, char** argv) {
// All symbols of Tkrzw are under the namespace "tkrzw".
using namespace tkrzw;
// Creates the database manager.
HashDBM dbm;
// Opens a new database.
dbm.Open("casket.tkh", true);
// Stores records.
dbm.Set("foo", "hop");
dbm.Set("bar", "step");
dbm.Set("baz", "jump");
// Retrieves records.
std::cout << dbm.GetSimple("foo", "*") << std::endl;
std::cout << dbm.GetSimple("bar", "*") << std::endl;
std::cout << dbm.GetSimple("baz", "*") << std::endl;
std::cout << dbm.GetSimple("outlier", "*") << std::endl;
// Traverses records.
std::unique_ptr<DBM::Iterator> iter = dbm.MakeIterator();
iter->First();
std::string key, value;
while (iter->Get(&key, &value) == Status::SUCCESS) {
std::cout << key << ":" << value << std::endl;
iter->Next();
}
// Closes the database.
dbm.Close();
return 0;
}
tkrzw::DBM::GetSimple
virtual std::string GetSimple(std::string_view key, std::string_view default_value="")
Gets the value of a record of a key, in a simple way.
Definition: tkrzw_dbm.h:666
tkrzw_dbm_hash.h
File database manager implementation based on hash table.
tkrzw::HashDBM::MakeIterator
std::unique_ptr< DBM::Iterator > MakeIterator() override
Makes an iterator for each record.
tkrzw::Status::SUCCESS
@ SUCCESS
Success.
Definition: tkrzw_lib_common.h:116
tkrzw::HashDBM::Open
Status Open(const std::string &path, bool writable, int32_t options=File::OPEN_DEFAULT) override
Opens a database file.
Definition: tkrzw_dbm_hash.h:257
tkrzw::HashDBM::Close
Status Close() override
Closes the database file.
tkrzw::DBM::Set
virtual Status Set(std::string_view key, std::string_view value, bool overwrite=true)
Sets a record of a key and a value.
Definition: tkrzw_dbm.h:713
tkrzw::HashDBM
File database manager implementation based on hash table.
Definition: tkrzw_dbm_hash.h:39
tkrzw
Common namespace of Tkrzw.
Definition: doxy-overview.h:74