Don't return common key prefix in SqliteKeyValue::get_by_prefix.

This commit is contained in:
levlam 2024-01-05 15:11:49 +03:00
parent f4bafbdc86
commit 9e0bb80dcf

View File

@ -79,11 +79,17 @@ class SqliteKeyValue {
if (!prefix.empty()) { if (!prefix.empty()) {
next = next_prefix(prefix); next = next_prefix(prefix);
} }
get_by_range(prefix, next, callback); get_by_range_impl(prefix, next, true, callback);
} }
template <class CallbackT> template <class CallbackT>
void get_by_range(Slice from, Slice till, CallbackT &&callback) { void get_by_range(Slice from, Slice till, CallbackT &&callback) {
get_by_range_impl(from, till, false, std::move(callback));
}
private:
template <class CallbackT>
void get_by_range_impl(Slice from, Slice till, bool strip_key_prefix, CallbackT &&callback) {
SqliteStatement *stmt = nullptr; SqliteStatement *stmt = nullptr;
if (from.empty()) { if (from.empty()) {
stmt = &get_all_stmt_; stmt = &get_all_stmt_;
@ -100,14 +106,17 @@ class SqliteKeyValue {
auto guard = stmt->guard(); auto guard = stmt->guard();
stmt->step().ensure(); stmt->step().ensure();
while (stmt->has_row()) { while (stmt->has_row()) {
if (!callback(stmt->view_blob(0), stmt->view_blob(1))) { auto key = stmt->view_blob(0);
if (strip_key_prefix) {
key.remove_prefix(from.size());
}
if (!callback(key, stmt->view_blob(1))) {
return; return;
} }
stmt->step().ensure(); stmt->step().ensure();
} }
} }
private:
string table_name_; string table_name_;
SqliteDb db_; SqliteDb db_;
SqliteStatement get_stmt_; SqliteStatement get_stmt_;