Add OrderedMessages::find_message_by_date.

This commit is contained in:
levlam 2023-05-02 18:47:20 +03:00
parent f92d108319
commit 0a3ecfd03c
4 changed files with 44 additions and 37 deletions

View File

@ -22798,7 +22798,7 @@ int64 MessagesManager::get_dialog_message_by_date(DialogId dialog_id, int32 date
} while (random_id == 0 || get_dialog_message_by_date_results_.count(random_id) > 0);
get_dialog_message_by_date_results_[random_id]; // reserve place for result
auto message_id = find_message_by_date(d, d->ordered_messages.messages_.get(), date);
auto message_id = d->ordered_messages.find_message_by_date(date, get_get_message_date(d));
if (message_id.is_valid() &&
(message_id == d->last_message_id || (*MessagesConstIterator(d, message_id))->have_next)) {
get_dialog_message_by_date_results_[random_id] = {dialog_id, message_id};
@ -22874,25 +22874,6 @@ std::function<int32(MessageId)> MessagesManager::get_get_message_date(const Dial
};
}
MessageId MessagesManager::find_message_by_date(const Dialog *d, const OrderedMessage *ordered_message, int32 date) {
if (ordered_message == nullptr) {
return MessageId();
}
const Message *m = get_message(d, ordered_message->message_id);
CHECK(m != nullptr);
if (m->date > date) {
return find_message_by_date(d, ordered_message->left.get(), date);
}
auto message_id = find_message_by_date(d, ordered_message->right.get(), date);
if (message_id.is_valid()) {
return message_id;
}
return ordered_message->message_id;
}
void MessagesManager::on_get_dialog_message_by_date_from_database(DialogId dialog_id, int32 date, int64 random_id,
Result<MessageDbDialogMessage> result,
Promise<Unit> promise) {
@ -22903,7 +22884,7 @@ void MessagesManager::on_get_dialog_message_by_date_from_database(DialogId dialo
if (result.is_ok()) {
Message *m = on_get_message_from_database(d, result.ok(), false, "on_get_dialog_message_by_date_from_database");
if (m != nullptr) {
auto message_id = find_message_by_date(d, d->ordered_messages.messages_.get(), date);
auto message_id = d->ordered_messages.find_message_by_date(date, get_get_message_date(d));
if (!message_id.is_valid()) {
LOG(ERROR) << "Failed to find " << m->message_id << " in " << dialog_id << " by date " << date;
message_id = m->message_id;
@ -22927,7 +22908,7 @@ void MessagesManager::get_dialog_message_by_date_from_server(const Dialog *d, in
return promise.set_value(Unit());
}
auto message_id = find_message_by_date(d, d->ordered_messages.messages_.get(), date);
auto message_id = d->ordered_messages.find_message_by_date(date, get_get_message_date(d));
if (message_id.is_valid()) {
get_dialog_message_by_date_results_[random_id] = {d->dialog_id, message_id};
}
@ -22965,7 +22946,7 @@ void MessagesManager::on_get_dialog_message_by_date_success(DialogId dialog_id,
if (result != FullMessageId()) {
const Dialog *d = get_dialog(dialog_id);
CHECK(d != nullptr);
auto message_id = find_message_by_date(d, d->ordered_messages.messages_.get(), date);
auto message_id = d->ordered_messages.find_message_by_date(date, get_get_message_date(d));
if (!message_id.is_valid()) {
LOG(ERROR) << "Failed to find " << result.get_message_id() << " in " << dialog_id << " by date " << date;
message_id = result.get_message_id();

View File

@ -2182,8 +2182,6 @@ class MessagesManager final : public Actor {
std::function<int32(MessageId)> get_get_message_date(const Dialog *d) const;
static MessageId find_message_by_date(const Dialog *d, const OrderedMessage *ordered_message, int32 date);
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;

View File

@ -119,28 +119,54 @@ vector<MessageId> OrderedMessages::find_newer_messages(MessageId min_message_id)
return message_ids;
}
void do_find_messages_by_date(const OrderedMessage *ordered_message, int32 min_date, int32 max_date,
const std::function<int32(MessageId)> &get_date, vector<MessageId> &message_ids) {
static MessageId do_find_message_by_date(const OrderedMessage *ordered_message, int32 date,
const std::function<int32(MessageId)> &get_message_date) {
if (ordered_message == nullptr) {
return MessageId();
}
auto message_date = get_message_date(ordered_message->message_id);
if (message_date > date) {
return do_find_message_by_date(ordered_message->left.get(), date, get_message_date);
}
auto message_id = do_find_message_by_date(ordered_message->right.get(), date, get_message_date);
if (message_id.is_valid()) {
return message_id;
}
return ordered_message->message_id;
}
MessageId OrderedMessages::find_message_by_date(int32 date,
const std::function<int32(MessageId)> &get_message_date) const {
return do_find_message_by_date(messages_.get(), date, get_message_date);
}
static void do_find_messages_by_date(const OrderedMessage *ordered_message, int32 min_date, int32 max_date,
const std::function<int32(MessageId)> &get_message_date,
vector<MessageId> &message_ids) {
if (ordered_message == nullptr) {
return;
}
auto date = get_date(ordered_message->message_id);
if (date >= min_date) {
do_find_messages_by_date(ordered_message->left.get(), min_date, max_date, get_date, message_ids);
if (date <= max_date) {
auto message_date = get_message_date(ordered_message->message_id);
if (message_date >= min_date) {
do_find_messages_by_date(ordered_message->left.get(), min_date, max_date, get_message_date, message_ids);
if (message_date <= max_date) {
message_ids.push_back(ordered_message->message_id);
}
}
if (date <= max_date) {
do_find_messages_by_date(ordered_message->right.get(), min_date, max_date, get_date, message_ids);
if (message_date <= max_date) {
do_find_messages_by_date(ordered_message->right.get(), min_date, max_date, get_message_date, message_ids);
}
}
vector<MessageId> OrderedMessages::find_messages_by_date(int32 min_date, int32 max_date,
const std::function<int32(MessageId)> &get_date) const {
vector<MessageId> OrderedMessages::find_messages_by_date(
int32 min_date, int32 max_date, const std::function<int32(MessageId)> &get_message_date) const {
vector<MessageId> message_ids;
do_find_messages_by_date(messages_.get(), min_date, max_date, get_date, message_ids);
do_find_messages_by_date(messages_.get(), min_date, max_date, get_message_date, message_ids);
return message_ids;
}
} // namespace td

View File

@ -37,8 +37,10 @@ struct OrderedMessages {
vector<MessageId> find_newer_messages(MessageId min_message_id) const;
MessageId find_message_by_date(int32 date, const std::function<int32(MessageId)> &get_message_date) const;
vector<MessageId> find_messages_by_date(int32 min_date, int32 max_date,
const std::function<int32(MessageId)> &get_date) const;
const std::function<int32(MessageId)> &get_message_date) const;
};
} // namespace td