2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
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/Binlog.h"
|
|
|
|
#include "td/db/binlog/BinlogEvent.h"
|
|
|
|
#include "td/db/binlog/ConcurrentBinlog.h"
|
|
|
|
#include "td/db/BinlogKeyValue.h"
|
|
|
|
#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
|
|
|
|
2018-11-11 13:58:52 +01:00
|
|
|
#include "td/actor/PromiseFuture.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 {
|
|
|
|
|
|
|
|
class SqliteConnectionSafe;
|
|
|
|
class SqliteKeyValueSafe;
|
|
|
|
class SqliteKeyValueAsyncInterface;
|
|
|
|
class SqliteKeyValue;
|
|
|
|
class MessagesDbSyncInterface;
|
|
|
|
class MessagesDbSyncSafeInterface;
|
|
|
|
class MessagesDbAsyncInterface;
|
|
|
|
class DialogDbSyncInterface;
|
|
|
|
class DialogDbSyncSafeInterface;
|
|
|
|
class DialogDbAsyncInterface;
|
|
|
|
class FileDbInterface;
|
|
|
|
|
|
|
|
class TdDb {
|
|
|
|
public:
|
|
|
|
TdDb();
|
|
|
|
TdDb(const TdDb &) = delete;
|
|
|
|
TdDb &operator=(const TdDb &) = delete;
|
|
|
|
TdDb(TdDb &&) = delete;
|
|
|
|
TdDb &operator=(TdDb &&) = delete;
|
|
|
|
~TdDb();
|
|
|
|
|
|
|
|
struct Events {
|
|
|
|
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;
|
|
|
|
vector<BinlogEvent> to_messages_manager;
|
|
|
|
};
|
2019-01-06 20:11:02 +01:00
|
|
|
static Result<unique_ptr<TdDb>> open(int32 scheduler_id, const TdParameters ¶meters, DbKey key, Events &events);
|
2019-01-06 20:59:17 +01:00
|
|
|
|
|
|
|
struct EncryptionInfo {
|
|
|
|
bool is_encrypted{false};
|
|
|
|
};
|
2019-01-06 20:11:02 +01:00
|
|
|
static Result<EncryptionInfo> check_encryption(const TdParameters ¶meters);
|
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();
|
|
|
|
ConcurrentBinlog *get_binlog();
|
|
|
|
|
2019-01-06 22:06:52 +01:00
|
|
|
std::shared_ptr<KeyValueSyncInterface> get_binlog_pmc_shared();
|
2019-01-06 23:20:38 +01:00
|
|
|
std::shared_ptr<BinlogKeyValue<ConcurrentBinlog>> get_config_pmc_shared();
|
2019-01-06 22:06:52 +01:00
|
|
|
BinlogKeyValue<ConcurrentBinlog> *get_binlog_pmc();
|
|
|
|
BinlogKeyValue<ConcurrentBinlog> *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);
|
|
|
|
|
|
|
|
MessagesDbSyncInterface *get_messages_db_sync();
|
|
|
|
MessagesDbAsyncInterface *get_messages_db_async();
|
|
|
|
|
|
|
|
DialogDbSyncInterface *get_dialog_db_sync();
|
|
|
|
DialogDbAsyncInterface *get_dialog_db_async();
|
|
|
|
|
|
|
|
void change_key(DbKey key, Promise<> promise);
|
|
|
|
|
|
|
|
void with_db_path(std::function<void(CSlice)> callback);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
std::shared_ptr<MessagesDbSyncSafeInterface> messages_db_sync_safe_;
|
|
|
|
std::shared_ptr<MessagesDbAsyncInterface> messages_db_async_;
|
|
|
|
|
|
|
|
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_;
|
|
|
|
|
|
|
|
Status init(int32 scheduler_id, const TdParameters ¶meters, DbKey key, Events &events);
|
|
|
|
Status init_sqlite(int32 scheduler_id, const TdParameters ¶meters, DbKey key, DbKey old_key,
|
|
|
|
BinlogKeyValue<Binlog> &binlog_pmc);
|
|
|
|
|
|
|
|
void do_close(Promise<> on_finished, bool destroy_flag);
|
|
|
|
};
|
|
|
|
} // namespace td
|