Skip to content

Commit fd09741

Browse files
committed
Convert MDB_cursor and operations to ::lmdb variants.
1 parent 30c6644 commit fd09741

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

nano/store/lmdb/iterator.hpp

+12-20
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ class iterator : public iterator_impl<T, U>
1515
{
1616
public:
1717
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) :
18-
nano::store::iterator_impl<T, U> (transaction_a)
18+
nano::store::iterator_impl<T, U> (transaction_a),
19+
cursor{ ::lmdb::cursor::open (tx (transaction_a), db_a) }
1920
{
20-
auto status (mdb_cursor_open (tx (transaction_a), db_a, &cursor));
21-
release_assert (status == 0);
22-
auto operation (MDB_SET_RANGE);
21+
auto operation = MDB_SET_RANGE;
2322
if (val_a.mv_size != 0)
2423
{
2524
current.first = val_a;
@@ -28,12 +27,9 @@ class iterator : public iterator_impl<T, U>
2827
{
2928
operation = direction_asc ? MDB_FIRST : MDB_LAST;
3029
}
31-
auto status2 (mdb_cursor_get (cursor, &current.first.value, &current.second.value, operation));
32-
release_assert (status2 == 0 || status2 == MDB_NOTFOUND);
33-
if (status2 != MDB_NOTFOUND)
30+
if (::lmdb::cursor_get (cursor, &current.first.value, &current.second.value, operation))
3431
{
35-
auto status3 (mdb_cursor_get (cursor, &current.first.value, &current.second.value, MDB_GET_CURRENT));
36-
release_assert (status3 == 0 || status3 == MDB_NOTFOUND);
32+
::lmdb::cursor_get (cursor, &current.first.value, &current.second.value, MDB_GET_CURRENT);
3733
if (current.first.size () != sizeof (T))
3834
{
3935
clear ();
@@ -60,16 +56,14 @@ class iterator : public iterator_impl<T, U>
6056
{
6157
if (cursor != nullptr)
6258
{
63-
mdb_cursor_close (cursor);
59+
cursor.close ();
6460
}
6561
}
6662

6763
store::iterator_impl<T, U> & operator++ () override
6864
{
6965
debug_assert (cursor != nullptr);
70-
auto status (mdb_cursor_get (cursor, &current.first.value, &current.second.value, MDB_NEXT));
71-
release_assert (status == 0 || status == MDB_NOTFOUND);
72-
if (status == MDB_NOTFOUND)
66+
if (!::lmdb::cursor_get (cursor, &current.first.value, &current.second.value, MDB_NEXT))
7367
{
7468
clear ();
7569
}
@@ -83,9 +77,7 @@ class iterator : public iterator_impl<T, U>
8377
store::iterator_impl<T, U> & operator-- () override
8478
{
8579
debug_assert (cursor != nullptr);
86-
auto status (mdb_cursor_get (cursor, &current.first.value, &current.second.value, MDB_PREV));
87-
release_assert (status == 0 || status == MDB_NOTFOUND);
88-
if (status == MDB_NOTFOUND)
80+
if (!::lmdb::cursor_get (cursor, &current.first.value, &current.second.value, MDB_PREV))
8981
{
9082
clear ();
9183
}
@@ -155,7 +147,7 @@ class iterator : public iterator_impl<T, U>
155147
{
156148
if (cursor != nullptr)
157149
{
158-
mdb_cursor_close (cursor);
150+
cursor.close ();
159151
}
160152
cursor = other_a.cursor;
161153
other_a.cursor = nullptr;
@@ -165,7 +157,7 @@ class iterator : public iterator_impl<T, U>
165157
}
166158

167159
store::iterator_impl<T, U> & operator= (store::iterator_impl<T, U> const &) = delete;
168-
MDB_cursor * cursor{ nullptr };
160+
::lmdb::cursor cursor;
169161
std::pair<store::db_val<MDB_val>, store::db_val<MDB_val>> current;
170162
};
171163

@@ -282,7 +274,7 @@ class merge_iterator : public iterator_impl<T, U>
282274
}
283275
else
284276
{
285-
auto key_cmp (mdb_cmp (mdb_cursor_txn (impl1->cursor), mdb_cursor_dbi (impl1->cursor), impl1->current.first, impl2->current.first));
277+
auto key_cmp (mdb_cmp (impl1->cursor.txn (), impl1->cursor.dbi (), impl1->current.first, impl2->current.first));
286278

287279
if (key_cmp < 0)
288280
{
@@ -296,7 +288,7 @@ class merge_iterator : public iterator_impl<T, U>
296288
}
297289
else
298290
{
299-
auto val_cmp (mdb_cmp (mdb_cursor_txn (impl1->cursor), mdb_cursor_dbi (impl1->cursor), impl1->current.second, impl2->current.second));
291+
auto val_cmp (mdb_cmp (impl1->cursor.txn (), impl1->cursor.dbi (), impl1->current.second, impl2->current.second));
300292
result = val_cmp < 0 ? impl1.get () : impl2.get ();
301293
from_first_database = (result == impl1.get ());
302294
}

0 commit comments

Comments
 (0)