From fbb2dc6ccb60556580fa43c875f2f1718e90442f Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 20 Jan 2018 14:47:53 +0300 Subject: [PATCH] Use int8 for upload/download priority. GitOrigin-RevId: 3cc1c006c5ab3989be920b4d6e7e5f49aeaa42ce --- td/telegram/files/FileFromBytes.h | 2 +- td/telegram/files/FileHashUploader.h | 2 +- td/telegram/files/FileLoadManager.cpp | 8 +++--- td/telegram/files/FileLoadManager.h | 8 +++--- td/telegram/files/FileLoader.cpp | 2 +- td/telegram/files/FileLoader.h | 2 +- td/telegram/files/FileLoaderActor.h | 2 +- td/telegram/files/FileManager.cpp | 38 +++++++++++++++------------ td/telegram/files/FileManager.h | 25 +++++++++--------- td/telegram/files/ResourceManager.cpp | 6 ++--- td/telegram/files/ResourceManager.h | 8 +++--- tdutils/td/utils/base64.cpp | 2 +- 12 files changed, 55 insertions(+), 50 deletions(-) diff --git a/td/telegram/files/FileFromBytes.h b/td/telegram/files/FileFromBytes.h index 7a16a30a..23f4b2ff 100644 --- a/td/telegram/files/FileFromBytes.h +++ b/td/telegram/files/FileFromBytes.h @@ -46,7 +46,7 @@ class FileFromBytes : public FileLoaderActor { void wakeup() override; void set_resource_manager(ActorShared) override { } - void update_priority(int32) override { + void update_priority(int8 priority) override { } void update_resources(const ResourceState &other) override { } diff --git a/td/telegram/files/FileHashUploader.h b/td/telegram/files/FileHashUploader.h index 3e0ccc62..d836c521 100644 --- a/td/telegram/files/FileHashUploader.h +++ b/td/telegram/files/FileHashUploader.h @@ -38,7 +38,7 @@ class FileHashUploader : public FileLoaderActor { send_closure(resource_manager_, &ResourceManager::update_resources, resource_state_); } - void update_priority(int32 priority) override { + void update_priority(int8 priority) override { send_closure(resource_manager_, &ResourceManager::update_priority, priority); } void update_resources(const ResourceState &other) override { diff --git a/td/telegram/files/FileLoadManager.cpp b/td/telegram/files/FileLoadManager.cpp index 6d80c9bc..fd9fddab 100644 --- a/td/telegram/files/FileLoadManager.cpp +++ b/td/telegram/files/FileLoadManager.cpp @@ -30,7 +30,7 @@ void FileLoadManager::start_up() { void FileLoadManager::download(QueryId id, const FullRemoteFileLocation &remote_location, const LocalFileLocation &local, int64 size, string name, - const FileEncryptionKey &encryption_key, int priority) { + const FileEncryptionKey &encryption_key, int8 priority) { if (stop_flag_) { return; } @@ -51,7 +51,7 @@ void FileLoadManager::download(QueryId id, const FullRemoteFileLocation &remote_ void FileLoadManager::upload(QueryId id, const LocalFileLocation &local_location, const RemoteFileLocation &remote_location, int64 size, - const FileEncryptionKey &encryption_key, int priority, vector bad_parts) { + const FileEncryptionKey &encryption_key, int8 priority, vector bad_parts) { if (stop_flag_) { return; } @@ -69,7 +69,7 @@ void FileLoadManager::upload(QueryId id, const LocalFileLocation &local_location } void FileLoadManager::upload_by_hash(QueryId id, const FullLocalFileLocation &local_location, int64 size, - int priority) { + int8 priority) { if (stop_flag_) { return; } @@ -85,7 +85,7 @@ void FileLoadManager::upload_by_hash(QueryId id, const FullLocalFileLocation &lo query_id_to_node_id_[id] = node_id; } -void FileLoadManager::update_priority(QueryId id, int priority) { +void FileLoadManager::update_priority(QueryId id, int8 priority) { if (stop_flag_) { return; } diff --git a/td/telegram/files/FileLoadManager.h b/td/telegram/files/FileLoadManager.h index b8eb4714..efa381d3 100644 --- a/td/telegram/files/FileLoadManager.h +++ b/td/telegram/files/FileLoadManager.h @@ -44,11 +44,11 @@ class FileLoadManager final : public Actor { explicit FileLoadManager(ActorShared callback, ActorShared<> parent); void download(QueryId id, const FullRemoteFileLocation &remote_location, const LocalFileLocation &local, int64 size, - string name, const FileEncryptionKey &encryption_key, int priority); + string name, const FileEncryptionKey &encryption_key, int8 priority); void upload(QueryId id, const LocalFileLocation &local_location, const RemoteFileLocation &remote_location, - int64 size, const FileEncryptionKey &encryption_key, int priority, vector bad_parts); - void upload_by_hash(QueryId id, const FullLocalFileLocation &local_location, int64 size, int priority); - void update_priority(QueryId id, int priority); + int64 size, const FileEncryptionKey &encryption_key, int8 priority, vector bad_parts); + void upload_by_hash(QueryId id, const FullLocalFileLocation &local_location, int64 size, int8 priority); + void update_priority(QueryId id, int8 priority); void from_bytes(QueryId id, FileType type, BufferSlice bytes, string name); void cancel(QueryId id); void update_local_file_location(QueryId id, const LocalFileLocation &local); diff --git a/td/telegram/files/FileLoader.cpp b/td/telegram/files/FileLoader.cpp index c0e44985..9ebd6f8e 100644 --- a/td/telegram/files/FileLoader.cpp +++ b/td/telegram/files/FileLoader.cpp @@ -24,7 +24,7 @@ void FileLoader::set_resource_manager(ActorShared resource_mana resource_manager_ = std::move(resource_manager); send_closure(resource_manager_, &ResourceManager::update_resources, resource_state_); } -void FileLoader::update_priority(int32 priority) { +void FileLoader::update_priority(int8 priority) { send_closure(resource_manager_, &ResourceManager::update_priority, priority); } void FileLoader::update_resources(const ResourceState &other) { diff --git a/td/telegram/files/FileLoader.h b/td/telegram/files/FileLoader.h index d46711e7..7a4d35d7 100644 --- a/td/telegram/files/FileLoader.h +++ b/td/telegram/files/FileLoader.h @@ -33,7 +33,7 @@ class FileLoader : public FileLoaderActor { virtual ~Callback() = default; }; void set_resource_manager(ActorShared resource_manager) override; - void update_priority(int32 priority) override; + void update_priority(int8 priority) override; void update_resources(const ResourceState &other) override; void update_local_file_location(const LocalFileLocation &local) override; diff --git a/td/telegram/files/FileLoaderActor.h b/td/telegram/files/FileLoaderActor.h index f785a2c0..9121c8a4 100644 --- a/td/telegram/files/FileLoaderActor.h +++ b/td/telegram/files/FileLoaderActor.h @@ -17,7 +17,7 @@ class ResourceManager; class FileLoaderActor : public NetQueryCallback { public: virtual void set_resource_manager(ActorShared) = 0; - virtual void update_priority(int32 priority) = 0; + virtual void update_priority(int8 priority) = 0; virtual void update_resources(const ResourceState &other) = 0; // TODO: existence of this function is a dirty hack. Refactoring is highly appreciated diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index 70a4d51a..dcfe61c7 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -124,7 +124,7 @@ void FileNode::set_encryption_key(FileEncryptionKey key) { } } -void FileNode::set_download_priority(int32 priority) { +void FileNode::set_download_priority(int8 priority) { if ((download_priority_ == 0) != (priority == 0)) { VLOG(update_file) << "File " << main_file_id_ << " has changed download priority to " << priority; on_info_changed(); @@ -132,7 +132,7 @@ void FileNode::set_download_priority(int32 priority) { download_priority_ = priority; } -void FileNode::set_upload_priority(int32 priority) { +void FileNode::set_upload_priority(int8 priority) { if ((upload_priority_ == 0) != (priority == 0)) { VLOG(update_file) << "File " << main_file_id_ << " has changed upload priority to " << priority; on_info_changed(); @@ -140,7 +140,7 @@ void FileNode::set_upload_priority(int32 priority) { upload_priority_ = priority; } -void FileNode::set_generate_priority(int32 download_priority, int32 upload_priority) { +void FileNode::set_generate_priority(int8 download_priority, int8 upload_priority) { if ((download_priority_ == 0) != (download_priority == 0) || (upload_priority_ == 0) != (upload_priority == 0)) { VLOG(update_file) << "File " << main_file_id_ << " has changed generate priority to " << download_priority << "/" << upload_priority; @@ -265,7 +265,7 @@ int64 FileView::local_total_size() const { return node_->size_; case LocalFileLocation::Type::Partial: return std::max( - static_cast(node_->local_.partial().part_size_ * node_->local_.partial().ready_part_count_), + static_cast(node_->local_.partial().part_size_) * node_->local_.partial().ready_part_count_, node_->local_ready_size_); default: UNREACHABLE(); @@ -719,7 +719,7 @@ static int merge_choose(const LocalFileLocation &x, const LocalFileLocation &y) return 2; } -static int merge_choose(const RemoteFileLocation &x, int x_priority, const RemoteFileLocation &y, int y_priority) { +static int merge_choose(const RemoteFileLocation &x, int8 x_source, const RemoteFileLocation &y, int8 y_source) { int32 x_type = static_cast(x.type_); int32 y_type = static_cast(y.type_); if (x_type != y_type) { @@ -728,7 +728,7 @@ static int merge_choose(const RemoteFileLocation &x, int x_priority, const Remot // If access_hash changed use a newer one if (x.type_ == RemoteFileLocation::Type::Full) { if (x.full().get_access_hash() != y.full().get_access_hash()) { - return x_priority < y_priority; + return x_source < y_source; } } return 2; @@ -867,8 +867,8 @@ Result FileManager::merge(FileId x_file_id, FileId y_file_id, bool no_sy FileNodeId node_ids[] = {x_node_id, y_node_id}; int local_i = merge_choose(x_node->local_, y_node->local_); - int remote_i = merge_choose(x_node->remote_, static_cast(x_node->remote_source_), y_node->remote_, - static_cast(y_node->remote_source_)); + int remote_i = merge_choose(x_node->remote_, static_cast(x_node->remote_source_), y_node->remote_, + static_cast(y_node->remote_source_)); int generate_i = merge_choose(x_node->generate_, y_node->generate_); int size_i = merge_choose_size(x_node->size_, y_node->size_); int expected_size_i = merge_choose_expected_size(x_node->expected_size_, y_node->expected_size_); @@ -1348,7 +1348,7 @@ void FileManager::download(FileId file_id, std::shared_ptr cal auto *file_info = get_file_id_info(file_id); CHECK(new_priority == 0 || callback); - file_info->download_priority_ = new_priority; + file_info->download_priority_ = narrow_cast(new_priority); file_info->download_callback_ = std::move(callback); // TODO: send current progress? @@ -1369,7 +1369,7 @@ void FileManager::run_download(FileNode *node) { if (!file_view.can_download_from_server()) { return; } - int32 priority = 0; + int8 priority = 0; for (auto id : node->file_ids_) { auto *info = get_file_id_info(id); if (info->download_priority_ > priority) { @@ -1443,7 +1443,7 @@ void FileManager::resume_upload(FileId file_id, std::vector bad_parts, std: auto *file_info = get_file_id_info(file_id); CHECK(new_priority == 0 || callback); file_info->upload_order_ = upload_order; - file_info->upload_priority_ = new_priority; + file_info->upload_priority_ = narrow_cast(new_priority); file_info->upload_callback_ = std::move(callback); // TODO: send current progress? @@ -1505,8 +1505,8 @@ void FileManager::run_generate(FileNode *node) { return; } - int32 download_priority = 0; - int32 upload_priority = 0; + int8 download_priority = 0; + int8 upload_priority = 0; FileId file_id; for (auto id : node->file_ids_) { auto *info = get_file_id_info(id); @@ -1581,7 +1581,7 @@ void FileManager::run_upload(FileNode *node, std::vector bad_parts) { return; } } - int32 priority = 0; + int8 priority = 0; FileId file_id; for (auto id : node->file_ids_) { auto *info = get_file_id_info(id); @@ -1614,7 +1614,7 @@ void FileManager::run_upload(FileNode *node, std::vector bad_parts) { if (old_priority != 0) { LOG(INFO) << "File " << file_id << " is already uploading"; CHECK(node->upload_id_ != 0); - send_closure(file_load_manager_, &FileLoadManager::update_priority, node->upload_id_, -priority); + send_closure(file_load_manager_, &FileLoadManager::update_priority, node->upload_id_, narrow_cast(-priority)); return; } @@ -1623,14 +1623,16 @@ void FileManager::run_upload(FileNode *node, std::vector bad_parts) { QueryId id = queries_container_.create(Query{file_id, Query::UploadByHash}); node->upload_id_ = id; - send_closure(file_load_manager_, &FileLoadManager::upload_by_hash, id, node->local_.full(), node->size_, -priority); + send_closure(file_load_manager_, &FileLoadManager::upload_by_hash, id, node->local_.full(), node->size_, + narrow_cast(-priority)); return; } QueryId id = queries_container_.create(Query{file_id, Query::Upload}); node->upload_id_ = id; send_closure(file_load_manager_, &FileLoadManager::upload, id, node->local_, node->remote_, node->size_, - node->encryption_key_, bad_parts.empty() ? -priority : priority, std::move(bad_parts)); + node->encryption_key_, narrow_cast(bad_parts.empty() ? -priority : priority), + std::move(bad_parts)); LOG(INFO) << "File " << file_id << " upload request has sent to FileLoadManager"; } @@ -2243,12 +2245,14 @@ std::pair FileManager::finish_query(QueryId query_id) } return std::make_pair(res, was_active); } + void FileManager::hangup() { file_db_.reset(); file_generate_manager_.reset(); file_load_manager_.reset(); stop(); } + void FileManager::tear_down() { parent_.reset(); } diff --git a/td/telegram/files/FileManager.h b/td/telegram/files/FileManager.h index aa048b4c..21e1cc6d 100644 --- a/td/telegram/files/FileManager.h +++ b/td/telegram/files/FileManager.h @@ -59,9 +59,9 @@ class FileNode { void set_owner_dialog_id(DialogId owner_id); void set_encryption_key(FileEncryptionKey key); - void set_download_priority(int32 priority); - void set_upload_priority(int32 priority); - void set_generate_priority(int32 download_priority, int32 upload_priority); + void set_download_priority(int8 priority); + void set_upload_priority(int8 priority); + void set_generate_priority(int8 download_priority, int8 upload_priority); void on_changed(); void on_info_changed(); @@ -102,12 +102,12 @@ class FileNode { FileId main_file_id_; FileId upload_pause_; - int32 upload_priority_ = 0; - int32 download_priority_ = 0; - int32 generate_priority_ = 0; + int8 upload_priority_ = 0; + int8 download_priority_ = 0; + int8 generate_priority_ = 0; - int32 generate_download_priority_ = 0; - int32 generate_upload_priority_ = 0; + int8 generate_download_priority_ = 0; + int8 generate_upload_priority_ = 0; int8 main_file_id_priority_ = 0; @@ -299,7 +299,7 @@ class FileManager : public FileLoadManager::Callback { FileId register_url(string url, FileType file_type, DialogId owner_dialog_id); - static constexpr int32 FROM_BYTES_PRIORITY = 10; + static constexpr int8 FROM_BYTES_PRIORITY = 10; using FileNodeId = int32; using QueryId = FileLoadManager::QueryId; class Query { @@ -312,11 +312,12 @@ class FileManager : public FileLoadManager::Callback { bool send_updates_flag_{false}; bool pin_flag_{false}; - int32 download_priority_{0}; - std::shared_ptr download_callback_; + int8 download_priority_{0}; + int8 upload_priority_{0}; - int32 upload_priority_{0}; uint64 upload_order_; + + std::shared_ptr download_callback_; std::shared_ptr upload_callback_; }; diff --git a/td/telegram/files/ResourceManager.cpp b/td/telegram/files/ResourceManager.cpp index cd2c09f6..63afa26a 100644 --- a/td/telegram/files/ResourceManager.cpp +++ b/td/telegram/files/ResourceManager.cpp @@ -15,7 +15,7 @@ namespace td { -void ResourceManager::register_worker(ActorShared callback, int32 priority) { +void ResourceManager::register_worker(ActorShared callback, int8 priority) { auto node_id = nodes_container_.create(); auto *node_ptr = nodes_container_.get(node_id); *node_ptr = std::make_unique(); @@ -28,7 +28,7 @@ void ResourceManager::register_worker(ActorShared callback, int send_closure(node->callback_, &FileLoaderActor::set_resource_manager, actor_shared(this, node_id)); } -void ResourceManager::update_priority(int32 priority) { +void ResourceManager::update_priority(int8 priority) { if (stop_flag_) { return; } @@ -158,7 +158,7 @@ void ResourceManager::loop() { } } } -void ResourceManager::add_node(NodeId node_id, int32 priority) { +void ResourceManager::add_node(NodeId node_id, int8 priority) { if (priority >= 0) { auto it = std::find_if(to_xload_.begin(), to_xload_.end(), [&](auto &x) { return x.first <= priority; }); to_xload_.insert(it, std::make_pair(priority, node_id)); diff --git a/td/telegram/files/ResourceManager.h b/td/telegram/files/ResourceManager.h index 9f618cfc..48a32f18 100644 --- a/td/telegram/files/ResourceManager.h +++ b/td/telegram/files/ResourceManager.h @@ -24,10 +24,10 @@ class ResourceManager : public Actor { explicit ResourceManager(Mode mode) : mode_(mode) { } // use through ActorShared - void update_priority(int32 priority); + void update_priority(int8 priority); void update_resources(const ResourceState &resource_state); - void register_worker(ActorShared callback, int32 priority); + void register_worker(ActorShared callback, int8 priority); private: Mode mode_; @@ -47,7 +47,7 @@ class ResourceManager : public Actor { }; Container> nodes_container_; - vector> to_xload_; + vector> to_xload_; KHeap by_estimated_extra_; ResourceState resource_state_; @@ -60,7 +60,7 @@ class ResourceManager : public Actor { void add_to_heap(Node *node); bool satisfy_node(NodeId file_node_id); - void add_node(NodeId node_id, int32 priority); + void add_node(NodeId node_id, int8 priority); bool remove_node(NodeId node_id); }; } // namespace td diff --git a/tdutils/td/utils/base64.cpp b/tdutils/td/utils/base64.cpp index 884fcf9a..5e75c8ee 100644 --- a/tdutils/td/utils/base64.cpp +++ b/tdutils/td/utils/base64.cpp @@ -213,7 +213,7 @@ static bool is_base64_impl(Slice input) { init_base64_table(); table = char_to_value; } - for (auto c:input) { + for (auto c : input) { if (table[static_cast(c)] == 64) { return false; }