Commit Graph

26 Commits

Author SHA1 Message Date
Martchus 4580d7242a Fix typos 2022-03-01 00:42:50 +01:00
Martchus 2e7d278c9b Apply uniform formatting via clang-format 2022-01-30 21:14:43 +01:00
Martchus 3bafada5cc Use distinct exception class and unify format of error messages
* Avoid manual code for making error message
* Use `const` for the return code variable where possible
* Unify formatting of error messages
2022-01-22 18:59:54 +01:00
Martchus b833bfef3b Move everything under a namespace 2022-01-18 22:09:12 +01:00
Martchus 64bde44fa2 Rearange includes to group them more systematically preferring C++ headers 2022-01-18 22:09:08 +01:00
Jonas Schäfer 91920e7c3b Allow reader and writer transactions in the same thread
This is supported by LMDB if the MDB_NOTLS flag is set on the
environment, which it always is with lmdb-safe.

Fixes #9.
2022-01-18 20:36:24 +01:00
Martchus 6a2a752203 Allow configuring maximum number of DBs 2022-01-18 20:35:43 +01:00
Martchus 97fab01b15 Improve error handling when calling `mdb_env_set_mapsize`/`mdb_env_set_maxdbs`
* Handle all errors
* Include error string in exception
2022-01-18 20:35:43 +01:00
Martchus ef0bbb4462 Handle errors when committing transaction 2022-01-18 20:35:38 +01:00
Martchus 155c2c36dd Avoid warnings about bypassing virtual dispatch in destructors 2021-12-25 23:05:49 +01:00
Martchus b621c27ad4 Fix several warnings
* Avoid implicit signedness conversions where possible
* Avoid c-style casts
2021-12-25 23:02:10 +01:00
Jonas Schäfer 7ce9a82141 Implement support for nesting transactions
RW transactions can have RW and RO transactions nested (although
the RO transactions are really just disguised RW transactions,
because LMDB does not support nesting real RO transactions to RW
transactions).

RO transactions can not be nested (again an LMDB limitation).
2019-11-11 21:23:37 +01:00
Jonas Schäfer 3c57c6a113 Hide MDB*Transaction behind a unique_ptr front
This is to prevent the issue with Object Slicing. With the previous
solution (where MDB*Transaction are normal objects), consider the
following code:

    MDBRWTransaction txn = env.getRWTransaction();

    //! Invalid: We explicitly break this move because it would be
    //! unsafe:
    // MDBROTransaction ro_txn(std::move(txn));

    //! Valid, RW inherits from RO now, so we can bind an RO
    //! reference to an RW transaction.
    MDBROTransaction &ro_txn = txn;

    //! Dangerous!!
    MDBROTransaction ro_txn2(std::move(ro_txn));

The last move there breaks the semantics of the RW transaction which
is bound to the reference ro_txn. It looses its RW cursors, which
remain partly inside the txn instance. All kinds of weird and bad
things can happen here. For instance, the ro_txn2 would go out of
scope before the txn, calling the destructor MDBROTransaction
destructor (which defaults to commit instead of abort!) and only
freeing parts of the cursors. Only then the MDBRWTransaction
destructor is called, which will free the cursors which belong to
the RW transaction which has already been committed.

The only safe way to prevent Object Slicing in this scenario I
could come up with is to disallow moves of the objects altogether
and instead use unique_ptr as front for them. This also removes
an additional dynamic allocation per RW transaction (for the
cursor vector), since the address of that vector is now constant
over the lifetime of the transaction without indirection.
2019-11-11 21:23:20 +01:00
Jonas Schäfer d2b0ee057a Re-work the class hierarchy of cursors and transactions
MDBRWTransaction now inherits from MDBROTransaction. Both
transaction types will free their cursors before aborting or
committing. In addition, transactions are now virtual classes.

The reason for this is that RW and RO transactions are handled
very differently in LMDB. Nevertheless, it is very useful to be
able to write read-only code in a way which also can use an RW
transaction. This saves code duplication or unnecessary templates.

Since the only methods which are reqiured to be virtual on
transactions for this to work are the destructor and the
abort/commit methods, the overhead should be neglegible.

This also comes in very handy when going forward to implement
nested transactions, because it is not possible to nest RO
transactions below RW transactions, exacerbating the issue
described above.

Fixes #7 en passant.
2019-11-11 21:23:20 +01:00
bert hubert 59b3b602fa lots of changes 2018-12-27 17:49:41 +01:00
bert hubert d98d657765 eradicate some MDB_dbi use, more string_view 2018-12-14 23:00:44 +01:00
bert hubert ac4876876e make MDBDbi a value type, improve some error messages, make a delete with no data part 2018-12-13 23:16:46 +01:00
bert hubert 3dd2f703db namespace cleanup, reennable support for c++2011 2018-12-10 22:08:49 +01:00
bert hubert f15076bc2b make everything compile again 2018-12-10 15:02:36 +01:00
bert hubert 0a22c24024 big mdbval redo 2018-12-10 14:51:02 +01:00
bert hubert ac9da997a0 fix mixup with mode & flags parameters, implement transaction restart in case of resize 2018-12-10 10:27:07 +01:00
bert hubert 273c32435b add unreport, check errors better 2018-12-09 14:37:08 +01:00
bert hubert 2a8240cd1e improve error reporting, add mutex to database opening 2018-12-08 20:58:19 +01:00
bert hubert c40719f671 some docs, basic example 2018-12-08 14:08:26 +01:00
bert hubert e664945846 wip 2018-12-07 18:17:03 +01:00
bert hubert 8665a8bf08 interim 2018-12-07 13:52:17 +01:00