2020-09-21 14:21:41 +02:00
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
|
2020-12-23 08:41:56 +01:00
|
|
|
#include "td/db/binlog/BinlogHelper.h"
|
2020-09-21 14:21:41 +02:00
|
|
|
|
|
|
|
#include "td/utils/logging.h"
|
2020-09-24 18:15:42 +02:00
|
|
|
#include "td/utils/Status.h"
|
2020-09-21 14:21:41 +02:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2020-09-24 18:15:42 +02:00
|
|
|
void add_log_event(LogEventIdWithGeneration &log_event_id, const Storer &storer, uint32 type, Slice name) {
|
2020-09-21 14:21:41 +02:00
|
|
|
LOG(INFO) << "Save " << name << " to binlog";
|
2020-09-22 01:15:09 +02:00
|
|
|
if (log_event_id.log_event_id == 0) {
|
|
|
|
log_event_id.log_event_id = binlog_add(G()->td_db()->get_binlog(), type, storer);
|
|
|
|
LOG(INFO) << "Add " << name << " log event " << log_event_id.log_event_id;
|
2020-09-21 14:21:41 +02:00
|
|
|
} else {
|
2020-09-22 01:15:09 +02:00
|
|
|
auto new_log_event_id = binlog_rewrite(G()->td_db()->get_binlog(), log_event_id.log_event_id, type, storer);
|
|
|
|
LOG(INFO) << "Rewrite " << name << " log event " << log_event_id.log_event_id << " with " << new_log_event_id;
|
2020-09-21 14:21:41 +02:00
|
|
|
}
|
2020-09-22 01:15:09 +02:00
|
|
|
log_event_id.generation++;
|
2020-09-21 14:21:41 +02:00
|
|
|
}
|
|
|
|
|
2020-09-24 18:15:42 +02:00
|
|
|
void delete_log_event(LogEventIdWithGeneration &log_event_id, uint64 generation, Slice name) {
|
2020-09-22 01:15:09 +02:00
|
|
|
LOG(INFO) << "Finish to process " << name << " log event " << log_event_id.log_event_id << " with generation "
|
|
|
|
<< generation;
|
|
|
|
if (log_event_id.generation == generation) {
|
|
|
|
CHECK(log_event_id.log_event_id != 0);
|
|
|
|
LOG(INFO) << "Delete " << name << " log event " << log_event_id.log_event_id;
|
|
|
|
binlog_erase(G()->td_db()->get_binlog(), log_event_id.log_event_id);
|
|
|
|
log_event_id.log_event_id = 0;
|
2020-09-21 14:21:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
Promise<Unit> get_erase_log_event_promise(uint64 log_event_id, Promise<Unit> promise) {
|
|
|
|
if (log_event_id == 0) {
|
2020-09-21 14:21:41 +02:00
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
return PromiseCreator::lambda([log_event_id, promise = std::move(promise)](Result<Unit> result) mutable {
|
2020-09-21 14:21:41 +02:00
|
|
|
if (!G()->close_flag()) {
|
2020-09-22 01:15:09 +02:00
|
|
|
binlog_erase(G()->td_db()->get_binlog(), log_event_id);
|
2020-09-21 14:21:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
promise.set_result(std::move(result));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|