Add LogeventIdWithGeneration helper class.

GitOrigin-RevId: 838115e565a4b1b349d5248117ac869d2e73b6e1
This commit is contained in:
levlam 2020-09-21 15:21:41 +03:00
parent 3148dc0871
commit 621c920655
5 changed files with 108 additions and 133 deletions

View File

@ -439,6 +439,7 @@ set(TDLIB_SOURCE
td/telegram/JsonValue.cpp
td/telegram/LanguagePackManager.cpp
td/telegram/Location.cpp
td/telegram/logevent/LogEventHelper.cpp
td/telegram/Logging.cpp
td/telegram/MessageContent.cpp
td/telegram/MessageContentType.cpp

View File

@ -26,7 +26,6 @@
#include "td/telegram/InputMessageText.h"
#include "td/telegram/Location.h"
#include "td/telegram/logevent/LogEvent.h"
#include "td/telegram/logevent/LogEventHelper.h"
#include "td/telegram/MessageContent.h"
#include "td/telegram/MessageEntity.h"
#include "td/telegram/MessageEntity.hpp"
@ -17258,20 +17257,11 @@ Status MessagesManager::set_dialog_draft_message(DialogId dialog_id,
if (update_dialog_draft_message(d, std::move(new_draft_message), false, true)) {
if (dialog_id.get_type() != DialogType::SecretChat) {
if (G()->parameters().use_message_db) {
LOG(INFO) << "Save draft of " << dialog_id << " to binlog";
SaveDialogDraftMessageOnServerLogEvent logevent;
logevent.dialog_id_ = dialog_id;
auto storer = LogEventStorerImpl<SaveDialogDraftMessageOnServerLogEvent>(logevent);
if (d->save_draft_message_logevent_id == 0) {
d->save_draft_message_logevent_id =
binlog_add(G()->td_db()->get_binlog(), LogEvent::HandlerType::SaveDialogDraftMessageOnServer, storer);
LOG(INFO) << "Add draft logevent " << d->save_draft_message_logevent_id;
} else {
auto new_logevent_id = binlog_rewrite(G()->td_db()->get_binlog(), d->save_draft_message_logevent_id,
LogEvent::HandlerType::SaveDialogDraftMessageOnServer, storer);
LOG(INFO) << "Rewrite draft logevent " << d->save_draft_message_logevent_id << " with " << new_logevent_id;
}
d->save_draft_message_logevent_id_generation++;
add_log_event(d->save_draft_message_logevent_id,
LogEventStorerImpl<SaveDialogDraftMessageOnServerLogEvent>(logevent),
LogEvent::HandlerType::SaveDialogDraftMessageOnServer, "draft");
}
pending_draft_message_timeout_.set_timeout_in(dialog_id.get(), d->is_opened ? MIN_SAVE_DRAFT_DELAY : 0);
@ -17289,10 +17279,10 @@ void MessagesManager::save_dialog_draft_message_on_server(DialogId dialog_id) {
CHECK(d != nullptr);
Promise<> promise;
if (d->save_draft_message_logevent_id != 0) {
d->save_draft_message_logevent_id_generation++;
if (d->save_draft_message_logevent_id.logevent_id != 0) {
d->save_draft_message_logevent_id.generation++;
promise = PromiseCreator::lambda([actor_id = actor_id(this), dialog_id,
generation = d->save_draft_message_logevent_id_generation](Result<Unit> result) {
generation = d->save_draft_message_logevent_id.generation](Result<Unit> result) {
if (!G()->close_flag()) {
send_closure(actor_id, &MessagesManager::on_saved_dialog_draft_message, dialog_id, generation);
}
@ -17306,13 +17296,7 @@ void MessagesManager::save_dialog_draft_message_on_server(DialogId dialog_id) {
void MessagesManager::on_saved_dialog_draft_message(DialogId dialog_id, uint64 generation) {
auto d = get_dialog(dialog_id);
CHECK(d != nullptr);
LOG(INFO) << "Saved draft in " << dialog_id << " with logevent " << d->save_draft_message_logevent_id;
if (d->save_draft_message_logevent_id_generation == generation) {
CHECK(d->save_draft_message_logevent_id != 0);
LOG(INFO) << "Delete draft logevent " << d->save_draft_message_logevent_id;
binlog_erase(G()->td_db()->get_binlog(), d->save_draft_message_logevent_id);
d->save_draft_message_logevent_id = 0;
}
delete_log_event(d->save_draft_message_logevent_id, generation, "draft");
}
void MessagesManager::clear_all_draft_messages(bool exclude_secret_chats, Promise<Unit> &&promise) {
@ -17820,29 +17804,19 @@ void MessagesManager::update_dialog_notification_settings_on_server(DialogId dia
CHECK(d != nullptr);
if (!from_binlog && G()->parameters().use_message_db) {
LOG(INFO) << "Save notification settings of " << dialog_id << " to binlog";
UpdateDialogNotificationSettingsOnServerLogEvent logevent;
logevent.dialog_id_ = dialog_id;
auto storer = LogEventStorerImpl<UpdateDialogNotificationSettingsOnServerLogEvent>(logevent);
if (d->save_notification_settings_logevent_id == 0) {
d->save_notification_settings_logevent_id = binlog_add(
G()->td_db()->get_binlog(), LogEvent::HandlerType::UpdateDialogNotificationSettingsOnServer, storer);
LOG(INFO) << "Add notification settings logevent " << d->save_notification_settings_logevent_id;
} else {
auto new_logevent_id = binlog_rewrite(G()->td_db()->get_binlog(), d->save_notification_settings_logevent_id,
LogEvent::HandlerType::UpdateDialogNotificationSettingsOnServer, storer);
LOG(INFO) << "Rewrite notification settings logevent " << d->save_notification_settings_logevent_id << " with "
<< new_logevent_id;
}
d->save_notification_settings_logevent_id_generation++;
add_log_event(d->save_notification_settings_logevent_id,
LogEventStorerImpl<UpdateDialogNotificationSettingsOnServerLogEvent>(logevent),
LogEvent::HandlerType::UpdateDialogNotificationSettingsOnServer, "notification settings");
}
Promise<> promise;
if (d->save_notification_settings_logevent_id != 0) {
d->save_notification_settings_logevent_id_generation++;
if (d->save_notification_settings_logevent_id.logevent_id != 0) {
d->save_notification_settings_logevent_id.generation++;
promise = PromiseCreator::lambda(
[actor_id = actor_id(this), dialog_id,
generation = d->save_notification_settings_logevent_id_generation](Result<Unit> result) {
generation = d->save_notification_settings_logevent_id.generation](Result<Unit> result) {
if (!G()->close_flag()) {
send_closure(actor_id, &MessagesManager::on_updated_dialog_notification_settings, dialog_id, generation);
}
@ -17864,14 +17838,7 @@ void MessagesManager::on_updated_dialog_notification_settings(DialogId dialog_id
CHECK(!td_->auth_manager_->is_bot());
auto d = get_dialog(dialog_id);
CHECK(d != nullptr);
LOG(INFO) << "Saved notification settings in " << dialog_id << " with logevent "
<< d->save_notification_settings_logevent_id;
if (d->save_notification_settings_logevent_id_generation == generation) {
CHECK(d->save_notification_settings_logevent_id != 0);
LOG(INFO) << "Delete notification settings logevent " << d->save_notification_settings_logevent_id;
binlog_erase(G()->td_db()->get_binlog(), d->save_notification_settings_logevent_id);
d->save_notification_settings_logevent_id = 0;
}
delete_log_event(d->save_notification_settings_logevent_id, generation, "notification settings");
}
Status MessagesManager::set_dialog_client_data(DialogId dialog_id, string &&client_data) {
@ -19157,36 +19124,16 @@ void MessagesManager::read_history_on_server(Dialog *d, MessageId max_message_id
ReadHistoryInSecretChatLogEvent logevent;
logevent.dialog_id_ = dialog_id;
logevent.max_date_ = m->date;
add_log_event(d->read_history_logevent_id, LogEventStorerImpl<ReadHistoryInSecretChatLogEvent>(logevent),
LogEvent::HandlerType::ReadHistoryInSecretChat, "read history");
d->last_read_inbox_message_date = m->date;
auto storer = LogEventStorerImpl<ReadHistoryInSecretChatLogEvent>(logevent);
if (d->read_history_logevent_id == 0) {
d->read_history_logevent_id =
binlog_add(G()->td_db()->get_binlog(), LogEvent::HandlerType::ReadHistoryInSecretChat, storer);
LOG(INFO) << "Add read history logevent " << d->read_history_logevent_id;
} else {
auto new_logevent_id = binlog_rewrite(G()->td_db()->get_binlog(), d->read_history_logevent_id,
LogEvent::HandlerType::ReadHistoryInSecretChat, storer);
LOG(INFO) << "Rewrite read history logevent " << d->read_history_logevent_id << " with " << new_logevent_id;
}
d->read_history_logevent_id_generation++;
} else if (G()->parameters().use_message_db) {
ReadHistoryOnServerLogEvent logevent;
logevent.dialog_id_ = dialog_id;
logevent.max_message_id_ = max_message_id;
auto storer = LogEventStorerImpl<ReadHistoryOnServerLogEvent>(logevent);
if (d->read_history_logevent_id == 0) {
d->read_history_logevent_id =
binlog_add(G()->td_db()->get_binlog(), LogEvent::HandlerType::ReadHistoryOnServer, storer);
LOG(INFO) << "Add read history logevent " << d->read_history_logevent_id;
} else {
auto new_logevent_id = binlog_rewrite(G()->td_db()->get_binlog(), d->read_history_logevent_id,
LogEvent::HandlerType::ReadHistoryOnServer, storer);
LOG(INFO) << "Rewrite read history logevent " << d->read_history_logevent_id << " with " << new_logevent_id;
}
d->read_history_logevent_id_generation++;
add_log_event(d->read_history_logevent_id, LogEventStorerImpl<ReadHistoryOnServerLogEvent>(logevent),
LogEvent::HandlerType::ReadHistoryOnServer, "read history");
}
bool need_delay = d->is_opened && !is_secret && d->server_unread_count > 0;
@ -19210,10 +19157,10 @@ void MessagesManager::read_history_on_server_impl(DialogId dialog_id, MessageId
}
Promise<> promise;
if (d->read_history_logevent_id != 0) {
d->read_history_logevent_id_generation++;
if (d->read_history_logevent_id.logevent_id != 0) {
d->read_history_logevent_id.generation++;
promise = PromiseCreator::lambda([actor_id = actor_id(this), dialog_id,
generation = d->read_history_logevent_id_generation](Result<Unit> result) {
generation = d->read_history_logevent_id.generation](Result<Unit> result) {
if (!G()->close_flag()) {
send_closure(actor_id, &MessagesManager::on_read_history_finished, dialog_id, generation);
}
@ -19262,13 +19209,7 @@ void MessagesManager::read_history_on_server_impl(DialogId dialog_id, MessageId
void MessagesManager::on_read_history_finished(DialogId dialog_id, uint64 generation) {
auto d = get_dialog(dialog_id);
CHECK(d != nullptr);
LOG(INFO) << "Finished reading history in " << dialog_id << " with logevent " << d->read_history_logevent_id;
if (d->read_history_logevent_id_generation == generation) {
CHECK(d->read_history_logevent_id != 0);
LOG(INFO) << "Delete read history logevent " << d->read_history_logevent_id;
binlog_erase(G()->td_db()->get_binlog(), d->read_history_logevent_id);
d->read_history_logevent_id = 0;
}
delete_log_event(d->read_history_logevent_id, generation, "read history");
}
std::pair<int32, vector<MessageId>> MessagesManager::search_dialog_messages(
@ -28567,21 +28508,14 @@ void MessagesManager::set_dialog_folder_id_on_server(DialogId dialog_id, bool fr
SetDialogFolderIdOnServerLogEvent logevent;
logevent.dialog_id_ = dialog_id;
logevent.folder_id_ = d->folder_id;
auto storer = LogEventStorerImpl<SetDialogFolderIdOnServerLogEvent>(logevent);
if (d->set_folder_id_logevent_id == 0) {
d->set_folder_id_logevent_id =
binlog_add(G()->td_db()->get_binlog(), LogEvent::HandlerType::SetDialogFolderIdOnServer, storer);
} else {
binlog_rewrite(G()->td_db()->get_binlog(), d->set_folder_id_logevent_id,
LogEvent::HandlerType::SetDialogFolderIdOnServer, storer);
}
d->set_folder_id_logevent_id_generation++;
add_log_event(d->set_folder_id_logevent_id, LogEventStorerImpl<SetDialogFolderIdOnServerLogEvent>(logevent),
LogEvent::HandlerType::SetDialogFolderIdOnServer, "set chat folder");
}
Promise<> promise;
if (d->set_folder_id_logevent_id != 0) {
if (d->set_folder_id_logevent_id.logevent_id != 0) {
promise = PromiseCreator::lambda([actor_id = actor_id(this), dialog_id,
generation = d->set_folder_id_logevent_id_generation](Result<Unit> result) {
generation = d->set_folder_id_logevent_id.generation](Result<Unit> result) {
if (!G()->close_flag()) {
send_closure(actor_id, &MessagesManager::on_updated_dialog_folder_id, dialog_id, generation);
}
@ -28595,13 +28529,7 @@ void MessagesManager::set_dialog_folder_id_on_server(DialogId dialog_id, bool fr
void MessagesManager::on_updated_dialog_folder_id(DialogId dialog_id, uint64 generation) {
auto d = get_dialog(dialog_id);
CHECK(d != nullptr);
LOG(INFO) << "Saved folder_id of " << dialog_id << " with logevent " << d->set_folder_id_logevent_id;
if (d->set_folder_id_logevent_id_generation == generation) {
CHECK(d->set_folder_id_logevent_id != 0);
LOG(INFO) << "Delete set folder_id logevent " << d->set_folder_id_logevent_id;
binlog_erase(G()->td_db()->get_binlog(), d->set_folder_id_logevent_id);
d->set_folder_id_logevent_id = 0;
}
delete_log_event(d->set_folder_id_logevent_id, generation, "set chat folder");
}
void MessagesManager::set_dialog_photo(DialogId dialog_id, const tl_object_ptr<td_api::InputChatPhoto> &input_photo,
@ -34229,11 +34157,11 @@ void MessagesManager::on_binlog_events(vector<BinlogEvent> &&events) {
binlog_erase(G()->td_db()->get_binlog(), event.id_);
break;
}
if (d->read_history_logevent_id != 0) {
if (d->read_history_logevent_id.logevent_id != 0) {
// we need only latest read history event
binlog_erase(G()->td_db()->get_binlog(), d->read_history_logevent_id);
binlog_erase(G()->td_db()->get_binlog(), d->read_history_logevent_id.logevent_id);
}
d->read_history_logevent_id = event.id_;
d->read_history_logevent_id.logevent_id = event.id_;
read_history_on_server_impl(dialog_id, log_event.max_message_id_);
break;
@ -34255,11 +34183,11 @@ void MessagesManager::on_binlog_events(vector<BinlogEvent> &&events) {
binlog_erase(G()->td_db()->get_binlog(), event.id_);
break;
}
if (d->read_history_logevent_id != 0) {
if (d->read_history_logevent_id.logevent_id != 0) {
// we need only latest read history event
binlog_erase(G()->td_db()->get_binlog(), d->read_history_logevent_id);
binlog_erase(G()->td_db()->get_binlog(), d->read_history_logevent_id.logevent_id);
}
d->read_history_logevent_id = event.id_;
d->read_history_logevent_id.logevent_id = event.id_;
d->last_read_inbox_message_date = log_event.max_date_;
read_history_on_server_impl(dialog_id, MessageId());
@ -34399,7 +34327,7 @@ void MessagesManager::on_binlog_events(vector<BinlogEvent> &&events) {
binlog_erase(G()->td_db()->get_binlog(), event.id_);
break;
}
d->save_draft_message_logevent_id = event.id_;
d->save_draft_message_logevent_id.logevent_id = event.id_;
save_dialog_draft_message_on_server(dialog_id);
break;
@ -34419,7 +34347,7 @@ void MessagesManager::on_binlog_events(vector<BinlogEvent> &&events) {
binlog_erase(G()->td_db()->get_binlog(), event.id_);
break;
}
d->save_notification_settings_logevent_id = event.id_;
d->save_notification_settings_logevent_id.logevent_id = event.id_;
update_dialog_notification_settings_on_server(dialog_id, true);
break;
@ -34472,8 +34400,8 @@ void MessagesManager::on_binlog_events(vector<BinlogEvent> &&events) {
binlog_erase(G()->td_db()->get_binlog(), event.id_);
break;
}
d->set_folder_id_logevent_id = event.id_;
d->set_folder_id_logevent_id_generation++;
d->set_folder_id_logevent_id.logevent_id = event.id_;
d->set_folder_id_logevent_id.generation++;
set_dialog_folder_id(d, log_event.folder_id_);

View File

@ -24,6 +24,7 @@
#include "td/telegram/FullMessageId.h"
#include "td/telegram/Global.h"
#include "td/telegram/InputDialogId.h"
#include "td/telegram/logevent/LogEventHelper.h"
#include "td/telegram/MessageContentType.h"
#include "td/telegram/MessageCopyOptions.h"
#include "td/telegram/MessageId.h"
@ -1144,14 +1145,10 @@ class MessagesManager : public Actor {
MessageId reply_markup_message_id;
DialogNotificationSettings notification_settings;
unique_ptr<DraftMessage> draft_message;
uint64 save_draft_message_logevent_id = 0;
uint64 save_draft_message_logevent_id_generation = 0;
uint64 save_notification_settings_logevent_id = 0;
uint64 save_notification_settings_logevent_id_generation = 0;
uint64 read_history_logevent_id = 0;
uint64 read_history_logevent_id_generation = 0;
uint64 set_folder_id_logevent_id = 0;
uint64 set_folder_id_logevent_id_generation = 0;
LogeventIdWithGeneration save_draft_message_logevent_id;
LogeventIdWithGeneration save_notification_settings_logevent_id;
LogeventIdWithGeneration read_history_logevent_id;
LogeventIdWithGeneration set_folder_id_logevent_id;
FolderId folder_id;
vector<DialogListId> dialog_list_ids; // TODO replace with mask

View File

@ -0,0 +1,57 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/telegram/logevent/LogEventHelper.h"
#include "td/telegram/Global.h"
#include "td/telegram/TdDb.h"
#include "tddb/td/db/binlog/BinlogHelper.h"
#include "td/utils/logging.h"
namespace td {
void add_log_event(LogeventIdWithGeneration &logevent_id, const Storer &storer, uint32 type, Slice name) {
LOG(INFO) << "Save " << name << " to binlog";
if (logevent_id.logevent_id == 0) {
logevent_id.logevent_id =
binlog_add(G()->td_db()->get_binlog(), type, storer);
LOG(INFO) << "Add " << name << " logevent " << logevent_id.logevent_id;
} else {
auto new_logevent_id = binlog_rewrite(G()->td_db()->get_binlog(), logevent_id.logevent_id,
type, storer);
LOG(INFO) << "Rewrite " << name << " logevent " << logevent_id.logevent_id << " with " << new_logevent_id;
}
logevent_id.generation++;
}
void delete_log_event(LogeventIdWithGeneration &logevent_id, uint64 generation, Slice name) {
LOG(INFO) << "Finish to process " << name << " logevent " << logevent_id.logevent_id << " with generation " << generation;
if (logevent_id.generation == generation) {
CHECK(logevent_id.logevent_id != 0);
LOG(INFO) << "Delete " << name << " logevent " << logevent_id.logevent_id;
binlog_erase(G()->td_db()->get_binlog(), logevent_id.logevent_id);
logevent_id.logevent_id = 0;
}
}
Promise<Unit> get_erase_logevent_promise(uint64 logevent_id, Promise<Unit> promise) {
if (logevent_id == 0) {
return promise;
}
return PromiseCreator::lambda([logevent_id, promise = std::move(promise)](Result<Unit> result) mutable {
if (!G()->close_flag()) {
binlog_erase(G()->td_db()->get_binlog(), logevent_id);
}
promise.set_result(std::move(result));
});
}
} // namespace td

View File

@ -8,11 +8,6 @@
#include "td/actor/PromiseFuture.h"
#include "td/db/binlog/BinlogHelper.h"
#include "td/telegram/Global.h"
#include "td/telegram/TdDb.h"
#include "td/utils/common.h"
#include "td/utils/Status.h"
#include "td/utils/Time.h"
@ -20,19 +15,16 @@
namespace td {
inline Promise<Unit> get_erase_logevent_promise(uint64 logevent_id, Promise<Unit> promise = Promise<Unit>()) {
if (logevent_id == 0) {
return promise;
}
struct LogeventIdWithGeneration {
uint64 logevent_id = 0;
uint64 generation = 0;
};
return PromiseCreator::lambda([logevent_id, promise = std::move(promise)](Result<Unit> result) mutable {
if (!G()->close_flag()) {
binlog_erase(G()->td_db()->get_binlog(), logevent_id);
}
void add_log_event(LogeventIdWithGeneration &logevent_id, const Storer &storer, uint32 type, Slice name);
promise.set_result(std::move(result));
});
}
void delete_log_event(LogeventIdWithGeneration &logevent_id, uint64 generation, Slice name);
Promise<Unit> get_erase_logevent_promise(uint64 logevent_id, Promise<Unit> promise = Promise<Unit>());
template <class StorerT>
void store_time(double time_at, StorerT &storer) {