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"
|
|
|
|
|
|
|
|
#include "td/telegram/Version.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/SqliteKeyValue.h"
|
|
|
|
#include "td/db/SqliteStatement.h"
|
|
|
|
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
#include "td/actor/SchedulerLocalStorage.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"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#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-11-28 18:18:50 +01:00
|
|
|
LOG(INFO) << "Init dialog database " << tag("version", version);
|
2018-12-31 20:04:05 +01:00
|
|
|
was_created = false;
|
|
|
|
|
|
|
|
// Check if database exists
|
|
|
|
TRY_RESULT(has_table, db.has_table("dialogs"));
|
|
|
|
if (!has_table) {
|
|
|
|
version = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (version < static_cast<int32>(DbVersion::DialogDbCreated) || version > current_db_version()) {
|
|
|
|
TRY_STATUS(drop_dialog_db(db, version));
|
|
|
|
version = 0;
|
|
|
|
}
|
2019-08-26 19:08:51 +02:00
|
|
|
|
2020-02-26 16:15:19 +01:00
|
|
|
auto create_notification_group_table = [&db] {
|
2018-11-06 22:11:34 +01:00
|
|
|
return db.exec(
|
2018-12-20 18:24:49 +01:00
|
|
|
"CREATE TABLE IF NOT EXISTS notification_groups (notification_group_id INT4 PRIMARY KEY, dialog_id "
|
|
|
|
"INT8, last_notification_date INT4)");
|
2018-11-06 22:11:34 +01:00
|
|
|
};
|
|
|
|
|
2020-02-26 16:15:19 +01:00
|
|
|
auto create_last_notification_date_index = [&db] {
|
2018-11-27 15:39:13 +01:00
|
|
|
return db.exec(
|
2018-12-20 18:24:49 +01:00
|
|
|
"CREATE INDEX IF NOT EXISTS notification_group_by_last_notification_date ON notification_groups "
|
2018-12-20 20:48:46 +01:00
|
|
|
"(last_notification_date, dialog_id, notification_group_id) WHERE last_notification_date IS NOT NULL");
|
2018-11-27 15:39:13 +01:00
|
|
|
};
|
|
|
|
|
2020-02-26 16:15:19 +01:00
|
|
|
auto add_dialogs_in_folder_index = [&db] {
|
2019-08-26 19:08:51 +02:00
|
|
|
return db.exec(
|
|
|
|
"CREATE INDEX IF NOT EXISTS dialog_in_folder_by_dialog_order ON dialogs (folder_id, dialog_order, dialog_id) "
|
|
|
|
"WHERE folder_id IS NOT NULL");
|
|
|
|
};
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
if (version == 0) {
|
2018-11-28 18:18:50 +01:00
|
|
|
LOG(INFO) << "Create new dialog database";
|
2018-12-31 20:04:05 +01:00
|
|
|
was_created = true;
|
|
|
|
TRY_STATUS(
|
2019-08-26 19:08:51 +02:00
|
|
|
db.exec("CREATE TABLE IF NOT EXISTS dialogs (dialog_id INT8 PRIMARY KEY, dialog_order INT8, data BLOB, "
|
|
|
|
"folder_id INT4)"));
|
2018-12-20 18:24:49 +01:00
|
|
|
TRY_STATUS(create_notification_group_table());
|
2018-11-06 22:11:34 +01:00
|
|
|
TRY_STATUS(create_last_notification_date_index());
|
2019-08-26 19:08:51 +02:00
|
|
|
TRY_STATUS(add_dialogs_in_folder_index());
|
2018-11-06 22:11:34 +01:00
|
|
|
version = current_db_version();
|
|
|
|
}
|
2018-11-28 18:18:50 +01:00
|
|
|
if (version < static_cast<int32>(DbVersion::AddNotificationsSupport)) {
|
2018-12-20 18:24:49 +01:00
|
|
|
TRY_STATUS(create_notification_group_table());
|
2018-11-06 22:11:34 +01:00
|
|
|
TRY_STATUS(create_last_notification_date_index());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-08-26 19:08:51 +02:00
|
|
|
if (version < static_cast<int32>(DbVersion::AddFolders)) {
|
|
|
|
TRY_STATUS(db.exec("DROP INDEX IF EXISTS dialog_by_dialog_order"));
|
|
|
|
TRY_STATUS(db.exec("ALTER TABLE dialogs ADD COLUMN folder_id INT4"));
|
|
|
|
TRY_STATUS(add_dialogs_in_folder_index());
|
2020-03-16 14:32:27 +01:00
|
|
|
TRY_STATUS(db.exec("UPDATE dialogs SET folder_id = 0 WHERE dialog_id < -1500000000000 AND dialog_order > 0"));
|
2019-08-26 19:08:51 +02:00
|
|
|
}
|
2020-05-03 00:10:54 +02:00
|
|
|
if (version < static_cast<int32>(DbVersion::StorePinnedDialogsInBinlog)) {
|
2020-06-07 17:14:52 +02:00
|
|
|
// 9221294780217032704 == get_dialog_order(Auto(), MIN_PINNED_DIALOG_DATE - 1)
|
2020-05-03 00:10:54 +02:00
|
|
|
TRY_RESULT(get_pinned_dialogs_stmt,
|
|
|
|
db.get_statement("SELECT dialog_id FROM dialogs WHERE folder_id == ?1 AND dialog_order > "
|
|
|
|
"9221294780217032704 ORDER BY dialog_order DESC, dialog_id DESC"));
|
|
|
|
for (auto folder_id = 0; folder_id < 2; folder_id++) {
|
|
|
|
vector<string> pinned_dialog_ids;
|
|
|
|
TRY_STATUS(get_pinned_dialogs_stmt.bind_int32(1, folder_id));
|
|
|
|
TRY_STATUS(get_pinned_dialogs_stmt.step());
|
|
|
|
while (get_pinned_dialogs_stmt.has_row()) {
|
|
|
|
pinned_dialog_ids.push_back(PSTRING() << get_pinned_dialogs_stmt.view_int64(0));
|
|
|
|
TRY_STATUS(get_pinned_dialogs_stmt.step());
|
|
|
|
}
|
|
|
|
get_pinned_dialogs_stmt.reset();
|
|
|
|
|
|
|
|
binlog_pmc.set(PSTRING() << "pinned_dialog_ids" << folder_id, implode(pinned_dialog_ids, ','));
|
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
if (version < static_cast<int32>(DbVersion::DialogDbCreated)) {
|
2021-09-22 19:17:37 +02:00
|
|
|
if (version != 0) {
|
|
|
|
LOG(WARNING) << "Drop old pmc dialog_db";
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
SqliteKeyValue kv;
|
|
|
|
kv.init_with_connection(db.clone(), "common").ensure();
|
|
|
|
kv.erase_by_prefix("di");
|
|
|
|
}
|
|
|
|
|
2021-09-22 19:17:37 +02:00
|
|
|
if (version != 0) {
|
|
|
|
LOG(WARNING) << "Drop dialog_db " << tag("version", version) << tag("current_db_version", current_db_version());
|
|
|
|
}
|
2018-12-20 18:24:49 +01:00
|
|
|
auto status = db.exec("DROP TABLE IF EXISTS dialogs");
|
|
|
|
TRY_STATUS(db.exec("DROP TABLE IF EXISTS notification_groups"));
|
|
|
|
return status;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class DialogDbImpl final : public DialogDbSyncInterface {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit DialogDbImpl(SqliteDb db) : db_(std::move(db)) {
|
|
|
|
init().ensure();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status init() {
|
2019-09-02 20:11:56 +02:00
|
|
|
TRY_RESULT_ASSIGN(add_dialog_stmt_, db_.get_statement("INSERT OR REPLACE INTO dialogs VALUES(?1, ?2, ?3, ?4)"));
|
|
|
|
TRY_RESULT_ASSIGN(add_notification_group_stmt_,
|
|
|
|
db_.get_statement("INSERT OR REPLACE INTO notification_groups VALUES(?1, ?2, ?3)"));
|
|
|
|
TRY_RESULT_ASSIGN(delete_notification_group_stmt_,
|
|
|
|
db_.get_statement("DELETE FROM notification_groups WHERE notification_group_id = ?1"));
|
|
|
|
TRY_RESULT_ASSIGN(get_dialog_stmt_, db_.get_statement("SELECT data FROM dialogs WHERE dialog_id = ?1"));
|
|
|
|
TRY_RESULT_ASSIGN(
|
|
|
|
get_dialogs_stmt_,
|
2019-08-26 19:08:51 +02:00
|
|
|
db_.get_statement("SELECT data, dialog_id, dialog_order FROM dialogs WHERE "
|
|
|
|
"folder_id == ?1 AND (dialog_order < ?2 OR (dialog_order = ?2 AND dialog_id < ?3)) ORDER "
|
|
|
|
"BY dialog_order DESC, dialog_id DESC LIMIT ?4"));
|
2019-09-02 20:11:56 +02:00
|
|
|
TRY_RESULT_ASSIGN(
|
|
|
|
get_notification_groups_by_last_notification_date_stmt_,
|
2018-12-20 18:24:49 +01:00
|
|
|
db_.get_statement("SELECT notification_group_id, dialog_id, last_notification_date FROM notification_groups "
|
|
|
|
"WHERE last_notification_date < ?1 OR (last_notification_date = ?1 "
|
2018-12-20 20:48:46 +01:00
|
|
|
"AND (dialog_id < ?2 OR (dialog_id = ?2 AND notification_group_id < ?3))) ORDER BY "
|
|
|
|
"last_notification_date DESC, dialog_id DESC LIMIT ?4"));
|
|
|
|
// "WHERE (last_notification_date, dialog_id, notification_group_id) < (?1, ?2, ?3) ORDER BY "
|
|
|
|
// "last_notification_date DESC, dialog_id DESC, notification_group_id DESC LIMIT ?4"));
|
2019-09-02 20:11:56 +02:00
|
|
|
TRY_RESULT_ASSIGN(
|
|
|
|
get_notification_group_stmt_,
|
2018-12-20 18:24:49 +01:00
|
|
|
db_.get_statement(
|
|
|
|
"SELECT dialog_id, last_notification_date FROM notification_groups WHERE notification_group_id = ?1"));
|
2019-12-26 02:12:26 +01:00
|
|
|
TRY_RESULT_ASSIGN(
|
|
|
|
get_secret_chat_count_stmt_,
|
|
|
|
db_.get_statement(
|
2020-03-16 14:32:27 +01:00
|
|
|
"SELECT COUNT(*) FROM dialogs WHERE folder_id = ?1 AND dialog_order > 0 AND dialog_id < -1500000000000"));
|
2018-11-27 15:39:13 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
// LOG(ERROR) << get_dialog_stmt_.explain().ok();
|
|
|
|
// LOG(ERROR) << get_dialogs_stmt_.explain().ok();
|
2018-12-20 20:48:46 +01:00
|
|
|
// LOG(ERROR) << get_notification_groups_by_last_notification_date_stmt_.explain().ok();
|
|
|
|
// LOG(ERROR) << get_notification_group_stmt_.explain().ok();
|
2018-11-27 15:39:13 +01:00
|
|
|
// LOG(FATAL) << "EXPLAINED";
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-08-26 19:08:51 +02:00
|
|
|
Status add_dialog(DialogId dialog_id, FolderId folder_id, int64 order, BufferSlice data,
|
2021-07-03 22:51:36 +02:00
|
|
|
vector<NotificationGroupKey> notification_groups) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
SCOPE_EXIT {
|
|
|
|
add_dialog_stmt_.reset();
|
|
|
|
};
|
|
|
|
add_dialog_stmt_.bind_int64(1, dialog_id.get()).ensure();
|
|
|
|
add_dialog_stmt_.bind_int64(2, order).ensure();
|
|
|
|
add_dialog_stmt_.bind_blob(3, data.as_slice()).ensure();
|
2019-08-26 19:08:51 +02:00
|
|
|
if (order > 0) {
|
|
|
|
add_dialog_stmt_.bind_int32(4, folder_id.get()).ensure();
|
|
|
|
} else {
|
|
|
|
add_dialog_stmt_.bind_null(4).ensure();
|
|
|
|
}
|
2018-11-06 22:11:34 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_STATUS(add_dialog_stmt_.step());
|
2018-12-20 18:24:49 +01:00
|
|
|
|
2018-12-20 20:48:46 +01:00
|
|
|
for (auto &to_add : notification_groups) {
|
2019-01-12 01:40:06 +01:00
|
|
|
if (to_add.dialog_id.is_valid()) {
|
|
|
|
SCOPE_EXIT {
|
|
|
|
add_notification_group_stmt_.reset();
|
|
|
|
};
|
|
|
|
add_notification_group_stmt_.bind_int32(1, to_add.group_id.get()).ensure();
|
|
|
|
add_notification_group_stmt_.bind_int64(2, to_add.dialog_id.get()).ensure();
|
|
|
|
if (to_add.last_notification_date != 0) {
|
|
|
|
add_notification_group_stmt_.bind_int32(3, to_add.last_notification_date).ensure();
|
|
|
|
} else {
|
|
|
|
add_notification_group_stmt_.bind_null(3).ensure();
|
|
|
|
}
|
|
|
|
TRY_STATUS(add_notification_group_stmt_.step());
|
2018-12-20 18:24:49 +01:00
|
|
|
} else {
|
2019-01-12 01:40:06 +01:00
|
|
|
SCOPE_EXIT {
|
|
|
|
delete_notification_group_stmt_.reset();
|
|
|
|
};
|
|
|
|
delete_notification_group_stmt_.bind_int32(1, to_add.group_id.get()).ensure();
|
|
|
|
TRY_STATUS(delete_notification_group_stmt_.step());
|
2018-12-20 18:24:49 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Result<BufferSlice> get_dialog(DialogId dialog_id) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
SCOPE_EXIT {
|
|
|
|
get_dialog_stmt_.reset();
|
|
|
|
};
|
|
|
|
|
|
|
|
get_dialog_stmt_.bind_int64(1, dialog_id.get()).ensure();
|
|
|
|
TRY_STATUS(get_dialog_stmt_.step());
|
|
|
|
if (!get_dialog_stmt_.has_row()) {
|
|
|
|
return Status::Error("Not found");
|
|
|
|
}
|
|
|
|
return BufferSlice(get_dialog_stmt_.view_blob(0));
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Result<NotificationGroupKey> get_notification_group(NotificationGroupId notification_group_id) final {
|
2018-11-27 15:39:13 +01:00
|
|
|
SCOPE_EXIT {
|
2018-12-20 18:24:49 +01:00
|
|
|
get_notification_group_stmt_.reset();
|
2018-11-27 15:39:13 +01:00
|
|
|
};
|
2018-12-20 18:24:49 +01:00
|
|
|
get_notification_group_stmt_.bind_int32(1, notification_group_id.get()).ensure();
|
|
|
|
TRY_STATUS(get_notification_group_stmt_.step());
|
|
|
|
if (!get_notification_group_stmt_.has_row()) {
|
2018-11-27 15:39:13 +01:00
|
|
|
return Status::Error("Not found");
|
|
|
|
}
|
2018-12-20 18:24:49 +01:00
|
|
|
return NotificationGroupKey(notification_group_id, DialogId(get_notification_group_stmt_.view_int64(0)),
|
2019-01-07 04:23:42 +01:00
|
|
|
get_last_notification_date(get_notification_group_stmt_, 1));
|
2018-11-27 15:39:13 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Result<int32> get_secret_chat_count(FolderId folder_id) final {
|
2019-12-26 02:12:26 +01:00
|
|
|
SCOPE_EXIT {
|
|
|
|
get_secret_chat_count_stmt_.reset();
|
|
|
|
};
|
|
|
|
get_secret_chat_count_stmt_.bind_int32(1, folder_id.get()).ensure();
|
|
|
|
TRY_STATUS(get_secret_chat_count_stmt_.step());
|
|
|
|
CHECK(get_secret_chat_count_stmt_.has_row());
|
|
|
|
return get_secret_chat_count_stmt_.view_int32(0);
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Result<DialogDbGetDialogsResult> get_dialogs(FolderId folder_id, int64 order, DialogId dialog_id, int32 limit) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
SCOPE_EXIT {
|
|
|
|
get_dialogs_stmt_.reset();
|
|
|
|
};
|
|
|
|
|
2019-08-26 19:08:51 +02:00
|
|
|
get_dialogs_stmt_.bind_int32(1, folder_id.get()).ensure();
|
|
|
|
get_dialogs_stmt_.bind_int64(2, order).ensure();
|
|
|
|
get_dialogs_stmt_.bind_int64(3, dialog_id.get()).ensure();
|
|
|
|
get_dialogs_stmt_.bind_int32(4, limit).ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-09-15 03:15:46 +02:00
|
|
|
DialogDbGetDialogsResult result;
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_STATUS(get_dialogs_stmt_.step());
|
|
|
|
while (get_dialogs_stmt_.has_row()) {
|
|
|
|
BufferSlice data(get_dialogs_stmt_.view_blob(0));
|
2019-09-15 03:15:46 +02:00
|
|
|
result.next_dialog_id = DialogId(get_dialogs_stmt_.view_int64(1));
|
|
|
|
result.next_order = get_dialogs_stmt_.view_int64(2);
|
|
|
|
LOG(INFO) << "Load " << result.next_dialog_id << " with order " << result.next_order;
|
|
|
|
result.dialogs.emplace_back(std::move(data));
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_STATUS(get_dialogs_stmt_.step());
|
|
|
|
}
|
|
|
|
|
2019-09-15 03:15:46 +02:00
|
|
|
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(
|
2021-07-03 22:51:36 +02:00
|
|
|
NotificationGroupKey notification_group_key, int32 limit) final {
|
2018-12-20 18:24:49 +01:00
|
|
|
auto &stmt = get_notification_groups_by_last_notification_date_stmt_;
|
2018-11-06 22:11:34 +01:00
|
|
|
SCOPE_EXIT {
|
2018-12-20 18:24:49 +01:00
|
|
|
stmt.reset();
|
2018-11-06 22:11:34 +01:00
|
|
|
};
|
|
|
|
|
2018-12-20 18:24:49 +01:00
|
|
|
stmt.bind_int32(1, notification_group_key.last_notification_date).ensure();
|
|
|
|
stmt.bind_int64(2, notification_group_key.dialog_id.get()).ensure();
|
2018-12-20 20:48:46 +01:00
|
|
|
stmt.bind_int32(3, notification_group_key.group_id.get()).ensure();
|
|
|
|
stmt.bind_int32(4, limit).ensure();
|
2018-11-06 22:11:34 +01:00
|
|
|
|
2018-12-22 21:24:18 +01:00
|
|
|
vector<NotificationGroupKey> notification_groups;
|
2018-12-20 18:24:49 +01:00
|
|
|
TRY_STATUS(stmt.step());
|
|
|
|
while (stmt.has_row()) {
|
2018-12-20 20:48:46 +01:00
|
|
|
notification_groups.emplace_back(NotificationGroupId(stmt.view_int32(0)), DialogId(stmt.view_int64(1)),
|
2019-01-07 04:23:42 +01:00
|
|
|
get_last_notification_date(stmt, 2));
|
2018-12-20 18:24:49 +01:00
|
|
|
TRY_STATUS(stmt.step());
|
2018-11-06 22:11:34 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Status begin_transaction() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return db_.begin_transaction();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
Status commit_transaction() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return db_.commit_transaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SqliteDb db_;
|
|
|
|
|
|
|
|
SqliteStatement add_dialog_stmt_;
|
2018-12-20 18:24:49 +01:00
|
|
|
SqliteStatement add_notification_group_stmt_;
|
2019-01-12 01:40:06 +01:00
|
|
|
SqliteStatement delete_notification_group_stmt_;
|
2018-12-31 20:04:05 +01:00
|
|
|
SqliteStatement get_dialog_stmt_;
|
|
|
|
SqliteStatement get_dialogs_stmt_;
|
2018-12-20 18:24:49 +01:00
|
|
|
SqliteStatement get_notification_groups_by_last_notification_date_stmt_;
|
|
|
|
SqliteStatement get_notification_group_stmt_;
|
2019-12-26 02:12:26 +01:00
|
|
|
SqliteStatement get_secret_chat_count_stmt_;
|
2019-01-07 04:23:42 +01:00
|
|
|
|
|
|
|
static int32 get_last_notification_date(SqliteStatement &stmt, int id) {
|
|
|
|
if (stmt.view_datatype(id) == SqliteStatement::Datatype::Null) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return stmt.view_int32(id);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
std::shared_ptr<DialogDbSyncSafeInterface> create_dialog_db_sync(
|
|
|
|
std::shared_ptr<SqliteConnectionSafe> sqlite_connection) {
|
2021-07-04 04:58:54 +02:00
|
|
|
class DialogDbSyncSafe final : public DialogDbSyncSafeInterface {
|
2018-12-31 20:04:05 +01:00
|
|
|
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
|
|
|
}) {
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
DialogDbSyncInterface &get() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class DialogDbAsync final : public DialogDbAsyncInterface {
|
2018-12-31 20:04:05 +01:00
|
|
|
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,
|
2021-07-03 22:51:36 +02:00
|
|
|
vector<NotificationGroupKey> notification_groups, Promise<> promise) final {
|
2019-08-26 19:08:51 +02:00
|
|
|
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,
|
2021-07-03 22:51:36 +02:00
|
|
|
Promise<vector<NotificationGroupKey>> promise) final {
|
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));
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void get_notification_group(NotificationGroupId notification_group_id, Promise<NotificationGroupKey> promise) final {
|
2018-12-20 18:24:49 +01:00
|
|
|
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
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void get_secret_chat_count(FolderId folder_id, Promise<int32> promise) final {
|
2019-12-26 02:12:26 +01:00
|
|
|
send_closure(impl_, &Impl::get_secret_chat_count, folder_id, std::move(promise));
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void get_dialog(DialogId dialog_id, Promise<BufferSlice> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
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,
|
2021-07-03 22:51:36 +02:00
|
|
|
Promise<DialogDbGetDialogsResult> promise) final {
|
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
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void close(Promise<> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure_later(impl_, &Impl::close, std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-04 04:58:54 +02:00
|
|
|
class Impl final : public Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
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() {
|
|
|
|
do_flush();
|
|
|
|
}
|
2019-12-26 02:12:26 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void do_flush() {
|
|
|
|
if (pending_writes_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sync_db_->begin_transaction().ensure();
|
|
|
|
for (auto &query : pending_writes_) {
|
|
|
|
query.set_value(Unit());
|
|
|
|
}
|
|
|
|
sync_db_->commit_transaction().ensure();
|
|
|
|
pending_writes_.clear();
|
2019-08-09 20:30:01 +02:00
|
|
|
for (auto &p : pending_write_results_) {
|
|
|
|
p.first.set_result(std::move(p.second));
|
|
|
|
}
|
|
|
|
pending_write_results_.clear();
|
2018-12-31 20:04:05 +01:00
|
|
|
cancel_timeout();
|
|
|
|
}
|
2019-12-26 02:12:26 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void timeout_expired() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
do_flush();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
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
|