Store scheduled messages in a separate treap.

GitOrigin-RevId: dc56897b3faa45e5092d036bbb73f357a6e4c607
This commit is contained in:
levlam 2019-11-30 17:04:59 +03:00
parent 1d9c00e9f4
commit 40d13deeaa
2 changed files with 16 additions and 20 deletions

View File

@ -12290,16 +12290,16 @@ unique_ptr<MessagesManager::Message> MessagesManager::do_delete_scheduled_messag
CHECK(message_id.is_valid_scheduled()); CHECK(message_id.is_valid_scheduled());
FullMessageId full_message_id(d->dialog_id, message_id); FullMessageId full_message_id(d->dialog_id, message_id);
unique_ptr<Message> *v = treap_find_message(&d->messages, message_id); // TODO search in persistent_messages unique_ptr<Message> *v = treap_find_message(&d->scheduled_messages, message_id);
if (*v == nullptr) { if (*v == nullptr) {
LOG(INFO) << message_id << " is not found in " << d->dialog_id << " to be deleted from " << source; LOG(INFO) << message_id << " is not found in " << d->dialog_id << " to be deleted from " << source;
if (get_message_force(d, message_id, "do_delete_message") == nullptr) { if (get_message_force(d, message_id, "do_delete_scheduled_message") == nullptr) {
// currently there may be a race between add_message_to_database and get_message_force, // currently there may be a race between add_message_to_database and get_message_force,
// so delete a message from database just in case // so delete a message from database just in case
delete_message_from_database(d, message_id, nullptr, true); delete_message_from_database(d, message_id, nullptr, true);
return nullptr; return nullptr;
} }
v = treap_find_message(&d->messages, message_id); v = treap_find_message(&d->scheduled_messages, message_id);
CHECK(*v != nullptr); CHECK(*v != nullptr);
} }
@ -12312,9 +12312,9 @@ unique_ptr<MessagesManager::Message> MessagesManager::do_delete_scheduled_messag
remove_message_file_sources(d->dialog_id, m); remove_message_file_sources(d->dialog_id, m);
// TODO delete the message auto result = treap_delete_message(v);
// cancel_send_deleted_message(d->dialog_id, result.get()); cancel_send_deleted_message(d->dialog_id, result.get());
return nullptr; return nullptr;
} }
@ -24422,17 +24422,15 @@ MessagesManager::Message *MessagesManager::get_message(Dialog *d, MessageId mess
} }
const MessagesManager::Message *MessagesManager::get_message(const Dialog *d, MessageId message_id) { const MessagesManager::Message *MessagesManager::get_message(const Dialog *d, MessageId message_id) {
if (!message_id.is_valid()) { if (!message_id.is_valid() && !message_id.is_valid_scheduled()) {
if (message_id.is_valid_scheduled()) {
// TODO return scheduled message
}
return nullptr; return nullptr;
} }
CHECK(d != nullptr); CHECK(d != nullptr);
LOG(DEBUG) << "Search for " << message_id << " in " << d->dialog_id; LOG(DEBUG) << "Search for " << message_id << " in " << d->dialog_id;
auto result = treap_find_message(&d->messages, message_id)->get(); bool is_scheduled = message_id.is_scheduled();
if (result != nullptr) { auto result = treap_find_message(is_scheduled ? &d->scheduled_messages : &d->messages, message_id)->get();
if (result != nullptr && !is_scheduled) {
result->last_access_date = G()->unix_time_cached(); result->last_access_date = G()->unix_time_cached();
} }
return result; return result;
@ -24747,7 +24745,7 @@ MessagesManager::Message *MessagesManager::add_message_to_dialog(Dialog *d, uniq
{ {
unique_ptr<Message> *v = treap_find_message(&d->messages, message_id); unique_ptr<Message> *v = treap_find_message(&d->messages, message_id);
if (*v == nullptr && !message->from_database) { if (*v == nullptr && !message->from_database) {
if (G()->parameters().use_message_db && get_message_force(d, message_id, "add_message_to_dialog 2") != nullptr) { if (get_message_force(d, message_id, "add_message_to_dialog 2") != nullptr) {
v = treap_find_message(&d->messages, message_id); v = treap_find_message(&d->messages, message_id);
CHECK(*v != nullptr); CHECK(*v != nullptr);
} }
@ -25380,13 +25378,11 @@ MessagesManager::Message *MessagesManager::add_scheduled_message_to_dialog(Dialo
} }
{ {
// TODO find the scheduled message unique_ptr<Message> *v = treap_find_message(&d->scheduled_messages, message_id);
unique_ptr<Message> *v = treap_find_message(&d->messages, message_id);
if (*v == nullptr && !message->from_database) { if (*v == nullptr && !message->from_database) {
// load message from database before updating it // load message from database before updating it
if (G()->parameters().use_message_db && if (get_message_force(d, message_id, "add_scheduled_message_to_dialog") != nullptr) {
get_message_force(d, message_id, "add_scheduled_message_to_dialog") != nullptr) { v = treap_find_message(&d->scheduled_messages, message_id);
v = treap_find_message(&d->messages, message_id);
CHECK(*v != nullptr); CHECK(*v != nullptr);
} }
} }
@ -25450,8 +25446,7 @@ MessagesManager::Message *MessagesManager::add_scheduled_message_to_dialog(Dialo
update_used_hashtags(dialog_id, m); update_used_hashtags(dialog_id, m);
} }
// TODO insert scheduled message Message *result_message = treap_insert_message(&d->scheduled_messages, std::move(message));
Message *result_message = treap_insert_message(&d->messages, std::move(message));
CHECK(result_message != nullptr); CHECK(result_message != nullptr);
return result_message; return result_message;
} }

View File

@ -1123,7 +1123,8 @@ class MessagesManager : public Actor {
std::unordered_set<MessageId, MessageIdHash> pending_viewed_message_ids; std::unordered_set<MessageId, MessageIdHash> pending_viewed_message_ids;
unique_ptr<Message> messages = nullptr; unique_ptr<Message> messages;
unique_ptr<Message> scheduled_messages;
struct MessageOp { struct MessageOp {
enum : int8 { Add, SetPts, Delete, DeleteAll } type; enum : int8 { Add, SetPts, Delete, DeleteAll } type;