LMDBAL 0.6.2
LMDB (Lightning Memory-Mapped Database Manager) Abstraction Layer
Loading...
Searching...
No Matches
Getting Started

Everything begins with a database represented by the class LMDBAL::Base. It represents a collection of key-value storages that are going to be stored in a single directory. To create a LMDBAL::Base you need to pick a path for the directory that is going to be created on your machine.

#include "base.h"
//...
LMDBAL::Base base(QDir(QDir::tempPath()).absoluteFilePath("myDataBase"), QStringLiteral("myDataBase"));
LMDBAL::Session session = base.open();
Database abstraction.
Definition base.h:55
RAII guard that keeps the database environment active.
Definition session.h:25

LMDBAL::Base creates or opens the directory you pass as the first argument. The second argument is optional; omit it to reuse the path as the database name. The returned LMDBAL::Session object keeps the environment opened while it exists and can be closed explicitly via LMDBAL::Session::close().

After you have created a LMDBAL::Base you probably want to obtain storage handlers. Now there are two available types of them: LMDBAL::Storage and LMDBAL::Cache. The only difference between them is that LMDBAL::Cache additionally stores elements in a std::map to speed up the access.

#include "storage.h"
#include "cache.h"
//...
auto* storage = base.addStorage<uint32_t, uint32_t>("storage");
auto* cache = base.addCache<int8_t, std::string>("cache");

You can obtain handlers by calling LMDBAL::Base::addStorage() or LMDBAL::Base::addCache(). Note that the handlers still belong to the LMDBAL::Base and it is its responsibility to destroy them. You are not obliged to save those handlers; you can obtain them at any time later using methods LMDBAL::Base::getStorage() or LMDBAL::Base::getCache() calling them with the same template types and names. Pass true as the optional second argument of addStorage() if duplicate keys should be allowed. When serializing Qt types you can keep compatibility with older releases by defining LMDBAL_QDATASTREAM_VERSION_VALUE (or the more advanced LMDBAL_CUSTOM_QDATASTREAM_APPLY(stream) hook) before including LMDBAL headers. Define LMDBAL_DISABLE_QDATASTREAM_VERSIONING instead if you want to skip the version call altogether.

After you have added all the storages you wanted it's time to open the database with LMDBAL::Base::open(). The function returns a LMDBAL::Session object that keeps the database opened while it exists. At this point you are not allowed to add any more storages, otherwise LMDBAL::Opened exception will be thrown. It's currently the limitation of this little library. You can close the session explicitly with LMDBAL::Session::close(); it is also closed automatically on destruction.

//...
LMDBAL::Session session = base.open();
storage->addRecord(54, 75);
cache->addRecord(9, "my value");
uint32_t value1 = storage->getRecord(54); //75
std::string value2 = cache->getRecord(9); //"my value"
uint32_t count1 = storage->count(); //1
uint32_t count2 = cache->count(); //1
storage->removeRecord(54);
cache->removeRecord(9);
session.close(); // optional, closes the database immediately
void close()
Closes the session explicitly.
Definition session.cpp:93

To discover how to store, read and modify data take a look at LMDBAL::Storage and LMDBAL::Cache classes, as well as LMDBAL::Transaction and LMDBAL::WriteTransaction for handling transactions.