using std::tuple;
using std::weak_ptr;
-static string MDBError(int rc)
-{
- return mdb_strerror(rc);
-}
-
#ifndef DNSDIST
namespace LMDBLS {
throw std::runtime_error("Duplicate RW transaction");
}
- if(int rc=mdb_txn_begin(env->d_env, parent, flags, &result))
- throw std::runtime_error("Unable to start RW transaction: "+std::string(mdb_strerror(rc)));
+ if(int retCode=mdb_txn_begin(env->d_env, parent, flags, &result); retCode != 0) {
+ throw std::runtime_error("Unable to start RW transaction: "+MDBError(retCode));
+ }
env->incRWTX();
return result;
return;
}
- if(int rc = mdb_txn_commit(d_txn)) {
- throw std::runtime_error("committing: " + std::string(mdb_strerror(rc)));
+ if(int retCode = mdb_txn_commit(d_txn); retCode != 0) {
+ throw std::runtime_error("committing: " + MDBError(retCode));
}
environment().decRWTX();
d_txn = nullptr;
A transaction and its cursors must only be used by a single thread, and a thread may only have a single transaction at a time. If MDB_NOTLS is in use, this does not apply to read-only transactions. */
MDB_txn *result = nullptr;
- if(int rc=mdb_txn_begin(env->d_env, parent, MDB_RDONLY | flags, &result))
- throw std::runtime_error("Unable to start RO transaction: "+string(mdb_strerror(rc)));
+ if(int retCode=mdb_txn_begin(env->d_env, parent, MDB_RDONLY | flags, &result); retCode != 0) {
+ throw std::runtime_error("Unable to start RO transaction: "+MDBError(retCode));
+ }
env->incROTX();
void MDBRWTransactionImpl::clear(MDB_dbi dbi)
{
- if(int rc = mdb_drop(d_txn, dbi, 0)) {
- throw runtime_error("Error clearing database: " + MDBError(rc));
+ if(int retCode = mdb_drop(d_txn, dbi, 0); retCode != 0) {
+ throw runtime_error("Error clearing database: " + MDBError(retCode));
}
}
{
MDB_cursor *cursor;
int rc= mdb_cursor_open(d_txn, dbi, &cursor);
- if(rc) {
- throw std::runtime_error("Error creating RW cursor: "+std::string(mdb_strerror(rc)));
+ if(rc != 0) {
+ throw std::runtime_error("Error creating RW cursor: "+MDBError(rc));
}
return MDBRWCursor(d_rw_cursors, cursor, d_txn, d_txtime);
MDBRWTransaction MDBRWTransactionImpl::getRWTransaction()
{
MDB_txn *txn;
- if (int rc = mdb_txn_begin(environment(), *this, 0, &txn)) {
- throw std::runtime_error(std::string("failed to start child transaction: ")+mdb_strerror(rc));
+ if (int retCode = mdb_txn_begin(environment(), *this, 0, &txn); retCode != 0) {
+ throw std::runtime_error(std::string("failed to start child transaction: ")+MDBError(retCode));
}
// we need to increase the counter here because commit/abort on the child transaction will decrease it
environment().incRWTX();
{
MDB_cursor *cursor;
int rc= mdb_cursor_open(d_txn, dbi, &cursor);
- if(rc) {
- throw std::runtime_error("Error creating RO cursor: "+std::string(mdb_strerror(rc)));
+ if(rc != 0) {
+ throw std::runtime_error("Error creating RO cursor: "+MDBError(rc));
}
return MDBROCursor(d_cursors, cursor);
}
#define StringView string
#endif
+static inline string MDBError(int ret)
+{
+ return mdb_strerror(ret);
+}
+
/* open issues:
*
* - Missing convenience functions (string_view, string).
int rc = mdb_get(d_txn, dbi, const_cast<MDB_val*>(&key.d_mdbval),
const_cast<MDB_val*>(&val.d_mdbval));
- if(rc && rc != MDB_NOTFOUND) {
- throw std::runtime_error("getting data: " + std::string(mdb_strerror(rc)));
+ if(rc != 0 && rc != MDB_NOTFOUND) {
+ throw std::runtime_error("getting data: " + MDBError(rc));
}
#ifndef DNSDIST
}
rc = mdb_cursor_get(d_cursor, &key.d_mdbval, &data.d_mdbval, op);
- if(rc && rc != MDB_NOTFOUND) {
- throw std::runtime_error("Unable to get from cursor: " + std::string(mdb_strerror(rc)));
+ if(rc != 0 && rc != MDB_NOTFOUND) {
+ throw std::runtime_error("Unable to get from cursor: " + MDBError(rc));
}
if (rc == MDB_NOTFOUND) {
{
d_prefix.clear();
int rc = mdb_cursor_get(d_cursor, &key.d_mdbval, &data.d_mdbval, op);
- if(rc && rc != MDB_NOTFOUND)
- throw std::runtime_error("Unable to get from cursor: " + std::string(mdb_strerror(rc)));
+ if(rc != 0 && rc != MDB_NOTFOUND) {
+ throw std::runtime_error("Unable to get from cursor: " + MDBError(rc));
+ }
return skipDeleted(key, data, op, rc);
}
d_prefix.clear();
key.d_mdbval = in.d_mdbval;
int rc=mdb_cursor_get(d_cursor, const_cast<MDB_val*>(&key.d_mdbval), &data.d_mdbval, MDB_SET);
- if(rc && rc != MDB_NOTFOUND)
- throw std::runtime_error("Unable to find from cursor: " + std::string(mdb_strerror(rc)));
+ if(rc != 0 && rc != MDB_NOTFOUND) {
+ throw std::runtime_error("Unable to find from cursor: " + MDBError(rc));
+ }
return skipDeleted(key, data, MDB_SET, rc);
}
key.d_mdbval = in.d_mdbval;
int rc = mdb_cursor_get(d_cursor, const_cast<MDB_val*>(&key.d_mdbval), &data.d_mdbval, MDB_SET_RANGE);
- if(rc && rc != MDB_NOTFOUND)
- throw std::runtime_error("Unable to lower_bound from cursor: " + std::string(mdb_strerror(rc)));
+ if(rc != 0 && rc != MDB_NOTFOUND) {
+ throw std::runtime_error("Unable to lower_bound from cursor: " + MDBError(rc));
+ }
return skipDeleted(key, data, MDB_SET_RANGE, rc);
}
int nextprev(MDBOutVal& key, MDBOutVal& data, MDB_cursor_op op)
{
int rc = mdb_cursor_get(d_cursor, const_cast<MDB_val*>(&key.d_mdbval), &data.d_mdbval, op);
- if(rc && rc != MDB_NOTFOUND)
- throw std::runtime_error("Unable to prevnext from cursor: " + std::string(mdb_strerror(rc)));
+ if(rc != 0 && rc != MDB_NOTFOUND) {
+ throw std::runtime_error("Unable to prevnext from cursor: " + MDBError(rc));
+ }
return skipDeleted(key, data, op, rc);
}
int currentlast(MDBOutVal& key, MDBOutVal& data, MDB_cursor_op op)
{
int rc = mdb_cursor_get(d_cursor, const_cast<MDB_val*>(&key.d_mdbval), &data.d_mdbval, op);
- if(rc && rc != MDB_NOTFOUND)
- throw std::runtime_error("Unable to next from cursor: " + std::string(mdb_strerror(rc)));
+ if(rc != 0 && rc != MDB_NOTFOUND) {
+ throw std::runtime_error("Unable to next from cursor: " + MDBError(rc));
+ }
return skipDeleted(key, data, op, rc);
}
const_cast<MDB_val*>(&key.d_mdbval),
const_cast<MDB_val*>(&pval.d_mdbval), flags);
if (mdbPutRc != 0) {
- throw std::runtime_error("putting data: " + std::string(mdb_strerror(mdbPutRc)));
+ throw std::runtime_error("putting data: " + MDBError(mdbPutRc));
}
}
#else
int rc;
if ((rc = mdb_put(d_txn, dbi,
const_cast<MDB_val*>(&key.d_mdbval),
- const_cast<MDB_val*>(&val.d_mdbval), flags)))
- throw std::runtime_error("putting data: " + std::string(mdb_strerror(rc)));
+ const_cast<MDB_val*>(&val.d_mdbval), flags))) {
+ throw std::runtime_error("putting data: " + MDBError(rc));
+ }
}
#endif
const_cast<MDB_val*>(&key.d_mdbval),
const_cast<MDB_val*>(&pval.d_mdbval), 0);
if (mdbPutRc != 0) {
- throw std::runtime_error("marking data deleted: " + std::string(mdb_strerror(mdbPutRc)));
+ throw std::runtime_error("marking data deleted: " + MDBError(mdbPutRc));
}
return;
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
int mdbDelRc = mdb_del(d_txn, dbi, (MDB_val*)&key.d_mdbval, nullptr);
if (mdbDelRc != 0 && mdbDelRc != MDB_NOTFOUND) {
- throw std::runtime_error("deleting data: " + std::string(mdb_strerror(mdbDelRc)));
+ throw std::runtime_error("deleting data: " + MDBError(mdbDelRc));
}
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
const_cast<MDB_val*>(&key.d_mdbval),
const_cast<MDB_val*>(&val.d_mdbval));
- if ((mdbGetRc != 0) && mdbGetRc != MDB_NOTFOUND) {
- throw std::runtime_error("getting data: " + std::string(mdb_strerror(mdbGetRc)));
+ if (mdbGetRc != 0 && mdbGetRc != MDB_NOTFOUND) {
+ throw std::runtime_error("getting data: " + MDBError(mdbGetRc));
}
#ifndef DNSDIST
int rc = mdb_cursor_put(*this,
const_cast<MDB_val*>(&key.d_mdbval),
const_cast<MDB_val*>(&pval.d_mdbval), MDB_CURRENT);
- if(rc)
- throw std::runtime_error("mdb_cursor_put: " + std::string(mdb_strerror(rc)));
+ if(rc != 0) {
+ throw std::runtime_error("mdb_cursor_put: " + MDBError(rc));
+ }
}
#else
void put(const MDBOutVal& key, const MDBInVal& data)
int rc = mdb_cursor_put(*this,
const_cast<MDB_val*>(&key.d_mdbval),
const_cast<MDB_val*>(&data.d_mdbval), MDB_CURRENT);
- if(rc)
- throw std::runtime_error("mdb_cursor_put: " + std::string(mdb_strerror(rc)));
+ if(rc != 0) {
+ throw std::runtime_error("mdb_cursor_put: " + MDBError(rc));
+ }
}
#endif
const_cast<MDB_val*>(&pkey.d_mdbval),
const_cast<MDB_val*>(&pval.d_mdbval), 0);
if(rc_put) {
- throw std::runtime_error("marking data deleted: " + std::string(mdb_strerror(rc_put)));
+ throw std::runtime_error("marking data deleted: " + MDBError(rc_put));
}
}
else {
// do a normal delete
if (int rc_del = mdb_cursor_del(*this, 0); rc_del != 0) {
- throw std::runtime_error("deleting data: " + std::string(mdb_strerror(rc_del)));
+ throw std::runtime_error("deleting data: " + MDBError(rc_del));
}
}
}