From e9ee762f6082c248b40001191c04fbe4797fdf09 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 2 May 2023 21:54:11 +0300 Subject: [PATCH] Implement find_unloadable_messages with traverse_messages. --- td/telegram/MessagesManager.cpp | 63 ++++++++++++++++----------------- td/telegram/MessagesManager.h | 4 +-- 2 files changed, 32 insertions(+), 35 deletions(-) diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 52db35e0a..42d36c2f7 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -11624,37 +11624,35 @@ vector MessagesManager::find_dialog_messages(const Dialog *d, return message_ids; } -void MessagesManager::find_unloadable_messages(const Dialog *d, int32 unload_before_date, - const OrderedMessage *ordered_message, vector &message_ids, - bool &has_left_to_unload_messages) const { - if (ordered_message == nullptr) { - return; - } - if (message_ids.size() >= MAX_UNLOADED_MESSAGES) { - has_left_to_unload_messages = true; - return; - } +vector MessagesManager::find_unloadable_messages(const Dialog *d, int32 unload_before_date, + bool &has_left_to_unload_messages) const { + vector message_ids; + d->ordered_messages.traverse_messages( + [&](MessageId message_id) { + if (message_ids.size() >= MAX_UNLOADED_MESSAGES) { + has_left_to_unload_messages = true; + return false; + } + return true; + }, + [&](MessageId message_id) { + const Message *m = get_message(d, message_id); + CHECK(m != nullptr); + if (can_unload_message(d, m)) { + if (m->last_access_date <= unload_before_date) { + message_ids.push_back(message_id); + } else { + has_left_to_unload_messages = true; + } + } - find_unloadable_messages(d, unload_before_date, ordered_message->left.get(), message_ids, - has_left_to_unload_messages); - - const Message *m = get_message(d, ordered_message->message_id); - CHECK(m != nullptr); - if (can_unload_message(d, m)) { - if (m->last_access_date <= unload_before_date) { - message_ids.push_back(ordered_message->message_id); - } else { - has_left_to_unload_messages = true; - } - } - - if (has_left_to_unload_messages && m->date > unload_before_date) { - // we aren't interested in unloading too new messages - return; - } - - find_unloadable_messages(d, unload_before_date, ordered_message->right.get(), message_ids, - has_left_to_unload_messages); + if (has_left_to_unload_messages && m->date > unload_before_date) { + // we aren't interested in unloading too new messages + return false; + } + return true; + }); + return message_ids; } void MessagesManager::delete_dialog_messages_by_sender(DialogId dialog_id, DialogId sender_dialog_id, @@ -11919,10 +11917,9 @@ void MessagesManager::unload_dialog(DialogId dialog_id) { return; } - vector to_unload_message_ids; bool has_left_to_unload_messages = false; - find_unloadable_messages(d, G()->unix_time_cached() - get_unload_dialog_delay() + 2, - d->ordered_messages.messages_.get(), to_unload_message_ids, has_left_to_unload_messages); + auto to_unload_message_ids = + find_unloadable_messages(d, G()->unix_time_cached() - get_unload_dialog_delay() + 2, has_left_to_unload_messages); vector unloaded_message_ids; vector> unloaded_messages; diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 8caa32fae..9fc151f31 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -2055,8 +2055,8 @@ class MessagesManager final : public Actor { std::function get_get_message_date(const Dialog *d) const; - void find_unloadable_messages(const Dialog *d, int32 unload_before_date, const OrderedMessage *ordered_message, - vector &message_ids, bool &has_left_to_unload_messages) const; + vector find_unloadable_messages(const Dialog *d, int32 unload_before_date, + bool &has_left_to_unload_messages) const; void on_pending_message_views_timeout(DialogId dialog_id);