2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// 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/db/binlog/detail/BinlogEventsProcessor.h"
|
|
|
|
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-07-17 13:27:24 +02:00
|
|
|
#include "td/utils/Status.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace detail {
|
2018-06-17 21:56:21 +02:00
|
|
|
|
2018-07-16 12:07:00 +02:00
|
|
|
Status BinlogEventsProcessor::do_event(BinlogEvent &&event) {
|
2018-12-31 20:04:05 +01:00
|
|
|
offset_ = event.offset_;
|
2023-01-16 10:47:37 +01:00
|
|
|
auto fixed_event_id = event.id_ * 2;
|
|
|
|
if ((event.flags_ & BinlogEvent::Flags::Rewrite) && !event_ids_.empty() && event_ids_.back() >= fixed_event_id) {
|
|
|
|
auto it = std::lower_bound(event_ids_.begin(), event_ids_.end(), fixed_event_id);
|
|
|
|
if (it == event_ids_.end() || *it != fixed_event_id) {
|
2020-09-22 01:15:09 +02:00
|
|
|
return Status::Error(PSLICE() << "Ignore rewrite log event " << event.public_to_string());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-01-16 10:47:37 +01:00
|
|
|
auto pos = it - event_ids_.begin();
|
2018-12-31 20:04:05 +01:00
|
|
|
total_raw_events_size_ -= static_cast<int64>(events_[pos].raw_event_.size());
|
|
|
|
if (event.type_ == BinlogEvent::ServiceTypes::Empty) {
|
|
|
|
*it += 1;
|
|
|
|
empty_events_++;
|
2023-02-02 19:13:52 +01:00
|
|
|
events_[pos] = {};
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
event.flags_ &= ~BinlogEvent::Flags::Rewrite;
|
|
|
|
total_raw_events_size_ += static_cast<int64>(event.raw_event_.size());
|
|
|
|
events_[pos] = std::move(event);
|
|
|
|
}
|
2018-06-07 14:41:31 +02:00
|
|
|
} else if (event.type_ < 0) {
|
|
|
|
// just skip service events
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2023-01-16 10:47:37 +01:00
|
|
|
if (!(event_ids_.empty() || event_ids_.back() < fixed_event_id)) {
|
2023-02-02 13:45:20 +01:00
|
|
|
return Status::Error(PSLICE() << offset_ << ' ' << event_ids_.size() << ' ' << event_ids_.back() << ' '
|
|
|
|
<< fixed_event_id << ' ' << event.public_to_string() << ' ' << total_events_ << ' '
|
2018-07-16 12:07:00 +02:00
|
|
|
<< total_raw_events_size_);
|
|
|
|
}
|
2023-01-16 10:47:37 +01:00
|
|
|
last_event_id_ = event.id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
total_raw_events_size_ += static_cast<int64>(event.raw_event_.size());
|
|
|
|
total_events_++;
|
2023-01-16 10:47:37 +01:00
|
|
|
event_ids_.push_back(fixed_event_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
events_.emplace_back(std::move(event));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (total_events_ > 10 && empty_events_ * 4 > total_events_ * 3) {
|
|
|
|
compactify();
|
|
|
|
}
|
2018-07-16 12:07:00 +02:00
|
|
|
return Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BinlogEventsProcessor::compactify() {
|
2023-01-16 10:47:37 +01:00
|
|
|
CHECK(event_ids_.size() == events_.size());
|
|
|
|
auto event_ids_from = event_ids_.begin();
|
|
|
|
auto event_ids_to = event_ids_from;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto events_from = events_.begin();
|
|
|
|
auto events_to = events_from;
|
2023-01-16 10:47:37 +01:00
|
|
|
for (; event_ids_from != event_ids_.end(); event_ids_from++, events_from++) {
|
|
|
|
if ((*event_ids_from & 1) == 0) {
|
|
|
|
*event_ids_to++ = *event_ids_from;
|
2023-02-09 22:27:25 +01:00
|
|
|
if (events_to != events_from) {
|
|
|
|
*events_to = std::move(*events_from);
|
|
|
|
}
|
|
|
|
events_to++;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-16 10:47:37 +01:00
|
|
|
event_ids_.erase(event_ids_to, event_ids_.end());
|
2018-12-31 20:04:05 +01:00
|
|
|
events_.erase(events_to, events_.end());
|
2023-01-16 10:47:37 +01:00
|
|
|
total_events_ = event_ids_.size();
|
2018-12-31 20:04:05 +01:00
|
|
|
empty_events_ = 0;
|
2023-01-16 10:47:37 +01:00
|
|
|
CHECK(event_ids_.size() == events_.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-06-17 21:56:21 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace td
|