2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
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)
|
|
|
|
//
|
|
|
|
#include "td/telegram/DialogDb.h"
|
2018-07-03 21:29:04 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
#include "td/actor/SchedulerLocalStorage.h"
|
|
|
|
|
2019-01-07 01:17:11 +01:00
|
|
|
#include "td/db/SqliteConnectionSafe.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/db/SqliteDb.h"
|
|
|
|
#include "td/db/SqliteStatement.h"
|
|
|
|
|
2020-06-07 17:14:52 +02:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
2020-06-07 17:14:52 +02:00
|
|
|
#include "td/utils/misc.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/ScopeGuard.h"
|
|
|
|
#include "td/utils/Time.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
// NB: must happen inside a transaction
|
2020-05-03 00:10:54 +02:00
|
|
|
Status init_dialog_db(SqliteDb &db, int32 version, KeyValueSyncInterface &binlog_pmc, bool &was_created) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: must happen inside a transaction
|
|
|
|
Status drop_dialog_db(SqliteDb &db, int version) {
|
2020-05-23 21:27:24 +02:00
|
|
|
return Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class DialogDbImpl : public DialogDbSyncInterface {
|
|
|
|
public:
|
2020-05-23 21:27:24 +02:00
|
|
|
explicit DialogDbImpl(SqliteDb db) {
|
2018-12-31 20:04:05 +01:00
|
|
|
init().ensure();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status init() {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-08-26 19:08:51 +02:00
|
|
|
Status add_dialog(DialogId dialog_id, FolderId folder_id, int64 order, BufferSlice data,
|
2018-12-22 21:24:18 +01:00
|
|
|
vector<NotificationGroupKey> notification_groups) override {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<BufferSlice> get_dialog(DialogId dialog_id) override {
|
2020-05-23 21:27:24 +02:00
|
|
|
return Status::Error("Not found");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-12-20 18:24:49 +01:00
|
|
|
Result<NotificationGroupKey> get_notification_group(NotificationGroupId notification_group_id) override {
|
2020-05-23 21:27:24 +02:00
|
|
|
return Status::Error("Not found");
|
2018-11-27 15:39:13 +01:00
|
|
|
}
|
|
|
|
|
2019-12-26 02:12:26 +01:00
|
|
|
Result<int32> get_secret_chat_count(FolderId folder_id) override {
|
2020-05-23 21:27:24 +02:00
|
|
|
return 0;
|
2019-12-26 02:12:26 +01:00
|
|
|
}
|
|
|
|
|
2019-09-15 03:15:46 +02:00
|
|
|
Result<DialogDbGetDialogsResult> get_dialogs(FolderId folder_id, int64 order, DialogId dialog_id,
|
|
|
|
int32 limit) override {
|
|
|
|
DialogDbGetDialogsResult result;
|
|
|
|
return std::move(result);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-04-23 01:31:16 +02:00
|
|
|
|
2018-12-22 21:24:18 +01:00
|
|
|
Result<vector<NotificationGroupKey>> get_notification_groups_by_last_notification_date(
|
2018-12-20 18:24:49 +01:00
|
|
|
NotificationGroupKey notification_group_key, int32 limit) override {
|
2018-12-22 21:24:18 +01:00
|
|
|
vector<NotificationGroupKey> notification_groups;
|
2018-12-20 18:24:49 +01:00
|
|
|
return std::move(notification_groups);
|
2018-11-06 22:11:34 +01:00
|
|
|
}
|
2020-04-23 01:31:16 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status begin_transaction() override {
|
2020-05-23 21:27:24 +02:00
|
|
|
return Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
Status commit_transaction() override {
|
2020-05-23 21:27:24 +02:00
|
|
|
return Status::OK();
|
2019-01-07 04:23:42 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
std::shared_ptr<DialogDbSyncSafeInterface> create_dialog_db_sync(
|
|
|
|
std::shared_ptr<SqliteConnectionSafe> sqlite_connection) {
|
|
|
|
class DialogDbSyncSafe : public DialogDbSyncSafeInterface {
|
|
|
|
public:
|
|
|
|
explicit DialogDbSyncSafe(std::shared_ptr<SqliteConnectionSafe> sqlite_connection)
|
|
|
|
: lsls_db_([safe_connection = std::move(sqlite_connection)] {
|
2018-09-27 03:19:03 +02:00
|
|
|
return make_unique<DialogDbImpl>(safe_connection->get().clone());
|
2018-12-31 20:04:05 +01:00
|
|
|
}) {
|
|
|
|
}
|
|
|
|
DialogDbSyncInterface &get() override {
|
|
|
|
return *lsls_db_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-09-27 03:19:03 +02:00
|
|
|
LazySchedulerLocalStorage<unique_ptr<DialogDbSyncInterface>> lsls_db_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
return std::make_shared<DialogDbSyncSafe>(std::move(sqlite_connection));
|
|
|
|
}
|
|
|
|
|
|
|
|
class DialogDbAsync : public DialogDbAsyncInterface {
|
|
|
|
public:
|
|
|
|
DialogDbAsync(std::shared_ptr<DialogDbSyncSafeInterface> sync_db, int32 scheduler_id) {
|
|
|
|
impl_ = create_actor_on_scheduler<Impl>("DialogDbActor", scheduler_id, std::move(sync_db));
|
|
|
|
}
|
|
|
|
|
2019-08-26 19:08:51 +02:00
|
|
|
void add_dialog(DialogId dialog_id, FolderId folder_id, int64 order, BufferSlice data,
|
|
|
|
vector<NotificationGroupKey> notification_groups, Promise<> promise) override {
|
|
|
|
send_closure(impl_, &Impl::add_dialog, dialog_id, folder_id, order, std::move(data), std::move(notification_groups),
|
2018-12-20 18:24:49 +01:00
|
|
|
std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void get_notification_groups_by_last_notification_date(NotificationGroupKey notification_group_key, int32 limit,
|
2018-12-22 21:24:18 +01:00
|
|
|
Promise<vector<NotificationGroupKey>> promise) override {
|
2018-12-20 18:24:49 +01:00
|
|
|
send_closure(impl_, &Impl::get_notification_groups_by_last_notification_date, notification_group_key, limit,
|
|
|
|
std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void get_notification_group(NotificationGroupId notification_group_id,
|
|
|
|
Promise<NotificationGroupKey> promise) override {
|
|
|
|
send_closure(impl_, &Impl::get_notification_group, notification_group_id, std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-12-20 18:24:49 +01:00
|
|
|
|
2019-12-26 02:12:26 +01:00
|
|
|
void get_secret_chat_count(FolderId folder_id, Promise<int32> promise) override {
|
|
|
|
send_closure(impl_, &Impl::get_secret_chat_count, folder_id, std::move(promise));
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void get_dialog(DialogId dialog_id, Promise<BufferSlice> promise) override {
|
|
|
|
send_closure_later(impl_, &Impl::get_dialog, dialog_id, std::move(promise));
|
|
|
|
}
|
2019-12-26 02:12:26 +01:00
|
|
|
|
2019-08-22 17:24:02 +02:00
|
|
|
void get_dialogs(FolderId folder_id, int64 order, DialogId dialog_id, int32 limit,
|
2019-09-15 03:15:46 +02:00
|
|
|
Promise<DialogDbGetDialogsResult> promise) override {
|
2019-08-22 17:24:02 +02:00
|
|
|
send_closure_later(impl_, &Impl::get_dialogs, folder_id, order, dialog_id, limit, std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-12-26 02:12:26 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void close(Promise<> promise) override {
|
|
|
|
send_closure_later(impl_, &Impl::close, std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
class Impl : public Actor {
|
|
|
|
public:
|
|
|
|
explicit Impl(std::shared_ptr<DialogDbSyncSafeInterface> sync_db_safe) : sync_db_safe_(std::move(sync_db_safe)) {
|
|
|
|
}
|
2019-12-26 02:12:26 +01:00
|
|
|
|
2019-08-26 19:08:51 +02:00
|
|
|
void add_dialog(DialogId dialog_id, FolderId folder_id, int64 order, BufferSlice data,
|
|
|
|
vector<NotificationGroupKey> notification_groups, Promise<> promise) {
|
2020-09-27 01:20:42 +02:00
|
|
|
add_write_query([this, dialog_id, folder_id, order, promise = std::move(promise), data = std::move(data),
|
2018-12-20 18:24:49 +01:00
|
|
|
notification_groups = std::move(notification_groups)](Unit) mutable {
|
2020-09-27 01:20:42 +02:00
|
|
|
on_write_result(std::move(promise), sync_db_->add_dialog(dialog_id, folder_id, order, std::move(data),
|
|
|
|
std::move(notification_groups)));
|
2018-12-31 20:04:05 +01:00
|
|
|
});
|
|
|
|
}
|
2019-12-26 02:12:26 +01:00
|
|
|
|
2019-08-09 20:30:01 +02:00
|
|
|
void on_write_result(Promise<> promise, Status status) {
|
2020-07-27 17:03:22 +02:00
|
|
|
// We are inside a transaction and don't know how to handle the error
|
|
|
|
status.ensure();
|
2019-08-09 20:30:01 +02:00
|
|
|
pending_write_results_.emplace_back(std::move(promise), std::move(status));
|
|
|
|
}
|
2019-12-26 02:12:26 +01:00
|
|
|
|
2018-12-20 18:24:49 +01:00
|
|
|
void get_notification_groups_by_last_notification_date(NotificationGroupKey notification_group_key, int32 limit,
|
2018-12-22 21:24:18 +01:00
|
|
|
Promise<vector<NotificationGroupKey>> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
add_read_query();
|
2018-12-20 18:24:49 +01:00
|
|
|
promise.set_result(sync_db_->get_notification_groups_by_last_notification_date(notification_group_key, limit));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-12-20 18:24:49 +01:00
|
|
|
|
|
|
|
void get_notification_group(NotificationGroupId notification_group_id, Promise<NotificationGroupKey> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
add_read_query();
|
2018-12-20 18:24:49 +01:00
|
|
|
promise.set_result(sync_db_->get_notification_group(notification_group_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-12-26 02:12:26 +01:00
|
|
|
|
|
|
|
void get_secret_chat_count(FolderId folder_id, Promise<int32> promise) {
|
|
|
|
add_read_query();
|
|
|
|
promise.set_result(sync_db_->get_secret_chat_count(folder_id));
|
|
|
|
}
|
|
|
|
|
2018-12-20 18:24:49 +01:00
|
|
|
void get_dialog(DialogId dialog_id, Promise<BufferSlice> promise) {
|
2018-11-06 22:11:34 +01:00
|
|
|
add_read_query();
|
2018-12-20 18:24:49 +01:00
|
|
|
promise.set_result(sync_db_->get_dialog(dialog_id));
|
2018-11-06 22:11:34 +01:00
|
|
|
}
|
2019-12-26 02:12:26 +01:00
|
|
|
|
2019-08-22 17:24:02 +02:00
|
|
|
void get_dialogs(FolderId folder_id, int64 order, DialogId dialog_id, int32 limit,
|
2019-09-15 03:15:46 +02:00
|
|
|
Promise<DialogDbGetDialogsResult> promise) {
|
2018-11-27 15:39:13 +01:00
|
|
|
add_read_query();
|
2019-08-22 17:24:02 +02:00
|
|
|
promise.set_result(sync_db_->get_dialogs(folder_id, order, dialog_id, limit));
|
2018-11-27 15:39:13 +01:00
|
|
|
}
|
2019-12-26 02:12:26 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void close(Promise<> promise) {
|
|
|
|
do_flush();
|
|
|
|
sync_db_safe_.reset();
|
|
|
|
sync_db_ = nullptr;
|
2018-08-12 14:44:24 +02:00
|
|
|
promise.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<DialogDbSyncSafeInterface> sync_db_safe_;
|
|
|
|
DialogDbSyncInterface *sync_db_ = nullptr;
|
|
|
|
|
|
|
|
static constexpr size_t MAX_PENDING_QUERIES_COUNT{50};
|
2019-03-31 21:46:32 +02:00
|
|
|
static constexpr double MAX_PENDING_QUERIES_DELAY{0.01};
|
2019-08-09 20:30:01 +02:00
|
|
|
|
|
|
|
//NB: order is important, destructor of pending_writes_ will change pending_write_results_
|
|
|
|
std::vector<std::pair<Promise<>, Status>> pending_write_results_;
|
2018-12-22 21:24:18 +01:00
|
|
|
vector<Promise<>> pending_writes_;
|
2018-12-31 20:04:05 +01:00
|
|
|
double wakeup_at_ = 0;
|
2019-12-26 02:12:26 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class F>
|
|
|
|
void add_write_query(F &&f) {
|
|
|
|
pending_writes_.push_back(PromiseCreator::lambda(std::forward<F>(f), PromiseCreator::Ignore()));
|
|
|
|
if (pending_writes_.size() > MAX_PENDING_QUERIES_COUNT) {
|
|
|
|
do_flush();
|
|
|
|
wakeup_at_ = 0;
|
|
|
|
} else if (wakeup_at_ == 0) {
|
|
|
|
wakeup_at_ = Time::now_cached() + MAX_PENDING_QUERIES_DELAY;
|
|
|
|
}
|
|
|
|
if (wakeup_at_ != 0) {
|
|
|
|
set_timeout_at(wakeup_at_);
|
|
|
|
}
|
|
|
|
}
|
2019-12-26 02:12:26 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void add_read_query() {
|
|
|
|
}
|
2019-12-26 02:12:26 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void do_flush() {
|
|
|
|
}
|
2019-12-26 02:12:26 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void timeout_expired() override {
|
|
|
|
}
|
|
|
|
|
|
|
|
void start_up() override {
|
|
|
|
sync_db_ = &sync_db_safe_->get();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
ActorOwn<Impl> impl_;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::shared_ptr<DialogDbAsyncInterface> create_dialog_db_async(std::shared_ptr<DialogDbSyncSafeInterface> sync_db,
|
|
|
|
int32 scheduler_id) {
|
|
|
|
return std::make_shared<DialogDbAsync>(std::move(sync_db), scheduler_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|