lmdb-safe/lmdb-safe.hh

554 lines
12 KiB
C++
Raw Normal View History

#pragma once
2018-12-07 13:52:17 +01:00
#include <lmdb.h>
#include <iostream>
#include <fstream>
#include <set>
2018-12-07 18:17:03 +01:00
#include <map>
#include <thread>
#include <memory>
2018-12-10 14:51:02 +01:00
#include <string>
#include <string.h>
2018-12-08 14:08:26 +01:00
#include <mutex>
2018-12-07 13:52:17 +01:00
#if __cplusplus < 201703L
#include <boost/utility/string_view.hpp>
using boost::string_view;
#else
using std::string_view;
#endif
2018-12-07 13:52:17 +01:00
/* open issues:
*
* - missing convenience functions (string_view, string)
*/
/*
The error strategy. Anything that "should never happen" turns into an exception. But things like 'duplicate entry' or 'no such key' are for you to deal with.
*/
/*
Thread safety: we are as safe as lmdb. You can talk to MDBEnv from as many threads as you want
*/
2018-12-08 14:08:26 +01:00
/** MDBDbi is our only 'value type' object, as 1) a dbi is actually an integer
and 2) per LMDB documentation, we never close it. */
2018-12-07 13:52:17 +01:00
class MDBDbi
{
public:
MDBDbi()
{
d_dbi = -1;
}
explicit MDBDbi(MDB_env* env, MDB_txn* txn, string_view dbname, int flags);
2018-12-07 13:52:17 +01:00
operator const MDB_dbi&() const
{
return d_dbi;
}
MDB_dbi d_dbi;
};
class MDBRWTransaction;
class MDBROTransaction;
class MDBEnv
{
public:
MDBEnv(const char* fname, int flags, int mode);
2018-12-07 13:52:17 +01:00
~MDBEnv()
{
// Only a single thread may call this function. All transactions, databases, and cursors must already be closed before calling this function
mdb_env_close(d_env);
// but, elsewhere, docs say database handles do not need to be closed?
}
MDBDbi openDB(const string_view dbname, int flags);
2018-12-07 13:52:17 +01:00
MDBRWTransaction getRWTransaction();
MDBROTransaction getROTransaction();
operator MDB_env*& ()
{
return d_env;
}
MDB_env* d_env;
2018-12-08 14:08:26 +01:00
int getRWTX();
void incRWTX();
void decRWTX();
int getROTX();
void incROTX();
void decROTX();
private:
std::mutex d_openmut;
std::mutex d_countmutex;
2018-12-07 18:17:03 +01:00
std::map<std::thread::id, int> d_RWtransactionsOut;
std::map<std::thread::id, int> d_ROtransactionsOut;
2018-12-07 13:52:17 +01:00
};
std::shared_ptr<MDBEnv> getMDBEnv(const char* fname, int flags, int mode);
2018-12-07 18:17:03 +01:00
2018-12-10 14:51:02 +01:00
struct MDBOutVal
{
operator MDB_val&()
{
return d_mdbval;
}
template <class T,
typename std::enable_if<std::is_arithmetic<T>::value,
T>::type* = nullptr>
T get()
{
T ret;
if(d_mdbval.mv_size != sizeof(T))
throw std::runtime_error("MDB data has wrong length for type");
2018-12-10 14:51:02 +01:00
memcpy(&ret, d_mdbval.mv_data, sizeof(T));
return ret;
}
template <class T,
typename std::enable_if<std::is_class<T>::value,T>::type* = nullptr>
T get();
template<class T>
T get_struct()
{
T ret;
if(d_mdbval.mv_size != sizeof(T))
throw std::runtime_error("MDB data has wrong length for type");
2018-12-10 14:51:02 +01:00
memcpy(&ret, d_mdbval.mv_data, sizeof(T));
return ret;
}
MDB_val d_mdbval;
};
template<> inline std::string MDBOutVal::get<std::string>()
{
return std::string((char*)d_mdbval.mv_data, d_mdbval.mv_size);
}
template<> inline string_view MDBOutVal::get<string_view>()
2018-12-10 14:51:02 +01:00
{
return string_view((char*)d_mdbval.mv_data, d_mdbval.mv_size);
2018-12-10 14:51:02 +01:00
}
class MDBInVal
{
public:
MDBInVal(const MDBOutVal& rhs)
{
d_mdbval = rhs.d_mdbval;
}
2018-12-15 21:06:47 +01:00
2018-12-10 14:51:02 +01:00
template <class T,
typename std::enable_if<std::is_arithmetic<T>::value,
T>::type* = nullptr>
MDBInVal(T i)
{
memcpy(&d_memory[0], &i, sizeof(i));
d_mdbval.mv_size = sizeof(T);
d_mdbval.mv_data = d_memory;;
}
MDBInVal(const char* s)
{
d_mdbval.mv_size = strlen(s);
d_mdbval.mv_data = (void*)s;
}
MDBInVal(const string_view& v)
{
d_mdbval.mv_size = v.size();
d_mdbval.mv_data = (void*)&v[0];
}
MDBInVal(const std::string& v)
2018-12-10 15:02:36 +01:00
{
d_mdbval.mv_size = v.size();
d_mdbval.mv_data = (void*)&v[0];
}
2018-12-10 14:51:02 +01:00
template<typename T>
static MDBInVal fromStruct(const T& t)
{
MDBInVal ret;
ret.d_mdbval.mv_size = sizeof(T);
ret.d_mdbval.mv_data = (void*)&t;
return ret;
}
operator MDB_val&()
{
return d_mdbval;
}
MDB_val d_mdbval;
private:
MDBInVal(){}
char d_memory[sizeof(double)];
};
2018-12-07 13:52:17 +01:00
class MDBROCursor;
class MDBROTransaction
{
public:
explicit MDBROTransaction(MDBEnv* parent, int flags=0);
2018-12-07 13:52:17 +01:00
MDBROTransaction(MDBROTransaction&& rhs)
{
d_parent = rhs.d_parent;
d_txn = rhs.d_txn;
rhs.d_parent = 0;
rhs.d_txn = 0;
}
void reset()
{
// this does not free cursors
mdb_txn_reset(d_txn);
2018-12-08 14:08:26 +01:00
d_parent->decROTX();
2018-12-07 13:52:17 +01:00
}
void renew()
{
2018-12-08 14:08:26 +01:00
if(d_parent->getROTX())
throw std::runtime_error("Duplicate RO transaction");
if(int rc = mdb_txn_renew(d_txn))
throw std::runtime_error("Renewing RO transaction: "+std::string(mdb_strerror(rc)));
2018-12-08 14:08:26 +01:00
d_parent->incROTX();
2018-12-07 13:52:17 +01:00
}
2018-12-10 14:51:02 +01:00
int get(MDB_dbi dbi, const MDBInVal& key, MDBOutVal& val)
2018-12-07 13:52:17 +01:00
{
2018-12-08 14:08:26 +01:00
if(!d_txn)
throw std::runtime_error("Attempt to use a closed RO transaction for get");
2018-12-10 14:51:02 +01:00
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)));
return rc;
2018-12-07 13:52:17 +01:00
}
2018-12-10 14:51:02 +01:00
int get(MDB_dbi dbi, const MDBInVal& key, string_view& val)
{
MDBOutVal out;
int rc = get(dbi, key, out);
if(!rc)
val = out.get<string_view>();
2018-12-10 14:51:02 +01:00
return rc;
}
2018-12-07 13:52:17 +01:00
// this is something you can do, readonly
MDBDbi openDB(string_view dbname, int flags)
2018-12-07 13:52:17 +01:00
{
2018-12-27 17:49:41 +01:00
return MDBDbi( d_parent->d_env, d_txn, dbname, flags);
2018-12-07 13:52:17 +01:00
}
MDBROCursor getCursor(const MDBDbi&);
2018-12-07 14:16:21 +01:00
2018-12-07 13:52:17 +01:00
~MDBROTransaction()
{
if(d_txn) {
2018-12-08 14:08:26 +01:00
d_parent->decROTX();
2018-12-07 14:16:21 +01:00
mdb_txn_commit(d_txn); // this appears to work better than abort for r/o database opening
2018-12-07 13:52:17 +01:00
}
}
operator MDB_txn*&()
{
return d_txn;
}
MDBEnv* d_parent;
MDB_txn* d_txn;
};
/*
A cursor in a read-only transaction must be closed explicitly, before or after its transaction ends. It can be reused with mdb_cursor_renew() before finally closing it.
"If the parent transaction commits, the cursor must not be used again."
*/
class MDBROCursor
{
public:
MDBROCursor(MDBROTransaction* parent, const MDB_dbi& dbi) : d_parent(parent)
{
int rc= mdb_cursor_open(d_parent->d_txn, dbi, &d_cursor);
if(rc) {
2018-12-07 14:16:21 +01:00
throw std::runtime_error("Error creating RO cursor: "+std::string(mdb_strerror(rc)));
2018-12-07 13:52:17 +01:00
}
}
MDBROCursor(MDBROCursor&& rhs)
{
d_cursor = rhs.d_cursor;
rhs.d_cursor=0;
}
void close()
{
mdb_cursor_close(d_cursor);
d_cursor=0;
}
~MDBROCursor()
{
if(d_cursor)
mdb_cursor_close(d_cursor);
}
2018-12-10 17:42:25 +01:00
2018-12-10 14:51:02 +01:00
int get(MDBOutVal& key, MDBOutVal& data, MDB_cursor_op op)
2018-12-07 13:52:17 +01:00
{
// XXX add rc check
2018-12-10 14:51:02 +01:00
return mdb_cursor_get(d_cursor, &key.d_mdbval, &data.d_mdbval, op);
2018-12-07 13:52:17 +01:00
}
2018-12-08 14:08:26 +01:00
int find(const MDBInVal& in, MDBOutVal& key, MDBOutVal& data)
{
// XXX add rc check
key.d_mdbval = in.d_mdbval;
return mdb_cursor_get(d_cursor, const_cast<MDB_val*>(&key.d_mdbval), &data.d_mdbval, MDB_SET);
}
2018-12-07 13:52:17 +01:00
MDB_cursor* d_cursor;
MDBROTransaction* d_parent;
};
class MDBRWCursor;
class MDBRWTransaction
{
public:
explicit MDBRWTransaction(MDBEnv* parent, int flags=0);
2018-12-07 13:52:17 +01:00
MDBRWTransaction(MDBRWTransaction&& rhs)
{
d_parent = rhs.d_parent;
d_txn = rhs.d_txn;
rhs.d_parent = 0;
rhs.d_txn = 0;
}
MDBRWTransaction& operator=(MDBRWTransaction&& rhs)
{
if(d_txn)
abort();
d_parent = rhs.d_parent;
d_txn = rhs.d_txn;
rhs.d_parent = 0;
rhs.d_txn = 0;
return *this;
}
~MDBRWTransaction()
{
if(d_txn) {
2018-12-08 14:08:26 +01:00
d_parent->decRWTX();
2018-12-07 13:52:17 +01:00
closeCursors();
2018-12-08 14:08:26 +01:00
mdb_txn_abort(d_txn); // XXX check response?
2018-12-07 13:52:17 +01:00
}
}
void closeCursors();
void commit()
{
closeCursors();
2018-12-23 16:18:37 +01:00
if(int rc = mdb_txn_commit(d_txn)) {
throw std::runtime_error("committing: " + std::string(mdb_strerror(rc)));
2018-12-07 13:52:17 +01:00
}
2018-12-08 14:08:26 +01:00
d_parent->decRWTX();
2018-12-07 13:52:17 +01:00
d_txn=0;
}
void abort()
{
closeCursors();
2018-12-08 14:08:26 +01:00
mdb_txn_abort(d_txn); // XXX check error?
2018-12-07 13:52:17 +01:00
d_txn = 0;
2018-12-08 14:08:26 +01:00
d_parent->decRWTX();
2018-12-07 13:52:17 +01:00
}
2018-12-09 14:37:08 +01:00
void clear(MDB_dbi dbi);
2018-12-10 15:02:36 +01:00
void put(MDB_dbi dbi, const MDBInVal& key, const MDBInVal& val, int flags=0)
2018-12-07 13:52:17 +01:00
{
2018-12-08 14:08:26 +01:00
if(!d_txn)
throw std::runtime_error("Attempt to use a closed RW transaction for put");
2018-12-07 13:52:17 +01:00
int rc;
2018-12-10 15:02:36 +01:00
if((rc=mdb_put(d_txn, dbi,
const_cast<MDB_val*>(&key.d_mdbval),
const_cast<MDB_val*>(&val.d_mdbval), flags)))
2018-12-07 13:52:17 +01:00
throw std::runtime_error("putting data: " + std::string(mdb_strerror(rc)));
}
int del(MDBDbi& dbi, const MDBInVal& key, const MDBInVal& val)
2018-12-10 15:02:36 +01:00
{
int rc;
rc=mdb_del(d_txn, dbi, (MDB_val*)&key.d_mdbval, (MDB_val*)&val.d_mdbval);
if(rc && rc != MDB_NOTFOUND)
throw std::runtime_error("deleting data: " + std::string(mdb_strerror(rc)));
return rc;
2018-12-10 15:02:36 +01:00
}
2018-12-07 13:52:17 +01:00
int del(MDBDbi& dbi, const MDBInVal& key)
2018-12-07 13:52:17 +01:00
{
int rc;
rc=mdb_del(d_txn, dbi, (MDB_val*)&key.d_mdbval, 0);
2018-12-07 13:52:17 +01:00
if(rc && rc != MDB_NOTFOUND)
throw std::runtime_error("deleting data: " + std::string(mdb_strerror(rc)));
return rc;
}
2018-12-10 14:51:02 +01:00
int get(MDBDbi& dbi, const MDBInVal& key, MDBOutVal& val)
2018-12-07 13:52:17 +01:00
{
2018-12-08 14:08:26 +01:00
if(!d_txn)
throw std::runtime_error("Attempt to use a closed RW transaction for get");
2018-12-08 14:08:26 +01:00
2018-12-10 14:51:02 +01:00
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)));
return rc;
2018-12-07 13:52:17 +01:00
}
int get(MDBDbi& dbi, const MDBInVal& key, string_view& val)
2018-12-10 14:51:02 +01:00
{
MDBOutVal out;
int rc = get(dbi, key, out);
if(!rc)
val = out.get<string_view>();
2018-12-10 14:51:02 +01:00
return rc;
}
2018-12-07 13:52:17 +01:00
2018-12-27 17:49:41 +01:00
MDBDbi openDB(string_view dbname, int flags)
2018-12-07 13:52:17 +01:00
{
return MDBDbi(d_parent->d_env, d_txn, dbname, flags);
}
MDBRWCursor getCursor(const MDBDbi&);
void reportCursor(MDBRWCursor* child)
{
d_cursors.insert(child);
}
2018-12-09 14:37:08 +01:00
void unreportCursor(MDBRWCursor* child)
{
d_cursors.erase(child);
}
2018-12-07 13:52:17 +01:00
void reportCursorMove(MDBRWCursor* from, MDBRWCursor* to)
{
d_cursors.erase(from);
d_cursors.insert(to);
}
operator MDB_txn*&()
{
return d_txn;
}
std::set<MDBRWCursor*> d_cursors;
MDBEnv* d_parent;
MDB_txn* d_txn;
};
/* "A cursor in a write-transaction can be closed before its transaction ends, and will otherwise be closed when its transaction ends"
This is a problem for us since it may means we are closing the cursor twice, which is bad
*/
class MDBRWCursor
{
public:
MDBRWCursor(MDBRWTransaction* parent, const MDB_dbi& dbi) : d_parent(parent)
{
int rc= mdb_cursor_open(d_parent->d_txn, dbi, &d_cursor);
if(rc) {
2018-12-07 14:16:21 +01:00
throw std::runtime_error("Error creating RW cursor: "+std::string(mdb_strerror(rc)));
2018-12-07 13:52:17 +01:00
}
d_parent->reportCursor(this);
}
MDBRWCursor(MDBRWCursor&& rhs)
{
d_parent = rhs.d_parent;
d_cursor = rhs.d_cursor;
rhs.d_cursor=0;
d_parent->reportCursorMove(&rhs, this);
}
void close()
{
if(d_cursor)
mdb_cursor_close(d_cursor);
d_cursor=0;
}
~MDBRWCursor()
{
if(d_cursor)
mdb_cursor_close(d_cursor);
2018-12-09 14:37:08 +01:00
d_parent->unreportCursor(this);
2018-12-07 13:52:17 +01:00
}
2018-12-10 14:51:02 +01:00
int get(MDBOutVal& key, MDBOutVal& data, MDB_cursor_op op)
2018-12-07 13:52:17 +01:00
{
2018-12-10 14:51:02 +01:00
int rc = mdb_cursor_get(d_cursor, &key.d_mdbval, &data.d_mdbval, op);
2018-12-09 14:37:08 +01:00
if(rc && rc != MDB_NOTFOUND)
throw std::runtime_error("mdb_cursor_get: " + std::string(mdb_strerror(rc)));
2018-12-09 14:37:08 +01:00
return rc;
2018-12-07 13:52:17 +01:00
}
2018-12-10 17:42:25 +01:00
int find(const MDBInVal& in, MDBOutVal& key, MDBOutVal& data)
{
key.d_mdbval = in.d_mdbval;
return mdb_cursor_get(d_cursor, const_cast<MDB_val*>(&key.d_mdbval), &data.d_mdbval, MDB_SET);
}
2018-12-10 15:02:36 +01:00
int put(const MDBOutVal& key, const MDBOutVal& data, int flags=0)
2018-12-07 13:52:17 +01:00
{
// XXX check errors
2018-12-10 15:02:36 +01:00
return mdb_cursor_put(d_cursor,
const_cast<MDB_val*>(&key.d_mdbval),
const_cast<MDB_val*>(&data.d_mdbval), flags);
2018-12-07 13:52:17 +01:00
}
int del(int flags=0)
2018-12-07 13:52:17 +01:00
{
return mdb_cursor_del(d_cursor, flags);
}
operator MDB_cursor*&()
{
return d_cursor;
}
2018-12-07 13:52:17 +01:00
MDB_cursor* d_cursor;
MDBRWTransaction* d_parent;
};