# lmdb-safe A safe modern & performant C++ wrapper of LMDB. For now briefly only available for C++17, will support C++ 11 again soon. MIT licensed. [LMDB](http://www.lmdb.tech/doc/index.html) is an outrageously fast key/value store with semantics that make it highly interesting for many applications. Of specific note, besides speed, is the full support for transactions and good read/write concurrency. LMDB is also famed for its robustness.. **when used correctly**. The design of LMDB is elegant and simple, which aids both the performance and stability. The downside of this elegant design is a [nontrivial set of rules](http://www.lmdb.tech/doc/starting.html) that [need to be followed](http://www.lmdb.tech/doc/group__mdb.html) to not break things. In other words, LMDB delivers great things but only if you use it exactly right. This is by [conscious design](https://twitter.com/hyc_symas/status/1056168832606392320). Among the things to keep in mind when using LMDB natively: * Never open a database file more than once anywhere in your process * Never open more than one transaction within a thread * .. unless they are all read-only and have MDB_NOTLS set * When opening a named database, no other threads may do that at the same time * Cursors within RO transactions need freeing, but cursors within RW transactions must not be freed. * A new transaction may indicate the database has grown, and you need to restart the transaction then. Breaking these rules may cause no immediate errors, but can lead to silent data corruption, missing updates, or random crashes. Again, this is not an actual bug in LMDB, it means that LMDB expects you to use it according to its exact rules. And who are we to disagree? The `lmdb-safe` library aims to deliver the full LMDB performance while programmatically making sure the LMDB semantics are adhered to, with very limited overhead. Most common LMDB functionality is wrapped within this library but the native MDB handles are all available should you want to use functionality we did not (yet) cater for. # Status Very early. If using this tiny library, be aware things might change rapidly. To use, add `lmdb-safe.cc` and `lmdb-safe.hh` to your project. # Philosophy This library tries to not restrict your use of LMDB, nor make it slower, except on operations that should be rare. The native LMDB handles (Environment, DBI, Transactions & Cursors) are all available for your direct use if need be. When using `lmdb-safe`, errors "that should never happen" are turned into exceptions. An error that merely indicates that a key can not be found is passed on as a regular LMDB error code. # Example The following example has no overhead compared to native LMDB, but already exhibits several ways in which lmdb-safe automates LMDB constraints: ``` auto env = getMDBEnv("./database", 0, 0600); auto dbi = env->openDB("example", MDB_CREATE); auto txn = env->getRWTransaction(); ``` The first line requests an LMDB environment for a database hosted in `./database`. **Within LMDB, it is not allowed to open a database file more than once**, not even from other threads, not even when using a different LMDB handle. `getMDBEnv` keeps a registry of LMDB environments, keyed to the exact inode & flags. If another part of your process requests access to the same inode, it will get the same environment. `MDBEnv` is threadsafe. On the second line, a database is opened within our environment. The semantics of opening or creating a database within LMDB are tricky. With some loss of generality, `MDBEnv::openDB` will create a transaction for you to open the database, and close it too. Most of the time this is what you want. It is also possible to open a database within a transaction manually. The third line opens a read/write transaction using the Resource Acquisition Is Initialization (RAII) technique. If `txn` goes out of scope, the transaction is aborted automatically. To commit or abort, use `commit()` or `abort()`, after which going out of scope has no further effect. ``` txn.put(dbi, "lmdb", "great"); string_view data; if(!txn.get(dbi, "lmdb", data)) { cout<< "Within RW transaction, found that lmdb = " << data <openDB("huge", MDB_CREATE); auto txn = env->getRWTransaction(); unsigned int limit=20000000; ``` This is the usual opening sequence. ``` auto cursor=txn.getCursor(dbi); MDB_val key, data; int count=0; cout<<"Counting records.. "; cout.flush(); while(!cursor.get(key, data, count ? MDB_NEXT : MDB_FIRST)) { count++; } cout<<"Have "<