From 4c618b6f920fd323cc4cc70fb5ece1f5217b71e2 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 27 Nov 2018 17:39:13 +0300 Subject: [PATCH] Add notification_group_id to DialogDb. GitOrigin-RevId: 119785b8a6904a9243ba559237a3f02bb4fe1950 --- td/telegram/DialogDb.cpp | 63 +++++++++++++++++++++++++++------ td/telegram/DialogDb.h | 11 ++++-- td/telegram/MessagesManager.cpp | 2 +- 3 files changed, 61 insertions(+), 15 deletions(-) diff --git a/td/telegram/DialogDb.cpp b/td/telegram/DialogDb.cpp index 55df022d..94a7be33 100644 --- a/td/telegram/DialogDb.cpp +++ b/td/telegram/DialogDb.cpp @@ -43,19 +43,28 @@ Status init_dialog_db(SqliteDb &db, int32 version, bool &was_created) { "dialog_id) WHERE last_notification_date IS NOT NULL"); }; + auto create_notification_group_id_index = [&]() { + return db.exec( + "CREATE INDEX IF NOT EXISTS dialog_by_notification_group_id ON dialogs (notification_group_id) WHERE " + "notification_group_id IS NOT NULL"); + }; + if (version == 0) { LOG(INFO) << "Create new dialog db"; was_created = true; TRY_STATUS( db.exec("CREATE TABLE IF NOT EXISTS dialogs (dialog_id INT8 PRIMARY KEY, dialog_order INT8, data BLOB, " - "last_notification_date INT4)")); + "last_notification_date INT4, notification_group_id INT4)")); TRY_STATUS(db.exec("CREATE INDEX IF NOT EXISTS dialog_by_dialog_order ON dialogs (dialog_order, dialog_id)")); TRY_STATUS(create_last_notification_date_index()); + TRY_STATUS(create_notification_group_id_index()); version = current_db_version(); } if (version < static_cast(DbVersion::DialogDbLastNotificationDate)) { TRY_STATUS(db.exec("ALTER TABLE dialogs ADD COLUMN last_notification_date INT4")); + TRY_STATUS(db.exec("ALTER TABLE dialogs ADD COLUMN notification_group_id INT4")); TRY_STATUS(create_last_notification_date_index()); + TRY_STATUS(create_notification_group_id_index()); } return Status::OK(); @@ -81,7 +90,7 @@ class DialogDbImpl : public DialogDbSyncInterface { } Status init() { - TRY_RESULT(add_dialog_stmt, db_.get_statement("INSERT OR REPLACE INTO dialogs VALUES(?1, ?2, ?3, ?4)")); + TRY_RESULT(add_dialog_stmt, db_.get_statement("INSERT OR REPLACE INTO dialogs VALUES(?1, ?2, ?3, ?4, ?5)")); TRY_RESULT(get_dialog_stmt, db_.get_statement("SELECT data FROM dialogs WHERE dialog_id = ?1")); TRY_RESULT(get_dialogs_stmt, db_.get_statement("SELECT data, dialog_id, dialog_order FROM dialogs WHERE " "dialog_order < ?1 OR (dialog_order = ?1 AND dialog_id < ?2) ORDER " @@ -91,6 +100,9 @@ class DialogDbImpl : public DialogDbSyncInterface { db_.get_statement("SELECT data, dialog_id, last_notification_date FROM dialogs WHERE " "last_notification_date < ?1 OR (last_notification_date = ?1 AND dialog_id < ?2) ORDER " "BY last_notification_date DESC, dialog_id DESC LIMIT ?3")); + TRY_RESULT(get_dialog_by_notification_group_id_stmt, + db_.get_statement("SELECT data FROM dialogs WHERE notification_group_id = ?1")); + /* TRY_RESULT(get_dialogs2_stmt, db_.get_statement("SELECT data FROM dialogs WHERE dialog_order <= ?1 AND (dialog_order != ?1 OR " @@ -101,16 +113,18 @@ class DialogDbImpl : public DialogDbSyncInterface { get_dialog_stmt_ = std::move(get_dialog_stmt); get_dialogs_stmt_ = std::move(get_dialogs_stmt); get_dialogs_by_last_notification_date_stmt_ = std::move(get_dialogs_by_last_notification_date_stmt); + get_dialog_by_notification_group_id_stmt_ = std::move(get_dialog_by_notification_group_id_stmt); // LOG(ERROR) << get_dialog_stmt_.explain().ok(); // LOG(ERROR) << get_dialogs_stmt_.explain().ok(); // LOG(ERROR) << get_dialogs2_stmt.explain().ok(); - // LOG(FATAL) << "EPLAINED"; + // LOG(FATAL) << "EXPLAINED"; return Status::OK(); } - Status add_dialog(DialogId dialog_id, int64 order, int32 last_notification_date, BufferSlice data) override { + Status add_dialog(DialogId dialog_id, int64 order, int32 last_notification_date, + NotificationGroupId notification_group_id, BufferSlice data) override { SCOPE_EXIT { add_dialog_stmt_.reset(); }; @@ -122,6 +136,11 @@ class DialogDbImpl : public DialogDbSyncInterface { } else { add_dialog_stmt_.bind_null(4).ensure(); } + if (notification_group_id.is_valid()) { + add_dialog_stmt_.bind_int32(5, notification_group_id.get()).ensure(); + } else { + add_dialog_stmt_.bind_null(5).ensure(); + } TRY_STATUS(add_dialog_stmt_.step()); return Status::OK(); @@ -140,6 +159,19 @@ class DialogDbImpl : public DialogDbSyncInterface { return BufferSlice(get_dialog_stmt_.view_blob(0)); } + Result get_dialog_by_notification_group_id(NotificationGroupId notification_group_id) override { + SCOPE_EXIT { + get_dialog_by_notification_group_id_stmt_.reset(); + }; + + get_dialog_by_notification_group_id_stmt_.bind_int32(1, notification_group_id.get()).ensure(); + TRY_STATUS(get_dialog_by_notification_group_id_stmt_.step()); + if (!get_dialog_by_notification_group_id_stmt_.has_row()) { + return Status::Error("Not found"); + } + return BufferSlice(get_dialog_by_notification_group_id_stmt_.view_blob(0)); + } + Result> get_dialogs(int64 order, DialogId dialog_id, int32 limit) override { SCOPE_EXIT { get_dialogs_stmt_.reset(); @@ -200,6 +232,7 @@ class DialogDbImpl : public DialogDbSyncInterface { SqliteStatement get_dialog_stmt_; SqliteStatement get_dialogs_stmt_; SqliteStatement get_dialogs_by_last_notification_date_stmt_; + SqliteStatement get_dialog_by_notification_group_id_stmt_; }; std::shared_ptr create_dialog_db_sync( @@ -227,10 +260,10 @@ class DialogDbAsync : public DialogDbAsyncInterface { impl_ = create_actor_on_scheduler("DialogDbActor", scheduler_id, std::move(sync_db)); } - void add_dialog(DialogId dialog_id, int64 order, int32 last_notification_date, BufferSlice data, - Promise<> promise) override { - send_closure_later(impl_, &Impl::add_dialog, dialog_id, order, last_notification_date, std::move(data), - std::move(promise)); + void add_dialog(DialogId dialog_id, int64 order, int32 last_notification_date, + NotificationGroupId notification_group_id, BufferSlice data, Promise<> promise) override { + send_closure_later(impl_, &Impl::add_dialog, dialog_id, order, last_notification_date, notification_group_id, + std::move(data), std::move(promise)); } void get_dialog(DialogId dialog_id, Promise promise) override { send_closure_later(impl_, &Impl::get_dialog, dialog_id, std::move(promise)); @@ -243,6 +276,9 @@ class DialogDbAsync : public DialogDbAsyncInterface { send_closure_later(impl_, &Impl::get_dialogs_by_last_notification_date, last_notification_date, dialog_id, limit, std::move(promise)); } + void get_dialog_by_notification_group_id(NotificationGroupId notification_group_id, Promise promise) override { + send_closure_later(impl_, &Impl::get_dialog_by_notification_group_id, notification_group_id, std::move(promise)); + } void close(Promise<> promise) override { send_closure_later(impl_, &Impl::close, std::move(promise)); } @@ -252,10 +288,11 @@ class DialogDbAsync : public DialogDbAsyncInterface { public: explicit Impl(std::shared_ptr sync_db_safe) : sync_db_safe_(std::move(sync_db_safe)) { } - void add_dialog(DialogId dialog_id, int64 order, int32 last_notification_date, BufferSlice data, - Promise<> promise) { + void add_dialog(DialogId dialog_id, int64 order, int32 last_notification_date, + NotificationGroupId notification_group_id, BufferSlice data, Promise<> promise) { add_write_query([=, promise = std::move(promise), data = std::move(data)](Unit) mutable { - promise.set_result(sync_db_->add_dialog(dialog_id, order, last_notification_date, std::move(data))); + promise.set_result( + sync_db_->add_dialog(dialog_id, order, last_notification_date, notification_group_id, std::move(data))); }); } void get_dialog(DialogId dialog_id, Promise promise) { @@ -271,6 +308,10 @@ class DialogDbAsync : public DialogDbAsyncInterface { add_read_query(); promise.set_result(sync_db_->get_dialogs_by_last_notification_date(last_notification_date, dialog_id, limit)); } + void get_dialog_by_notification_group_id(NotificationGroupId notification_group_id, Promise promise) { + add_read_query(); + promise.set_result(sync_db_->get_dialog_by_notification_group_id(notification_group_id)); + } void close(Promise<> promise) { do_flush(); sync_db_safe_.reset(); diff --git a/td/telegram/DialogDb.h b/td/telegram/DialogDb.h index cf52a1eb..3301872d 100644 --- a/td/telegram/DialogDb.h +++ b/td/telegram/DialogDb.h @@ -7,6 +7,7 @@ #pragma once #include "td/telegram/DialogId.h" +#include "td/telegram/NotificationGroupId.h" #include "td/actor/PromiseFuture.h" @@ -27,11 +28,13 @@ class DialogDbSyncInterface { DialogDbSyncInterface &operator=(const DialogDbSyncInterface &) = delete; virtual ~DialogDbSyncInterface() = default; - virtual Status add_dialog(DialogId dialog_id, int64 order, int32 last_notification_date, BufferSlice data) = 0; + virtual Status add_dialog(DialogId dialog_id, int64 order, int32 last_notification_date, + NotificationGroupId notification_group_id, BufferSlice data) = 0; virtual Result get_dialog(DialogId dialog_id) = 0; virtual Result> get_dialogs(int64 order, DialogId dialog_id, int32 limit) = 0; virtual Result> get_dialogs_by_last_notification_date(int32 last_notification_date, DialogId dialog_id, int32 limit) = 0; + virtual Result get_dialog_by_notification_group_id(NotificationGroupId notification_group_id) = 0; virtual Status begin_transaction() = 0; virtual Status commit_transaction() = 0; }; @@ -53,12 +56,14 @@ class DialogDbAsyncInterface { DialogDbAsyncInterface &operator=(const DialogDbAsyncInterface &) = delete; virtual ~DialogDbAsyncInterface() = default; - virtual void add_dialog(DialogId dialog_id, int64 order, int32 last_notification_date, BufferSlice data, - Promise<> promise) = 0; + virtual void add_dialog(DialogId dialog_id, int64 order, int32 last_notification_date, + NotificationGroupId notification_group_id, BufferSlice data, Promise<> promise) = 0; virtual void get_dialog(DialogId dialog_id, Promise promise) = 0; virtual void get_dialogs(int64 order, DialogId dialog_id, int32 limit, Promise> promise) = 0; virtual void get_dialogs_by_last_notification_date(int32 last_notification_date, DialogId dialog_id, int32 limit, Promise> promise) = 0; + virtual void get_dialog_by_notification_group_id(NotificationGroupId notification_group_id, + Promise promise) = 0; virtual void close(Promise<> promise) = 0; }; diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 0c5c9fcb..9c1c35d0 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -4265,7 +4265,7 @@ void MessagesManager::save_dialog_to_database(DialogId dialog_id) { CHECK(d != nullptr); LOG(INFO) << "Save " << dialog_id << " to database"; G()->td_db()->get_dialog_db_async()->add_dialog( - dialog_id, d->order, d->last_notification_date, get_dialog_database_value(d), + dialog_id, d->order, d->last_notification_date, d->message_notification_group_id, get_dialog_database_value(d), PromiseCreator::lambda([dialog_id](Result<> result) { send_closure(G()->messages_manager(), &MessagesManager::on_save_dialog_to_database, dialog_id, result.is_ok()); }));