Pass MessageSearchFilter instead of index_mask in message database queries.

This commit is contained in:
levlam 2021-11-01 12:52:51 +03:00
parent 13c76b349d
commit 463c4e96ac
3 changed files with 22 additions and 56 deletions

View File

@ -7,7 +7,6 @@
#include "td/telegram/MessagesDb.h" #include "td/telegram/MessagesDb.h"
#include "td/telegram/logevent/LogEvent.h" #include "td/telegram/logevent/LogEvent.h"
#include "td/telegram/MessageSearchFilter.h"
#include "td/telegram/Version.h" #include "td/telegram/Version.h"
#include "td/db/SqliteConnectionSafe.h" #include "td/db/SqliteConnectionSafe.h"
@ -335,7 +334,7 @@ class MessagesDbImpl final : public MessagesDbSyncInterface {
if (search_id != 0) { if (search_id != 0) {
// add dialog_id to text // add dialog_id to text
text += PSTRING() << " \a" << dialog_id.get(); text += PSTRING() << " \a" << dialog_id.get();
if (index_mask) { if (index_mask != 0) {
for (int i = 0; i < MESSAGES_DB_INDEX_COUNT; i++) { for (int i = 0; i < MESSAGES_DB_INDEX_COUNT; i++) {
if ((index_mask & (1 << i))) { if ((index_mask & (1 << i))) {
text += PSTRING() << " \a\a" << i; text += PSTRING() << " \a\a" << i;
@ -613,9 +612,8 @@ class MessagesDbImpl final : public MessagesDbSyncInterface {
} }
Result<vector<MessagesDbDialogMessage>> get_messages(MessagesDbMessagesQuery query) final { Result<vector<MessagesDbDialogMessage>> get_messages(MessagesDbMessagesQuery query) final {
if (query.index_mask != 0) { if (query.filter != MessageSearchFilter::Empty) {
return get_messages_from_index(query.dialog_id, query.from_message_id, query.index_mask, query.offset, return get_messages_from_index(query.dialog_id, query.from_message_id, query.filter, query.offset, query.limit);
query.limit);
} }
return get_messages_impl(get_messages_stmt_, query.dialog_id, query.from_message_id, query.offset, query.limit); return get_messages_impl(get_messages_stmt_, query.dialog_id, query.from_message_id, query.offset, query.limit);
} }
@ -699,7 +697,7 @@ class MessagesDbImpl final : public MessagesDbSyncInterface {
get_messages_fts_stmt_.reset(); get_messages_fts_stmt_.reset();
}; };
LOG(INFO) << tag("query", query.query) << query.dialog_id << tag("index_mask", query.index_mask) LOG(INFO) << tag("query", query.query) << query.dialog_id << tag("filter", query.filter)
<< tag("from_search_id", query.from_search_id) << tag("limit", query.limit); << tag("from_search_id", query.from_search_id) << tag("limit", query.limit);
string words = prepare_query(query.query); string words = prepare_query(query.query);
LOG(INFO) << tag("from", query.query) << tag("to", words); LOG(INFO) << tag("from", query.query) << tag("to", words);
@ -710,18 +708,8 @@ class MessagesDbImpl final : public MessagesDbSyncInterface {
} }
// index_mask kludge // index_mask kludge
if (query.index_mask != 0) { if (query.filter != MessageSearchFilter::Empty) {
int index_i = -1; words += PSTRING() << " \"\a\a" << message_search_filter_index(query.filter) << "\"";
for (int i = 0; i < MESSAGES_DB_INDEX_COUNT; i++) {
if (query.index_mask == (1 << i)) {
index_i = i;
break;
}
}
if (index_i == -1) {
return Status::Error("Union of index types is not supported");
}
words += PSTRING() << " \"\a\a" << index_i << "\"";
} }
auto &stmt = get_messages_fts_stmt_; auto &stmt = get_messages_fts_stmt_;
@ -750,44 +738,20 @@ class MessagesDbImpl final : public MessagesDbSyncInterface {
} }
Result<vector<MessagesDbDialogMessage>> get_messages_from_index(DialogId dialog_id, MessageId from_message_id, Result<vector<MessagesDbDialogMessage>> get_messages_from_index(DialogId dialog_id, MessageId from_message_id,
int32 index_mask, int32 offset, int32 limit) { MessageSearchFilter filter, int32 offset,
CHECK(index_mask != 0); int32 limit) {
LOG_CHECK(index_mask < (1 << MESSAGES_DB_INDEX_COUNT)) << tag("index_mask", index_mask); auto &stmt = get_messages_from_index_stmts_[message_search_filter_index(filter)];
int index_i = -1;
for (int i = 0; i < MESSAGES_DB_INDEX_COUNT; i++) {
if (index_mask == (1 << i)) {
index_i = i;
break;
}
}
if (index_i == -1) {
return Status::Error("Union is not supported");
}
auto &stmt = get_messages_from_index_stmts_[index_i];
return get_messages_impl(stmt, dialog_id, from_message_id, offset, limit); return get_messages_impl(stmt, dialog_id, from_message_id, offset, limit);
} }
Result<MessagesDbCallsResult> get_calls(MessagesDbCallsQuery query) final { Result<MessagesDbCallsResult> get_calls(MessagesDbCallsQuery query) final {
CHECK(query.index_mask != 0);
LOG_CHECK(query.index_mask < (1 << MESSAGES_DB_INDEX_COUNT)) << tag("index_mask", query.index_mask);
int index_i = -1;
for (int i = 0; i < MESSAGES_DB_INDEX_COUNT; i++) {
if (query.index_mask == (1 << i)) {
index_i = i;
break;
}
}
if (index_i == -1) {
return Status::Error("Union is not supported");
}
int32 pos; int32 pos;
if (index_i + 1 == static_cast<int>(MessageSearchFilter::Call)) { if (query.filter == MessageSearchFilter::Call) {
pos = 0; pos = 0;
} else if (index_i + 1 == static_cast<int>(MessageSearchFilter::MissedCall)) { } else if (query.filter == MessageSearchFilter::MissedCall) {
pos = 1; pos = 1;
} else { } else {
return Status::Error(PSLICE() << "Index mask is not Call or MissedCall " << query.index_mask); return Status::Error(PSLICE() << "Filter is not Call or MissedCall: " << query.filter);
} }
auto &stmt = get_calls_stmts_[pos]; auto &stmt = get_calls_stmts_[pos];

View File

@ -9,6 +9,7 @@
#include "td/telegram/DialogId.h" #include "td/telegram/DialogId.h"
#include "td/telegram/FullMessageId.h" #include "td/telegram/FullMessageId.h"
#include "td/telegram/MessageId.h" #include "td/telegram/MessageId.h"
#include "td/telegram/MessageSearchFilter.h"
#include "td/telegram/NotificationId.h" #include "td/telegram/NotificationId.h"
#include "td/telegram/ServerMessageId.h" #include "td/telegram/ServerMessageId.h"
@ -28,7 +29,7 @@ class SqliteDb;
struct MessagesDbMessagesQuery { struct MessagesDbMessagesQuery {
DialogId dialog_id; DialogId dialog_id;
int32 index_mask{0}; MessageSearchFilter filter{MessageSearchFilter::Empty};
MessageId from_message_id; MessageId from_message_id;
int32 offset{0}; int32 offset{0};
int32 limit{100}; int32 limit{100};
@ -48,7 +49,7 @@ struct MessagesDbMessage {
struct MessagesDbFtsQuery { struct MessagesDbFtsQuery {
string query; string query;
DialogId dialog_id; DialogId dialog_id;
int32 index_mask{0}; MessageSearchFilter filter{MessageSearchFilter::Empty};
int64 from_search_id{0}; int64 from_search_id{0};
int32 limit{100}; int32 limit{100};
}; };
@ -58,10 +59,11 @@ struct MessagesDbFtsResult {
}; };
struct MessagesDbCallsQuery { struct MessagesDbCallsQuery {
int32 index_mask{0}; MessageSearchFilter filter{MessageSearchFilter::Empty};
int32 from_unique_message_id{0}; int32 from_unique_message_id{0};
int32 limit{100}; int32 limit{100};
}; };
struct MessagesDbCallsResult { struct MessagesDbCallsResult {
vector<MessagesDbMessage> messages; vector<MessagesDbMessage> messages;
}; };

View File

@ -21717,7 +21717,7 @@ std::pair<int32, vector<MessageId>> MessagesManager::search_dialog_messages(
}); });
MessagesDbMessagesQuery db_query; MessagesDbMessagesQuery db_query;
db_query.dialog_id = dialog_id; db_query.dialog_id = dialog_id;
db_query.index_mask = message_search_filter_index_mask(filter); db_query.filter = filter;
db_query.from_message_id = fixed_from_message_id; db_query.from_message_id = fixed_from_message_id;
db_query.offset = offset; db_query.offset = offset;
db_query.limit = limit; db_query.limit = limit;
@ -21815,7 +21815,7 @@ std::pair<int32, vector<FullMessageId>> MessagesManager::search_call_messages(Me
LOG(INFO) << "Search messages in database from " << fixed_from_message_id << " and with limit " << limit; LOG(INFO) << "Search messages in database from " << fixed_from_message_id << " and with limit " << limit;
MessagesDbCallsQuery db_query; MessagesDbCallsQuery db_query;
db_query.index_mask = message_search_filter_index_mask(filter); db_query.filter = filter;
db_query.from_unique_message_id = fixed_from_message_id.get_server_message_id().get(); db_query.from_unique_message_id = fixed_from_message_id.get_server_message_id().get();
db_query.limit = limit; db_query.limit = limit;
G()->td_db()->get_messages_db_async()->get_calls( G()->td_db()->get_messages_db_async()->get_calls(
@ -22351,7 +22351,7 @@ MessagesManager::FoundMessages MessagesManager::offline_search_messages(DialogId
MessagesDbFtsQuery fts_query; MessagesDbFtsQuery fts_query;
fts_query.query = query; fts_query.query = query;
fts_query.dialog_id = dialog_id; fts_query.dialog_id = dialog_id;
fts_query.index_mask = message_search_filter_index_mask(filter); fts_query.filter = filter;
if (!offset.empty()) { if (!offset.empty()) {
auto r_from_search_id = to_integer_safe<int64>(offset); auto r_from_search_id = to_integer_safe<int64>(offset);
if (r_from_search_id.is_error()) { if (r_from_search_id.is_error()) {
@ -28181,7 +28181,7 @@ Result<vector<MessagesDbDialogMessage>> MessagesManager::do_get_message_notifica
// ignore first_db_message_id, notifications can be nonconsecutive // ignore first_db_message_id, notifications can be nonconsecutive
MessagesDbMessagesQuery db_query; MessagesDbMessagesQuery db_query;
db_query.dialog_id = d->dialog_id; db_query.dialog_id = d->dialog_id;
db_query.index_mask = message_search_filter_index_mask(MessageSearchFilter::UnreadMention); db_query.filter = MessageSearchFilter::UnreadMention;
db_query.from_message_id = from_message_id; db_query.from_message_id = from_message_id;
db_query.offset = 0; db_query.offset = 0;
db_query.limit = limit; db_query.limit = limit;
@ -28303,7 +28303,7 @@ void MessagesManager::do_get_message_notifications_from_database(Dialog *d, bool
// ignore first_db_message_id, notifications can be nonconsecutive // ignore first_db_message_id, notifications can be nonconsecutive
MessagesDbMessagesQuery db_query; MessagesDbMessagesQuery db_query;
db_query.dialog_id = dialog_id; db_query.dialog_id = dialog_id;
db_query.index_mask = message_search_filter_index_mask(MessageSearchFilter::UnreadMention); db_query.filter = MessageSearchFilter::UnreadMention;
db_query.from_message_id = from_message_id; db_query.from_message_id = from_message_id;
db_query.offset = 0; db_query.offset = 0;
db_query.limit = limit; db_query.limit = limit;