Make FileStats fields private.

This commit is contained in:
levlam 2021-09-26 12:44:18 +03:00
parent 724397dfbd
commit d882e222a7
7 changed files with 74 additions and 56 deletions

View File

@ -166,7 +166,7 @@ void StorageManager::on_all_files(FileGcParameters gc_parameters, Result<FileSta
create_gc_worker();
send_closure(gc_worker_, &FileGcWorker::run_gc, std::move(gc_parameters), std::move(r_file_stats.ok_ref().all_files),
send_closure(gc_worker_, &FileGcWorker::run_gc, std::move(gc_parameters), r_file_stats.ok_ref().get_all_files(),
PromiseCreator::lambda([actor_id = actor_id(this), dialog_limit](Result<FileGcResult> r_file_gc_result) {
send_closure(actor_id, &StorageManager::on_gc_finished, dialog_limit, std::move(r_file_gc_result));
}));

View File

@ -40,6 +40,7 @@
#include "td/telegram/files/FileId.h"
#include "td/telegram/files/FileManager.h"
#include "td/telegram/files/FileSourceId.h"
#include "td/telegram/files/FileStats.h"
#include "td/telegram/files/FileType.h"
#include "td/telegram/FolderId.h"
#include "td/telegram/FullMessageId.h"

View File

@ -82,9 +82,8 @@ void FileGcWorker::run_gc(const FileGcParameters &parameters, std::vector<FullFi
total_size += info.size;
}
FileStats new_stats;
FileStats removed_stats;
removed_stats.split_by_owner_dialog_id = new_stats.split_by_owner_dialog_id = parameters.dialog_limit != 0;
FileStats new_stats(false, parameters.dialog_limit != 0);
FileStats removed_stats(false, parameters.dialog_limit != 0);
auto do_remove_file = [&removed_stats](const FullFileInfo &info) {
removed_stats.add_copy(info);

View File

@ -17,7 +17,6 @@
#include "td/telegram/files/FileLoadManager.h"
#include "td/telegram/files/FileLocation.h"
#include "td/telegram/files/FileSourceId.h"
#include "td/telegram/files/FileStats.h"
#include "td/telegram/files/FileType.h"
#include "td/telegram/Location.h"
#include "td/telegram/PhotoSizeSource.h"

View File

@ -27,34 +27,34 @@ tl_object_ptr<td_api::storageStatisticsFast> FileStatsFast::get_storage_statisti
void FileStats::add(StatByType &by_type, FileType file_type, int64 size) {
auto pos = static_cast<size_t>(file_type);
CHECK(pos < stat_by_type.size());
CHECK(pos < stat_by_type_.size());
by_type[pos].size += size;
by_type[pos].cnt++;
}
void FileStats::add_impl(const FullFileInfo &info) {
if (split_by_owner_dialog_id) {
add(stat_by_owner_dialog_id[info.owner_dialog_id], info.file_type, info.size);
if (split_by_owner_dialog_id_) {
add(stat_by_owner_dialog_id_[info.owner_dialog_id], info.file_type, info.size);
} else {
add(stat_by_type, info.file_type, info.size);
add(stat_by_type_, info.file_type, info.size);
}
}
void FileStats::add_copy(const FullFileInfo &info) {
add_impl(info);
if (need_all_files) {
all_files.push_back(info);
if (need_all_files_) {
all_files_.push_back(info);
}
}
void FileStats::add(FullFileInfo &&info) {
add_impl(info);
if (need_all_files) {
all_files.push_back(std::move(info));
if (need_all_files_) {
all_files_.push_back(std::move(info));
}
}
FileTypeStat get_nontemp_stat(const FileStats::StatByType &by_type) {
FileTypeStat FileStats::get_nontemp_stat(const FileStats::StatByType &by_type) {
FileTypeStat stat;
for (int32 i = 0; i < MAX_FILE_TYPE; i++) {
if (FileType(i) != FileType::Temp) {
@ -66,11 +66,11 @@ FileTypeStat get_nontemp_stat(const FileStats::StatByType &by_type) {
}
FileTypeStat FileStats::get_total_nontemp_stat() const {
if (!split_by_owner_dialog_id) {
return get_nontemp_stat(stat_by_type);
if (!split_by_owner_dialog_id_) {
return get_nontemp_stat(stat_by_type_);
}
FileTypeStat stat;
for (auto &dialog : stat_by_owner_dialog_id) {
for (auto &dialog : stat_by_owner_dialog_id_) {
auto tmp = get_nontemp_stat(dialog.second);
stat.size += tmp.size;
stat.cnt += tmp.cnt;
@ -82,12 +82,12 @@ void FileStats::apply_dialog_limit(int32 limit) {
if (limit == -1) {
return;
}
if (!split_by_owner_dialog_id) {
if (!split_by_owner_dialog_id_) {
return;
}
std::vector<std::pair<int64, DialogId>> dialogs;
for (auto &dialog : stat_by_owner_dialog_id) {
for (auto &dialog : stat_by_owner_dialog_id_) {
if (!dialog.first.is_valid()) {
continue;
}
@ -113,7 +113,7 @@ void FileStats::apply_dialog_limit(int32 limit) {
StatByType other_stats;
bool other_flag = false;
for (auto it = stat_by_owner_dialog_id.begin(); it != stat_by_owner_dialog_id.end();) {
for (auto it = stat_by_owner_dialog_id_.begin(); it != stat_by_owner_dialog_id_.end();) {
if (all_dialogs.count(it->first)) {
++it;
} else {
@ -122,24 +122,24 @@ void FileStats::apply_dialog_limit(int32 limit) {
other_stats[i].cnt += it->second[i].cnt;
}
other_flag = true;
it = stat_by_owner_dialog_id.erase(it);
it = stat_by_owner_dialog_id_.erase(it);
}
}
if (other_flag) {
DialogId other_dialog_id;
stat_by_owner_dialog_id[other_dialog_id] = other_stats;
stat_by_owner_dialog_id_[other_dialog_id] = other_stats;
}
}
static tl_object_ptr<td_api::storageStatisticsByChat> get_storage_statistics_by_chat_object(
DialogId dialog_id, const FileStats::StatByType &stat_by_type) {
td_api::object_ptr<td_api::storageStatisticsByChat> FileStats::get_storage_statistics_by_chat_object(
DialogId dialog_id, const FileStats::StatByType &stat_by_type_) {
auto stats = make_tl_object<td_api::storageStatisticsByChat>(dialog_id.get(), 0, 0, Auto());
FileStats::StatByType aggregated_stats;
for (int32 i = 0; i < MAX_FILE_TYPE; i++) {
size_t file_type = narrow_cast<size_t>(get_main_file_type(static_cast<FileType>(i)));
aggregated_stats[file_type].size += stat_by_type[i].size;
aggregated_stats[file_type].cnt += stat_by_type[i].cnt;
aggregated_stats[file_type].size += stat_by_type_[i].size;
aggregated_stats[file_type].cnt += stat_by_type_[i].cnt;
}
for (int32 i = 0; i < MAX_FILE_TYPE; i++) {
@ -161,12 +161,12 @@ static tl_object_ptr<td_api::storageStatisticsByChat> get_storage_statistics_by_
tl_object_ptr<td_api::storageStatistics> FileStats::get_storage_statistics_object() const {
auto stats = make_tl_object<td_api::storageStatistics>(0, 0, Auto());
if (!split_by_owner_dialog_id) {
if (!split_by_owner_dialog_id_) {
stats->by_chat_.reserve(1);
stats->by_chat_.push_back(get_storage_statistics_by_chat_object(DialogId(), stat_by_type));
stats->by_chat_.push_back(get_storage_statistics_by_chat_object(DialogId(), stat_by_type_));
} else {
stats->by_chat_.reserve(stat_by_owner_dialog_id.size());
for (auto &by_dialog : stat_by_owner_dialog_id) {
stats->by_chat_.reserve(stat_by_owner_dialog_id_.size());
for (auto &by_dialog : stat_by_owner_dialog_id_) {
stats->by_chat_.push_back(get_storage_statistics_by_chat_object(by_dialog.first, by_dialog.second));
}
std::sort(stats->by_chat_.begin(), stats->by_chat_.end(), [](const auto &x, const auto &y) {
@ -183,13 +183,13 @@ tl_object_ptr<td_api::storageStatistics> FileStats::get_storage_statistics_objec
return stats;
}
std::vector<DialogId> FileStats::get_dialog_ids() const {
std::vector<DialogId> res;
if (!split_by_owner_dialog_id) {
vector<DialogId> FileStats::get_dialog_ids() const {
vector<DialogId> res;
if (!split_by_owner_dialog_id_) {
return res;
}
res.reserve(stat_by_owner_dialog_id.size());
for (auto &by_dialog : stat_by_owner_dialog_id) {
res.reserve(stat_by_owner_dialog_id_.size());
for (auto &by_dialog : stat_by_owner_dialog_id_) {
if (by_dialog.first.is_valid()) {
res.push_back(by_dialog.first);
}
@ -197,27 +197,31 @@ std::vector<DialogId> FileStats::get_dialog_ids() const {
return res;
}
StringBuilder &operator<<(StringBuilder &sb, const FileTypeStat &stat) {
vector<FullFileInfo> FileStats::get_all_files() {
return std::move(all_files_);
}
static StringBuilder &operator<<(StringBuilder &sb, const FileTypeStat &stat) {
return sb << tag("size", format::as_size(stat.size)) << tag("count", stat.cnt);
}
StringBuilder &operator<<(StringBuilder &sb, const FileStats &file_stats) {
if (!file_stats.split_by_owner_dialog_id) {
if (!file_stats.split_by_owner_dialog_id_) {
FileTypeStat total_stat;
for (auto &type_stat : file_stats.stat_by_type) {
for (auto &type_stat : file_stats.stat_by_type_) {
total_stat.size += type_stat.size;
total_stat.cnt += type_stat.cnt;
}
sb << "[FileStat " << tag("total", total_stat);
for (int32 i = 0; i < MAX_FILE_TYPE; i++) {
sb << tag(get_file_type_name(FileType(i)), file_stats.stat_by_type[i]);
sb << tag(get_file_type_name(FileType(i)), file_stats.stat_by_type_[i]);
}
sb << "]";
} else {
{
FileTypeStat total_stat;
for (auto &by_type : file_stats.stat_by_owner_dialog_id) {
for (auto &by_type : file_stats.stat_by_owner_dialog_id_) {
for (auto &type_stat : by_type.second) {
total_stat.size += type_stat.size;
total_stat.cnt += type_stat.cnt;
@ -225,7 +229,7 @@ StringBuilder &operator<<(StringBuilder &sb, const FileStats &file_stats) {
}
sb << "[FileStat " << tag("total", total_stat);
}
for (auto &by_type : file_stats.stat_by_owner_dialog_id) {
for (auto &by_type : file_stats.stat_by_owner_dialog_id_) {
FileTypeStat dialog_stat;
for (auto &type_stat : by_type.second) {
dialog_stat.size += type_stat.size;

View File

@ -66,28 +66,46 @@ struct FileStatsFast {
tl_object_ptr<td_api::storageStatisticsFast> get_storage_statistics_fast_object() const;
};
struct FileStats {
bool need_all_files{false};
bool split_by_owner_dialog_id{false};
class FileStats {
bool need_all_files_{false};
bool split_by_owner_dialog_id_{false};
using StatByType = std::array<FileTypeStat, MAX_FILE_TYPE>;
StatByType stat_by_type;
std::unordered_map<DialogId, StatByType, DialogIdHash> stat_by_owner_dialog_id;
StatByType stat_by_type_;
std::unordered_map<DialogId, StatByType, DialogIdHash> stat_by_owner_dialog_id_;
vector<FullFileInfo> all_files_;
std::vector<FullFileInfo> all_files;
void add_impl(const FullFileInfo &info);
void add(StatByType &by_type, FileType file_type, int64 size);
static FileTypeStat get_nontemp_stat(const StatByType &by_type);
static td_api::object_ptr<td_api::storageStatisticsByChat> get_storage_statistics_by_chat_object(
DialogId dialog_id, const StatByType &stat_by_type_);
friend StringBuilder &operator<<(StringBuilder &sb, const FileStats &file_stats);
public:
//FileStats() = default;
FileStats(bool need_all_files, bool split_by_owner_dialog_id)
: need_all_files_(need_all_files), split_by_owner_dialog_id_(split_by_owner_dialog_id) {
}
void add_copy(const FullFileInfo &info);
void add(FullFileInfo &&info);
void apply_dialog_limit(int32 limit);
tl_object_ptr<td_api::storageStatistics> get_storage_statistics_object() const;
std::vector<DialogId> get_dialog_ids() const;
vector<DialogId> get_dialog_ids() const;
FileTypeStat get_total_nontemp_stat() const;
private:
void add_impl(const FullFileInfo &info);
void add(StatByType &by_type, FileType file_type, int64 size);
vector<FullFileInfo> get_all_files();
};
StringBuilder &operator<<(StringBuilder &sb, const FileStats &file_stats);

View File

@ -147,8 +147,7 @@ void FileStatsWorker::get_stats(bool need_all_files, bool split_by_owner_dialog_
split_by_owner_dialog_id = false;
}
if (!split_by_owner_dialog_id) {
FileStats file_stats;
file_stats.need_all_files = need_all_files;
FileStats file_stats(need_all_files, false);
auto start = Time::now();
scan_fs(token_, [&](FsFileInfo &fs_info) {
FullFileInfo info;
@ -207,9 +206,7 @@ void FileStatsWorker::get_stats(bool need_all_files, bool split_by_owner_dialog_
return promise.set_error(Status::Error(500, "Request aborted"));
}
FileStats file_stats;
file_stats.need_all_files = need_all_files;
file_stats.split_by_owner_dialog_id = split_by_owner_dialog_id;
FileStats file_stats(need_all_files, split_by_owner_dialog_id);
for (auto &full_info : full_infos) {
file_stats.add(std::move(full_info));
if (token_) {