2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/db/binlog/Binlog.h"
|
|
|
|
#include "td/db/binlog/BinlogInterface.h"
|
2019-10-22 19:01:15 +02:00
|
|
|
#include "td/db/DbKey.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/common.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
class BinlogActor;
|
|
|
|
} // namespace detail
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class ConcurrentBinlog final : public BinlogInterface {
|
2018-12-31 20:04:05 +01:00
|
|
|
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();
|
2018-09-27 03:19:03 +02:00
|
|
|
explicit ConcurrentBinlog(unique_ptr<Binlog> binlog, int scheduler_id = -1);
|
2018-12-31 20:04:05 +01:00
|
|
|
ConcurrentBinlog(const ConcurrentBinlog &other) = delete;
|
|
|
|
ConcurrentBinlog &operator=(const ConcurrentBinlog &other) = delete;
|
|
|
|
ConcurrentBinlog(ConcurrentBinlog &&other) = delete;
|
|
|
|
ConcurrentBinlog &operator=(ConcurrentBinlog &&other) = delete;
|
2021-07-03 22:51:36 +02:00
|
|
|
~ConcurrentBinlog() final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void force_sync(Promise<> promise) final;
|
|
|
|
void force_flush() final;
|
|
|
|
void change_key(DbKey db_key, Promise<> promise) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-01-16 10:47:37 +01:00
|
|
|
uint64 next_event_id() final {
|
|
|
|
return last_event_id_.fetch_add(1, std::memory_order_relaxed);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-01-16 10:47:37 +01:00
|
|
|
uint64 next_event_id(int32 shift) final {
|
|
|
|
return last_event_id_.fetch_add(shift, std::memory_order_relaxed);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CSlice get_path() const {
|
|
|
|
return path_;
|
|
|
|
}
|
2023-02-12 01:18:22 +01:00
|
|
|
|
|
|
|
uint64 erase_batch(vector<uint64> event_ids) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
2018-09-27 03:19:03 +02:00
|
|
|
void init_impl(unique_ptr<Binlog> binlog, int scheduler_id);
|
2021-07-03 22:51:36 +02:00
|
|
|
void close_impl(Promise<> promise) final;
|
|
|
|
void close_and_destroy_impl(Promise<> promise) final;
|
2023-01-16 10:47:37 +01:00
|
|
|
void add_raw_event_impl(uint64 event_id, BufferSlice &&raw_event, Promise<> promise, BinlogDebugInfo info) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
ActorOwn<detail::BinlogActor> binlog_actor_;
|
|
|
|
string path_;
|
2023-01-16 10:47:37 +01:00
|
|
|
std::atomic<uint64> last_event_id_{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2019-01-06 23:58:09 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|