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
|
|
|
|
|
2018-11-11 13:58:52 +01:00
|
|
|
#include "td/telegram/TdParameters.h"
|
2018-07-03 21:29:04 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/db/binlog/BinlogEvent.h"
|
2019-01-19 18:19:29 +01:00
|
|
|
#include "td/db/binlog/BinlogInterface.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/db/DbKey.h"
|
2019-01-06 22:06:52 +01:00
|
|
|
#include "td/db/KeyValueSyncInterface.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
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 <functional>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2019-01-07 00:49:49 +01:00
|
|
|
class Binlog;
|
2019-01-07 00:44:29 +01:00
|
|
|
template <class BinlogT>
|
|
|
|
class BinlogKeyValue;
|
|
|
|
class ConcurrentBinlog;
|
2018-12-31 20:04:05 +01:00
|
|
|
class DialogDbSyncInterface;
|
|
|
|
class DialogDbSyncSafeInterface;
|
|
|
|
class DialogDbAsyncInterface;
|
|
|
|
class FileDbInterface;
|
2022-11-09 18:35:22 +01:00
|
|
|
class MessageDbSyncInterface;
|
|
|
|
class MessageDbSyncSafeInterface;
|
|
|
|
class MessageDbAsyncInterface;
|
2022-11-10 17:46:17 +01:00
|
|
|
class MessageThreadDbSyncInterface;
|
|
|
|
class MessageThreadDbSyncSafeInterface;
|
|
|
|
class MessageThreadDbAsyncInterface;
|
2019-01-07 00:44:29 +01:00
|
|
|
class SqliteConnectionSafe;
|
|
|
|
class SqliteKeyValueSafe;
|
|
|
|
class SqliteKeyValueAsyncInterface;
|
|
|
|
class SqliteKeyValue;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
class TdDb {
|
|
|
|
public:
|
|
|
|
TdDb();
|
|
|
|
TdDb(const TdDb &) = delete;
|
|
|
|
TdDb &operator=(const TdDb &) = delete;
|
|
|
|
TdDb(TdDb &&) = delete;
|
|
|
|
TdDb &operator=(TdDb &&) = delete;
|
|
|
|
~TdDb();
|
|
|
|
|
2022-09-07 20:50:41 +02:00
|
|
|
struct OpenedDatabase {
|
2022-06-21 01:02:48 +02:00
|
|
|
string database_directory;
|
|
|
|
string files_directory;
|
|
|
|
|
2022-06-10 15:44:53 +02:00
|
|
|
unique_ptr<TdDb> database;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
vector<BinlogEvent> to_secret_chats_manager;
|
|
|
|
vector<BinlogEvent> user_events;
|
|
|
|
vector<BinlogEvent> chat_events;
|
|
|
|
vector<BinlogEvent> channel_events;
|
|
|
|
vector<BinlogEvent> secret_chat_events;
|
|
|
|
vector<BinlogEvent> web_page_events;
|
2022-07-18 18:21:47 +02:00
|
|
|
vector<BinlogEvent> save_app_log_events;
|
2019-02-21 15:40:37 +01:00
|
|
|
vector<BinlogEvent> to_poll_manager;
|
2018-12-31 20:04:05 +01:00
|
|
|
vector<BinlogEvent> to_messages_manager;
|
2019-03-31 03:30:25 +02:00
|
|
|
vector<BinlogEvent> to_notification_manager;
|
2022-04-11 11:45:52 +02:00
|
|
|
vector<BinlogEvent> to_notification_settings_manager;
|
2023-01-29 23:29:33 +01:00
|
|
|
|
|
|
|
int64 since_last_open = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2022-06-10 15:44:53 +02:00
|
|
|
static void open(int32 scheduler_id, TdParameters parameters, DbKey key, Promise<OpenedDatabase> &&promise);
|
2019-01-06 20:59:17 +01:00
|
|
|
|
2019-01-06 20:11:02 +01:00
|
|
|
static Status destroy(const TdParameters ¶meters);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
std::shared_ptr<FileDbInterface> get_file_db_shared();
|
|
|
|
std::shared_ptr<SqliteConnectionSafe> &get_sqlite_connection_safe();
|
2019-04-03 11:26:20 +02:00
|
|
|
#define get_binlog() get_binlog_impl(__FILE__, __LINE__)
|
|
|
|
BinlogInterface *get_binlog_impl(const char *file, int line);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-01-06 22:06:52 +01:00
|
|
|
std::shared_ptr<KeyValueSyncInterface> get_binlog_pmc_shared();
|
2019-01-06 23:58:09 +01:00
|
|
|
std::shared_ptr<KeyValueSyncInterface> get_config_pmc_shared();
|
2022-10-21 23:38:04 +02:00
|
|
|
|
|
|
|
#define get_binlog_pmc() get_binlog_pmc_impl(__FILE__, __LINE__)
|
|
|
|
KeyValueSyncInterface *get_binlog_pmc_impl(const char *file, int line);
|
2019-01-07 00:44:29 +01:00
|
|
|
KeyValueSyncInterface *get_config_pmc();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-07-18 03:11:48 +02:00
|
|
|
SqliteKeyValue *get_sqlite_sync_pmc();
|
2018-12-31 20:04:05 +01:00
|
|
|
SqliteKeyValueAsyncInterface *get_sqlite_pmc();
|
2019-01-06 20:59:17 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
CSlice binlog_path() const;
|
|
|
|
CSlice sqlite_path() const;
|
2019-01-06 20:59:17 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void flush_all();
|
2019-01-06 20:59:17 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void close_all(Promise<> on_finished);
|
|
|
|
void close_and_destroy_all(Promise<> on_finished);
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
MessageDbSyncInterface *get_message_db_sync();
|
|
|
|
MessageDbAsyncInterface *get_message_db_async();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-11-10 17:46:17 +01:00
|
|
|
MessageThreadDbSyncInterface *get_message_thread_db_sync();
|
|
|
|
MessageThreadDbAsyncInterface *get_message_thread_db_async();
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
DialogDbSyncInterface *get_dialog_db_sync();
|
|
|
|
DialogDbAsyncInterface *get_dialog_db_async();
|
|
|
|
|
|
|
|
void change_key(DbKey key, Promise<> promise);
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
void with_db_path(const std::function<void(CSlice)> &callback);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-04-17 11:17:51 +02:00
|
|
|
Result<string> get_stats();
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
string sqlite_path_;
|
|
|
|
std::shared_ptr<SqliteConnectionSafe> sql_connection_;
|
|
|
|
|
|
|
|
std::shared_ptr<FileDbInterface> file_db_;
|
|
|
|
|
|
|
|
std::shared_ptr<SqliteKeyValueSafe> common_kv_safe_;
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<SqliteKeyValueAsyncInterface> common_kv_async_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
std::shared_ptr<MessageDbSyncSafeInterface> message_db_sync_safe_;
|
|
|
|
std::shared_ptr<MessageDbAsyncInterface> message_db_async_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-11-10 17:46:17 +01:00
|
|
|
std::shared_ptr<MessageThreadDbSyncSafeInterface> message_thread_db_sync_safe_;
|
|
|
|
std::shared_ptr<MessageThreadDbAsyncInterface> message_thread_db_async_;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
std::shared_ptr<DialogDbSyncSafeInterface> dialog_db_sync_safe_;
|
|
|
|
std::shared_ptr<DialogDbAsyncInterface> dialog_db_async_;
|
|
|
|
|
|
|
|
std::shared_ptr<BinlogKeyValue<ConcurrentBinlog>> binlog_pmc_;
|
|
|
|
std::shared_ptr<BinlogKeyValue<ConcurrentBinlog>> config_pmc_;
|
|
|
|
std::shared_ptr<ConcurrentBinlog> binlog_;
|
|
|
|
|
2022-06-29 23:46:02 +02:00
|
|
|
static void open_impl(TdParameters parameters, DbKey key, Promise<OpenedDatabase> &&promise);
|
2022-06-29 23:28:25 +02:00
|
|
|
|
2022-09-07 20:50:41 +02:00
|
|
|
static Status check_parameters(TdParameters ¶meters);
|
2022-06-29 23:28:25 +02:00
|
|
|
|
2022-06-29 23:46:02 +02:00
|
|
|
Status init_sqlite(const TdParameters ¶meters, const DbKey &key, const DbKey &old_key,
|
2018-12-31 20:04:05 +01:00
|
|
|
BinlogKeyValue<Binlog> &binlog_pmc);
|
|
|
|
|
|
|
|
void do_close(Promise<> on_finished, bool destroy_flag);
|
|
|
|
};
|
2019-10-03 19:38:47 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|