aboutsummaryrefslogtreecommitdiffhomepage
path: root/tddb/td/db/binlog/ConcurrentBinlog.h
blob: 33e91b11b63a3878fc219266cd7447543ac73b57 (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
62
63
64
65
66
67
68
69
70
71
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
//
// 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/Binlog.h"
#include "td/db/binlog/BinlogInterface.h"
#include "td/db/DbKey.h"

#include "td/actor/actor.h"

#include "td/utils/buffer.h"
#include "td/utils/common.h"
#include "td/utils/Promise.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"

#include <atomic>
#include <functional>

namespace td {

namespace detail {
class BinlogActor;
}  // namespace detail

class ConcurrentBinlog final : public BinlogInterface {
 public:
  using Callback = std::function<void(const BinlogEvent &)>;
  Result<BinlogInfo> init(string path, const Callback &callback, DbKey db_key = DbKey::empty(),
                          DbKey old_db_key = DbKey::empty(), int scheduler_id = -1) TD_WARN_UNUSED_RESULT;

  ConcurrentBinlog();
  explicit ConcurrentBinlog(unique_ptr<Binlog> binlog, int scheduler_id = -1);
  ConcurrentBinlog(const ConcurrentBinlog &other) = delete;
  ConcurrentBinlog &operator=(const ConcurrentBinlog &other) = delete;
  ConcurrentBinlog(ConcurrentBinlog &&other) = delete;
  ConcurrentBinlog &operator=(ConcurrentBinlog &&other) = delete;
  ~ConcurrentBinlog() final;

  void force_sync(Promise<> promise) final;
  void force_flush() final;
  void change_key(DbKey db_key, Promise<> promise) final;

  uint64 next_event_id() final {
    return last_event_id_.fetch_add(1, std::memory_order_relaxed);
  }
  uint64 next_event_id(int32 shift) final {
    return last_event_id_.fetch_add(shift, std::memory_order_relaxed);
  }

  CSlice get_path() const {
    return path_;
  }
  uint64 erase_batch(std::vector<uint64> event_ids) final;

 private:
  void init_impl(unique_ptr<Binlog> binlog, int scheduler_id);
  void close_impl(Promise<> promise) final;
  void close_and_destroy_impl(Promise<> promise) final;
  void add_raw_event_impl(uint64 event_id, BufferSlice &&raw_event, Promise<> promise, BinlogDebugInfo info) final;

  ActorOwn<detail::BinlogActor> binlog_actor_;
  string path_;
  std::atomic<uint64> last_event_id_{0};
};

}  // namespace td