From 439ddc45adeeb34edb3016e4c645aa8fc292d52f Mon Sep 17 00:00:00 2001 From: hrissan Date: Fri, 16 Jun 2017 18:48:28 +0300 Subject: [PATCH 1/3] This put overload fixes a very major bug. Consider dbi.put(txn, key, val{data, size}); as val is not an lvalue, the following template overload will be selected. template bool put(MDB_txn* const txn, const K& key, const V& val, const unsigned int flags = default_put_flags) { const lmdb::val k{&key, sizeof(K)}; lmdb::val v{&val, sizeof(V)}; return lmdb::dbi_put(txn, handle(), k, v, flags); } Which instead of storing (data, size) will store (&val, sizeof(val)) --- lmdb++.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lmdb++.h b/lmdb++.h index ab75f8c..a6ee144 100644 --- a/lmdb++.h +++ b/lmdb++.h @@ -1600,6 +1600,14 @@ class lmdb::dbi { return lmdb::dbi_put(txn, handle(), key, data, flags); } + bool put(MDB_txn* const txn, + const val& key, + const val& data, + const unsigned int flags = default_put_flags) { + val rwdata{data.data(), data.size()}; + return lmdb::dbi_put(txn, handle(), key, rwdata, flags); + } + /** * Stores a key into this database. * From fce8f4719050fc5ea9a170c40aaf61ed38725aa9 Mon Sep 17 00:00:00 2001 From: hrissan Date: Fri, 16 Jun 2017 19:40:39 +0300 Subject: [PATCH 2/3] removed a ticking bomb - template get/put which are selected automatically for values like std::string --- lmdb++.h | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lmdb++.h b/lmdb++.h index a6ee144..311f33d 100644 --- a/lmdb++.h +++ b/lmdb++.h @@ -1534,13 +1534,13 @@ class lmdb::dbi { * @param key * @throws lmdb::error on failure */ - template +/* template bool get(MDB_txn* const txn, const K& key) const { const lmdb::val k{&key, sizeof(K)}; lmdb::val v{}; return lmdb::dbi_get(txn, handle(), k, v); - } + }*/ /** * Retrieves a key/value pair from this database. @@ -1550,7 +1550,7 @@ class lmdb::dbi { * @param val * @throws lmdb::error on failure */ - template +/* template bool get(MDB_txn* const txn, const K& key, V& val) const { @@ -1561,7 +1561,7 @@ class lmdb::dbi { val = *v.data(); } return result; - } + }*/ /** * Retrieves a key/value pair from this database. @@ -1571,7 +1571,7 @@ class lmdb::dbi { * @param val * @throws lmdb::error on failure */ - template +/* template bool get(MDB_txn* const txn, const char* const key, V& val) const { @@ -1582,7 +1582,7 @@ class lmdb::dbi { val = *v.data(); } return result; - } + }*/ /** * Stores a key/value pair into this database. @@ -1616,14 +1616,14 @@ class lmdb::dbi { * @param flags * @throws lmdb::error on failure */ - template +/* template bool put(MDB_txn* const txn, const K& key, const unsigned int flags = default_put_flags) { const lmdb::val k{&key, sizeof(K)}; lmdb::val v{}; return lmdb::dbi_put(txn, handle(), k, v, flags); - } + }*/ /** * Stores a key/value pair into this database. @@ -1634,7 +1634,7 @@ class lmdb::dbi { * @param flags * @throws lmdb::error on failure */ - template +/* template bool put(MDB_txn* const txn, const K& key, const V& val, @@ -1642,7 +1642,7 @@ class lmdb::dbi { const lmdb::val k{&key, sizeof(K)}; lmdb::val v{&val, sizeof(V)}; return lmdb::dbi_put(txn, handle(), k, v, flags); - } + }*/ /** * Stores a key/value pair into this database. @@ -1653,7 +1653,7 @@ class lmdb::dbi { * @param flags * @throws lmdb::error on failure */ - template +/* template bool put(MDB_txn* const txn, const char* const key, const V& val, @@ -1661,7 +1661,7 @@ class lmdb::dbi { const lmdb::val k{key, std::strlen(key)}; lmdb::val v{&val, sizeof(V)}; return lmdb::dbi_put(txn, handle(), k, v, flags); - } + }*/ /** * Stores a key/value pair into this database. @@ -1700,12 +1700,12 @@ class lmdb::dbi { * @param key * @throws lmdb::error on failure */ - template +/* template bool del(MDB_txn* const txn, const K& key) { const lmdb::val k{&key, sizeof(K)}; return lmdb::dbi_del(txn, handle(), k); - } + }*/ }; //////////////////////////////////////////////////////////////////////////////// From e2e537f0e49abd7caeadc3bde8cc2c40d69159ac Mon Sep 17 00:00:00 2001 From: hrissan Date: Wed, 22 Nov 2017 15:23:53 +0300 Subject: [PATCH 3/3] Changed size_t to mdb_size_t to build with MDB_VL32 setting --- lmdb++.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lmdb++.h b/lmdb++.h index 311f33d..fee6332 100644 --- a/lmdb++.h +++ b/lmdb++.h @@ -245,7 +245,7 @@ namespace lmdb { static inline void env_get_flags(MDB_env* env, unsigned int* flags); static inline void env_get_path(MDB_env* env, const char** path); static inline void env_get_fd(MDB_env* env, mdb_filehandle_t* fd); - static inline void env_set_mapsize(MDB_env* env, std::size_t size); + static inline void env_set_mapsize(MDB_env* env, mdb_size_t size); static inline void env_set_max_readers(MDB_env* env, unsigned int count); static inline void env_get_max_readers(MDB_env* env, unsigned int* count); static inline void env_set_max_dbs(MDB_env* env, MDB_dbi count); @@ -432,7 +432,7 @@ lmdb::env_get_fd(MDB_env* const env, */ static inline void lmdb::env_set_mapsize(MDB_env* const env, - const std::size_t size) { + const mdb_size_t size) { const int rc = ::mdb_env_set_mapsize(env, size); if (rc != MDB_SUCCESS) { error::raise("mdb_env_set_mapsize", rc); @@ -812,7 +812,7 @@ namespace lmdb { static inline bool cursor_get(MDB_cursor* cursor, MDB_val* key, MDB_val* data, MDB_cursor_op op); static inline void cursor_put(MDB_cursor* cursor, MDB_val* key, MDB_val* data, unsigned int flags); static inline void cursor_del(MDB_cursor* cursor, unsigned int flags); - static inline void cursor_count(MDB_cursor* cursor, std::size_t& count); + static inline void cursor_count(MDB_cursor* cursor, mdb_size_t& count); } /** @@ -916,7 +916,7 @@ lmdb::cursor_del(MDB_cursor* const cursor, */ static inline void lmdb::cursor_count(MDB_cursor* const cursor, - std::size_t& count) { + mdb_size_t& count) { const int rc = ::mdb_cursor_count(cursor, &count); if (rc != MDB_SUCCESS) { error::raise("mdb_cursor_count", rc); @@ -1213,7 +1213,7 @@ class lmdb::env { * @param size * @throws lmdb::error on failure */ - env& set_mapsize(const std::size_t size) { + env& set_mapsize(const mdb_size_t size) { lmdb::env_set_mapsize(handle(), size); return *this; } @@ -1486,7 +1486,7 @@ class lmdb::dbi { * @param txn a transaction handle * @throws lmdb::error on failure */ - std::size_t size(MDB_txn* const txn) const { + mdb_size_t size(MDB_txn* const txn) const { return stat(txn).ms_entries; }