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. */
52 explicit MDBDbi(MDB_env* env, MDB_txn* txn, string_view dbname, int flags);
54 operator const MDB_dbi&() const
62 class MDBRWTransactionImpl;
63 class MDBROTransactionImpl;
65 using MDBROTransaction = std::unique_ptr<MDBROTransactionImpl>;
66 using MDBRWTransaction = std::unique_ptr<MDBRWTransactionImpl>;
71 MDBEnv(const char* fname, int flags, int mode);
75 // Only a single thread may call this function. All transactions, databases, and cursors must already be closed before calling this function
77 // but, elsewhere, docs say database handles do not need to be closed?
80 MDBDbi openDB(const string_view dbname, int flags);
82 MDBRWTransaction getRWTransaction();
83 MDBROTransaction getROTransaction();
99 std::mutex d_countmutex;
100 std::map<std::thread::id, int> d_RWtransactionsOut;
101 std::map<std::thread::id, int> d_ROtransactionsOut;
104 std::shared_ptr<MDBEnv> getMDBEnv(const char* fname, int flags, int mode);
116 typename std::enable_if<std::is_arithmetic<T>::value,
117 T>::type* = nullptr> const
121 if(d_mdbval.mv_size != sizeof(T))
122 throw std::runtime_error("MDB data has wrong length for type");
124 memcpy(&ret, d_mdbval.mv_data, sizeof(T));
129 typename std::enable_if<std::is_class<T>::value,T>::type* = nullptr>
136 if(d_mdbval.mv_size != sizeof(T))
137 throw std::runtime_error("MDB data has wrong length for type");
139 memcpy(&ret, d_mdbval.mv_data, sizeof(T));
144 const T* get_struct_ptr() const
146 if(d_mdbval.mv_size != sizeof(T))
147 throw std::runtime_error("MDB data has wrong length for type");
149 return reinterpret_cast<const T*>(d_mdbval.mv_data);
156 template<> inline std::string MDBOutVal::get<std::string>() const
158 return std::string((char*)d_mdbval.mv_data, d_mdbval.mv_size);
161 template<> inline string_view MDBOutVal::get<string_view>() const
163 return string_view((char*)d_mdbval.mv_data, d_mdbval.mv_size);
169 MDBInVal(const MDBOutVal& rhs)
171 d_mdbval = rhs.d_mdbval;
175 typename std::enable_if<std::is_arithmetic<T>::value,
179 memcpy(&d_memory[0], &i, sizeof(i));
180 d_mdbval.mv_size = sizeof(T);
181 d_mdbval.mv_data = d_memory;;
184 MDBInVal(const char* s)
186 d_mdbval.mv_size = strlen(s);
187 d_mdbval.mv_data = (void*)s;
190 MDBInVal(const string_view& v)
192 d_mdbval.mv_size = v.size();
193 d_mdbval.mv_data = (void*)&v[0];
196 MDBInVal(const std::string& v)
198 d_mdbval.mv_size = v.size();
199 d_mdbval.mv_data = (void*)&v[0];
204 static MDBInVal fromStruct(const T& t)
207 ret.d_mdbval.mv_size = sizeof(T);
208 ret.d_mdbval.mv_data = (void*)&t;
219 char d_memory[sizeof(double)];
228 class MDBROTransactionImpl
231 MDBROTransactionImpl(MDBEnv *parent, MDB_txn *txn);
234 static MDB_txn *openROTransaction(MDBEnv *env, MDB_txn *parent, int flags=0);
237 std::vector<MDBROCursor*> d_cursors;
242 void closeROCursors();
245 explicit MDBROTransactionImpl(MDBEnv* parent, int flags=0);
247 MDBROTransactionImpl(const MDBROTransactionImpl& src) = delete;
248 MDBROTransactionImpl &operator=(const MDBROTransactionImpl& src) = delete;
250 // The move constructor/operator cannot be made safe due to Object Slicing with MDBRWTransaction.
251 MDBROTransactionImpl(MDBROTransactionImpl&& rhs) = delete;
252 MDBROTransactionImpl &operator=(MDBROTransactionImpl &&rhs) = delete;
254 virtual ~MDBROTransactionImpl();
256 virtual void abort();
257 virtual void commit();
259 int get(MDB_dbi dbi, const MDBInVal& key, MDBOutVal& val)
262 throw std::runtime_error("Attempt to use a closed RO transaction for get");
264 int rc = mdb_get(d_txn, dbi, const_cast<MDB_val*>(&key.d_mdbval),
265 const_cast<MDB_val*>(&val.d_mdbval));
266 if(rc && rc != MDB_NOTFOUND)
267 throw std::runtime_error("getting data: " + std::string(mdb_strerror(rc)));
272 int get(MDB_dbi dbi, const MDBInVal& key, string_view& val)
275 int rc = get(dbi, key, out);
277 val = out.get<string_view>();
282 // this is something you can do, readonly
283 MDBDbi openDB(string_view dbname, int flags)
285 return MDBDbi( d_parent->d_env, d_txn, dbname, flags);
288 MDBROCursor getCursor(const MDBDbi&);
289 MDBROCursor getROCursor(const MDBDbi&);
296 inline operator bool() const {
300 inline MDBEnv &environment()
307 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.
309 "If the parent transaction commits, the cursor must not be used again."
312 template<class Transaction, class T>
316 std::vector<T*> *d_registry;
317 MDB_cursor* d_cursor;
327 MDBGenCursor(std::vector<T*> ®istry, MDB_cursor *cursor):
328 d_registry(®istry),
331 registry.emplace_back(static_cast<T*>(this));
335 void move_from(MDBGenCursor *src)
341 auto iter = std::find(d_registry->begin(),
344 if (iter != d_registry->end()) {
345 *iter = static_cast<T*>(this);
347 d_registry->emplace_back(static_cast<T*>(this));
352 MDBGenCursor(const MDBGenCursor &src) = delete;
354 MDBGenCursor(MDBGenCursor &&src) noexcept:
355 d_registry(src.d_registry),
356 d_cursor(src.d_cursor)
359 src.d_registry = nullptr;
360 src.d_cursor = nullptr;
363 MDBGenCursor &operator=(const MDBGenCursor &src) = delete;
365 MDBGenCursor &operator=(MDBGenCursor &&src) noexcept
367 d_registry = src.d_registry;
368 d_cursor = src.d_cursor;
370 src.d_registry = nullptr;
371 src.d_cursor = nullptr;
381 int get(MDBOutVal& key, MDBOutVal& data, MDB_cursor_op op)
383 int rc = mdb_cursor_get(d_cursor, &key.d_mdbval, &data.d_mdbval, op);
384 if(rc && rc != MDB_NOTFOUND)
385 throw std::runtime_error("Unable to get from cursor: " + std::string(mdb_strerror(rc)));
389 int find(const MDBInVal& in, MDBOutVal& key, MDBOutVal& data)
391 key.d_mdbval = in.d_mdbval;
392 int rc=mdb_cursor_get(d_cursor, const_cast<MDB_val*>(&key.d_mdbval), &data.d_mdbval, MDB_SET);
393 if(rc && rc != MDB_NOTFOUND)
394 throw std::runtime_error("Unable to find from cursor: " + std::string(mdb_strerror(rc)));
398 int lower_bound(const MDBInVal& in, MDBOutVal& key, MDBOutVal& data)
400 key.d_mdbval = in.d_mdbval;
402 int rc = mdb_cursor_get(d_cursor, const_cast<MDB_val*>(&key.d_mdbval), &data.d_mdbval, MDB_SET_RANGE);
403 if(rc && rc != MDB_NOTFOUND)
404 throw std::runtime_error("Unable to lower_bound from cursor: " + std::string(mdb_strerror(rc)));
409 int nextprev(MDBOutVal& key, MDBOutVal& data, MDB_cursor_op op)
411 int rc = mdb_cursor_get(d_cursor, const_cast<MDB_val*>(&key.d_mdbval), &data.d_mdbval, op);
412 if(rc && rc != MDB_NOTFOUND)
413 throw std::runtime_error("Unable to prevnext from cursor: " + std::string(mdb_strerror(rc)));
417 int next(MDBOutVal& key, MDBOutVal& data)
419 return nextprev(key, data, MDB_NEXT);
422 int prev(MDBOutVal& key, MDBOutVal& data)
424 return nextprev(key, data, MDB_PREV);
427 int currentlast(MDBOutVal& key, MDBOutVal& data, MDB_cursor_op op)
429 int rc = mdb_cursor_get(d_cursor, const_cast<MDB_val*>(&key.d_mdbval), &data.d_mdbval, op);
430 if(rc && rc != MDB_NOTFOUND)
431 throw std::runtime_error("Unable to next from cursor: " + std::string(mdb_strerror(rc)));
435 int current(MDBOutVal& key, MDBOutVal& data)
437 return currentlast(key, data, MDB_GET_CURRENT);
439 int last(MDBOutVal& key, MDBOutVal& data)
441 return currentlast(key, data, MDB_LAST);
443 int first(MDBOutVal& key, MDBOutVal& data)
445 return currentlast(key, data, MDB_FIRST);
448 operator MDB_cursor*()
453 operator bool() const
461 auto iter = std::find(d_registry->begin(),
463 static_cast<T*>(this));
464 if (iter != d_registry->end()) {
465 d_registry->erase(iter);
467 d_registry = nullptr;
470 mdb_cursor_close(d_cursor);
476 class MDBROCursor : public MDBGenCursor<MDBROTransactionImpl, MDBROCursor>
479 MDBROCursor() = default;
480 using MDBGenCursor<MDBROTransactionImpl, MDBROCursor>::MDBGenCursor;
481 MDBROCursor(const MDBROCursor &src) = delete;
482 MDBROCursor(MDBROCursor &&src) = default;
483 MDBROCursor &operator=(const MDBROCursor &src) = delete;
484 MDBROCursor &operator=(MDBROCursor &&src) = default;
485 ~MDBROCursor() = default;
491 class MDBRWTransactionImpl: public MDBROTransactionImpl
494 MDBRWTransactionImpl(MDBEnv* parent, MDB_txn* txn);
497 static MDB_txn *openRWTransaction(MDBEnv* env, MDB_txn *parent, int flags);
500 std::vector<MDBRWCursor*> d_rw_cursors;
502 void closeRWCursors();
503 inline void closeRORWCursors() {
509 explicit MDBRWTransactionImpl(MDBEnv* parent, int flags=0);
511 MDBRWTransactionImpl(const MDBRWTransactionImpl& rhs) = delete;
512 MDBRWTransactionImpl(MDBRWTransactionImpl&& rhs) = delete;
513 MDBRWTransactionImpl &operator=(const MDBRWTransactionImpl& rhs) = delete;
514 MDBRWTransactionImpl &operator=(MDBRWTransactionImpl&& rhs) = delete;
516 ~MDBRWTransactionImpl() override;
518 void commit() override;
519 void abort() override;
521 void clear(MDB_dbi dbi);
523 void put(MDB_dbi dbi, const MDBInVal& key, const MDBInVal& val, int flags=0)
526 throw std::runtime_error("Attempt to use a closed RW transaction for put");
528 if((rc=mdb_put(d_txn, dbi,
529 const_cast<MDB_val*>(&key.d_mdbval),
530 const_cast<MDB_val*>(&val.d_mdbval), flags)))
531 throw std::runtime_error("putting data: " + std::string(mdb_strerror(rc)));
535 int del(MDBDbi& dbi, const MDBInVal& key, const MDBInVal& val)
538 rc=mdb_del(d_txn, dbi, (MDB_val*)&key.d_mdbval, (MDB_val*)&val.d_mdbval);
539 if(rc && rc != MDB_NOTFOUND)
540 throw std::runtime_error("deleting data: " + std::string(mdb_strerror(rc)));
544 int del(MDBDbi& dbi, const MDBInVal& key)
547 rc=mdb_del(d_txn, dbi, (MDB_val*)&key.d_mdbval, 0);
548 if(rc && rc != MDB_NOTFOUND)
549 throw std::runtime_error("deleting data: " + std::string(mdb_strerror(rc)));
554 int get(MDBDbi& dbi, const MDBInVal& key, MDBOutVal& val)
557 throw std::runtime_error("Attempt to use a closed RW transaction for get");
559 int rc = mdb_get(d_txn, dbi, const_cast<MDB_val*>(&key.d_mdbval),
560 const_cast<MDB_val*>(&val.d_mdbval));
561 if(rc && rc != MDB_NOTFOUND)
562 throw std::runtime_error("getting data: " + std::string(mdb_strerror(rc)));
566 int get(MDBDbi& dbi, const MDBInVal& key, string_view& val)
569 int rc = get(dbi, key, out);
571 val = out.get<string_view>();
575 MDBDbi openDB(string_view dbname, int flags)
577 return MDBDbi(environment().d_env, d_txn, dbname, flags);
580 MDBRWCursor getRWCursor(const MDBDbi&);
581 MDBRWCursor getCursor(const MDBDbi&);
583 MDBRWTransaction getRWTransaction();
584 MDBROTransaction getROTransaction();
587 /* "A cursor in a write-transaction can be closed before its transaction ends, and will otherwise be closed when its transaction ends"
588 This is a problem for us since it may means we are closing the cursor twice, which is bad
590 class MDBRWCursor : public MDBGenCursor<MDBRWTransactionImpl, MDBRWCursor>
593 MDBRWCursor() = default;
594 using MDBGenCursor<MDBRWTransactionImpl, MDBRWCursor>::MDBGenCursor;
595 MDBRWCursor(const MDBRWCursor &src) = delete;
596 MDBRWCursor(MDBRWCursor &&src) = default;
597 MDBRWCursor &operator=(const MDBRWCursor &src) = delete;
598 MDBRWCursor &operator=(MDBRWCursor &&src) = default;
599 ~MDBRWCursor() = default;
601 void put(const MDBOutVal& key, const MDBInVal& data)
603 int rc = mdb_cursor_put(*this,
604 const_cast<MDB_val*>(&key.d_mdbval),
605 const_cast<MDB_val*>(&data.d_mdbval), MDB_CURRENT);
607 throw std::runtime_error("mdb_cursor_put: " + std::string(mdb_strerror(rc)));
611 int put(const MDBOutVal& key, const MDBOutVal& data, int flags=0)
614 return mdb_cursor_put(*this,
615 const_cast<MDB_val*>(&key.d_mdbval),
616 const_cast<MDB_val*>(&data.d_mdbval), flags);
621 return mdb_cursor_del(*this, flags);