Add FileCounters to download manager updates.

This commit is contained in:
levlam 2022-03-06 03:20:18 +03:00
parent 5f8f41b7d6
commit 89c6b72c8b
6 changed files with 60 additions and 30 deletions

View File

@ -3954,16 +3954,17 @@ updateFileGenerationStop generation_id:int64 = Update;
//@downloaded_size Total downloaded size of files in the file download list, in bytes //@downloaded_size Total downloaded size of files in the file download list, in bytes
updateFileDownloads total_size:int53 total_count:int32 downloaded_size:int53 = Update; updateFileDownloads total_size:int53 total_count:int32 downloaded_size:int53 = Update;
//@description A file was added to the file download list. This update is sent only after file download list is loaded for the first time @file_download The added file download //@description A file was added to the file download list. This update is sent only after file download list is loaded for the first time @file_download The added file download @counts New number of being downloaded and recently downloaded files found
updateFileAddedToDownloads file_download:fileDownload = Update; updateFileAddedToDownloads file_download:fileDownload counts:downloadedFileCounts = Update;
//@description A file download was changed. This update is sent only after file download list is loaded for the first time @file_id File identifier //@description A file download was changed. This update is sent only after file download list is loaded for the first time @file_id File identifier
//@complete_date Point in time (Unix timestamp) when the file downloading was completed; 0 if the file downloading isn't completed //@complete_date Point in time (Unix timestamp) when the file downloading was completed; 0 if the file downloading isn't completed
//@is_paused True, if downloading of the file is paused //@is_paused True, if downloading of the file is paused
updateFileDownload file_id:int32 complete_date:int32 is_paused:Bool = Update; //@counts New number of being downloaded and recently downloaded files found
updateFileDownload file_id:int32 complete_date:int32 is_paused:Bool counts:downloadedFileCounts = Update;
//@description A file was removed from the file download list. This update is sent only after file download list is loaded for the first time @file_id File identifier //@description A file was removed from the file download list. This update is sent only after file download list is loaded for the first time @file_id File identifier @counts New number of being downloaded and recently downloaded files found
updateFileRemovedFromDownloads file_id:int32 = Update; updateFileRemovedFromDownloads file_id:int32 counts:downloadedFileCounts = Update;
//@description New call was created or information about a call was updated @call New data about a call //@description New call was created or information about a call was updated @call New data about a call
updateCall call:call = Update; updateCall call:call = Update;

View File

@ -118,7 +118,7 @@ class DownloadManagerImpl final : public DownloadManager {
remove_from_database(file_info); remove_from_database(file_info);
files_.erase(download_id); files_.erase(download_id);
if (is_search_inited_) { if (is_search_inited_) {
callback_->update_file_removed(file_id); callback_->update_file_removed(file_id, file_counters_);
} }
update_counters(); update_counters();
@ -299,6 +299,7 @@ class DownloadManagerImpl final : public DownloadManager {
return; return;
} }
bool need_update = false;
with_file_info(file_info, [&](FileInfo &file_info) { with_file_info(file_info, [&](FileInfo &file_info) {
file_info.size = size; file_info.size = size;
file_info.expected_size = expected_size; file_info.expected_size = expected_size;
@ -306,12 +307,12 @@ class DownloadManagerImpl final : public DownloadManager {
if (is_paused && file_info.is_paused != is_paused) { if (is_paused && file_info.is_paused != is_paused) {
file_info.is_paused = is_paused; file_info.is_paused = is_paused;
file_info.need_save_to_database = true; file_info.need_save_to_database = true;
need_update = true;
if (is_search_inited_) {
callback_->update_file_changed(file_info.file_id, file_info.completed_at, file_info.is_paused);
}
} }
}); });
if (is_search_inited_ && need_update) {
callback_->update_file_changed(file_info.file_id, file_info.completed_at, file_info.is_paused, file_counters_);
}
} }
void update_file_deleted(FileId internal_file_id) final { void update_file_deleted(FileId internal_file_id) final {
@ -369,6 +370,7 @@ class DownloadManagerImpl final : public DownloadManager {
Counters counters_; Counters counters_;
Counters sent_counters_; Counters sent_counters_;
FileCounters file_counters_;
bool is_inited_{false}; bool is_inited_{false};
bool is_database_loaded_{false}; bool is_database_loaded_{false};
bool is_search_inited_{false}; bool is_search_inited_{false};
@ -576,7 +578,7 @@ class DownloadManagerImpl final : public DownloadManager {
} }
if (is_search_inited_) { if (is_search_inited_) {
callback_->update_file_added(it->second->file_id, it->second->file_source_id, it->second->created_at, callback_->update_file_added(it->second->file_id, it->second->file_source_id, it->second->created_at,
it->second->completed_at, it->second->is_paused); it->second->completed_at, it->second->is_paused, file_counters_);
} }
} }
@ -623,7 +625,7 @@ class DownloadManagerImpl final : public DownloadManager {
callback_->start_file(file_info.internal_file_id, file_info.priority, actor_shared(this, file_info.link_token)); callback_->start_file(file_info.internal_file_id, file_info.priority, actor_shared(this, file_info.link_token));
} }
if (is_search_inited_) { if (is_search_inited_) {
callback_->update_file_changed(file_info.file_id, file_info.completed_at, file_info.is_paused); callback_->update_file_changed(file_info.file_id, file_info.completed_at, file_info.is_paused, file_counters_);
} }
} }
@ -689,11 +691,23 @@ class DownloadManagerImpl final : public DownloadManager {
counters_.total_size -= get_file_size(file_info); counters_.total_size -= get_file_size(file_info);
counters_.total_count--; counters_.total_count--;
} }
if (is_completed(file_info)) {
file_counters_.completed_count--;
CHECK(file_counters_.completed_count >= 0);
} else {
if (file_info.is_paused) {
file_counters_.paused_count--;
CHECK(file_counters_.paused_count >= 0);
}
file_counters_.active_count--;
CHECK(file_counters_.active_count >= file_counters_.paused_count);
}
} }
void register_file_info(FileInfo &file_info) { void register_file_info(FileInfo &file_info) {
CHECK(!file_info.is_registered); CHECK(!file_info.is_registered);
file_info.is_registered = true; file_info.is_registered = true;
bool need_update = false;
if (!is_completed(file_info) && file_info.size != 0 && file_info.downloaded_size == file_info.size) { if (!is_completed(file_info) && file_info.size != 0 && file_info.downloaded_size == file_info.size) {
LOG(INFO) << "Register file " << file_info.file_id; LOG(INFO) << "Register file " << file_info.file_id;
file_info.is_paused = false; file_info.is_paused = false;
@ -706,15 +720,24 @@ class DownloadManagerImpl final : public DownloadManager {
unviewed_completed_download_ids_.insert(file_info.download_id); unviewed_completed_download_ids_.insert(file_info.download_id);
} }
if (is_search_inited_) { need_update = true;
callback_->update_file_changed(file_info.file_id, file_info.completed_at, file_info.is_paused);
}
} }
if (file_info.is_counted && (is_completed(file_info) || !file_info.is_paused)) { if (file_info.is_counted && (is_completed(file_info) || !file_info.is_paused)) {
counters_.downloaded_size += file_info.downloaded_size; counters_.downloaded_size += file_info.downloaded_size;
counters_.total_size += get_file_size(file_info); counters_.total_size += get_file_size(file_info);
counters_.total_count++; counters_.total_count++;
} }
if (is_completed(file_info)) {
file_counters_.completed_count++;
} else {
if (file_info.is_paused) {
file_counters_.paused_count++;
}
file_counters_.active_count++;
}
if (is_search_inited_ && need_update) {
callback_->update_file_changed(file_info.file_id, file_info.completed_at, file_info.is_paused, file_counters_);
}
sync_with_database(file_info); sync_with_database(file_info);
update_counters(); update_counters();
CHECK(file_info.is_registered); CHECK(file_info.is_registered);

View File

@ -65,9 +65,9 @@ class DownloadManager : public Actor {
virtual ~Callback() = default; virtual ~Callback() = default;
virtual void update_counters(Counters counters) = 0; virtual void update_counters(Counters counters) = 0;
virtual void update_file_added(FileId file_id, FileSourceId file_source_id, int32 add_date, int32 complete_date, virtual void update_file_added(FileId file_id, FileSourceId file_source_id, int32 add_date, int32 complete_date,
bool is_paused) = 0; bool is_paused, FileCounters counters) = 0;
virtual void update_file_changed(FileId file_id, int32 complete_date, bool is_paused) = 0; virtual void update_file_changed(FileId file_id, int32 complete_date, bool is_paused, FileCounters counters) = 0;
virtual void update_file_removed(FileId file_id) = 0; virtual void update_file_removed(FileId file_id, FileCounters counters) = 0;
virtual void start_file(FileId file_id, int8 priority, ActorShared<DownloadManager> download_manager) = 0; virtual void start_file(FileId file_id, int8 priority, ActorShared<DownloadManager> download_manager) = 0;
virtual void pause_file(FileId file_id) = 0; virtual void pause_file(FileId file_id) = 0;
virtual void delete_file(FileId file_id) = 0; virtual void delete_file(FileId file_id) = 0;

View File

@ -23,20 +23,25 @@ void DownloadManagerCallback::update_counters(DownloadManager::Counters counters
} }
void DownloadManagerCallback::update_file_added(FileId file_id, FileSourceId file_source_id, int32 add_date, void DownloadManagerCallback::update_file_added(FileId file_id, FileSourceId file_source_id, int32 add_date,
int32 complete_date, bool is_paused) { int32 complete_date, bool is_paused,
DownloadManager::FileCounters counters) {
send_closure(td_->actor_id(td_), &Td::send_update, send_closure(td_->actor_id(td_), &Td::send_update,
td_api::make_object<td_api::updateFileAddedToDownloads>( td_api::make_object<td_api::updateFileAddedToDownloads>(
get_file_download_object(file_id, file_source_id, add_date, complete_date, is_paused))); get_file_download_object(file_id, file_source_id, add_date, complete_date, is_paused),
counters.get_downloaded_file_counts_object()));
} }
void DownloadManagerCallback::update_file_changed(FileId file_id, int32 complete_date, bool is_paused) { void DownloadManagerCallback::update_file_changed(FileId file_id, int32 complete_date, bool is_paused,
DownloadManager::FileCounters counters) {
send_closure(td_->actor_id(td_), &Td::send_update, send_closure(td_->actor_id(td_), &Td::send_update,
td_api::make_object<td_api::updateFileDownload>(file_id.get(), complete_date, is_paused)); td_api::make_object<td_api::updateFileDownload>(file_id.get(), complete_date, is_paused,
counters.get_downloaded_file_counts_object()));
} }
void DownloadManagerCallback::update_file_removed(FileId file_id) { void DownloadManagerCallback::update_file_removed(FileId file_id, DownloadManager::FileCounters counters) {
send_closure(td_->actor_id(td_), &Td::send_update, send_closure(td_->actor_id(td_), &Td::send_update,
td_api::make_object<td_api::updateFileRemovedFromDownloads>(file_id.get())); td_api::make_object<td_api::updateFileRemovedFromDownloads>(
file_id.get(), counters.get_downloaded_file_counts_object()));
} }
void DownloadManagerCallback::start_file(FileId file_id, int8 priority, ActorShared<DownloadManager> download_manager) { void DownloadManagerCallback::start_file(FileId file_id, int8 priority, ActorShared<DownloadManager> download_manager) {

View File

@ -27,11 +27,12 @@ class DownloadManagerCallback final : public DownloadManager::Callback {
void update_counters(DownloadManager::Counters counters) final; void update_counters(DownloadManager::Counters counters) final;
void update_file_added(FileId file_id, FileSourceId file_source_id, int32 add_date, int32 complete_date, void update_file_added(FileId file_id, FileSourceId file_source_id, int32 add_date, int32 complete_date,
bool is_paused) final; bool is_paused, DownloadManager::FileCounters counters) final;
void update_file_changed(FileId file_id, int32 complete_date, bool is_paused) final; void update_file_changed(FileId file_id, int32 complete_date, bool is_paused,
DownloadManager::FileCounters counters) final;
void update_file_removed(FileId file_id) final; void update_file_removed(FileId file_id, DownloadManager::FileCounters counters) final;
void start_file(FileId file_id, int8 priority, ActorShared<DownloadManager> download_manager) final; void start_file(FileId file_id, int8 priority, ActorShared<DownloadManager> download_manager) final;

View File

@ -4064,9 +4064,9 @@ void Td::send_update(tl_object_ptr<td_api::Update> &&object) {
case td_api::updateChatAction::ID / 2: case td_api::updateChatAction::ID / 2:
case td_api::updateChatFilters::ID / 2: case td_api::updateChatFilters::ID / 2:
case td_api::updateChatPosition::ID / 2: case td_api::updateChatPosition::ID / 2:
case td_api::updateFileAddedToDownloads::ID: case td_api::updateFileAddedToDownloads::ID / 2:
case td_api::updateFileDownload::ID: case td_api::updateFileDownload::ID / 2:
case td_api::updateFileRemovedFromDownloads::ID: case td_api::updateFileRemovedFromDownloads::ID / 2:
LOG(ERROR) << "Sending update: " << oneline(to_string(object)); LOG(ERROR) << "Sending update: " << oneline(to_string(object));
break; break;
default: default: