From a6ee07d8ef89f4fb5ae9ab80af87c15007dc3af4 Mon Sep 17 00:00:00 2001 From: bert hubert Date: Sat, 5 Jan 2019 14:38:52 +0100 Subject: [PATCH] add rc checking to cursors, add lower_bound(), next() --- lmdb-safe.hh | 67 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 11 deletions(-) diff --git a/lmdb-safe.hh b/lmdb-safe.hh index 6084aa8..5d27c0a 100644 --- a/lmdb-safe.hh +++ b/lmdb-safe.hh @@ -10,7 +10,8 @@ #include #include -#if __cplusplus < 201703L +// apple compiler somehow has string_view even in c++11! +#if __cplusplus < 201703L && !defined(__APPLE__) #include #if BOOST_VERSION > 105400 #include @@ -324,15 +325,37 @@ public: int get(MDBOutVal& key, MDBOutVal& data, MDB_cursor_op op) { - // XXX add rc check - return mdb_cursor_get(d_cursor, &key.d_mdbval, &data.d_mdbval, op); + 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))); + return rc; } 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(&key.d_mdbval), &data.d_mdbval, MDB_SET); + int rc=mdb_cursor_get(d_cursor, const_cast(&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))); + return rc; + } + + int lower_bound(const MDBInVal& in, MDBOutVal& key, MDBOutVal& data) + { + key.d_mdbval = in.d_mdbval; + + int rc = mdb_cursor_get(d_cursor, const_cast(&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))); + return rc; + } + + int next(MDBOutVal& key, MDBOutVal& data) + { + int rc = mdb_cursor_get(d_cursor, const_cast(&key.d_mdbval), &data.d_mdbval, MDB_NEXT); + if(rc && rc != MDB_NOTFOUND) + throw std::runtime_error("Unable to next from cursor: " + std::string(mdb_strerror(rc))); + return rc; } MDB_cursor* d_cursor; @@ -528,12 +551,6 @@ public: return rc; } - int find(const MDBInVal& in, MDBOutVal& key, MDBOutVal& data) - { - key.d_mdbval = in.d_mdbval; - return mdb_cursor_get(d_cursor, const_cast(&key.d_mdbval), &data.d_mdbval, MDB_SET); - } - int put(const MDBOutVal& key, const MDBOutVal& data, int flags=0) { @@ -543,6 +560,34 @@ public: const_cast(&data.d_mdbval), flags); } + int find(const MDBInVal& in, MDBOutVal& key, MDBOutVal& data) + { + key.d_mdbval = in.d_mdbval; + int rc=mdb_cursor_get(d_cursor, const_cast(&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))); + return rc; + } + + int lower_bound(const MDBInVal& in, MDBOutVal& key, MDBOutVal& data) + { + key.d_mdbval = in.d_mdbval; + + int rc = mdb_cursor_get(d_cursor, const_cast(&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))); + return rc; + } + + int next(MDBOutVal& key, MDBOutVal& data) + { + int rc = mdb_cursor_get(d_cursor, const_cast(&key.d_mdbval), &data.d_mdbval, MDB_NEXT); + if(rc && rc != MDB_NOTFOUND) + throw std::runtime_error("Unable to next from cursor: " + std::string(mdb_strerror(rc))); + return rc; + } + + int del(int flags=0) { return mdb_cursor_del(d_cursor, flags);