15 // apple compiler somehow has string_view even in c++11!
16 #if __cplusplus < 201703L && !defined(__APPLE__)
17 #include <boost/version.hpp>
18 #if BOOST_VERSION >= 106100
19 #include <boost/utility/string_view.hpp>
20 using boost::string_view;
22 #include <boost/utility/string_ref.hpp>
23 using string_view = boost::string_ref;
26 using std::string_view;
32 * - missing convenience functions (string_view, string)
36 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.
40 Thread safety: we are as safe as lmdb. You can talk to MDBEnv from as many threads as you want
43 /** MDBDbi is our only 'value type' object, as 1) a dbi is actually an integer
44 and 2) per LMDB documentation, we never close it. */
51 explicit MDBDbi(MDB_env* env, MDB_txn* txn, string_view dbname, int flags);
53 operator const MDB_dbi&() const
61 class MDBRWTransactionImpl;
62 class MDBROTransactionImpl;
64 using MDBROTransaction = std::unique_ptr<MDBROTransactionImpl>;
65 using MDBRWTransaction = std::unique_ptr<MDBRWTransactionImpl>;
70 MDBEnv(const char* fname, int flags, int mode);
74 // Only a single thread may call this function. All transactions, databases, and cursors must already be closed before calling this function
76 // but, elsewhere, docs say database handles do not need to be closed?
79 MDBDbi openDB(const string_view dbname, int flags);
81 MDBRWTransaction getRWTransaction();
82 MDBROTransaction getROTransaction();
98 std::mutex d_countmutex;
99 std::map<std::thread::id, int> d_RWtransactionsOut;
100 std::map<std::thread::id, int> d_ROtransactionsOut;
103 std::shared_ptr<MDBEnv> getMDBEnv(const char* fname, int flags, int mode);
115 typename std::enable_if<std::is_arithmetic<T>::value,
116 T>::type* = nullptr> const
120 if(d_mdbval.mv_size != sizeof(T))
121 throw std::runtime_error("MDB data has wrong length for type");
123 memcpy(&ret, d_mdbval.mv_data, sizeof(T));
128 typename std::enable_if<std::is_class<T>::value,T>::type* = nullptr>
135 if(d_mdbval.mv_size != sizeof(T))
136 throw std::runtime_error("MDB data has wrong length for type");
138 memcpy(&ret, d_mdbval.mv_data, sizeof(T));
143 const T* get_struct_ptr() const
145 if(d_mdbval.mv_size != sizeof(T))
146 throw std::runtime_error("MDB data has wrong length for type");
148 return reinterpret_cast<const T*>(d_mdbval.mv_data);
155 template<> inline std::string MDBOutVal::get<std::string>() const
157 return std::string((char*)d_mdbval.mv_data, d_mdbval.mv_size);
160 template<> inline string_view MDBOutVal::get<string_view>() const
162 return string_view((char*)d_mdbval.mv_data, d_mdbval.mv_size);
168 MDBInVal(const MDBOutVal& rhs): d_mdbval(rhs.d_mdbval)
173 typename std::enable_if<std::is_arithmetic<T>::value,
177 memcpy(&d_memory[0], &i, sizeof(i));
178 d_mdbval.mv_size = sizeof(T);
179 d_mdbval.mv_data = d_memory;;
182 MDBInVal(const char* s)
184 d_mdbval.mv_size = strlen(s);
185 d_mdbval.mv_data = (void*)s;
188 MDBInVal(const string_view& v)
190 d_mdbval.mv_size = v.size();
191 d_mdbval.mv_data = (void*)&v[0];
194 MDBInVal(const std::string& v)
196 d_mdbval.mv_size = v.size();
197 d_mdbval.mv_data = (void*)&v[0];
202 static MDBInVal fromStruct(const T& t)
205 ret.d_mdbval.mv_size = sizeof(T);
206 ret.d_mdbval.mv_data = (void*)&t;
217 char d_memory[sizeof(double)];
226 class MDBROTransactionImpl
229 MDBROTransactionImpl(MDBEnv *parent, MDB_txn *txn);
232 static MDB_txn *openROTransaction(MDBEnv *env, MDB_txn *parent, int flags=0);
235 std::vector<MDBROCursor*> d_cursors;
240 void closeROCursors();
243 explicit MDBROTransactionImpl(MDBEnv* parent, int flags=0);
245 MDBROTransactionImpl(const MDBROTransactionImpl& src) = delete;
246 MDBROTransactionImpl &operator=(const MDBROTransactionImpl& src) = delete;
248 // The move constructor/operator cannot be made safe due to Object Slicing with MDBRWTransaction.
249 MDBROTransactionImpl(MDBROTransactionImpl&& rhs) = delete;
250 MDBROTransactionImpl &operator=(MDBROTransactionImpl &&rhs) = delete;
252 virtual ~MDBROTransactionImpl();
254 virtual void abort();
255 virtual void commit();
257 int get(MDB_dbi dbi, const MDBInVal& key, MDBOutVal& val)
260 throw std::runtime_error("Attempt to use a closed RO transaction for get");
262 int rc = mdb_get(d_txn, dbi, const_cast<MDB_val*>(&key.d_mdbval),
263 const_cast<MDB_val*>(&val.d_mdbval));
264 if(rc && rc != MDB_NOTFOUND)
265 throw std::runtime_error("getting data: " + std::string(mdb_strerror(rc)));
270 int get(MDB_dbi dbi, const MDBInVal& key, string_view& val)
273 int rc = get(dbi, key, out);
275 val = out.get<string_view>();
280 // this is something you can do, readonly
281 MDBDbi openDB(string_view dbname, int flags)
283 return MDBDbi( d_parent->d_env, d_txn, dbname, flags);
286 MDBROCursor getCursor(const MDBDbi&);
287 MDBROCursor getROCursor(const MDBDbi&);
294 inline operator bool() const {
298 inline MDBEnv &environment()
305 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.
307 "If the parent transaction commits, the cursor must not be used again."
310 template<class Transaction, class T>
314 std::vector<T*> *d_registry;
315 MDB_cursor* d_cursor{nullptr};
325 MDBGenCursor(std::vector<T*> ®istry, MDB_cursor *cursor):
326 d_registry(®istry),
329 registry.emplace_back(static_cast<T*>(this));
333 void move_from(MDBGenCursor *src)
339 auto iter = std::find(d_registry->begin(),
342 if (iter != d_registry->end()) {
343 *iter = static_cast<T*>(this);
345 d_registry->emplace_back(static_cast<T*>(this));
350 MDBGenCursor(const MDBGenCursor &src) = delete;
352 MDBGenCursor(MDBGenCursor &&src) noexcept:
353 d_registry(src.d_registry),
354 d_cursor(src.d_cursor)
357 src.d_registry = nullptr;
358 src.d_cursor = nullptr;
361 MDBGenCursor &operator=(const MDBGenCursor &src) = delete;
363 MDBGenCursor &operator=(MDBGenCursor &&src) noexcept
365 d_registry = src.d_registry;
366 d_cursor = src.d_cursor;
368 src.d_registry = nullptr;
369 src.d_cursor = nullptr;
379 int get(MDBOutVal& key, MDBOutVal& data, MDB_cursor_op op)
381 int rc = mdb_cursor_get(d_cursor, &key.d_mdbval, &data.d_mdbval, op);
382 if(rc && rc != MDB_NOTFOUND)
383 throw std::runtime_error("Unable to get from cursor: " + std::string(mdb_strerror(rc)));
387 int find(const MDBInVal& in, MDBOutVal& key, MDBOutVal& data)
389 key.d_mdbval = in.d_mdbval;
390 int rc=mdb_cursor_get(d_cursor, const_cast<MDB_val*>(&key.d_mdbval), &data.d_mdbval, MDB_SET);
391 if(rc && rc != MDB_NOTFOUND)
392 throw std::runtime_error("Unable to find from cursor: " + std::string(mdb_strerror(rc)));
396 int lower_bound(const MDBInVal& in, MDBOutVal& key, MDBOutVal& data)
398 key.d_mdbval = in.d_mdbval;
400 int rc = mdb_cursor_get(d_cursor, const_cast<MDB_val*>(&key.d_mdbval), &data.d_mdbval, MDB_SET_RANGE);
401 if(rc && rc != MDB_NOTFOUND)
402 throw std::runtime_error("Unable to lower_bound from cursor: " + std::string(mdb_strerror(rc)));
407 int nextprev(MDBOutVal& key, MDBOutVal& data, MDB_cursor_op op)
409 int rc = mdb_cursor_get(d_cursor, const_cast<MDB_val*>(&key.d_mdbval), &data.d_mdbval, op);
410 if(rc && rc != MDB_NOTFOUND)
411 throw std::runtime_error("Unable to prevnext from cursor: " + std::string(mdb_strerror(rc)));
415 int next(MDBOutVal& key, MDBOutVal& data)
417 return nextprev(key, data, MDB_NEXT);
420 int prev(MDBOutVal& key, MDBOutVal& data)
422 return nextprev(key, data, MDB_PREV);
425 int currentlast(MDBOutVal& key, MDBOutVal& data, MDB_cursor_op op)
427 int rc = mdb_cursor_get(d_cursor, const_cast<MDB_val*>(&key.d_mdbval), &data.d_mdbval, op);
428 if(rc && rc != MDB_NOTFOUND)
429 throw std::runtime_error("Unable to next from cursor: " + std::string(mdb_strerror(rc)));
433 int current(MDBOutVal& key, MDBOutVal& data)
435 return currentlast(key, data, MDB_GET_CURRENT);
437 int last(MDBOutVal& key, MDBOutVal& data)
439 return currentlast(key, data, MDB_LAST);
441 int first(MDBOutVal& key, MDBOutVal& data)
443 return currentlast(key, data, MDB_FIRST);
446 operator MDB_cursor*()
451 operator bool() const
459 auto iter = std::find(d_registry->begin(),
461 static_cast<T*>(this));
462 if (iter != d_registry->end()) {
463 d_registry->erase(iter);
465 d_registry = nullptr;
468 mdb_cursor_close(d_cursor);
474 class MDBROCursor : public MDBGenCursor<MDBROTransactionImpl, MDBROCursor>
477 MDBROCursor() = default;
478 using MDBGenCursor<MDBROTransactionImpl, MDBROCursor>::MDBGenCursor;
479 MDBROCursor(const MDBROCursor &src) = delete;
480 MDBROCursor(MDBROCursor &&src) = default;
481 MDBROCursor &operator=(const MDBROCursor &src) = delete;
482 MDBROCursor &operator=(MDBROCursor &&src) = default;
483 ~MDBROCursor() = default;
489 class MDBRWTransactionImpl: public MDBROTransactionImpl
492 MDBRWTransactionImpl(MDBEnv* parent, MDB_txn* txn);
495 static MDB_txn *openRWTransaction(MDBEnv* env, MDB_txn *parent, int flags);
498 std::vector<MDBRWCursor*> d_rw_cursors;
500 void closeRWCursors();
501 inline void closeRORWCursors() {
507 explicit MDBRWTransactionImpl(MDBEnv* parent, int flags=0);
509 MDBRWTransactionImpl(const MDBRWTransactionImpl& rhs) = delete;
510 MDBRWTransactionImpl(MDBRWTransactionImpl&& rhs) = delete;
511 MDBRWTransactionImpl &operator=(const MDBRWTransactionImpl& rhs) = delete;
512 MDBRWTransactionImpl &operator=(MDBRWTransactionImpl&& rhs) = delete;
514 ~MDBRWTransactionImpl() override;
516 void commit() override;
517 void abort() override;
519 void clear(MDB_dbi dbi);
521 void put(MDB_dbi dbi, const MDBInVal& key, const MDBInVal& val, int flags=0)
524 throw std::runtime_error("Attempt to use a closed RW transaction for put");
526 if((rc=mdb_put(d_txn, dbi,
527 const_cast<MDB_val*>(&key.d_mdbval),
528 const_cast<MDB_val*>(&val.d_mdbval), flags)))
529 throw std::runtime_error("putting data: " + std::string(mdb_strerror(rc)));
533 int del(MDBDbi& dbi, const MDBInVal& key, const MDBInVal& val)
536 rc=mdb_del(d_txn, dbi, (MDB_val*)&key.d_mdbval, (MDB_val*)&val.d_mdbval);
537 if(rc && rc != MDB_NOTFOUND)
538 throw std::runtime_error("deleting data: " + std::string(mdb_strerror(rc)));
542 int del(MDBDbi& dbi, const MDBInVal& key)
545 rc=mdb_del(d_txn, dbi, (MDB_val*)&key.d_mdbval, 0);
546 if(rc && rc != MDB_NOTFOUND)
547 throw std::runtime_error("deleting data: " + std::string(mdb_strerror(rc)));
552 int get(MDBDbi& dbi, const MDBInVal& key, MDBOutVal& val)
555 throw std::runtime_error("Attempt to use a closed RW transaction for get");
557 int rc = mdb_get(d_txn, dbi, const_cast<MDB_val*>(&key.d_mdbval),
558 const_cast<MDB_val*>(&val.d_mdbval));
559 if(rc && rc != MDB_NOTFOUND)
560 throw std::runtime_error("getting data: " + std::string(mdb_strerror(rc)));
564 int get(MDBDbi& dbi, const MDBInVal& key, string_view& val)
567 int rc = get(dbi, key, out);
569 val = out.get<string_view>();
573 MDBDbi openDB(string_view dbname, int flags)
575 return MDBDbi(environment().d_env, d_txn, dbname, flags);
578 MDBRWCursor getRWCursor(const MDBDbi&);
579 MDBRWCursor getCursor(const MDBDbi&);
581 MDBRWTransaction getRWTransaction();
582 MDBROTransaction getROTransaction();
585 /* "A cursor in a write-transaction can be closed before its transaction ends, and will otherwise be closed when its transaction ends"
586 This is a problem for us since it may means we are closing the cursor twice, which is bad
588 class MDBRWCursor : public MDBGenCursor<MDBRWTransactionImpl, MDBRWCursor>
591 MDBRWCursor() = default;
592 using MDBGenCursor<MDBRWTransactionImpl, MDBRWCursor>::MDBGenCursor;
593 MDBRWCursor(const MDBRWCursor &src) = delete;
594 MDBRWCursor(MDBRWCursor &&src) = default;
595 MDBRWCursor &operator=(const MDBRWCursor &src) = delete;
596 MDBRWCursor &operator=(MDBRWCursor &&src) = default;
597 ~MDBRWCursor() = default;
599 void put(const MDBOutVal& key, const MDBInVal& data)
601 int rc = mdb_cursor_put(*this,
602 const_cast<MDB_val*>(&key.d_mdbval),
603 const_cast<MDB_val*>(&data.d_mdbval), MDB_CURRENT);
605 throw std::runtime_error("mdb_cursor_put: " + std::string(mdb_strerror(rc)));
609 int put(const MDBOutVal& key, const MDBOutVal& data, int flags=0)
612 return mdb_cursor_put(*this,
613 const_cast<MDB_val*>(&key.d_mdbval),
614 const_cast<MDB_val*>(&data.d_mdbval), flags);
619 return mdb_cursor_del(*this, flags);