Add OrderedMessages::find_newer_messages.

This commit is contained in:
levlam 2023-05-02 18:17:06 +03:00
parent 2e0cba4eea
commit 961c9802af
4 changed files with 25 additions and 22 deletions

View File

@ -9811,8 +9811,7 @@ void MessagesManager::on_get_messages(vector<tl_object_ptr<telegram_api::Message
} }
bool MessagesManager::delete_newer_server_messages_at_the_end(Dialog *d, MessageId max_message_id) { bool MessagesManager::delete_newer_server_messages_at_the_end(Dialog *d, MessageId max_message_id) {
vector<MessageId> message_ids; auto message_ids = d->ordered_messages.find_newer_messages(max_message_id);
find_newer_messages(d->ordered_messages.messages_.get(), max_message_id, message_ids);
if (message_ids.empty()) { if (message_ids.empty()) {
return false; return false;
} }
@ -11625,21 +11624,6 @@ vector<MessageId> MessagesManager::find_dialog_messages(const Dialog *d,
return message_ids; return message_ids;
} }
void MessagesManager::find_newer_messages(const OrderedMessage *ordered_message, MessageId min_message_id,
vector<MessageId> &message_ids) {
if (ordered_message == nullptr) {
return;
}
if (ordered_message->message_id > min_message_id) {
find_newer_messages(ordered_message->left.get(), min_message_id, message_ids);
message_ids.push_back(ordered_message->message_id);
}
find_newer_messages(ordered_message->right.get(), min_message_id, message_ids);
}
void MessagesManager::find_unloadable_messages(const Dialog *d, int32 unload_before_date, void MessagesManager::find_unloadable_messages(const Dialog *d, int32 unload_before_date,
const OrderedMessage *ordered_message, vector<MessageId> &message_ids, const OrderedMessage *ordered_message, vector<MessageId> &message_ids,
bool &has_left_to_unload_messages) const { bool &has_left_to_unload_messages) const {
@ -15053,8 +15037,7 @@ void MessagesManager::remove_dialog_newer_messages(Dialog *d, MessageId from_mes
} }
invalidate_message_indexes(d); invalidate_message_indexes(d);
vector<MessageId> to_delete_message_ids; auto to_delete_message_ids = d->ordered_messages.find_newer_messages(from_message_id);
find_newer_messages(d->ordered_messages.messages_.get(), from_message_id, to_delete_message_ids);
td::remove_if(to_delete_message_ids, [](MessageId message_id) { return message_id.is_yet_unsent(); }); td::remove_if(to_delete_message_ids, [](MessageId message_id) { return message_id.is_yet_unsent(); });
if (!to_delete_message_ids.empty()) { if (!to_delete_message_ids.empty()) {
LOG(INFO) << "Delete " << format::as_array(to_delete_message_ids) << " newer than " << from_message_id << " in " LOG(INFO) << "Delete " << format::as_array(to_delete_message_ids) << " newer than " << from_message_id << " in "

View File

@ -2185,9 +2185,6 @@ class MessagesManager final : public Actor {
static void find_messages_by_date(const Dialog *d, const OrderedMessage *ordered_message, int32 min_date, static void find_messages_by_date(const Dialog *d, const OrderedMessage *ordered_message, int32 min_date,
int32 max_date, vector<MessageId> &message_ids); int32 max_date, vector<MessageId> &message_ids);
static void find_newer_messages(const OrderedMessage *ordered_message, MessageId min_message_id,
vector<MessageId> &message_ids);
void find_unloadable_messages(const Dialog *d, int32 unload_before_date, const OrderedMessage *ordered_message, void find_unloadable_messages(const Dialog *d, int32 unload_before_date, const OrderedMessage *ordered_message,
vector<MessageId> &message_ids, bool &has_left_to_unload_messages) const; vector<MessageId> &message_ids, bool &has_left_to_unload_messages) const;

View File

@ -98,4 +98,25 @@ vector<MessageId> OrderedMessages::find_older_messages(MessageId max_message_id)
return message_ids; return message_ids;
} }
static void do_find_newer_messages(const OrderedMessage *ordered_message, MessageId min_message_id,
vector<MessageId> &message_ids) {
if (ordered_message == nullptr) {
return;
}
if (ordered_message->message_id > min_message_id) {
do_find_newer_messages(ordered_message->left.get(), min_message_id, message_ids);
message_ids.push_back(ordered_message->message_id);
}
do_find_newer_messages(ordered_message->right.get(), min_message_id, message_ids);
}
vector<MessageId> OrderedMessages::find_newer_messages(MessageId min_message_id) const {
vector<MessageId> message_ids;
do_find_newer_messages(messages_.get(), min_message_id, message_ids);
return message_ids;
}
} // namespace td } // namespace td

View File

@ -32,6 +32,8 @@ struct OrderedMessages {
void erase(MessageId message_id); void erase(MessageId message_id);
vector<MessageId> find_older_messages(MessageId max_message_id) const; vector<MessageId> find_older_messages(MessageId max_message_id) const;
vector<MessageId> find_newer_messages(MessageId min_message_id) const;
}; };
} // namespace td } // namespace td