diff --git a/lmdb-typed.cc b/lmdb-typed.cc index a625316..fea6674 100644 --- a/lmdb-typed.cc +++ b/lmdb-typed.cc @@ -2,13 +2,13 @@ namespace LMDBSafe { -unsigned int MDBGetMaxID(MDBRWTransaction &txn, MDBDbi &dbi) +IDType MDBGetMaxID(MDBRWTransaction &txn, MDBDbi &dbi) { auto cursor = txn->getRWCursor(dbi); MDBOutVal maxidval, maxcontent; - unsigned int maxid{ 0 }; + auto maxid = IDType(0); if (!cursor.get(maxidval, maxcontent, MDB_LAST)) { - maxid = maxidval.get(); + maxid = maxidval.get(); } return maxid; } diff --git a/lmdb-typed.hh b/lmdb-typed.hh index 3117b0f..627ac4b 100644 --- a/lmdb-typed.hh +++ b/lmdb-typed.hh @@ -10,20 +10,21 @@ namespace LMDBSafe { /* Open issues: - - What is an error? What is an exception? - could id=0 be magic? ('no such id') - yes Perhaps use the separate index concept from multi_index perhaps get eiter to be of same type so for(auto& a : x) works make it more value "like" with unique_ptr */ +/*! + * \brief The type used to store IDs. "0" indicates "no such ID". + */ +using IDType = std::uint32_t; + /** Return the highest ID used in a database. Returns 0 for an empty DB. This makes us start everything at ID=1, which might make it possible to treat id 0 as special */ -LMDB_SAFE_EXPORT unsigned int MDBGetMaxID(MDBRWTransaction &txn, MDBDbi &dbi); +LMDB_SAFE_EXPORT IDType MDBGetMaxID(MDBRWTransaction &txn, MDBDbi &dbi); /** This is the serialization interface. You need to define your these functions for the types you'd like to store. @@ -142,12 +143,12 @@ template struct LMDB_SAFE_EXPORT L : d_parent(parent) { } - void put(MDBRWTransaction &txn, const Class &t, std::uint32_t id, unsigned int flags = 0) + void put(MDBRWTransaction &txn, const Class &t, IDType id, unsigned int flags = 0) { txn->put(d_idx, keyConv(d_parent->getMember(t)), id, flags); } - void del(MDBRWTransaction &txn, const Class &t, std::uint32_t id) + void del(MDBRWTransaction &txn, const Class &t, IDType id) { if (const auto rc = txn->del(d_idx, keyConv(d_parent->getMember(t)), id)) { throw LMDBError("Error deleting from index: ", rc); @@ -273,7 +274,7 @@ public: } //! Get item with id, from main table directly - bool get(std::uint32_t id, T &t) + bool get(IDType id, T &t) { MDBOutVal data; if ((*d_parent.d_txn)->get(d_parent.d_parent->d_main, id, data)) @@ -284,23 +285,23 @@ public: } //! Get item through index N, then via the main database - template std::uint32_t get(const index_t &key, T &out) + template IDType get(const index_t &key, T &out) { MDBOutVal id; if (!(*d_parent.d_txn)->get(std::get(d_parent.d_parent->d_tuple).d_idx, keyConv(key), id)) { - if (get(id.get(), out)) - return id.get(); + if (get(id.get(), out)) + return id.get(); } return 0; } //! Cardinality of index N - template std::uint32_t cardinality() + template IDType cardinality() { auto cursor = (*d_parent.d_txn)->getCursor(std::get(d_parent.d_parent->d_tuple).d_idx); bool first = true; MDBOutVal key, data; - std::uint32_t count = 0; + IDType count = 0; while (!cursor.get(key, data, first ? MDB_FIRST : MDB_NEXT_NODUP)) { ++count; first = false; @@ -463,9 +464,9 @@ public: } // get ID this iterator points to - std::uint32_t getID() + IDType getID() { - return d_on_index ? d_id.get() : d_key.get(); + return d_on_index ? d_id.get() : d_key.get(); } const MDBOutVal &getKey() @@ -526,7 +527,7 @@ public: return iter_t{ &d_parent, std::move(cursor), false, false }; }; - template class StorageType = DirectStorage> iter_t begin_idx() + template class StorageType = DirectStorage> iter_t begin_idx() { typename Parent::cursor_t cursor = (*d_parent.d_txn)->getCursor(std::get(d_parent.d_parent->d_tuple).d_idx); @@ -534,10 +535,10 @@ public: if (cursor.get(out, id, MDB_FIRST)) { // on_index, one_key, end - return iter_t{ &d_parent, std::move(cursor), false, false, true }; + return iter_t{ &d_parent, std::move(cursor), false, false, true }; } - return iter_t{ &d_parent, std::move(cursor), false, false }; + return iter_t{ &d_parent, std::move(cursor), false, false }; } eiter_t end() @@ -721,7 +722,7 @@ public: } // insert something, with possibly a specific id - std::uint32_t put(const T &t, std::uint32_t id = 0) + IDType put(const T &t, IDType id = 0) { unsigned int flags = 0; if (!id) { @@ -734,7 +735,7 @@ public: } // modify an item 'in place', plus update indexes - void modify(std::uint32_t id, std::function func) + void modify(IDType id, std::function func) { T t; if (!this->get(id, t)) @@ -746,7 +747,7 @@ public: } //! delete an item, and from indexes - void del(std::uint32_t id) + void del(IDType id) { T t; if (!this->get(id, t)) @@ -786,7 +787,7 @@ public: private: // clear this ID from all indexes - void clearIndex(std::uint32_t id, const T &t) + void clearIndex(IDType id, const T &t) { d_parent->forEachIndex([&](auto &&i) { i.del(*d_txn, t, id); }); }