Use data from the database for max_dialog_date.

GitOrigin-RevId: a1dc2d8ecf64a68209637bb1ce0d16a8b661770c
This commit is contained in:
levlam 2019-09-15 04:15:46 +03:00
parent 74219aa2d3
commit 003aee4268
4 changed files with 30 additions and 26 deletions

View File

@ -201,8 +201,8 @@ class DialogDbImpl : public DialogDbSyncInterface {
get_last_notification_date(get_notification_group_stmt_, 1));
}
Result<vector<BufferSlice>> get_dialogs(FolderId folder_id, int64 order, DialogId dialog_id, int32 limit) override {
// TODO use folder_id
Result<DialogDbGetDialogsResult> get_dialogs(FolderId folder_id, int64 order, DialogId dialog_id,
int32 limit) override {
SCOPE_EXIT {
get_dialogs_stmt_.reset();
};
@ -212,18 +212,18 @@ class DialogDbImpl : public DialogDbSyncInterface {
get_dialogs_stmt_.bind_int64(3, dialog_id.get()).ensure();
get_dialogs_stmt_.bind_int32(4, limit).ensure();
vector<BufferSlice> dialogs;
DialogDbGetDialogsResult result;
TRY_STATUS(get_dialogs_stmt_.step());
while (get_dialogs_stmt_.has_row()) {
BufferSlice data(get_dialogs_stmt_.view_blob(0));
auto loaded_dialog_id = get_dialogs_stmt_.view_int64(1);
auto loaded_dialog_order = get_dialogs_stmt_.view_int64(2);
LOG(INFO) << "Load chat " << loaded_dialog_id << " with order " << loaded_dialog_order;
dialogs.emplace_back(std::move(data));
result.next_dialog_id = DialogId(get_dialogs_stmt_.view_int64(1));
result.next_order = get_dialogs_stmt_.view_int64(2);
LOG(INFO) << "Load " << result.next_dialog_id << " with order " << result.next_order;
result.dialogs.emplace_back(std::move(data));
TRY_STATUS(get_dialogs_stmt_.step());
}
return std::move(dialogs);
return std::move(result);
}
Result<vector<NotificationGroupKey>> get_notification_groups_by_last_notification_date(
NotificationGroupKey notification_group_key, int32 limit) override {
@ -319,7 +319,7 @@ class DialogDbAsync : public DialogDbAsyncInterface {
send_closure_later(impl_, &Impl::get_dialog, dialog_id, std::move(promise));
}
void get_dialogs(FolderId folder_id, int64 order, DialogId dialog_id, int32 limit,
Promise<vector<BufferSlice>> promise) override {
Promise<DialogDbGetDialogsResult> promise) override {
send_closure_later(impl_, &Impl::get_dialogs, folder_id, order, dialog_id, limit, std::move(promise));
}
void close(Promise<> promise) override {
@ -357,7 +357,7 @@ class DialogDbAsync : public DialogDbAsyncInterface {
promise.set_result(sync_db_->get_dialog(dialog_id));
}
void get_dialogs(FolderId folder_id, int64 order, DialogId dialog_id, int32 limit,
Promise<vector<BufferSlice>> promise) {
Promise<DialogDbGetDialogsResult> promise) {
add_read_query();
promise.set_result(sync_db_->get_dialogs(folder_id, order, dialog_id, limit));
}

View File

@ -25,6 +25,12 @@ namespace td {
class SqliteConnectionSafe;
class SqliteDb;
struct DialogDbGetDialogsResult {
vector<BufferSlice> dialogs;
int64 next_order = 0;
DialogId next_dialog_id;
};
class DialogDbSyncInterface {
public:
DialogDbSyncInterface() = default;
@ -37,7 +43,8 @@ class DialogDbSyncInterface {
virtual Result<BufferSlice> get_dialog(DialogId dialog_id) = 0;
virtual Result<vector<BufferSlice>> get_dialogs(FolderId folder_id, int64 order, DialogId dialog_id, int32 limit) = 0;
virtual Result<DialogDbGetDialogsResult> get_dialogs(FolderId folder_id, int64 order, DialogId dialog_id,
int32 limit) = 0;
virtual Result<vector<NotificationGroupKey>> get_notification_groups_by_last_notification_date(
NotificationGroupKey notification_group_key, int32 limit) = 0;
@ -71,7 +78,7 @@ class DialogDbAsyncInterface {
virtual void get_dialog(DialogId dialog_id, Promise<BufferSlice> promise) = 0;
virtual void get_dialogs(FolderId folder_id, int64 order, DialogId dialog_id, int32 limit,
Promise<vector<BufferSlice>> promise) = 0;
Promise<DialogDbGetDialogsResult> promise) = 0;
virtual void get_notification_groups_by_last_notification_date(NotificationGroupKey notification_group_key,
int32 limit,

View File

@ -12114,27 +12114,26 @@ void MessagesManager::load_dialog_list_from_database(FolderId folder_id, int32 l
folder_id, list.last_loaded_database_dialog_date_.get_order(),
list.last_loaded_database_dialog_date_.get_dialog_id(), limit,
PromiseCreator::lambda([actor_id = actor_id(this), folder_id, limit,
promise = std::move(promise)](vector<BufferSlice> result) mutable {
promise = std::move(promise)](DialogDbGetDialogsResult result) mutable {
send_closure(actor_id, &MessagesManager::on_get_dialogs_from_database, folder_id, limit, std::move(result),
std::move(promise));
}));
}
void MessagesManager::on_get_dialogs_from_database(FolderId folder_id, int32 limit, vector<BufferSlice> &&dialogs,
void MessagesManager::on_get_dialogs_from_database(FolderId folder_id, int32 limit, DialogDbGetDialogsResult &&dialogs,
Promise<Unit> &&promise) {
auto &list = get_dialog_list(folder_id);
LOG(INFO) << "Receive " << dialogs.size() << " from expected " << limit << " chats in " << folder_id
<< " in from database";
LOG(INFO) << "Receive " << dialogs.dialogs.size() << " from expected " << limit << " chats in " << folder_id
<< " in from database with next order " << dialogs.next_order << " and next " << dialogs.next_dialog_id;
int32 new_get_dialogs_limit = 0;
int32 have_more_dialogs_in_database = (limit == static_cast<int32>(dialogs.size()));
int32 have_more_dialogs_in_database = (limit == static_cast<int32>(dialogs.dialogs.size()));
if (have_more_dialogs_in_database && limit < list.load_dialog_list_limit_max_) {
new_get_dialogs_limit = list.load_dialog_list_limit_max_ - limit;
}
list.load_dialog_list_limit_max_ = 0;
DialogDate max_dialog_date = MIN_DIALOG_DATE;
size_t dialogs_skipped = 0;
for (auto &dialog : dialogs) {
for (auto &dialog : dialogs.dialogs) {
Dialog *d = on_load_dialog_from_database(DialogId(), std::move(dialog));
if (d == nullptr) {
dialogs_skipped++;
@ -12147,13 +12146,10 @@ void MessagesManager::on_get_dialogs_from_database(FolderId folder_id, int32 lim
continue;
}
DialogDate dialog_date(d->order, d->dialog_id);
if (max_dialog_date < dialog_date) {
max_dialog_date = dialog_date;
}
LOG(INFO) << "Chat " << dialog_date << " is loaded from database";
LOG(INFO) << "Chat " << d->dialog_id << " with order " << d->order << " is loaded from database";
}
DialogDate max_dialog_date(dialogs.next_order, dialogs.next_dialog_id);
if (!have_more_dialogs_in_database) {
list.last_loaded_database_dialog_date_ = MAX_DIALOG_DATE;
LOG(INFO) << "Set last loaded database dialog date to " << list.last_loaded_database_dialog_date_;
@ -12168,7 +12164,7 @@ void MessagesManager::on_get_dialogs_from_database(FolderId folder_id, int32 lim
update_last_dialog_date(folder_id);
} else {
LOG(ERROR) << "Last loaded database dialog date didn't increased, skipped " << dialogs_skipped << " chats out of "
<< dialogs.size();
<< dialogs.dialogs.size();
}
if (!(list.last_loaded_database_dialog_date_ < list.last_database_server_dialog_date_)) {

View File

@ -14,6 +14,7 @@
#include "td/telegram/ChannelId.h"
#include "td/telegram/Dependencies.h"
#include "td/telegram/DialogDate.h"
#include "td/telegram/DialogDb.h"
#include "td/telegram/DialogId.h"
#include "td/telegram/DialogParticipant.h"
#include "td/telegram/files/FileId.h"
@ -1930,7 +1931,7 @@ class MessagesManager : public Actor {
Dialog *on_load_dialog_from_database(DialogId dialog_id, const BufferSlice &value);
void on_get_dialogs_from_database(FolderId folder_id, int32 limit, vector<BufferSlice> &&dialogs,
void on_get_dialogs_from_database(FolderId folder_id, int32 limit, DialogDbGetDialogsResult &&dialogs,
Promise<Unit> &&promise);
void send_get_dialog_query(DialogId dialog_id, Promise<Unit> &&promise, uint64 logevent_id = 0);