Skip to content

Commit

Permalink
Convert MDB_cursor and operations to ::lmdb variants.
Browse files Browse the repository at this point in the history
  • Loading branch information
clemahieu committed Sep 10, 2024
1 parent ff962bf commit 519f57e
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions nano/store/lmdb/iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ class iterator : public iterator_impl<T, U>
{
public:
iterator (store::transaction const & transaction_a, ::lmdb::env const & env_a, ::lmdb::dbi const & db_a, MDB_val const & val_a = MDB_val{}, bool const direction_asc = true) :
nano::store::iterator_impl<T, U> (transaction_a)
nano::store::iterator_impl<T, U> (transaction_a),
cursor{ ::lmdb::cursor::open (tx (transaction_a), db_a) }
{
auto status (mdb_cursor_open (tx (transaction_a), db_a, &cursor));
release_assert (status == 0);
auto operation (MDB_SET_RANGE);
auto operation = MDB_SET_RANGE;
if (val_a.mv_size != 0)
{
current.first = val_a;
Expand All @@ -28,12 +27,9 @@ class iterator : public iterator_impl<T, U>
{
operation = direction_asc ? MDB_FIRST : MDB_LAST;
}
auto status2 (mdb_cursor_get (cursor, &current.first.value, &current.second.value, operation));
release_assert (status2 == 0 || status2 == MDB_NOTFOUND);
if (status2 != MDB_NOTFOUND)
if (::lmdb::cursor_get (cursor, &current.first.value, &current.second.value, operation))
{
auto status3 (mdb_cursor_get (cursor, &current.first.value, &current.second.value, MDB_GET_CURRENT));
release_assert (status3 == 0 || status3 == MDB_NOTFOUND);
::lmdb::cursor_get (cursor, &current.first.value, &current.second.value, MDB_GET_CURRENT);
if (current.first.size () != sizeof (T))
{
clear ();
Expand All @@ -60,16 +56,14 @@ class iterator : public iterator_impl<T, U>
{
if (cursor != nullptr)
{
mdb_cursor_close (cursor);
cursor.close ();
}
}

store::iterator_impl<T, U> & operator++ () override
{
debug_assert (cursor != nullptr);
auto status (mdb_cursor_get (cursor, &current.first.value, &current.second.value, MDB_NEXT));
release_assert (status == 0 || status == MDB_NOTFOUND);
if (status == MDB_NOTFOUND)
if (!::lmdb::cursor_get (cursor, &current.first.value, &current.second.value, MDB_NEXT))
{
clear ();
}
Expand All @@ -83,9 +77,7 @@ class iterator : public iterator_impl<T, U>
store::iterator_impl<T, U> & operator-- () override
{
debug_assert (cursor != nullptr);
auto status (mdb_cursor_get (cursor, &current.first.value, &current.second.value, MDB_PREV));
release_assert (status == 0 || status == MDB_NOTFOUND);
if (status == MDB_NOTFOUND)
if (!::lmdb::cursor_get (cursor, &current.first.value, &current.second.value, MDB_PREV))
{
clear ();
}
Expand Down Expand Up @@ -155,7 +147,7 @@ class iterator : public iterator_impl<T, U>
{
if (cursor != nullptr)
{
mdb_cursor_close (cursor);
cursor.close ();
}
cursor = other_a.cursor;
other_a.cursor = nullptr;
Expand All @@ -165,7 +157,7 @@ class iterator : public iterator_impl<T, U>
}

store::iterator_impl<T, U> & operator= (store::iterator_impl<T, U> const &) = delete;
MDB_cursor * cursor{ nullptr };
::lmdb::cursor cursor;
std::pair<store::db_val<MDB_val>, store::db_val<MDB_val>> current;
};

Expand Down Expand Up @@ -282,7 +274,7 @@ class merge_iterator : public iterator_impl<T, U>
}
else
{
auto key_cmp (mdb_cmp (mdb_cursor_txn (impl1->cursor), mdb_cursor_dbi (impl1->cursor), impl1->current.first, impl2->current.first));
auto key_cmp (mdb_cmp (impl1->cursor.txn (), impl1->cursor.dbi (), impl1->current.first, impl2->current.first));

if (key_cmp < 0)
{
Expand All @@ -296,7 +288,7 @@ class merge_iterator : public iterator_impl<T, U>
}
else
{
auto val_cmp (mdb_cmp (mdb_cursor_txn (impl1->cursor), mdb_cursor_dbi (impl1->cursor), impl1->current.second, impl2->current.second));
auto val_cmp (mdb_cmp (impl1->cursor.txn (), impl1->cursor.dbi (), impl1->current.second, impl2->current.second));
result = val_cmp < 0 ? impl1.get () : impl2.get ();
from_first_database = (result == impl1.get ());
}
Expand Down

0 comments on commit 519f57e

Please sign in to comment.