mdbx++: добавление cursor::estimation_result и переделка cursor::estimate() (backport).

This commit is contained in:
Леонид Юрьев (Leonid Yuriev) 2023-11-12 18:34:23 +03:00
parent b905a6a391
commit 476da5f8cf

View File

@ -4038,10 +4038,12 @@ public:
put_multiple(map, key, vector.data(), vector.size(), mode); put_multiple(map, key, vector.data(), vector.size(), mode);
} }
inline ptrdiff_t estimate(map_handle map, pair from, pair to) const; inline ptrdiff_t estimate(map_handle map, const pair &from,
inline ptrdiff_t estimate(map_handle map, slice from, slice to) const; const pair &to) const;
inline ptrdiff_t estimate_from_first(map_handle map, slice to) const; inline ptrdiff_t estimate(map_handle map, const slice &from,
inline ptrdiff_t estimate_to_last(map_handle map, slice from) const; const slice &to) const;
inline ptrdiff_t estimate_from_first(map_handle map, const slice &to) const;
inline ptrdiff_t estimate_to_last(map_handle map, const slice &from) const;
}; };
/// \brief Managed database transaction. /// \brief Managed database transaction.
@ -4154,10 +4156,11 @@ public:
struct move_result : public pair_result { struct move_result : public pair_result {
inline move_result(const cursor &cursor, bool throw_notfound); inline move_result(const cursor &cursor, bool throw_notfound);
inline move_result(cursor &cursor, move_operation operation, move_result(cursor &cursor, move_operation operation, bool throw_notfound)
bool throw_notfound); : move_result(cursor, operation, slice(), slice(), throw_notfound) {}
inline move_result(cursor &cursor, move_operation operation, move_result(cursor &cursor, move_operation operation, const slice &key,
const slice &key, bool throw_notfound); bool throw_notfound)
: move_result(cursor, operation, key, slice(), throw_notfound) {}
inline move_result(cursor &cursor, move_operation operation, inline move_result(cursor &cursor, move_operation operation,
const slice &key, const slice &value, const slice &key, const slice &value,
bool throw_notfound); bool throw_notfound);
@ -4165,6 +4168,19 @@ public:
move_result &operator=(const move_result &) noexcept = default; move_result &operator=(const move_result &) noexcept = default;
}; };
struct estimate_result : public pair {
ptrdiff_t approximate_quantity;
estimate_result(const cursor &cursor, move_operation operation)
: estimate_result(cursor, operation, slice(), slice()) {}
estimate_result(const cursor &cursor, move_operation operation,
const slice &key)
: estimate_result(cursor, operation, key, slice()) {}
inline estimate_result(const cursor &cursor, move_operation operation,
const slice &key, const slice &value);
estimate_result(const estimate_result &) noexcept = default;
estimate_result &operator=(const estimate_result &) noexcept = default;
};
protected: protected:
inline bool move(move_operation operation, MDBX_val *key, MDBX_val *value, inline bool move(move_operation operation, MDBX_val *key, MDBX_val *value,
bool throw_notfound) const bool throw_notfound) const
@ -4209,9 +4225,10 @@ public:
inline bool eof() const; inline bool eof() const;
inline bool on_first() const; inline bool on_first() const;
inline bool on_last() const; inline bool on_last() const;
inline ptrdiff_t estimate(slice key, slice value) const; inline estimate_result estimate(const slice &key, const slice &value) const;
inline ptrdiff_t estimate(slice key) const; inline estimate_result estimate(const slice &key) const;
inline ptrdiff_t estimate(move_operation operation) const; inline estimate_result estimate(move_operation operation) const;
inline estimate_result estimate(move_operation operation, slice &key) const;
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -5855,28 +5872,32 @@ inline size_t txn::put_multiple(map_handle map, const slice &key,
return args[1].iov_len /* done item count */; return args[1].iov_len /* done item count */;
} }
inline ptrdiff_t txn::estimate(map_handle map, pair from, pair to) const { inline ptrdiff_t txn::estimate(map_handle map, const pair &from,
const pair &to) const {
ptrdiff_t result; ptrdiff_t result;
error::success_or_throw(mdbx_estimate_range( error::success_or_throw(mdbx_estimate_range(
handle_, map.dbi, &from.key, &from.value, &to.key, &to.value, &result)); handle_, map.dbi, &from.key, &from.value, &to.key, &to.value, &result));
return result; return result;
} }
inline ptrdiff_t txn::estimate(map_handle map, slice from, slice to) const { inline ptrdiff_t txn::estimate(map_handle map, const slice &from,
const slice &to) const {
ptrdiff_t result; ptrdiff_t result;
error::success_or_throw(mdbx_estimate_range(handle_, map.dbi, &from, nullptr, error::success_or_throw(mdbx_estimate_range(handle_, map.dbi, &from, nullptr,
&to, nullptr, &result)); &to, nullptr, &result));
return result; return result;
} }
inline ptrdiff_t txn::estimate_from_first(map_handle map, slice to) const { inline ptrdiff_t txn::estimate_from_first(map_handle map,
const slice &to) const {
ptrdiff_t result; ptrdiff_t result;
error::success_or_throw(mdbx_estimate_range(handle_, map.dbi, nullptr, error::success_or_throw(mdbx_estimate_range(handle_, map.dbi, nullptr,
nullptr, &to, nullptr, &result)); nullptr, &to, nullptr, &result));
return result; return result;
} }
inline ptrdiff_t txn::estimate_to_last(map_handle map, slice from) const { inline ptrdiff_t txn::estimate_to_last(map_handle map,
const slice &from) const {
ptrdiff_t result; ptrdiff_t result;
error::success_or_throw(mdbx_estimate_range(handle_, map.dbi, &from, nullptr, error::success_or_throw(mdbx_estimate_range(handle_, map.dbi, &from, nullptr,
nullptr, nullptr, &result)); nullptr, nullptr, &result));
@ -5925,22 +5946,8 @@ MDBX_CXX11_CONSTEXPR bool operator!=(const cursor &a,
inline cursor::move_result::move_result(const cursor &cursor, inline cursor::move_result::move_result(const cursor &cursor,
bool throw_notfound) bool throw_notfound)
: pair_result(key, value, false) { : pair_result(slice(), slice(), false) {
done = cursor.move(get_current, &key, &value, throw_notfound); done = cursor.move(get_current, &this->key, &this->value, throw_notfound);
}
inline cursor::move_result::move_result(cursor &cursor,
move_operation operation,
bool throw_notfound)
: pair_result(key, value, false) {
done = cursor.move(operation, &key, &value, throw_notfound);
}
inline cursor::move_result::move_result(cursor &cursor,
move_operation operation,
const slice &key, bool throw_notfound)
: pair_result(key, slice(), false) {
this->done = cursor.move(operation, &this->key, &this->value, throw_notfound);
} }
inline cursor::move_result::move_result(cursor &cursor, inline cursor::move_result::move_result(cursor &cursor,
@ -5967,6 +5974,14 @@ inline bool cursor::move(move_operation operation, MDBX_val *key,
} }
} }
inline cursor::estimate_result::estimate_result(const cursor &cursor,
move_operation operation,
const slice &key,
const slice &value)
: pair(key, value), approximate_quantity(PTRDIFF_MIN) {
approximate_quantity = cursor.estimate(operation, &this->key, &this->value);
}
inline ptrdiff_t cursor::estimate(move_operation operation, MDBX_val *key, inline ptrdiff_t cursor::estimate(move_operation operation, MDBX_val *key,
MDBX_val *value) const { MDBX_val *value) const {
ptrdiff_t result; ptrdiff_t result;
@ -6089,17 +6104,18 @@ inline bool cursor::on_last() const {
return error::boolean_or_throw(::mdbx_cursor_on_last(*this)); return error::boolean_or_throw(::mdbx_cursor_on_last(*this));
} }
inline ptrdiff_t cursor::estimate(slice key, slice value) const { inline cursor::estimate_result cursor::estimate(const slice &key,
return estimate(multi_exactkey_lowerboundvalue, &key, &value); const slice &value) const {
return estimate_result(*this, multi_exactkey_lowerboundvalue, key, value);
} }
inline ptrdiff_t cursor::estimate(slice key) const { inline cursor::estimate_result cursor::estimate(const slice &key) const {
return estimate(key_lowerbound, &key, nullptr); return estimate_result(*this, key_lowerbound, key);
} }
inline ptrdiff_t cursor::estimate(move_operation operation) const { inline cursor::estimate_result
slice unused_key; cursor::estimate(move_operation operation) const {
return estimate(operation, &unused_key, nullptr); return estimate_result(*this, operation);
} }
inline void cursor::renew(const ::mdbx::txn &txn) { inline void cursor::renew(const ::mdbx::txn &txn) {