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/MessagesDb.h"
|
|
|
|
|
|
|
|
#include "td/telegram/logevent/LogEvent.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/SqliteStatement.h"
|
|
|
|
|
2018-07-03 21:29:04 +02:00
|
|
|
#include "td/actor/actor.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/actor/PromiseFuture.h"
|
2018-07-03 21:29:04 +02:00
|
|
|
#include "td/actor/SchedulerLocalStorage.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/ScopeGuard.h"
|
|
|
|
#include "td/utils/Slice.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/StackAllocator.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
#include "td/utils/Time.h"
|
|
|
|
#include "td/utils/tl_helpers.h"
|
|
|
|
#include "td/utils/unicode.h"
|
|
|
|
#include "td/utils/utf8.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
#include <iterator>
|
|
|
|
#include <limits>
|
2021-10-21 11:51:16 +02:00
|
|
|
#include <tuple>
|
2021-10-04 10:01:49 +02:00
|
|
|
#include <utility>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
static constexpr int32 MESSAGES_DB_INDEX_COUNT = 30;
|
|
|
|
static constexpr int32 MESSAGES_DB_INDEX_COUNT_OLD = 9;
|
|
|
|
|
|
|
|
// NB: must happen inside a transaction
|
|
|
|
Status init_messages_db(SqliteDb &db, int32 version) {
|
2018-11-28 18:18:50 +01:00
|
|
|
LOG(INFO) << "Init message database " << tag("version", version);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// Check if database exists
|
|
|
|
TRY_RESULT(has_table, db.has_table("messages"));
|
|
|
|
if (!has_table) {
|
|
|
|
version = 0;
|
|
|
|
} else if (version < static_cast<int32>(DbVersion::DialogDbCreated) || version > current_db_version()) {
|
|
|
|
TRY_STATUS(drop_messages_db(db, version));
|
|
|
|
version = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto add_media_indices = [&db](int begin, int end) {
|
|
|
|
for (int i = begin; i < end; i++) {
|
|
|
|
TRY_STATUS(db.exec(PSLICE() << "CREATE INDEX IF NOT EXISTS message_index_" << i
|
|
|
|
<< " ON messages (dialog_id, message_id) WHERE (index_mask & " << (1 << i)
|
|
|
|
<< ") != 0"));
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
};
|
|
|
|
|
|
|
|
auto add_fts = [&db] {
|
|
|
|
TRY_STATUS(
|
|
|
|
db.exec("CREATE INDEX IF NOT EXISTS message_by_search_id ON messages "
|
|
|
|
"(search_id) WHERE search_id IS NOT NULL"));
|
|
|
|
|
|
|
|
TRY_STATUS(
|
|
|
|
db.exec("CREATE VIRTUAL TABLE IF NOT EXISTS messages_fts USING fts5(text, content='messages', "
|
|
|
|
"content_rowid='search_id', tokenize = \"unicode61 remove_diacritics 0 tokenchars '\a'\")"));
|
|
|
|
TRY_STATUS(db.exec(
|
|
|
|
"CREATE TRIGGER IF NOT EXISTS trigger_fts_delete BEFORE DELETE ON messages WHEN OLD.search_id IS NOT NULL"
|
|
|
|
" BEGIN INSERT INTO messages_fts(messages_fts, rowid, text) VALUES(\'delete\', OLD.search_id, OLD.text); END"));
|
|
|
|
TRY_STATUS(db.exec(
|
|
|
|
"CREATE TRIGGER IF NOT EXISTS trigger_fts_insert AFTER INSERT ON messages WHEN NEW.search_id IS NOT NULL"
|
|
|
|
" BEGIN INSERT INTO messages_fts(rowid, text) VALUES(NEW.search_id, NEW.text); END"));
|
|
|
|
//TRY_STATUS(db.exec(
|
|
|
|
//"CREATE TRIGGER IF NOT EXISTS trigger_fts_update AFTER UPDATE ON messages WHEN NEW.search_id IS NOT NULL OR "
|
|
|
|
//"OLD.search_id IS NOT NULL"
|
|
|
|
//" BEGIN "
|
|
|
|
//"INSERT INTO messages_fts(messages_fts, rowid, text) VALUES(\'delete\', OLD.search_id, OLD.text); "
|
|
|
|
//"INSERT INTO messages_fts(rowid, text) VALUES(NEW.search_id, NEW.text); "
|
|
|
|
//" END"));
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
};
|
2020-02-26 16:15:19 +01:00
|
|
|
auto add_call_index = [&db] {
|
2020-08-22 09:05:51 +02:00
|
|
|
for (int i = static_cast<int>(MessageSearchFilter::Call) - 1; i < static_cast<int>(MessageSearchFilter::MissedCall);
|
|
|
|
i++) {
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_STATUS(db.exec(PSLICE() << "CREATE INDEX IF NOT EXISTS full_message_index_" << i
|
|
|
|
<< " ON messages (unique_message_id) WHERE (index_mask & " << (1 << i) << ") != 0"));
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
};
|
2020-02-26 16:15:19 +01:00
|
|
|
auto add_notification_id_index = [&db] {
|
2019-12-02 01:33:35 +01:00
|
|
|
return db.exec(
|
|
|
|
"CREATE INDEX IF NOT EXISTS message_by_notification_id ON messages (dialog_id, notification_id) WHERE "
|
|
|
|
"notification_id IS NOT NULL");
|
|
|
|
};
|
2020-02-26 16:15:19 +01:00
|
|
|
auto add_scheduled_messages_table = [&db] {
|
2019-12-02 01:33:35 +01:00
|
|
|
TRY_STATUS(
|
|
|
|
db.exec("CREATE TABLE IF NOT EXISTS scheduled_messages (dialog_id INT8, message_id INT8, "
|
|
|
|
"server_message_id INT4, data BLOB, PRIMARY KEY (dialog_id, message_id))"));
|
|
|
|
|
2018-11-28 18:18:50 +01:00
|
|
|
TRY_STATUS(
|
2019-12-02 01:33:35 +01:00
|
|
|
db.exec("CREATE INDEX IF NOT EXISTS message_by_server_message_id ON scheduled_messages "
|
|
|
|
"(dialog_id, server_message_id) WHERE server_message_id IS NOT NULL"));
|
2018-11-28 18:18:50 +01:00
|
|
|
return Status::OK();
|
|
|
|
};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (version == 0) {
|
2018-11-28 18:18:50 +01:00
|
|
|
LOG(INFO) << "Create new message database";
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_STATUS(
|
2020-09-28 14:13:25 +02:00
|
|
|
db.exec("CREATE TABLE IF NOT EXISTS messages (dialog_id INT8, message_id INT8, unique_message_id INT4, "
|
2021-03-29 10:28:43 +02:00
|
|
|
"sender_user_id INT8, random_id INT8, data BLOB, ttl_expires_at INT4, index_mask INT4, search_id INT8, "
|
2020-09-28 14:13:25 +02:00
|
|
|
"text STRING, notification_id INT4, top_thread_message_id INT8, PRIMARY KEY (dialog_id, message_id))"));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
TRY_STATUS(
|
|
|
|
db.exec("CREATE INDEX IF NOT EXISTS message_by_random_id ON messages (dialog_id, random_id) "
|
|
|
|
"WHERE random_id IS NOT NULL"));
|
2018-11-28 18:18:50 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_STATUS(
|
|
|
|
db.exec("CREATE INDEX IF NOT EXISTS message_by_unique_message_id ON messages "
|
|
|
|
"(unique_message_id) WHERE unique_message_id IS NOT NULL"));
|
|
|
|
|
|
|
|
TRY_STATUS(
|
|
|
|
db.exec("CREATE INDEX IF NOT EXISTS message_by_ttl ON messages "
|
|
|
|
"(ttl_expires_at) WHERE ttl_expires_at IS NOT NULL"));
|
|
|
|
|
|
|
|
TRY_STATUS(add_media_indices(0, MESSAGES_DB_INDEX_COUNT));
|
|
|
|
|
|
|
|
TRY_STATUS(add_fts());
|
|
|
|
|
|
|
|
TRY_STATUS(add_call_index());
|
|
|
|
|
2018-11-28 18:18:50 +01:00
|
|
|
TRY_STATUS(add_notification_id_index());
|
|
|
|
|
2019-12-02 01:33:35 +01:00
|
|
|
TRY_STATUS(add_scheduled_messages_table());
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
version = current_db_version();
|
|
|
|
}
|
|
|
|
if (version < static_cast<int32>(DbVersion::MessagesDbMediaIndex)) {
|
|
|
|
TRY_STATUS(db.exec("ALTER TABLE messages ADD COLUMN index_mask INT4"));
|
|
|
|
TRY_STATUS(add_media_indices(0, MESSAGES_DB_INDEX_COUNT_OLD));
|
|
|
|
}
|
|
|
|
if (version < static_cast<int32>(DbVersion::MessagesDb30MediaIndex)) {
|
|
|
|
TRY_STATUS(add_media_indices(MESSAGES_DB_INDEX_COUNT_OLD, MESSAGES_DB_INDEX_COUNT));
|
|
|
|
}
|
|
|
|
if (version < static_cast<int32>(DbVersion::MessagesDbFts)) {
|
|
|
|
TRY_STATUS(db.exec("ALTER TABLE messages ADD COLUMN search_id INT8"));
|
|
|
|
TRY_STATUS(db.exec("ALTER TABLE messages ADD COLUMN text STRING"));
|
|
|
|
TRY_STATUS(add_fts());
|
|
|
|
}
|
|
|
|
if (version < static_cast<int32>(DbVersion::MessagesCallIndex)) {
|
|
|
|
TRY_STATUS(add_call_index());
|
|
|
|
}
|
2018-11-28 18:18:50 +01:00
|
|
|
if (version < static_cast<int32>(DbVersion::AddNotificationsSupport)) {
|
|
|
|
TRY_STATUS(db.exec("ALTER TABLE messages ADD COLUMN notification_id INT4"));
|
|
|
|
TRY_STATUS(add_notification_id_index());
|
|
|
|
}
|
2019-12-02 01:33:35 +01:00
|
|
|
if (version < static_cast<int32>(DbVersion::AddScheduledMessages)) {
|
|
|
|
TRY_STATUS(add_scheduled_messages_table());
|
|
|
|
}
|
2020-09-28 14:13:25 +02:00
|
|
|
if (version < static_cast<int32>(DbVersion::AddMessageThreadSupport)) {
|
|
|
|
TRY_STATUS(db.exec("ALTER TABLE messages ADD COLUMN top_thread_message_id INT8"));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: must happen inside a transaction
|
|
|
|
Status drop_messages_db(SqliteDb &db, int32 version) {
|
2018-11-28 18:18:50 +01:00
|
|
|
LOG(WARNING) << "Drop message database " << tag("version", version)
|
|
|
|
<< tag("current_db_version", current_db_version());
|
2018-12-31 20:04:05 +01:00
|
|
|
return db.exec("DROP TABLE IF EXISTS messages");
|
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class MessagesDbImpl final : public MessagesDbSyncInterface {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit MessagesDbImpl(SqliteDb db) : db_(std::move(db)) {
|
|
|
|
init().ensure();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status init() {
|
2019-09-02 20:11:56 +02:00
|
|
|
TRY_RESULT_ASSIGN(
|
|
|
|
add_message_stmt_,
|
2020-09-28 14:13:25 +02:00
|
|
|
db_.get_statement("INSERT OR REPLACE INTO messages VALUES(?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)"));
|
2019-09-02 20:11:56 +02:00
|
|
|
TRY_RESULT_ASSIGN(delete_message_stmt_,
|
|
|
|
db_.get_statement("DELETE FROM messages WHERE dialog_id = ?1 AND message_id = ?2"));
|
|
|
|
TRY_RESULT_ASSIGN(delete_all_dialog_messages_stmt_,
|
|
|
|
db_.get_statement("DELETE FROM messages WHERE dialog_id = ?1 AND message_id <= ?2"));
|
2021-11-19 14:00:21 +01:00
|
|
|
TRY_RESULT_ASSIGN(delete_dialog_messages_by_sender_stmt_,
|
2021-12-24 20:00:13 +01:00
|
|
|
db_.get_statement("DELETE FROM messages WHERE dialog_id = ?1 AND sender_user_id = ?2"));
|
2019-09-02 20:11:56 +02:00
|
|
|
|
2021-10-04 10:01:49 +02:00
|
|
|
TRY_RESULT_ASSIGN(
|
|
|
|
get_message_stmt_,
|
|
|
|
db_.get_statement("SELECT message_id, data FROM messages WHERE dialog_id = ?1 AND message_id = ?2"));
|
2021-10-01 14:23:38 +02:00
|
|
|
TRY_RESULT_ASSIGN(
|
|
|
|
get_message_by_random_id_stmt_,
|
|
|
|
db_.get_statement("SELECT message_id, data FROM messages WHERE dialog_id = ?1 AND random_id = ?2"));
|
|
|
|
TRY_RESULT_ASSIGN(
|
|
|
|
get_message_by_unique_message_id_stmt_,
|
|
|
|
db_.get_statement("SELECT dialog_id, message_id, data FROM messages WHERE unique_message_id = ?1"));
|
2019-09-02 20:11:56 +02:00
|
|
|
|
|
|
|
TRY_RESULT_ASSIGN(
|
|
|
|
get_expiring_messages_stmt_,
|
2021-10-01 14:23:38 +02:00
|
|
|
db_.get_statement(
|
|
|
|
"SELECT dialog_id, message_id, data FROM messages WHERE ?1 < ttl_expires_at AND ttl_expires_at <= ?2"));
|
2019-09-02 20:11:56 +02:00
|
|
|
TRY_RESULT_ASSIGN(get_expiring_messages_helper_stmt_,
|
|
|
|
db_.get_statement("SELECT MAX(ttl_expires_at), COUNT(*) FROM (SELECT ttl_expires_at FROM "
|
|
|
|
"messages WHERE ?1 < ttl_expires_at LIMIT ?2) AS T"));
|
|
|
|
|
|
|
|
TRY_RESULT_ASSIGN(get_messages_stmt_.asc_stmt_,
|
|
|
|
db_.get_statement("SELECT data, message_id FROM messages WHERE dialog_id = ?1 AND message_id > "
|
|
|
|
"?2 ORDER BY message_id ASC LIMIT ?3"));
|
|
|
|
TRY_RESULT_ASSIGN(get_messages_stmt_.desc_stmt_,
|
|
|
|
db_.get_statement("SELECT data, message_id FROM messages WHERE dialog_id = ?1 AND message_id < "
|
|
|
|
"?2 ORDER BY message_id DESC LIMIT ?3"));
|
2019-12-04 18:06:39 +01:00
|
|
|
TRY_RESULT_ASSIGN(get_scheduled_messages_stmt_,
|
|
|
|
db_.get_statement("SELECT data, message_id FROM scheduled_messages WHERE dialog_id = ?1 AND "
|
|
|
|
"message_id < ?2 ORDER BY message_id DESC LIMIT ?3"));
|
2019-09-02 20:11:56 +02:00
|
|
|
TRY_RESULT_ASSIGN(get_messages_from_notification_id_stmt_,
|
|
|
|
db_.get_statement("SELECT data, message_id FROM messages WHERE dialog_id = ?1 AND "
|
|
|
|
"notification_id < ?2 ORDER BY notification_id DESC LIMIT ?3"));
|
2021-10-01 14:23:38 +02:00
|
|
|
TRY_RESULT_ASSIGN(get_messages_fts_stmt_,
|
|
|
|
db_.get_statement("SELECT dialog_id, message_id, data, search_id FROM messages WHERE search_id "
|
|
|
|
"IN (SELECT rowid FROM messages_fts WHERE messages_fts MATCH ?1 AND rowid < ?2 "
|
|
|
|
"ORDER BY rowid DESC LIMIT ?3) ORDER BY search_id DESC"));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
for (int32 i = 0; i < MESSAGES_DB_INDEX_COUNT; i++) {
|
2021-11-01 14:30:03 +01:00
|
|
|
TRY_RESULT_ASSIGN(
|
|
|
|
get_message_ids_stmts_[i],
|
|
|
|
db_.get_statement(
|
|
|
|
PSLICE() << "SELECT message_id FROM messages WHERE dialog_id = ?1 AND message_id < ?2 AND (index_mask & "
|
|
|
|
<< (1 << i) << ") != 0 ORDER BY message_id DESC LIMIT 1000000"));
|
|
|
|
|
2021-10-06 12:48:29 +02:00
|
|
|
TRY_RESULT_ASSIGN(
|
|
|
|
get_messages_from_index_stmts_[i].desc_stmt_,
|
|
|
|
db_.get_statement(
|
|
|
|
PSLICE()
|
|
|
|
<< "SELECT data, message_id FROM messages WHERE dialog_id = ?1 AND message_id < ?2 AND (index_mask & "
|
|
|
|
<< (1 << i) << ") != 0 ORDER BY message_id DESC LIMIT ?3"));
|
|
|
|
|
|
|
|
TRY_RESULT_ASSIGN(
|
|
|
|
get_messages_from_index_stmts_[i].asc_stmt_,
|
|
|
|
db_.get_statement(
|
|
|
|
PSLICE()
|
|
|
|
<< "SELECT data, message_id FROM messages WHERE dialog_id = ?1 AND message_id > ?2 AND (index_mask & "
|
|
|
|
<< (1 << i) << ") != 0 ORDER BY message_id ASC LIMIT ?3"));
|
2019-09-02 20:11:56 +02:00
|
|
|
|
|
|
|
// LOG(ERROR) << get_messages_from_index_stmts_[i].desc_stmt_.explain().ok();
|
|
|
|
// LOG(ERROR) << get_messages_from_index_stmts_[i].asc_stmt_.explain().ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-08-22 09:05:51 +02:00
|
|
|
for (int i = static_cast<int>(MessageSearchFilter::Call) - 1, pos = 0;
|
|
|
|
i < static_cast<int>(MessageSearchFilter::MissedCall); i++, pos++) {
|
2019-09-02 20:11:56 +02:00
|
|
|
TRY_RESULT_ASSIGN(
|
|
|
|
get_calls_stmts_[pos],
|
|
|
|
db_.get_statement(
|
2021-10-01 14:23:38 +02:00
|
|
|
PSLICE()
|
|
|
|
<< "SELECT dialog_id, message_id, data FROM messages WHERE unique_message_id < ?1 AND (index_mask & "
|
|
|
|
<< (1 << i) << ") != 0 ORDER BY unique_message_id DESC LIMIT ?2"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-12-02 01:33:35 +01:00
|
|
|
TRY_RESULT_ASSIGN(add_scheduled_message_stmt_,
|
|
|
|
db_.get_statement("INSERT OR REPLACE INTO scheduled_messages VALUES(?1, ?2, ?3, ?4)"));
|
2019-12-02 15:27:28 +01:00
|
|
|
TRY_RESULT_ASSIGN(
|
|
|
|
get_scheduled_message_stmt_,
|
2021-10-04 10:01:49 +02:00
|
|
|
db_.get_statement("SELECT message_id, data FROM scheduled_messages WHERE dialog_id = ?1 AND message_id = ?2"));
|
2019-12-02 15:56:03 +01:00
|
|
|
TRY_RESULT_ASSIGN(
|
|
|
|
get_scheduled_server_message_stmt_,
|
2021-10-04 10:01:49 +02:00
|
|
|
db_.get_statement(
|
|
|
|
"SELECT message_id, data FROM scheduled_messages WHERE dialog_id = ?1 AND server_message_id = ?2"));
|
2019-12-02 01:33:35 +01:00
|
|
|
TRY_RESULT_ASSIGN(delete_scheduled_message_stmt_,
|
|
|
|
db_.get_statement("DELETE FROM scheduled_messages WHERE dialog_id = ?1 AND message_id = ?2"));
|
2019-12-02 16:25:41 +01:00
|
|
|
TRY_RESULT_ASSIGN(
|
|
|
|
delete_scheduled_server_message_stmt_,
|
|
|
|
db_.get_statement("DELETE FROM scheduled_messages WHERE dialog_id = ?1 AND server_message_id = ?2"));
|
2019-12-02 01:33:35 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
// LOG(ERROR) << get_message_stmt_.explain().ok();
|
2018-11-28 21:19:30 +01:00
|
|
|
// LOG(ERROR) << get_messages_from_notification_id_stmt.explain().ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
// LOG(ERROR) << get_message_by_random_id_stmt_.explain().ok();
|
|
|
|
// LOG(ERROR) << get_message_by_unique_message_id_stmt_.explain().ok();
|
|
|
|
|
|
|
|
// LOG(ERROR) << get_expiring_messages_stmt_.explain().ok();
|
|
|
|
// LOG(ERROR) << get_expiring_messages_helper_stmt_.explain().ok();
|
|
|
|
|
|
|
|
// LOG(FATAL) << "EXPLAINED";
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2021-11-19 14:00:21 +01:00
|
|
|
Status add_message(FullMessageId full_message_id, ServerMessageId unique_message_id, DialogId sender_dialog_id,
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 random_id, int32 ttl_expires_at, int32 index_mask, int64 search_id, string text,
|
2021-07-03 22:51:36 +02:00
|
|
|
NotificationId notification_id, MessageId top_thread_message_id, BufferSlice data) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(INFO) << "Add " << full_message_id << " to database";
|
|
|
|
auto dialog_id = full_message_id.get_dialog_id();
|
|
|
|
auto message_id = full_message_id.get_message_id();
|
2019-05-30 00:35:19 +02:00
|
|
|
LOG_CHECK(dialog_id.is_valid()) << dialog_id << ' ' << message_id << ' ' << full_message_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(message_id.is_valid());
|
|
|
|
SCOPE_EXIT {
|
|
|
|
add_message_stmt_.reset();
|
|
|
|
};
|
|
|
|
add_message_stmt_.bind_int64(1, dialog_id.get()).ensure();
|
|
|
|
add_message_stmt_.bind_int64(2, message_id.get()).ensure();
|
|
|
|
|
|
|
|
if (unique_message_id.is_valid()) {
|
|
|
|
add_message_stmt_.bind_int32(3, unique_message_id.get()).ensure();
|
|
|
|
} else {
|
|
|
|
add_message_stmt_.bind_null(3).ensure();
|
|
|
|
}
|
|
|
|
|
2021-11-19 14:00:21 +01:00
|
|
|
if (sender_dialog_id.is_valid()) {
|
|
|
|
add_message_stmt_.bind_int64(4, sender_dialog_id.get()).ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
add_message_stmt_.bind_null(4).ensure();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (random_id != 0) {
|
|
|
|
add_message_stmt_.bind_int64(5, random_id).ensure();
|
|
|
|
} else {
|
|
|
|
add_message_stmt_.bind_null(5).ensure();
|
|
|
|
}
|
|
|
|
|
|
|
|
add_message_stmt_.bind_blob(6, data.as_slice()).ensure();
|
|
|
|
|
|
|
|
if (ttl_expires_at != 0) {
|
|
|
|
add_message_stmt_.bind_int32(7, ttl_expires_at).ensure();
|
|
|
|
} else {
|
|
|
|
add_message_stmt_.bind_null(7).ensure();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index_mask != 0) {
|
|
|
|
add_message_stmt_.bind_int32(8, index_mask).ensure();
|
|
|
|
} else {
|
|
|
|
add_message_stmt_.bind_null(8).ensure();
|
|
|
|
}
|
|
|
|
if (search_id != 0) {
|
|
|
|
// add dialog_id to text
|
|
|
|
text += PSTRING() << " \a" << dialog_id.get();
|
2021-11-01 10:52:51 +01:00
|
|
|
if (index_mask != 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int i = 0; i < MESSAGES_DB_INDEX_COUNT; i++) {
|
|
|
|
if ((index_mask & (1 << i))) {
|
|
|
|
text += PSTRING() << " \a\a" << i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add_message_stmt_.bind_int64(9, search_id).ensure();
|
|
|
|
} else {
|
|
|
|
text = "";
|
|
|
|
add_message_stmt_.bind_null(9).ensure();
|
|
|
|
}
|
|
|
|
if (!text.empty()) {
|
|
|
|
add_message_stmt_.bind_string(10, text).ensure();
|
|
|
|
} else {
|
|
|
|
add_message_stmt_.bind_null(10).ensure();
|
|
|
|
}
|
2018-11-28 18:18:50 +01:00
|
|
|
if (notification_id.is_valid()) {
|
|
|
|
add_message_stmt_.bind_int32(11, notification_id.get()).ensure();
|
|
|
|
} else {
|
|
|
|
add_message_stmt_.bind_null(11).ensure();
|
|
|
|
}
|
2020-09-28 14:13:25 +02:00
|
|
|
if (top_thread_message_id.is_valid()) {
|
|
|
|
add_message_stmt_.bind_int64(12, top_thread_message_id.get()).ensure();
|
|
|
|
} else {
|
|
|
|
add_message_stmt_.bind_null(12).ensure();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
add_message_stmt_.step().ensure();
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Status add_scheduled_message(FullMessageId full_message_id, BufferSlice data) final {
|
2019-12-02 01:33:35 +01:00
|
|
|
LOG(INFO) << "Add " << full_message_id << " to database";
|
|
|
|
auto dialog_id = full_message_id.get_dialog_id();
|
|
|
|
auto message_id = full_message_id.get_message_id();
|
|
|
|
CHECK(dialog_id.is_valid());
|
|
|
|
CHECK(message_id.is_valid_scheduled());
|
|
|
|
SCOPE_EXIT {
|
|
|
|
add_scheduled_message_stmt_.reset();
|
|
|
|
};
|
|
|
|
add_scheduled_message_stmt_.bind_int64(1, dialog_id.get()).ensure();
|
|
|
|
add_scheduled_message_stmt_.bind_int64(2, message_id.get()).ensure();
|
|
|
|
|
|
|
|
if (message_id.is_scheduled_server()) {
|
|
|
|
add_scheduled_message_stmt_.bind_int32(3, message_id.get_scheduled_server_message_id().get()).ensure();
|
|
|
|
} else {
|
|
|
|
add_scheduled_message_stmt_.bind_null(3).ensure();
|
|
|
|
}
|
|
|
|
|
|
|
|
add_scheduled_message_stmt_.bind_blob(4, data.as_slice()).ensure();
|
|
|
|
|
|
|
|
add_scheduled_message_stmt_.step().ensure();
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Status delete_message(FullMessageId full_message_id) final {
|
2019-12-06 19:46:57 +01:00
|
|
|
LOG(INFO) << "Delete " << full_message_id << " from database";
|
2018-12-31 20:04:05 +01:00
|
|
|
auto dialog_id = full_message_id.get_dialog_id();
|
|
|
|
auto message_id = full_message_id.get_message_id();
|
|
|
|
CHECK(dialog_id.is_valid());
|
2019-12-02 01:33:35 +01:00
|
|
|
CHECK(message_id.is_valid() || message_id.is_valid_scheduled());
|
2019-12-02 16:25:41 +01:00
|
|
|
bool is_scheduled = message_id.is_scheduled();
|
|
|
|
bool is_scheduled_server = is_scheduled && message_id.is_scheduled_server();
|
|
|
|
auto &stmt = is_scheduled
|
|
|
|
? (is_scheduled_server ? delete_scheduled_server_message_stmt_ : delete_scheduled_message_stmt_)
|
|
|
|
: delete_message_stmt_;
|
2018-12-31 20:04:05 +01:00
|
|
|
SCOPE_EXIT {
|
2019-12-02 01:33:35 +01:00
|
|
|
stmt.reset();
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2019-12-02 01:33:35 +01:00
|
|
|
stmt.bind_int64(1, dialog_id.get()).ensure();
|
2019-12-02 16:25:41 +01:00
|
|
|
if (is_scheduled_server) {
|
|
|
|
stmt.bind_int32(2, message_id.get_scheduled_server_message_id().get()).ensure();
|
|
|
|
} else {
|
|
|
|
stmt.bind_int64(2, message_id.get()).ensure();
|
|
|
|
}
|
2019-12-02 01:33:35 +01:00
|
|
|
stmt.step().ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Status delete_all_dialog_messages(DialogId dialog_id, MessageId from_message_id) final {
|
2019-12-06 19:46:57 +01:00
|
|
|
LOG(INFO) << "Delete all messages in " << dialog_id << " up to " << from_message_id << " from database";
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(dialog_id.is_valid());
|
|
|
|
CHECK(from_message_id.is_valid());
|
|
|
|
SCOPE_EXIT {
|
|
|
|
delete_all_dialog_messages_stmt_.reset();
|
|
|
|
};
|
|
|
|
delete_all_dialog_messages_stmt_.bind_int64(1, dialog_id.get()).ensure();
|
|
|
|
delete_all_dialog_messages_stmt_.bind_int64(2, from_message_id.get()).ensure();
|
|
|
|
auto status = delete_all_dialog_messages_stmt_.step();
|
|
|
|
if (status.is_error()) {
|
|
|
|
LOG(ERROR) << status;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-11-19 14:00:21 +01:00
|
|
|
Status delete_dialog_messages_by_sender(DialogId dialog_id, DialogId sender_dialog_id) final {
|
|
|
|
LOG(INFO) << "Delete all messages in " << dialog_id << " sent by " << sender_dialog_id << " from database";
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(dialog_id.is_valid());
|
2021-11-19 14:00:21 +01:00
|
|
|
CHECK(sender_dialog_id.is_valid());
|
2018-12-31 20:04:05 +01:00
|
|
|
SCOPE_EXIT {
|
2021-11-19 14:00:21 +01:00
|
|
|
delete_dialog_messages_by_sender_stmt_.reset();
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2021-11-19 14:00:21 +01:00
|
|
|
delete_dialog_messages_by_sender_stmt_.bind_int64(1, dialog_id.get()).ensure();
|
|
|
|
delete_dialog_messages_by_sender_stmt_.bind_int64(2, sender_dialog_id.get()).ensure();
|
|
|
|
delete_dialog_messages_by_sender_stmt_.step().ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2021-10-01 11:55:12 +02:00
|
|
|
Result<MessagesDbDialogMessage> get_message(FullMessageId full_message_id) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto dialog_id = full_message_id.get_dialog_id();
|
|
|
|
auto message_id = full_message_id.get_message_id();
|
|
|
|
CHECK(dialog_id.is_valid());
|
2019-12-02 15:27:28 +01:00
|
|
|
CHECK(message_id.is_valid() || message_id.is_valid_scheduled());
|
2019-12-02 15:56:03 +01:00
|
|
|
bool is_scheduled = message_id.is_scheduled();
|
|
|
|
bool is_scheduled_server = is_scheduled && message_id.is_scheduled_server();
|
|
|
|
auto &stmt = is_scheduled ? (is_scheduled_server ? get_scheduled_server_message_stmt_ : get_scheduled_message_stmt_)
|
|
|
|
: get_message_stmt_;
|
2018-12-31 20:04:05 +01:00
|
|
|
SCOPE_EXIT {
|
2019-12-02 15:27:28 +01:00
|
|
|
stmt.reset();
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2019-12-02 15:27:28 +01:00
|
|
|
|
|
|
|
stmt.bind_int64(1, dialog_id.get()).ensure();
|
2019-12-02 15:56:03 +01:00
|
|
|
if (is_scheduled_server) {
|
|
|
|
stmt.bind_int32(2, message_id.get_scheduled_server_message_id().get()).ensure();
|
|
|
|
} else {
|
|
|
|
stmt.bind_int64(2, message_id.get()).ensure();
|
|
|
|
}
|
2019-12-02 15:27:28 +01:00
|
|
|
stmt.step().ensure();
|
|
|
|
if (!stmt.has_row()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error("Not found");
|
|
|
|
}
|
2021-10-04 10:01:49 +02:00
|
|
|
MessageId received_message_id(stmt.view_int64(0));
|
2021-11-01 14:30:03 +01:00
|
|
|
Slice data = stmt.view_blob(1);
|
2021-10-04 10:01:49 +02:00
|
|
|
if (is_scheduled_server) {
|
|
|
|
CHECK(received_message_id.is_scheduled());
|
|
|
|
CHECK(received_message_id.is_scheduled_server());
|
|
|
|
CHECK(received_message_id.get_scheduled_server_message_id() == message_id.get_scheduled_server_message_id());
|
|
|
|
} else {
|
|
|
|
LOG_CHECK(received_message_id == message_id)
|
2021-11-01 14:30:03 +01:00
|
|
|
<< received_message_id << ' ' << message_id << ' ' << get_message_info(received_message_id, data, true).first;
|
2021-10-04 10:01:49 +02:00
|
|
|
}
|
2021-11-01 14:30:03 +01:00
|
|
|
return MessagesDbDialogMessage{received_message_id, BufferSlice(data)};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-10-01 10:22:56 +02:00
|
|
|
Result<MessagesDbMessage> get_message_by_unique_message_id(ServerMessageId unique_message_id) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!unique_message_id.is_valid()) {
|
2019-08-26 03:53:22 +02:00
|
|
|
return Status::Error("Invalid unique_message_id");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
SCOPE_EXIT {
|
|
|
|
get_message_by_unique_message_id_stmt_.reset();
|
|
|
|
};
|
|
|
|
get_message_by_unique_message_id_stmt_.bind_int32(1, unique_message_id.get()).ensure();
|
|
|
|
get_message_by_unique_message_id_stmt_.step().ensure();
|
|
|
|
if (!get_message_by_unique_message_id_stmt_.has_row()) {
|
|
|
|
return Status::Error("Not found");
|
|
|
|
}
|
|
|
|
DialogId dialog_id(get_message_by_unique_message_id_stmt_.view_int64(0));
|
2021-10-01 14:23:38 +02:00
|
|
|
MessageId message_id(get_message_by_unique_message_id_stmt_.view_int64(1));
|
|
|
|
return MessagesDbMessage{dialog_id, message_id, BufferSlice(get_message_by_unique_message_id_stmt_.view_blob(2))};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-10-01 11:55:12 +02:00
|
|
|
Result<MessagesDbDialogMessage> get_message_by_random_id(DialogId dialog_id, int64 random_id) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
SCOPE_EXIT {
|
|
|
|
get_message_by_random_id_stmt_.reset();
|
|
|
|
};
|
|
|
|
get_message_by_random_id_stmt_.bind_int64(1, dialog_id.get()).ensure();
|
|
|
|
get_message_by_random_id_stmt_.bind_int64(2, random_id).ensure();
|
|
|
|
get_message_by_random_id_stmt_.step().ensure();
|
|
|
|
if (!get_message_by_random_id_stmt_.has_row()) {
|
|
|
|
return Status::Error("Not found");
|
|
|
|
}
|
2021-10-01 14:23:38 +02:00
|
|
|
MessageId message_id(get_message_by_random_id_stmt_.view_int64(0));
|
|
|
|
return MessagesDbDialogMessage{message_id, BufferSlice(get_message_by_random_id_stmt_.view_blob(1))};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-10-01 11:55:12 +02:00
|
|
|
Result<MessagesDbDialogMessage> get_dialog_message_by_date(DialogId dialog_id, MessageId first_message_id,
|
|
|
|
MessageId last_message_id, int32 date) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 left_message_id = first_message_id.get();
|
|
|
|
int64 right_message_id = last_message_id.get();
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(left_message_id <= right_message_id) << first_message_id << " " << last_message_id;
|
2019-12-04 18:06:39 +01:00
|
|
|
TRY_RESULT(first_messages, get_messages_inner(get_messages_stmt_.asc_stmt_, dialog_id, left_message_id - 1, 1));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!first_messages.empty()) {
|
|
|
|
MessageId real_first_message_id;
|
|
|
|
int32 real_first_message_date;
|
|
|
|
std::tie(real_first_message_id, real_first_message_date) = get_message_info(first_messages[0]);
|
|
|
|
if (real_first_message_date <= date) {
|
|
|
|
// we definitely have at least one suitable message, let's do a binary search
|
|
|
|
left_message_id = real_first_message_id.get();
|
|
|
|
|
|
|
|
MessageId prev_found_message_id;
|
|
|
|
while (left_message_id <= right_message_id) {
|
|
|
|
auto middle_message_id = left_message_id + ((right_message_id - left_message_id) >> 1);
|
2019-12-04 18:06:39 +01:00
|
|
|
TRY_RESULT(messages, get_messages_inner(get_messages_stmt_.asc_stmt_, dialog_id, middle_message_id, 1));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
MessageId message_id;
|
|
|
|
int32 message_date = std::numeric_limits<int32>::max();
|
|
|
|
if (!messages.empty()) {
|
|
|
|
std::tie(message_id, message_date) = get_message_info(messages[0]);
|
|
|
|
}
|
|
|
|
if (message_date <= date) {
|
|
|
|
left_message_id = message_id.get();
|
|
|
|
} else {
|
|
|
|
right_message_id = middle_message_id - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prev_found_message_id == message_id) {
|
|
|
|
// we may be very close to the result, let's check
|
|
|
|
TRY_RESULT(left_messages,
|
2019-12-04 18:06:39 +01:00
|
|
|
get_messages_inner(get_messages_stmt_.asc_stmt_, dialog_id, left_message_id - 1, 2));
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(!left_messages.empty());
|
|
|
|
if (left_messages.size() == 1) {
|
|
|
|
// only one message has left, result is found
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageId next_message_id;
|
|
|
|
int32 next_message_date;
|
|
|
|
std::tie(next_message_id, next_message_date) = get_message_info(left_messages[1]);
|
|
|
|
if (next_message_date <= date) {
|
|
|
|
// next message has lesser date, adjusting left message
|
|
|
|
left_message_id = next_message_id.get();
|
|
|
|
} else {
|
|
|
|
// next message has bigger date, result is found
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prev_found_message_id = message_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// left_message_id is always an id of suitable message, let's return it
|
|
|
|
return get_message({dialog_id, MessageId(left_message_id)});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status::Error("Not found");
|
|
|
|
}
|
|
|
|
|
2021-10-01 11:55:12 +02:00
|
|
|
Result<std::pair<vector<MessagesDbMessage>, int32>> get_expiring_messages(int32 expires_from, int32 expires_till,
|
|
|
|
int32 limit) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
SCOPE_EXIT {
|
|
|
|
get_expiring_messages_stmt_.reset();
|
|
|
|
get_expiring_messages_helper_stmt_.reset();
|
|
|
|
};
|
|
|
|
|
2021-10-01 11:55:12 +02:00
|
|
|
vector<MessagesDbMessage> messages;
|
2018-12-31 20:04:05 +01:00
|
|
|
// load messages
|
2019-02-10 01:45:15 +01:00
|
|
|
if (expires_from <= expires_till) {
|
|
|
|
get_expiring_messages_stmt_.bind_int32(1, expires_from).ensure();
|
|
|
|
get_expiring_messages_stmt_.bind_int32(2, expires_till).ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
get_expiring_messages_stmt_.step().ensure();
|
|
|
|
|
|
|
|
while (get_expiring_messages_stmt_.has_row()) {
|
|
|
|
DialogId dialog_id(get_expiring_messages_stmt_.view_int64(0));
|
2021-10-01 14:23:38 +02:00
|
|
|
MessageId message_id(get_expiring_messages_stmt_.view_int64(1));
|
|
|
|
BufferSlice data(get_expiring_messages_stmt_.view_blob(2));
|
|
|
|
messages.push_back(MessagesDbMessage{dialog_id, message_id, std::move(data)});
|
2018-12-31 20:04:05 +01:00
|
|
|
get_expiring_messages_stmt_.step().ensure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-10 01:45:15 +01:00
|
|
|
// calc next expires_till
|
|
|
|
get_expiring_messages_helper_stmt_.bind_int32(1, expires_till).ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
get_expiring_messages_helper_stmt_.bind_int32(2, limit).ensure();
|
|
|
|
get_expiring_messages_helper_stmt_.step().ensure();
|
|
|
|
CHECK(get_expiring_messages_helper_stmt_.has_row());
|
|
|
|
int32 count = get_expiring_messages_helper_stmt_.view_int32(1);
|
2019-02-10 01:45:15 +01:00
|
|
|
int32 next_expires_till = -1;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (count != 0) {
|
2019-02-10 01:45:15 +01:00
|
|
|
next_expires_till = get_expiring_messages_helper_stmt_.view_int32(0);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-02-10 01:45:15 +01:00
|
|
|
return std::make_pair(std::move(messages), next_expires_till);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-01 11:50:45 +01:00
|
|
|
Result<MessagesDbCalendar> get_dialog_message_calendar(MessagesDbDialogCalendarQuery query) final {
|
|
|
|
auto &stmt = get_messages_from_index_stmts_[message_search_filter_index(query.filter)].desc_stmt_;
|
|
|
|
SCOPE_EXIT {
|
|
|
|
stmt.reset();
|
|
|
|
};
|
|
|
|
int32 limit = 1000;
|
|
|
|
stmt.bind_int64(1, query.dialog_id.get()).ensure();
|
|
|
|
stmt.bind_int64(2, query.from_message_id.get()).ensure();
|
|
|
|
stmt.bind_int32(3, limit).ensure();
|
|
|
|
|
|
|
|
vector<MessagesDbDialogMessage> messages;
|
|
|
|
vector<int32> total_counts;
|
|
|
|
stmt.step().ensure();
|
|
|
|
int32 current_day = std::numeric_limits<int32>::max();
|
|
|
|
while (stmt.has_row()) {
|
|
|
|
auto data_slice = stmt.view_blob(0);
|
|
|
|
MessageId message_id(stmt.view_int64(1));
|
|
|
|
auto info = get_message_info(message_id, data_slice, false);
|
|
|
|
auto day = (query.tz_offset + info.second) / 86400;
|
|
|
|
if (day >= current_day) {
|
|
|
|
CHECK(!total_counts.empty());
|
|
|
|
total_counts.back()++;
|
|
|
|
} else {
|
|
|
|
current_day = day;
|
|
|
|
messages.push_back(MessagesDbDialogMessage{message_id, BufferSlice(data_slice)});
|
|
|
|
total_counts.push_back(1);
|
|
|
|
}
|
|
|
|
stmt.step().ensure();
|
|
|
|
}
|
|
|
|
return MessagesDbCalendar{std::move(messages), std::move(total_counts)};
|
|
|
|
}
|
|
|
|
|
2021-11-01 14:30:03 +01:00
|
|
|
Result<MessagesDbMessagePositions> get_dialog_sparse_message_positions(
|
|
|
|
MessagesDbGetDialogSparseMessagePositionsQuery query) final {
|
|
|
|
auto &stmt = get_message_ids_stmts_[message_search_filter_index(query.filter)];
|
|
|
|
SCOPE_EXIT {
|
|
|
|
stmt.reset();
|
|
|
|
};
|
|
|
|
stmt.bind_int64(1, query.dialog_id.get()).ensure();
|
|
|
|
stmt.bind_int64(2, query.from_message_id.get()).ensure();
|
|
|
|
|
|
|
|
vector<MessageId> message_ids;
|
|
|
|
stmt.step().ensure();
|
|
|
|
while (stmt.has_row()) {
|
|
|
|
message_ids.push_back(MessageId(stmt.view_int64(0)));
|
|
|
|
stmt.step().ensure();
|
|
|
|
}
|
|
|
|
|
2021-12-01 20:25:52 +01:00
|
|
|
MessagesDbMessagePositions positions;
|
2021-12-24 09:59:44 +01:00
|
|
|
int32 limit = min(query.limit, static_cast<int32>(message_ids.size()));
|
|
|
|
if (limit > 0) {
|
|
|
|
double delta = static_cast<double>(message_ids.size()) / limit;
|
|
|
|
positions.total_count = static_cast<int32>(message_ids.size());
|
|
|
|
positions.positions.reserve(limit);
|
|
|
|
for (int32 i = 0; i < limit; i++) {
|
|
|
|
auto position = static_cast<int32>((i + 0.5) * delta);
|
|
|
|
auto message_id = message_ids[position];
|
|
|
|
TRY_RESULT(message, get_message({query.dialog_id, message_id}));
|
|
|
|
auto date = get_message_info(message).second;
|
|
|
|
positions.positions.push_back(MessagesDbMessagePosition{position, date, message_id});
|
|
|
|
}
|
2021-11-01 14:30:03 +01:00
|
|
|
}
|
2021-12-01 20:25:52 +01:00
|
|
|
return positions;
|
2021-11-01 14:30:03 +01:00
|
|
|
}
|
|
|
|
|
2021-10-01 11:55:12 +02:00
|
|
|
Result<vector<MessagesDbDialogMessage>> get_messages(MessagesDbMessagesQuery query) final {
|
2021-11-01 10:52:51 +01:00
|
|
|
if (query.filter != MessageSearchFilter::Empty) {
|
|
|
|
return get_messages_from_index(query.dialog_id, query.from_message_id, query.filter, query.offset, query.limit);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return get_messages_impl(get_messages_stmt_, query.dialog_id, query.from_message_id, query.offset, query.limit);
|
|
|
|
}
|
|
|
|
|
2021-10-01 11:55:12 +02:00
|
|
|
Result<vector<MessagesDbDialogMessage>> get_scheduled_messages(DialogId dialog_id, int32 limit) final {
|
2019-12-04 19:52:53 +01:00
|
|
|
return get_messages_inner(get_scheduled_messages_stmt_, dialog_id, std::numeric_limits<int64>::max(), limit);
|
2019-12-04 18:06:39 +01:00
|
|
|
}
|
|
|
|
|
2021-10-01 11:55:12 +02:00
|
|
|
Result<vector<MessagesDbDialogMessage>> get_messages_from_notification_id(DialogId dialog_id,
|
|
|
|
NotificationId from_notification_id,
|
|
|
|
int32 limit) final {
|
2018-11-28 21:19:30 +01:00
|
|
|
auto &stmt = get_messages_from_notification_id_stmt_;
|
|
|
|
SCOPE_EXIT {
|
|
|
|
stmt.reset();
|
|
|
|
};
|
|
|
|
stmt.bind_int64(1, dialog_id.get()).ensure();
|
|
|
|
stmt.bind_int32(2, from_notification_id.get()).ensure();
|
|
|
|
stmt.bind_int32(3, limit).ensure();
|
|
|
|
|
2021-10-01 11:55:12 +02:00
|
|
|
vector<MessagesDbDialogMessage> result;
|
2018-11-28 21:19:30 +01:00
|
|
|
stmt.step().ensure();
|
|
|
|
while (stmt.has_row()) {
|
|
|
|
auto data_slice = stmt.view_blob(0);
|
2021-10-01 14:23:38 +02:00
|
|
|
MessageId message_id(stmt.view_int64(1));
|
|
|
|
result.push_back(MessagesDbDialogMessage{message_id, BufferSlice(data_slice)});
|
|
|
|
LOG(INFO) << "Load " << message_id << " in " << dialog_id << " from database";
|
2018-11-28 21:19:30 +01:00
|
|
|
stmt.step().ensure();
|
|
|
|
}
|
|
|
|
return std::move(result);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
static string prepare_query(Slice query) {
|
|
|
|
auto is_word_character = [](uint32 a) {
|
|
|
|
switch (get_unicode_simple_category(a)) {
|
|
|
|
case UnicodeSimpleCategory::Letter:
|
|
|
|
case UnicodeSimpleCategory::DecimalNumber:
|
|
|
|
case UnicodeSimpleCategory::Number:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return a == '_';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t MAX_QUERY_SIZE = 1024;
|
2019-03-03 02:23:57 +01:00
|
|
|
query = utf8_truncate(query, MAX_QUERY_SIZE);
|
2018-12-31 20:04:05 +01:00
|
|
|
auto buf = StackAllocator::alloc(query.size() * 4 + 100);
|
|
|
|
StringBuilder sb(buf.as_slice());
|
|
|
|
bool in_word{false};
|
|
|
|
|
|
|
|
for (auto ptr = query.ubegin(), end = query.uend(); ptr < end;) {
|
|
|
|
uint32 code;
|
|
|
|
auto code_ptr = ptr;
|
2018-10-08 14:53:05 +02:00
|
|
|
ptr = next_utf8_unsafe(ptr, &code, "prepare_query");
|
2018-12-31 20:04:05 +01:00
|
|
|
if (is_word_character(code)) {
|
|
|
|
if (!in_word) {
|
|
|
|
in_word = true;
|
|
|
|
sb << "\"";
|
|
|
|
}
|
|
|
|
sb << Slice(code_ptr, ptr);
|
|
|
|
} else {
|
|
|
|
if (in_word) {
|
|
|
|
in_word = false;
|
|
|
|
sb << "\" ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (in_word) {
|
|
|
|
sb << "\" ";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sb.is_error()) {
|
|
|
|
LOG(ERROR) << "StringBuilder buffer overflow";
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb.as_cslice().str();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Result<MessagesDbFtsResult> get_messages_fts(MessagesDbFtsQuery query) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
SCOPE_EXIT {
|
|
|
|
get_messages_fts_stmt_.reset();
|
|
|
|
};
|
|
|
|
|
2021-11-01 10:52:51 +01:00
|
|
|
LOG(INFO) << tag("query", query.query) << query.dialog_id << tag("filter", query.filter)
|
2018-12-31 20:04:05 +01:00
|
|
|
<< tag("from_search_id", query.from_search_id) << tag("limit", query.limit);
|
|
|
|
string words = prepare_query(query.query);
|
|
|
|
LOG(INFO) << tag("from", query.query) << tag("to", words);
|
|
|
|
|
|
|
|
// dialog_id kludge
|
|
|
|
if (query.dialog_id.is_valid()) {
|
|
|
|
words += PSTRING() << " \"\a" << query.dialog_id.get() << "\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
// index_mask kludge
|
2021-11-01 10:52:51 +01:00
|
|
|
if (query.filter != MessageSearchFilter::Empty) {
|
|
|
|
words += PSTRING() << " \"\a\a" << message_search_filter_index(query.filter) << "\"";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto &stmt = get_messages_fts_stmt_;
|
|
|
|
stmt.bind_string(1, words).ensure();
|
|
|
|
if (query.from_search_id == 0) {
|
|
|
|
query.from_search_id = std::numeric_limits<int64>::max();
|
|
|
|
}
|
|
|
|
stmt.bind_int64(2, query.from_search_id).ensure();
|
|
|
|
stmt.bind_int32(3, query.limit).ensure();
|
|
|
|
MessagesDbFtsResult result;
|
|
|
|
auto status = stmt.step();
|
|
|
|
if (status.is_error()) {
|
|
|
|
LOG(ERROR) << status;
|
|
|
|
return std::move(result);
|
|
|
|
}
|
|
|
|
while (stmt.has_row()) {
|
2021-10-01 14:23:38 +02:00
|
|
|
DialogId dialog_id(stmt.view_int64(0));
|
|
|
|
MessageId message_id(stmt.view_int64(1));
|
|
|
|
auto data_slice = stmt.view_blob(2);
|
|
|
|
auto search_id = stmt.view_int64(3);
|
2018-12-31 20:04:05 +01:00
|
|
|
result.next_search_id = search_id;
|
2021-10-01 14:23:38 +02:00
|
|
|
result.messages.push_back(MessagesDbMessage{dialog_id, message_id, BufferSlice(data_slice)});
|
2018-12-31 20:04:05 +01:00
|
|
|
stmt.step().ensure();
|
|
|
|
}
|
|
|
|
return std::move(result);
|
|
|
|
}
|
|
|
|
|
2021-10-01 11:55:12 +02:00
|
|
|
Result<vector<MessagesDbDialogMessage>> get_messages_from_index(DialogId dialog_id, MessageId from_message_id,
|
2021-11-01 10:52:51 +01:00
|
|
|
MessageSearchFilter filter, int32 offset,
|
|
|
|
int32 limit) {
|
|
|
|
auto &stmt = get_messages_from_index_stmts_[message_search_filter_index(filter)];
|
2018-12-31 20:04:05 +01:00
|
|
|
return get_messages_impl(stmt, dialog_id, from_message_id, offset, limit);
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Result<MessagesDbCallsResult> get_calls(MessagesDbCallsQuery query) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 pos;
|
2021-11-01 10:52:51 +01:00
|
|
|
if (query.filter == MessageSearchFilter::Call) {
|
2018-12-31 20:04:05 +01:00
|
|
|
pos = 0;
|
2021-11-01 10:52:51 +01:00
|
|
|
} else if (query.filter == MessageSearchFilter::MissedCall) {
|
2018-12-31 20:04:05 +01:00
|
|
|
pos = 1;
|
|
|
|
} else {
|
2021-11-01 10:52:51 +01:00
|
|
|
return Status::Error(PSLICE() << "Filter is not Call or MissedCall: " << query.filter);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto &stmt = get_calls_stmts_[pos];
|
|
|
|
SCOPE_EXIT {
|
|
|
|
stmt.reset();
|
|
|
|
};
|
|
|
|
|
|
|
|
stmt.bind_int32(1, query.from_unique_message_id).ensure();
|
|
|
|
stmt.bind_int32(2, query.limit).ensure();
|
|
|
|
|
|
|
|
MessagesDbCallsResult result;
|
|
|
|
stmt.step().ensure();
|
|
|
|
while (stmt.has_row()) {
|
2021-10-01 14:23:38 +02:00
|
|
|
DialogId dialog_id(stmt.view_int64(0));
|
|
|
|
MessageId message_id(stmt.view_int64(1));
|
|
|
|
auto data_slice = stmt.view_blob(2);
|
|
|
|
result.messages.push_back(MessagesDbMessage{dialog_id, message_id, BufferSlice(data_slice)});
|
2018-12-31 20:04:05 +01:00
|
|
|
stmt.step().ensure();
|
|
|
|
}
|
|
|
|
return std::move(result);
|
|
|
|
}
|
|
|
|
|
2021-10-07 12:18:00 +02:00
|
|
|
Status begin_write_transaction() final {
|
|
|
|
return db_.begin_write_transaction();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
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_message_stmt_;
|
|
|
|
|
|
|
|
SqliteStatement delete_message_stmt_;
|
|
|
|
SqliteStatement delete_all_dialog_messages_stmt_;
|
2021-11-19 14:00:21 +01:00
|
|
|
SqliteStatement delete_dialog_messages_by_sender_stmt_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
SqliteStatement get_message_stmt_;
|
|
|
|
SqliteStatement get_message_by_random_id_stmt_;
|
|
|
|
SqliteStatement get_message_by_unique_message_id_stmt_;
|
|
|
|
SqliteStatement get_expiring_messages_stmt_;
|
|
|
|
SqliteStatement get_expiring_messages_helper_stmt_;
|
|
|
|
|
|
|
|
struct GetMessagesStmt {
|
|
|
|
SqliteStatement asc_stmt_;
|
|
|
|
SqliteStatement desc_stmt_;
|
|
|
|
};
|
|
|
|
GetMessagesStmt get_messages_stmt_;
|
2019-12-04 18:06:39 +01:00
|
|
|
SqliteStatement get_scheduled_messages_stmt_;
|
2018-11-28 21:19:30 +01:00
|
|
|
SqliteStatement get_messages_from_notification_id_stmt_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-11-01 14:30:03 +01:00
|
|
|
std::array<SqliteStatement, MESSAGES_DB_INDEX_COUNT> get_message_ids_stmts_;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::array<GetMessagesStmt, MESSAGES_DB_INDEX_COUNT> get_messages_from_index_stmts_;
|
|
|
|
std::array<SqliteStatement, 2> get_calls_stmts_;
|
|
|
|
|
|
|
|
SqliteStatement get_messages_fts_stmt_;
|
|
|
|
|
2019-12-02 01:33:35 +01:00
|
|
|
SqliteStatement add_scheduled_message_stmt_;
|
2019-12-02 15:27:28 +01:00
|
|
|
SqliteStatement get_scheduled_message_stmt_;
|
2019-12-02 15:56:03 +01:00
|
|
|
SqliteStatement get_scheduled_server_message_stmt_;
|
2019-12-02 01:33:35 +01:00
|
|
|
SqliteStatement delete_scheduled_message_stmt_;
|
2019-12-02 16:25:41 +01:00
|
|
|
SqliteStatement delete_scheduled_server_message_stmt_;
|
2019-12-02 01:33:35 +01:00
|
|
|
|
2021-10-21 11:51:16 +02:00
|
|
|
static Result<vector<MessagesDbDialogMessage>> get_messages_impl(GetMessagesStmt &stmt, DialogId dialog_id,
|
|
|
|
MessageId from_message_id, int32 offset,
|
|
|
|
int32 limit) {
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(dialog_id.is_valid()) << dialog_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(from_message_id.is_valid());
|
|
|
|
|
2018-12-25 03:45:36 +01:00
|
|
|
LOG(INFO) << "Loading messages in " << dialog_id << " from " << from_message_id << " with offset = " << offset
|
|
|
|
<< " and limit = " << limit;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
auto message_id = from_message_id.get();
|
|
|
|
|
|
|
|
if (message_id >= MessageId::max().get()) {
|
|
|
|
message_id--;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto left_message_id = message_id;
|
|
|
|
auto left_cnt = limit + offset;
|
|
|
|
|
|
|
|
auto right_message_id = message_id - 1;
|
|
|
|
auto right_cnt = -offset;
|
|
|
|
|
2021-10-01 11:55:12 +02:00
|
|
|
vector<MessagesDbDialogMessage> left;
|
|
|
|
vector<MessagesDbDialogMessage> right;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (left_cnt != 0) {
|
|
|
|
if (right_cnt == 1 && false) {
|
|
|
|
left_message_id++;
|
|
|
|
left_cnt++;
|
|
|
|
}
|
|
|
|
|
2019-12-08 07:57:33 +01:00
|
|
|
TRY_RESULT_ASSIGN(left, get_messages_inner(stmt.desc_stmt_, dialog_id, left_message_id, left_cnt));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (right_cnt == 1 && !left.empty() && false /*get_message_id(left[0].as_slice()) == message_id*/) {
|
|
|
|
right_cnt = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (right_cnt != 0) {
|
2019-12-08 07:57:33 +01:00
|
|
|
TRY_RESULT_ASSIGN(right, get_messages_inner(stmt.asc_stmt_, dialog_id, right_message_id, right_cnt));
|
2018-12-31 20:04:05 +01:00
|
|
|
std::reverse(right.begin(), right.end());
|
|
|
|
}
|
|
|
|
if (left.empty()) {
|
2018-12-05 23:34:22 +01:00
|
|
|
return std::move(right);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (right.empty()) {
|
2018-12-05 23:34:22 +01:00
|
|
|
return std::move(left);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
right.reserve(right.size() + left.size());
|
|
|
|
std::move(left.begin(), left.end(), std::back_inserter(right));
|
|
|
|
|
2018-12-05 23:34:22 +01:00
|
|
|
return std::move(right);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
static Result<vector<MessagesDbDialogMessage>> get_messages_inner(SqliteStatement &stmt, DialogId dialog_id,
|
|
|
|
int64 from_message_id, int32 limit) {
|
2018-12-31 20:04:05 +01:00
|
|
|
SCOPE_EXIT {
|
|
|
|
stmt.reset();
|
|
|
|
};
|
2019-12-04 18:06:39 +01:00
|
|
|
stmt.bind_int64(1, dialog_id.get()).ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
stmt.bind_int64(2, from_message_id).ensure();
|
|
|
|
stmt.bind_int32(3, limit).ensure();
|
|
|
|
|
2019-12-04 18:06:39 +01:00
|
|
|
LOG(INFO) << "Begin to load " << limit << " messages in " << dialog_id << " from " << MessageId(from_message_id)
|
|
|
|
<< " from database";
|
2021-10-01 11:55:12 +02:00
|
|
|
vector<MessagesDbDialogMessage> result;
|
2018-12-31 20:04:05 +01:00
|
|
|
stmt.step().ensure();
|
|
|
|
while (stmt.has_row()) {
|
|
|
|
auto data_slice = stmt.view_blob(0);
|
2021-10-01 14:23:38 +02:00
|
|
|
MessageId message_id(stmt.view_int64(1));
|
|
|
|
result.push_back(MessagesDbDialogMessage{message_id, BufferSlice(data_slice)});
|
|
|
|
LOG(INFO) << "Loaded " << message_id << " in " << dialog_id << " from database";
|
2018-12-31 20:04:05 +01:00
|
|
|
stmt.step().ensure();
|
|
|
|
}
|
|
|
|
return std::move(result);
|
|
|
|
}
|
|
|
|
|
2021-10-04 10:01:49 +02:00
|
|
|
static std::pair<MessageId, int32> get_message_info(const MessagesDbDialogMessage &message, bool from_data = false) {
|
2021-11-01 11:50:45 +01:00
|
|
|
return get_message_info(message.message_id, message.data.as_slice(), from_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::pair<MessageId, int32> get_message_info(MessageId message_id, Slice data, bool from_data) {
|
|
|
|
LogEventParser message_date_parser(data);
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 flags;
|
2019-02-20 03:44:54 +01:00
|
|
|
int32 flags2 = 0;
|
2021-10-01 14:23:38 +02:00
|
|
|
int32 flags3 = 0;
|
|
|
|
td::parse(flags, message_date_parser);
|
2019-02-20 03:44:54 +01:00
|
|
|
if ((flags & (1 << 29)) != 0) {
|
|
|
|
td::parse(flags2, message_date_parser);
|
2021-10-01 14:23:38 +02:00
|
|
|
if ((flags2 & (1 << 29)) != 0) {
|
|
|
|
td::parse(flags3, message_date_parser);
|
|
|
|
}
|
2019-02-20 03:44:54 +01:00
|
|
|
}
|
2021-10-01 14:23:38 +02:00
|
|
|
bool has_sender = (flags & (1 << 10)) != 0;
|
2021-11-01 11:50:45 +01:00
|
|
|
MessageId data_message_id;
|
|
|
|
td::parse(data_message_id, message_date_parser);
|
2018-12-31 20:04:05 +01:00
|
|
|
UserId sender_user_id;
|
|
|
|
if (has_sender) {
|
|
|
|
td::parse(sender_user_id, message_date_parser);
|
|
|
|
}
|
|
|
|
int32 date;
|
|
|
|
td::parse(date, message_date_parser);
|
2021-11-01 11:50:45 +01:00
|
|
|
LOG(INFO) << "Loaded " << message_id << "(aka " << data_message_id << ") sent at " << date << " by "
|
2021-10-01 14:23:38 +02:00
|
|
|
<< sender_user_id;
|
2021-11-01 11:50:45 +01:00
|
|
|
return {from_data ? data_message_id : message_id, date};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::shared_ptr<MessagesDbSyncSafeInterface> create_messages_db_sync(
|
|
|
|
std::shared_ptr<SqliteConnectionSafe> sqlite_connection) {
|
2021-07-04 04:58:54 +02:00
|
|
|
class MessagesDbSyncSafe final : public MessagesDbSyncSafeInterface {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit MessagesDbSyncSafe(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<MessagesDbImpl>(safe_connection->get().clone());
|
2018-12-31 20:04:05 +01:00
|
|
|
}) {
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
MessagesDbSyncInterface &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<MessagesDbSyncInterface>> lsls_db_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
return std::make_shared<MessagesDbSyncSafe>(std::move(sqlite_connection));
|
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class MessagesDbAsync final : public MessagesDbAsyncInterface {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
MessagesDbAsync(std::shared_ptr<MessagesDbSyncSafeInterface> sync_db, int32 scheduler_id) {
|
|
|
|
impl_ = create_actor_on_scheduler<Impl>("MessagesDbActor", scheduler_id, std::move(sync_db));
|
|
|
|
}
|
|
|
|
|
2021-11-19 14:00:21 +01:00
|
|
|
void add_message(FullMessageId full_message_id, ServerMessageId unique_message_id, DialogId sender_dialog_id,
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 random_id, int32 ttl_expires_at, int32 index_mask, int64 search_id, string text,
|
2020-09-28 14:13:25 +02:00
|
|
|
NotificationId notification_id, MessageId top_thread_message_id, BufferSlice data,
|
2021-07-03 22:51:36 +02:00
|
|
|
Promise<> promise) final {
|
2021-11-19 14:00:21 +01:00
|
|
|
send_closure_later(impl_, &Impl::add_message, full_message_id, unique_message_id, sender_dialog_id, random_id,
|
2020-09-28 14:13:25 +02:00
|
|
|
ttl_expires_at, index_mask, search_id, std::move(text), notification_id, top_thread_message_id,
|
|
|
|
std::move(data), std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void add_scheduled_message(FullMessageId full_message_id, BufferSlice data, Promise<> promise) final {
|
2019-12-02 01:33:35 +01:00
|
|
|
send_closure_later(impl_, &Impl::add_scheduled_message, full_message_id, std::move(data), std::move(promise));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void delete_message(FullMessageId full_message_id, Promise<> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure_later(impl_, &Impl::delete_message, full_message_id, std::move(promise));
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void delete_all_dialog_messages(DialogId dialog_id, MessageId from_message_id, Promise<> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure_later(impl_, &Impl::delete_all_dialog_messages, dialog_id, from_message_id, std::move(promise));
|
|
|
|
}
|
2021-11-19 14:00:21 +01:00
|
|
|
void delete_dialog_messages_by_sender(DialogId dialog_id, DialogId sender_dialog_id, Promise<> promise) final {
|
|
|
|
send_closure_later(impl_, &Impl::delete_dialog_messages_by_sender, dialog_id, sender_dialog_id, std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-10-01 11:55:12 +02:00
|
|
|
void get_message(FullMessageId full_message_id, Promise<MessagesDbDialogMessage> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure_later(impl_, &Impl::get_message, full_message_id, std::move(promise));
|
|
|
|
}
|
2021-10-01 10:22:56 +02:00
|
|
|
void get_message_by_unique_message_id(ServerMessageId unique_message_id, Promise<MessagesDbMessage> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure_later(impl_, &Impl::get_message_by_unique_message_id, unique_message_id, std::move(promise));
|
|
|
|
}
|
2021-10-01 11:55:12 +02:00
|
|
|
void get_message_by_random_id(DialogId dialog_id, int64 random_id, Promise<MessagesDbDialogMessage> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure_later(impl_, &Impl::get_message_by_random_id, dialog_id, random_id, std::move(promise));
|
|
|
|
}
|
|
|
|
void get_dialog_message_by_date(DialogId dialog_id, MessageId first_message_id, MessageId last_message_id, int32 date,
|
2021-10-01 11:55:12 +02:00
|
|
|
Promise<MessagesDbDialogMessage> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure_later(impl_, &Impl::get_dialog_message_by_date, dialog_id, first_message_id, last_message_id, date,
|
|
|
|
std::move(promise));
|
|
|
|
}
|
|
|
|
|
2021-11-01 11:50:45 +01:00
|
|
|
void get_dialog_message_calendar(MessagesDbDialogCalendarQuery query, Promise<MessagesDbCalendar> promise) final {
|
|
|
|
send_closure_later(impl_, &Impl::get_dialog_message_calendar, std::move(query), std::move(promise));
|
|
|
|
}
|
|
|
|
|
2021-11-01 14:30:03 +01:00
|
|
|
void get_dialog_sparse_message_positions(MessagesDbGetDialogSparseMessagePositionsQuery query,
|
|
|
|
Promise<MessagesDbMessagePositions> promise) final {
|
|
|
|
send_closure_later(impl_, &Impl::get_dialog_sparse_message_positions, std::move(query), std::move(promise));
|
|
|
|
}
|
|
|
|
|
2021-10-01 11:55:12 +02:00
|
|
|
void get_messages(MessagesDbMessagesQuery query, Promise<vector<MessagesDbDialogMessage>> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure_later(impl_, &Impl::get_messages, std::move(query), std::move(promise));
|
|
|
|
}
|
2021-10-01 11:55:12 +02:00
|
|
|
void get_scheduled_messages(DialogId dialog_id, int32 limit, Promise<vector<MessagesDbDialogMessage>> promise) final {
|
2019-12-04 19:52:53 +01:00
|
|
|
send_closure_later(impl_, &Impl::get_scheduled_messages, dialog_id, limit, std::move(promise));
|
2019-12-04 18:06:39 +01:00
|
|
|
}
|
2018-11-28 21:19:30 +01:00
|
|
|
void get_messages_from_notification_id(DialogId dialog_id, NotificationId from_notification_id, int32 limit,
|
2021-10-01 11:55:12 +02:00
|
|
|
Promise<vector<MessagesDbDialogMessage>> promise) final {
|
2018-11-28 21:19:30 +01:00
|
|
|
send_closure_later(impl_, &Impl::get_messages_from_notification_id, dialog_id, from_notification_id, limit,
|
|
|
|
std::move(promise));
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void get_calls(MessagesDbCallsQuery query, Promise<MessagesDbCallsResult> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure_later(impl_, &Impl::get_calls, std::move(query), std::move(promise));
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void get_messages_fts(MessagesDbFtsQuery query, Promise<MessagesDbFtsResult> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure_later(impl_, &Impl::get_messages_fts, std::move(query), std::move(promise));
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void get_expiring_messages(int32 expires_from, int32 expires_till, int32 limit,
|
2021-10-01 11:55:12 +02:00
|
|
|
Promise<std::pair<vector<MessagesDbMessage>, int32>> promise) final {
|
2019-02-10 01:45:15 +01:00
|
|
|
send_closure_later(impl_, &Impl::get_expiring_messages, expires_from, expires_till, limit, std::move(promise));
|
2018-12-31 20:04:05 +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));
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void force_flush() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure_later(impl_, &Impl::force_flush);
|
|
|
|
}
|
|
|
|
|
|
|
|
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<MessagesDbSyncSafeInterface> sync_db_safe) : sync_db_safe_(std::move(sync_db_safe)) {
|
|
|
|
}
|
2021-11-19 14:00:21 +01:00
|
|
|
void add_message(FullMessageId full_message_id, ServerMessageId unique_message_id, DialogId sender_dialog_id,
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 random_id, int32 ttl_expires_at, int32 index_mask, int64 search_id, string text,
|
2020-09-28 14:13:25 +02:00
|
|
|
NotificationId notification_id, MessageId top_thread_message_id, BufferSlice data,
|
|
|
|
Promise<> promise) {
|
2021-11-19 14:00:21 +01:00
|
|
|
add_write_query([this, full_message_id, unique_message_id, sender_dialog_id, random_id, ttl_expires_at,
|
|
|
|
index_mask, search_id, text = std::move(text), notification_id, top_thread_message_id,
|
2020-09-28 14:13:25 +02:00
|
|
|
data = std::move(data), promise = std::move(promise)](Unit) mutable {
|
|
|
|
on_write_result(std::move(promise),
|
2021-11-19 14:00:21 +01:00
|
|
|
sync_db_->add_message(full_message_id, unique_message_id, sender_dialog_id, random_id,
|
2020-09-28 14:13:25 +02:00
|
|
|
ttl_expires_at, index_mask, search_id, std::move(text), notification_id,
|
|
|
|
top_thread_message_id, std::move(data)));
|
2018-12-31 20:04:05 +01:00
|
|
|
});
|
|
|
|
}
|
2019-12-02 01:33:35 +01:00
|
|
|
void add_scheduled_message(FullMessageId full_message_id, BufferSlice data, Promise<> promise) {
|
|
|
|
add_write_query([this, full_message_id, promise = std::move(promise), data = std::move(data)](Unit) mutable {
|
2020-09-27 01:20:42 +02:00
|
|
|
on_write_result(std::move(promise), sync_db_->add_scheduled_message(full_message_id, std::move(data)));
|
2019-12-02 01:33:35 +01:00
|
|
|
});
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void delete_message(FullMessageId full_message_id, Promise<> promise) {
|
2020-09-27 01:20:42 +02:00
|
|
|
add_write_query([this, full_message_id, promise = std::move(promise)](Unit) mutable {
|
|
|
|
on_write_result(std::move(promise), sync_db_->delete_message(full_message_id));
|
2018-12-31 20:04:05 +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));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
void delete_all_dialog_messages(DialogId dialog_id, MessageId from_message_id, Promise<> promise) {
|
|
|
|
add_read_query();
|
|
|
|
promise.set_result(sync_db_->delete_all_dialog_messages(dialog_id, from_message_id));
|
|
|
|
}
|
2021-11-19 14:00:21 +01:00
|
|
|
void delete_dialog_messages_by_sender(DialogId dialog_id, DialogId sender_dialog_id, Promise<> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
add_read_query();
|
2021-11-19 14:00:21 +01:00
|
|
|
promise.set_result(sync_db_->delete_dialog_messages_by_sender(dialog_id, sender_dialog_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-10-01 11:55:12 +02:00
|
|
|
void get_message(FullMessageId full_message_id, Promise<MessagesDbDialogMessage> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
add_read_query();
|
|
|
|
promise.set_result(sync_db_->get_message(full_message_id));
|
|
|
|
}
|
2021-10-01 10:22:56 +02:00
|
|
|
void get_message_by_unique_message_id(ServerMessageId unique_message_id, Promise<MessagesDbMessage> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
add_read_query();
|
|
|
|
promise.set_result(sync_db_->get_message_by_unique_message_id(unique_message_id));
|
|
|
|
}
|
2021-10-01 11:55:12 +02:00
|
|
|
void get_message_by_random_id(DialogId dialog_id, int64 random_id, Promise<MessagesDbDialogMessage> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
add_read_query();
|
|
|
|
promise.set_result(sync_db_->get_message_by_random_id(dialog_id, random_id));
|
|
|
|
}
|
|
|
|
void get_dialog_message_by_date(DialogId dialog_id, MessageId first_message_id, MessageId last_message_id,
|
2021-10-01 11:55:12 +02:00
|
|
|
int32 date, Promise<MessagesDbDialogMessage> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
add_read_query();
|
|
|
|
promise.set_result(sync_db_->get_dialog_message_by_date(dialog_id, first_message_id, last_message_id, date));
|
|
|
|
}
|
|
|
|
|
2021-11-01 11:50:45 +01:00
|
|
|
void get_dialog_message_calendar(MessagesDbDialogCalendarQuery query, Promise<MessagesDbCalendar> promise) {
|
|
|
|
add_read_query();
|
|
|
|
promise.set_result(sync_db_->get_dialog_message_calendar(std::move(query)));
|
|
|
|
}
|
|
|
|
|
2021-11-01 14:30:03 +01:00
|
|
|
void get_dialog_sparse_message_positions(MessagesDbGetDialogSparseMessagePositionsQuery query,
|
|
|
|
Promise<MessagesDbMessagePositions> promise) {
|
|
|
|
add_read_query();
|
|
|
|
promise.set_result(sync_db_->get_dialog_sparse_message_positions(std::move(query)));
|
|
|
|
}
|
|
|
|
|
2021-10-01 11:55:12 +02:00
|
|
|
void get_messages(MessagesDbMessagesQuery query, Promise<vector<MessagesDbDialogMessage>> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
add_read_query();
|
|
|
|
promise.set_result(sync_db_->get_messages(std::move(query)));
|
|
|
|
}
|
2021-10-01 11:55:12 +02:00
|
|
|
void get_scheduled_messages(DialogId dialog_id, int32 limit, Promise<vector<MessagesDbDialogMessage>> promise) {
|
2019-12-04 18:06:39 +01:00
|
|
|
add_read_query();
|
2019-12-04 19:52:53 +01:00
|
|
|
promise.set_result(sync_db_->get_scheduled_messages(dialog_id, limit));
|
2019-12-04 18:06:39 +01:00
|
|
|
}
|
2018-11-28 21:19:30 +01:00
|
|
|
void get_messages_from_notification_id(DialogId dialog_id, NotificationId from_notification_id, int32 limit,
|
2021-10-01 11:55:12 +02:00
|
|
|
Promise<vector<MessagesDbDialogMessage>> promise) {
|
2018-11-28 21:19:30 +01:00
|
|
|
add_read_query();
|
|
|
|
promise.set_result(sync_db_->get_messages_from_notification_id(dialog_id, from_notification_id, limit));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
void get_calls(MessagesDbCallsQuery query, Promise<MessagesDbCallsResult> promise) {
|
|
|
|
add_read_query();
|
|
|
|
promise.set_result(sync_db_->get_calls(std::move(query)));
|
|
|
|
}
|
|
|
|
void get_messages_fts(MessagesDbFtsQuery query, Promise<MessagesDbFtsResult> promise) {
|
|
|
|
add_read_query();
|
|
|
|
promise.set_result(sync_db_->get_messages_fts(std::move(query)));
|
|
|
|
}
|
2019-02-10 01:45:15 +01:00
|
|
|
void get_expiring_messages(int32 expires_from, int32 expires_till, int32 limit,
|
2021-10-01 11:55:12 +02:00
|
|
|
Promise<std::pair<vector<MessagesDbMessage>, int32>> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
add_read_query();
|
2019-02-10 01:45:15 +01:00
|
|
|
promise.set_result(sync_db_->get_expiring_messages(expires_from, expires_till, limit));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void close(Promise<> promise) {
|
|
|
|
do_flush();
|
|
|
|
sync_db_safe_.reset();
|
|
|
|
sync_db_ = nullptr;
|
|
|
|
promise.set_value(Unit());
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void force_flush() {
|
|
|
|
LOG(INFO) << "MessagesDb flushed";
|
|
|
|
do_flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<MessagesDbSyncSafeInterface> sync_db_safe_;
|
|
|
|
MessagesDbSyncInterface *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_
|
2021-10-01 11:55:12 +02:00
|
|
|
vector<std::pair<Promise<>, Status>> pending_write_results_;
|
|
|
|
vector<Promise<>> pending_writes_;
|
2018-12-31 20:04:05 +01:00
|
|
|
double wakeup_at_ = 0;
|
|
|
|
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_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void add_read_query() {
|
|
|
|
do_flush();
|
|
|
|
}
|
|
|
|
void do_flush() {
|
|
|
|
if (pending_writes_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-10-07 12:18:00 +02:00
|
|
|
sync_db_->begin_write_transaction().ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
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();
|
|
|
|
}
|
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<MessagesDbAsyncInterface> create_messages_db_async(std::shared_ptr<MessagesDbSyncSafeInterface> sync_db,
|
|
|
|
int32 scheduler_id) {
|
|
|
|
return std::make_shared<MessagesDbAsync>(std::move(sync_db), scheduler_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|