aboutsummaryrefslogtreecommitdiffhomepage
path: root/tddb/td/db/binlog/detail/BinlogEventsProcessor.h
blob: 50f243e52c3a35156de77e8ee3cf644c4c17b4d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
//
// 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)
//
#pragma once

#include "td/db/binlog/BinlogEvent.h"

#include "td/utils/common.h"
#include "td/utils/logging.h"
#include "td/utils/Status.h"

namespace td {
namespace detail {

class BinlogEventsProcessor {
 public:
  Status add_event(BinlogEvent &&event) TD_WARN_UNUSED_RESULT {
    return do_event(std::move(event));
  }

  template <class CallbackT>
  void for_each(CallbackT &&callback) {
    for (size_t i = 0; i < event_ids_.size(); i++) {
      LOG_CHECK(i == 0 || event_ids_[i - 1] < event_ids_[i])
          << event_ids_[i - 1] << " " << events_[i - 1].public_to_string() << " " << event_ids_[i] << " "
          << events_[i].public_to_string();
      if ((event_ids_[i] & 1) == 0) {
        callback(events_[i]);
      }
    }
  }

  uint64 last_event_id() const {
    return last_event_id_;
  }
  int64 offset() const {
    return offset_;
  }
  int64 total_raw_events_size() const {
    return total_raw_events_size_;
  }

 private:
  // holds (event_id * 2 + was_deleted)
  std::vector<uint64> event_ids_;
  std::vector<BinlogEvent> events_;
  size_t total_events_{0};
  size_t empty_events_{0};
  uint64 last_event_id_{0};
  int64 offset_{0};
  int64 total_raw_events_size_{0};

  Status do_event(BinlogEvent &&event);
  void compactify();
};

}  // namespace detail
}  // namespace td