2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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)
|
|
|
|
//
|
2022-11-09 18:35:22 +01:00
|
|
|
#include "td/telegram/MessageDb.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/telegram/logevent/LogEvent.h"
|
2022-10-12 14:59:58 +02:00
|
|
|
#include "td/telegram/UserId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#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"
|
|
|
|
#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 {
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
static constexpr int32 MESSAGE_DB_INDEX_COUNT = 30;
|
|
|
|
static constexpr int32 MESSAGE_DB_INDEX_COUNT_OLD = 9;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// NB: must happen inside a transaction
|
2022-11-09 18:35:22 +01:00
|
|
|
Status init_message_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;
|
2023-07-18 17:20:43 +02:00
|
|
|
} else if (version < static_cast<int32>(DbVersion::CreateDialogDb) || version > current_db_version()) {
|
2022-11-09 18:35:22 +01:00
|
|
|
TRY_STATUS(drop_message_db(db, version));
|
2018-12-31 20:04:05 +01:00
|
|
|
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"));
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
TRY_STATUS(add_media_indices(0, MESSAGE_DB_INDEX_COUNT));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2023-07-18 17:20:43 +02:00
|
|
|
if (version < static_cast<int32>(DbVersion::AddMessageDbMediaIndex)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_STATUS(db.exec("ALTER TABLE messages ADD COLUMN index_mask INT4"));
|
2022-11-09 18:35:22 +01:00
|
|
|
TRY_STATUS(add_media_indices(0, MESSAGE_DB_INDEX_COUNT_OLD));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-07-18 17:20:43 +02:00
|
|
|
if (version < static_cast<int32>(DbVersion::AddMessageDb30MediaIndex)) {
|
2022-11-09 18:35:22 +01:00
|
|
|
TRY_STATUS(add_media_indices(MESSAGE_DB_INDEX_COUNT_OLD, MESSAGE_DB_INDEX_COUNT));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-07-18 17:20:43 +02:00
|
|
|
if (version < static_cast<int32>(DbVersion::AddMessageDbFts)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
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());
|
|
|
|
}
|
2023-07-18 17:20:43 +02:00
|
|
|
if (version < static_cast<int32>(DbVersion::AddMessagesCallIndex)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
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
|
2022-11-09 18:35:22 +01:00
|
|
|
Status drop_message_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");
|
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
class MessageDbImpl final : public MessageDbSyncInterface {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2022-11-09 18:35:22 +01:00
|
|
|
explicit MessageDbImpl(SqliteDb db) : db_(std::move(db)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
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
|
|
|
|
2023-05-15 12:28:23 +02:00
|
|
|
TRY_RESULT_ASSIGN(
|
|
|
|
get_expiring_messages_stmt_,
|
|
|
|
db_.get_statement("SELECT dialog_id, message_id, data FROM messages WHERE ttl_expires_at <= ?1 LIMIT ?2"));
|
2019-09-02 20:11:56 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
for (int32 i = 0; i < MESSAGE_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(FATAL) << "EXPLAINED";
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void add_message(MessageFullId message_full_id, ServerMessageId unique_message_id, DialogId sender_dialog_id,
|
2022-11-07 20:19:44 +01:00
|
|
|
int64 random_id, int32 ttl_expires_at, int32 index_mask, int64 search_id, string text,
|
|
|
|
NotificationId notification_id, MessageId top_thread_message_id, BufferSlice data) final {
|
2023-09-21 18:11:17 +02:00
|
|
|
LOG(INFO) << "Add " << message_full_id << " to database";
|
|
|
|
auto dialog_id = message_full_id.get_dialog_id();
|
|
|
|
auto message_id = message_full_id.get_message_id();
|
|
|
|
LOG_CHECK(dialog_id.is_valid()) << dialog_id << ' ' << message_id << ' ' << message_full_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) {
|
2022-11-09 18:35:22 +01:00
|
|
|
for (int i = 0; i < MESSAGE_DB_INDEX_COUNT; i++) {
|
2018-12-31 20:04:05 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void add_scheduled_message(MessageFullId message_full_id, BufferSlice data) final {
|
|
|
|
LOG(INFO) << "Add " << message_full_id << " to database";
|
|
|
|
auto dialog_id = message_full_id.get_dialog_id();
|
|
|
|
auto message_id = message_full_id.get_message_id();
|
2019-12-02 01:33:35 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void delete_message(MessageFullId message_full_id) final {
|
|
|
|
LOG(INFO) << "Delete " << message_full_id << " from database";
|
|
|
|
auto dialog_id = message_full_id.get_dialog_id();
|
|
|
|
auto message_id = message_full_id.get_message_id();
|
2018-12-31 20:04:05 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-07 20:19:44 +01:00
|
|
|
void 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-07 20:19:44 +01:00
|
|
|
void delete_dialog_messages_by_sender(DialogId dialog_id, DialogId sender_dialog_id) final {
|
2021-11-19 14:00:21 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
Result<MessageDbDialogMessage> get_message(MessageFullId message_full_id) final {
|
|
|
|
auto dialog_id = message_full_id.get_dialog_id();
|
|
|
|
auto message_id = message_full_id.get_message_id();
|
2018-12-31 20:04:05 +01:00
|
|
|
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
|
|
|
}
|
2022-11-09 18:35:22 +01:00
|
|
|
return MessageDbDialogMessage{received_message_id, BufferSlice(data)};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
Result<MessageDbMessage> 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));
|
2022-11-09 18:35:22 +01:00
|
|
|
return MessageDbMessage{dialog_id, message_id, BufferSlice(get_message_by_unique_message_id_stmt_.view_blob(2))};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
Result<MessageDbDialogMessage> 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));
|
2022-11-09 18:35:22 +01:00
|
|
|
return MessageDbDialogMessage{message_id, BufferSlice(get_message_by_random_id_stmt_.view_blob(1))};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
Result<MessageDbDialogMessage> 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;
|
2022-11-07 20:19:44 +01:00
|
|
|
auto 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);
|
2022-11-07 20:19:44 +01:00
|
|
|
auto 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
|
2022-11-07 20:19:44 +01:00
|
|
|
auto left_messages = 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;
|
|
|
|
}
|
|
|
|
|
2023-01-16 10:59:43 +01:00
|
|
|
// left_message_id is always an identifier of suitable message, let's return it
|
2018-12-31 20:04:05 +01:00
|
|
|
return get_message({dialog_id, MessageId(left_message_id)});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status::Error("Not found");
|
|
|
|
}
|
|
|
|
|
2023-05-15 12:28:23 +02:00
|
|
|
vector<MessageDbMessage> get_expiring_messages(int32 expires_till, int32 limit) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
SCOPE_EXIT {
|
|
|
|
get_expiring_messages_stmt_.reset();
|
|
|
|
};
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
vector<MessageDbMessage> messages;
|
2023-05-15 12:04:03 +02:00
|
|
|
get_expiring_messages_stmt_.bind_int32(1, expires_till).ensure();
|
2023-05-15 12:28:23 +02:00
|
|
|
get_expiring_messages_stmt_.bind_int32(2, limit).ensure();
|
2023-05-15 12:04:03 +02:00
|
|
|
get_expiring_messages_stmt_.step().ensure();
|
|
|
|
|
|
|
|
while (get_expiring_messages_stmt_.has_row()) {
|
|
|
|
DialogId dialog_id(get_expiring_messages_stmt_.view_int64(0));
|
|
|
|
MessageId message_id(get_expiring_messages_stmt_.view_int64(1));
|
|
|
|
BufferSlice data(get_expiring_messages_stmt_.view_blob(2));
|
|
|
|
messages.push_back(MessageDbMessage{dialog_id, message_id, std::move(data)});
|
2018-12-31 20:04:05 +01:00
|
|
|
get_expiring_messages_stmt_.step().ensure();
|
|
|
|
}
|
|
|
|
|
2023-05-18 01:11:44 +02:00
|
|
|
return messages;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
MessageDbCalendar get_dialog_message_calendar(MessageDbDialogCalendarQuery query) final {
|
2021-11-01 11:50:45 +01:00
|
|
|
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();
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
vector<MessageDbDialogMessage> messages;
|
2021-11-01 11:50:45 +01:00
|
|
|
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;
|
2022-11-09 18:35:22 +01:00
|
|
|
messages.push_back(MessageDbDialogMessage{message_id, BufferSlice(data_slice)});
|
2021-11-01 11:50:45 +01:00
|
|
|
total_counts.push_back(1);
|
|
|
|
}
|
|
|
|
stmt.step().ensure();
|
|
|
|
}
|
2022-11-09 18:35:22 +01:00
|
|
|
return MessageDbCalendar{std::move(messages), std::move(total_counts)};
|
2021-11-01 11:50:45 +01:00
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
Result<MessageDbMessagePositions> get_dialog_sparse_message_positions(
|
|
|
|
MessageDbGetDialogSparseMessagePositionsQuery query) final {
|
2021-11-01 14:30:03 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
MessageDbMessagePositions 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;
|
2022-11-09 18:35:22 +01:00
|
|
|
positions.positions.push_back(MessageDbMessagePosition{position, date, message_id});
|
2021-12-24 09:59:44 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
vector<MessageDbDialogMessage> get_messages(MessageDbMessagesQuery 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);
|
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
vector<MessageDbDialogMessage> 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
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
vector<MessageDbDialogMessage> 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();
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
vector<MessageDbDialogMessage> 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));
|
2022-11-09 18:35:22 +01:00
|
|
|
result.push_back(MessageDbDialogMessage{message_id, BufferSlice(data_slice)});
|
2021-10-01 14:23:38 +02:00
|
|
|
LOG(INFO) << "Load " << message_id << " in " << dialog_id << " from database";
|
2018-11-28 21:19:30 +01:00
|
|
|
stmt.step().ensure();
|
|
|
|
}
|
2022-11-07 20:19:44 +01:00
|
|
|
return result;
|
2018-11-28 21:19:30 +01:00
|
|
|
}
|
|
|
|
|
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;
|
2022-08-19 15:37:44 +02:00
|
|
|
ptr = next_utf8_unsafe(ptr, &code);
|
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();
|
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
MessageDbFtsResult get_messages_fts(MessageDbFtsQuery 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();
|
2022-11-09 18:35:22 +01:00
|
|
|
MessageDbFtsResult result;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto status = stmt.step();
|
|
|
|
if (status.is_error()) {
|
|
|
|
LOG(ERROR) << status;
|
2022-11-07 20:19:44 +01:00
|
|
|
return result;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
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;
|
2022-11-09 18:35:22 +01:00
|
|
|
result.messages.push_back(MessageDbMessage{dialog_id, message_id, BufferSlice(data_slice)});
|
2018-12-31 20:04:05 +01:00
|
|
|
stmt.step().ensure();
|
|
|
|
}
|
2022-11-07 20:19:44 +01:00
|
|
|
return result;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
vector<MessageDbDialogMessage> get_messages_from_index(DialogId dialog_id, MessageId from_message_id,
|
|
|
|
MessageSearchFilter filter, int32 offset, int32 limit) {
|
2021-11-01 10:52:51 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
MessageDbCallsResult get_calls(MessageDbCallsQuery 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 {
|
2022-11-07 20:19:44 +01:00
|
|
|
UNREACHABLE();
|
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();
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
MessageDbCallsResult result;
|
2018-12-31 20:04:05 +01:00
|
|
|
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);
|
2022-11-09 18:35:22 +01:00
|
|
|
result.messages.push_back(MessageDbMessage{dialog_id, message_id, BufferSlice(data_slice)});
|
2018-12-31 20:04:05 +01:00
|
|
|
stmt.step().ensure();
|
|
|
|
}
|
2022-11-07 20:19:44 +01:00
|
|
|
return result;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
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_;
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
std::array<SqliteStatement, MESSAGE_DB_INDEX_COUNT> get_message_ids_stmts_;
|
|
|
|
std::array<GetMessagesStmt, MESSAGE_DB_INDEX_COUNT> get_messages_from_index_stmts_;
|
2018-12-31 20:04:05 +01:00
|
|
|
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
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
static vector<MessageDbDialogMessage> 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;
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
vector<MessageDbDialogMessage> left;
|
|
|
|
vector<MessageDbDialogMessage> right;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (left_cnt != 0) {
|
|
|
|
if (right_cnt == 1 && false) {
|
|
|
|
left_message_id++;
|
|
|
|
left_cnt++;
|
|
|
|
}
|
|
|
|
|
2022-11-07 20:19:44 +01:00
|
|
|
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) {
|
2022-11-07 20:19:44 +01:00
|
|
|
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()) {
|
2022-11-07 20:19:44 +01:00
|
|
|
return right;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (right.empty()) {
|
2022-11-07 20:19:44 +01:00
|
|
|
return 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));
|
|
|
|
|
2022-11-07 20:19:44 +01:00
|
|
|
return right;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
static vector<MessageDbDialogMessage> 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";
|
2022-11-09 18:35:22 +01:00
|
|
|
vector<MessageDbDialogMessage> 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));
|
2022-11-09 18:35:22 +01:00
|
|
|
result.push_back(MessageDbDialogMessage{message_id, BufferSlice(data_slice)});
|
2021-10-01 14:23:38 +02:00
|
|
|
LOG(INFO) << "Loaded " << message_id << " in " << dialog_id << " from database";
|
2018-12-31 20:04:05 +01:00
|
|
|
stmt.step().ensure();
|
|
|
|
}
|
2022-11-07 20:19:44 +01:00
|
|
|
return result;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
static std::pair<MessageId, int32> get_message_info(const MessageDbDialogMessage &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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
std::shared_ptr<MessageDbSyncSafeInterface> create_message_db_sync(
|
2018-12-31 20:04:05 +01:00
|
|
|
std::shared_ptr<SqliteConnectionSafe> sqlite_connection) {
|
2022-11-09 18:35:22 +01:00
|
|
|
class MessageDbSyncSafe final : public MessageDbSyncSafeInterface {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2022-11-09 18:35:22 +01:00
|
|
|
explicit MessageDbSyncSafe(std::shared_ptr<SqliteConnectionSafe> sqlite_connection)
|
2018-12-31 20:04:05 +01:00
|
|
|
: lsls_db_([safe_connection = std::move(sqlite_connection)] {
|
2022-11-09 18:35:22 +01:00
|
|
|
return make_unique<MessageDbImpl>(safe_connection->get().clone());
|
2018-12-31 20:04:05 +01:00
|
|
|
}) {
|
|
|
|
}
|
2022-11-09 18:35:22 +01:00
|
|
|
MessageDbSyncInterface &get() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return *lsls_db_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-11-09 18:35:22 +01:00
|
|
|
LazySchedulerLocalStorage<unique_ptr<MessageDbSyncInterface>> lsls_db_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2022-11-09 18:35:22 +01:00
|
|
|
return std::make_shared<MessageDbSyncSafe>(std::move(sqlite_connection));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
class MessageDbAsync final : public MessageDbAsyncInterface {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2022-11-09 18:35:22 +01:00
|
|
|
MessageDbAsync(std::shared_ptr<MessageDbSyncSafeInterface> sync_db, int32 scheduler_id) {
|
|
|
|
impl_ = create_actor_on_scheduler<Impl>("MessageDbActor", scheduler_id, std::move(sync_db));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void add_message(MessageFullId message_full_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 {
|
2023-09-21 18:11:17 +02:00
|
|
|
send_closure_later(impl_, &Impl::add_message, message_full_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
|
|
|
}
|
2023-09-21 18:11:17 +02:00
|
|
|
void add_scheduled_message(MessageFullId message_full_id, BufferSlice data, Promise<> promise) final {
|
|
|
|
send_closure_later(impl_, &Impl::add_scheduled_message, message_full_id, std::move(data), std::move(promise));
|
2019-12-02 01:33:35 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void delete_message(MessageFullId message_full_id, Promise<> promise) final {
|
|
|
|
send_closure_later(impl_, &Impl::delete_message, message_full_id, std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void get_message(MessageFullId message_full_id, Promise<MessageDbDialogMessage> promise) final {
|
|
|
|
send_closure_later(impl_, &Impl::get_message, message_full_id, std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-11-09 18:35:22 +01:00
|
|
|
void get_message_by_unique_message_id(ServerMessageId unique_message_id, Promise<MessageDbMessage> 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));
|
|
|
|
}
|
2022-11-09 18:35:22 +01:00
|
|
|
void get_message_by_random_id(DialogId dialog_id, int64 random_id, Promise<MessageDbDialogMessage> 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,
|
2022-11-09 18:35:22 +01:00
|
|
|
Promise<MessageDbDialogMessage> 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));
|
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
void get_dialog_message_calendar(MessageDbDialogCalendarQuery query, Promise<MessageDbCalendar> promise) final {
|
2021-11-01 11:50:45 +01:00
|
|
|
send_closure_later(impl_, &Impl::get_dialog_message_calendar, std::move(query), std::move(promise));
|
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
void get_dialog_sparse_message_positions(MessageDbGetDialogSparseMessagePositionsQuery query,
|
|
|
|
Promise<MessageDbMessagePositions> promise) final {
|
2021-11-01 14:30:03 +01:00
|
|
|
send_closure_later(impl_, &Impl::get_dialog_sparse_message_positions, std::move(query), std::move(promise));
|
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
void get_messages(MessageDbMessagesQuery query, Promise<vector<MessageDbDialogMessage>> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure_later(impl_, &Impl::get_messages, std::move(query), std::move(promise));
|
|
|
|
}
|
2022-11-09 18:35:22 +01:00
|
|
|
void get_scheduled_messages(DialogId dialog_id, int32 limit, Promise<vector<MessageDbDialogMessage>> 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,
|
2022-11-09 18:35:22 +01:00
|
|
|
Promise<vector<MessageDbDialogMessage>> 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));
|
|
|
|
}
|
2022-11-09 18:35:22 +01:00
|
|
|
void get_calls(MessageDbCallsQuery query, Promise<MessageDbCallsResult> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure_later(impl_, &Impl::get_calls, std::move(query), std::move(promise));
|
|
|
|
}
|
2022-11-09 18:35:22 +01:00
|
|
|
void get_messages_fts(MessageDbFtsQuery query, Promise<MessageDbFtsResult> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure_later(impl_, &Impl::get_messages_fts, std::move(query), std::move(promise));
|
|
|
|
}
|
2023-05-15 12:28:23 +02:00
|
|
|
void get_expiring_messages(int32 expires_till, int32 limit, Promise<vector<MessageDbMessage>> promise) final {
|
2023-05-15 12:04:03 +02:00
|
|
|
send_closure_later(impl_, &Impl::get_expiring_messages, 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:
|
2022-11-09 18:35:22 +01:00
|
|
|
explicit Impl(std::shared_ptr<MessageDbSyncSafeInterface> sync_db_safe) : sync_db_safe_(std::move(sync_db_safe)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-09-21 18:11:17 +02:00
|
|
|
void add_message(MessageFullId message_full_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) {
|
2023-09-21 18:11:17 +02:00
|
|
|
add_write_query([this, message_full_id, unique_message_id, sender_dialog_id, random_id, ttl_expires_at,
|
2021-11-19 14:00:21 +01:00
|
|
|
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 {
|
2023-09-21 18:11:17 +02:00
|
|
|
sync_db_->add_message(message_full_id, unique_message_id, sender_dialog_id, random_id, ttl_expires_at,
|
2022-11-07 20:19:44 +01:00
|
|
|
index_mask, search_id, std::move(text), notification_id, top_thread_message_id,
|
|
|
|
std::move(data));
|
|
|
|
on_write_result(std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
});
|
|
|
|
}
|
2023-09-21 18:11:17 +02:00
|
|
|
void add_scheduled_message(MessageFullId message_full_id, BufferSlice data, Promise<> promise) {
|
|
|
|
add_write_query([this, message_full_id, promise = std::move(promise), data = std::move(data)](Unit) mutable {
|
|
|
|
sync_db_->add_scheduled_message(message_full_id, std::move(data));
|
2022-11-07 20:19:44 +01:00
|
|
|
on_write_result(std::move(promise));
|
2019-12-02 01:33:35 +01:00
|
|
|
});
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void delete_message(MessageFullId message_full_id, Promise<> promise) {
|
|
|
|
add_write_query([this, message_full_id, promise = std::move(promise)](Unit) mutable {
|
|
|
|
sync_db_->delete_message(message_full_id);
|
2022-11-07 20:19:44 +01:00
|
|
|
on_write_result(std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
});
|
|
|
|
}
|
2022-11-07 20:19:44 +01:00
|
|
|
|
|
|
|
void on_write_result(Promise<Unit> &&promise) {
|
|
|
|
// We are inside a transaction and don't know how to handle errors
|
|
|
|
finished_writes_.push_back(std::move(promise));
|
2019-08-09 20:30:01 +02:00
|
|
|
}
|
2022-11-07 20:19:44 +01:00
|
|
|
|
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();
|
2022-11-07 20:19:44 +01:00
|
|
|
sync_db_->delete_all_dialog_messages(dialog_id, from_message_id);
|
|
|
|
promise.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-11-07 20:19:44 +01:00
|
|
|
|
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();
|
2022-11-07 20:19:44 +01:00
|
|
|
sync_db_->delete_dialog_messages_by_sender(dialog_id, sender_dialog_id);
|
|
|
|
promise.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void get_message(MessageFullId message_full_id, Promise<MessageDbDialogMessage> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
add_read_query();
|
2023-09-21 18:11:17 +02:00
|
|
|
promise.set_result(sync_db_->get_message(message_full_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-11-09 18:35:22 +01:00
|
|
|
void get_message_by_unique_message_id(ServerMessageId unique_message_id, Promise<MessageDbMessage> 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));
|
|
|
|
}
|
2022-11-09 18:35:22 +01:00
|
|
|
void get_message_by_random_id(DialogId dialog_id, int64 random_id, Promise<MessageDbDialogMessage> 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,
|
2022-11-09 18:35:22 +01:00
|
|
|
int32 date, Promise<MessageDbDialogMessage> 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));
|
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
void get_dialog_message_calendar(MessageDbDialogCalendarQuery query, Promise<MessageDbCalendar> promise) {
|
2021-11-01 11:50:45 +01:00
|
|
|
add_read_query();
|
2022-11-07 20:19:44 +01:00
|
|
|
promise.set_value(sync_db_->get_dialog_message_calendar(std::move(query)));
|
2021-11-01 11:50:45 +01:00
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
void get_dialog_sparse_message_positions(MessageDbGetDialogSparseMessagePositionsQuery query,
|
|
|
|
Promise<MessageDbMessagePositions> promise) {
|
2021-11-01 14:30:03 +01:00
|
|
|
add_read_query();
|
|
|
|
promise.set_result(sync_db_->get_dialog_sparse_message_positions(std::move(query)));
|
|
|
|
}
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
void get_messages(MessageDbMessagesQuery query, Promise<vector<MessageDbDialogMessage>> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
add_read_query();
|
2022-11-07 20:19:44 +01:00
|
|
|
promise.set_value(sync_db_->get_messages(std::move(query)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-11-09 18:35:22 +01:00
|
|
|
void get_scheduled_messages(DialogId dialog_id, int32 limit, Promise<vector<MessageDbDialogMessage>> promise) {
|
2019-12-04 18:06:39 +01:00
|
|
|
add_read_query();
|
2022-11-07 20:19:44 +01:00
|
|
|
promise.set_value(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,
|
2022-11-09 18:35:22 +01:00
|
|
|
Promise<vector<MessageDbDialogMessage>> promise) {
|
2018-11-28 21:19:30 +01:00
|
|
|
add_read_query();
|
2022-11-07 20:19:44 +01:00
|
|
|
promise.set_value(sync_db_->get_messages_from_notification_id(dialog_id, from_notification_id, limit));
|
2018-11-28 21:19:30 +01:00
|
|
|
}
|
2022-11-09 18:35:22 +01:00
|
|
|
void get_calls(MessageDbCallsQuery query, Promise<MessageDbCallsResult> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
add_read_query();
|
2022-11-07 20:19:44 +01:00
|
|
|
promise.set_value(sync_db_->get_calls(std::move(query)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-11-09 18:35:22 +01:00
|
|
|
void get_messages_fts(MessageDbFtsQuery query, Promise<MessageDbFtsResult> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
add_read_query();
|
2022-11-07 20:19:44 +01:00
|
|
|
promise.set_value(sync_db_->get_messages_fts(std::move(query)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-05-15 12:28:23 +02:00
|
|
|
void get_expiring_messages(int32 expires_till, int32 limit, Promise<vector<MessageDbMessage>> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
add_read_query();
|
2023-05-15 12:04:03 +02:00
|
|
|
promise.set_value(sync_db_->get_expiring_messages(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() {
|
|
|
|
do_flush();
|
2022-11-10 17:20:17 +01:00
|
|
|
LOG(INFO) << "MessageDb flushed";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-11-09 18:35:22 +01:00
|
|
|
std::shared_ptr<MessageDbSyncSafeInterface> sync_db_safe_;
|
|
|
|
MessageDbSyncInterface *sync_db_ = nullptr;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-11-07 20:19:44 +01:00
|
|
|
//NB: order is important, destructor of pending_writes_ will change finished_writes_
|
|
|
|
vector<Promise<Unit>> finished_writes_;
|
|
|
|
vector<Promise<Unit>> pending_writes_; // TODO use Action
|
2018-12-31 20:04:05 +01:00
|
|
|
double wakeup_at_ = 0;
|
2022-06-26 16:11:27 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class F>
|
|
|
|
void add_write_query(F &&f) {
|
2022-06-26 16:11:27 +02:00
|
|
|
pending_writes_.push_back(PromiseCreator::lambda(std::forward<F>(f)));
|
2018-12-31 20:04:05 +01:00
|
|
|
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();
|
2022-11-23 14:32:09 +01:00
|
|
|
set_promises(pending_writes_);
|
2018-12-31 20:04:05 +01:00
|
|
|
sync_db_->commit_transaction().ensure();
|
2022-11-23 14:32:09 +01:00
|
|
|
set_promises(finished_writes_);
|
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_;
|
|
|
|
};
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
std::shared_ptr<MessageDbAsyncInterface> create_message_db_async(std::shared_ptr<MessageDbSyncSafeInterface> sync_db,
|
|
|
|
int32 scheduler_id) {
|
|
|
|
return std::make_shared<MessageDbAsync>(std::move(sync_db), scheduler_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|