From 1384b48061eea9ce5032f1fd8f1b786973cd182e Mon Sep 17 00:00:00 2001 From: Arsentiy Milchakov Date: Tue, 19 Jan 2021 17:27:47 +0300 Subject: [PATCH] [storage] background downloader queue is unified --- storage/CMakeLists.txt | 7 +- .../downloader_adapter_ios.h | 7 +- .../downloader_adapter_ios.mm | 4 +- .../downloader_queue.hpp | 87 +++++++++++++++++++ .../downloader_queue_ios.cpp | 56 ------------ .../downloader_queue_ios.hpp | 47 ---------- .../storage/storage.xcodeproj/project.pbxproj | 12 +-- 7 files changed, 104 insertions(+), 116 deletions(-) create mode 100644 storage/background_downloading/downloader_queue.hpp delete mode 100644 storage/background_downloading/downloader_queue_ios.cpp delete mode 100644 storage/background_downloading/downloader_queue_ios.hpp diff --git a/storage/CMakeLists.txt b/storage/CMakeLists.txt index ea98581bc8d..9510688d802 100644 --- a/storage/CMakeLists.txt +++ b/storage/CMakeLists.txt @@ -51,7 +51,12 @@ set( storage_helpers.hpp ) -if (${PLATFORM_IPHONE}) +if (${PLATFORM_IPHONE} OR ${PLATFORM_ANDROID}) + append( + SRC + background_downloading/downloader_queue.hpp + ) +elseif (${PLATFORM_IPHONE}) append( SRC background_downloading/downloader_adapter_ios.h diff --git a/storage/background_downloading/downloader_adapter_ios.h b/storage/background_downloading/downloader_adapter_ios.h index 6a8e7a17e4f..b5299c4b5c2 100644 --- a/storage/background_downloading/downloader_adapter_ios.h +++ b/storage/background_downloading/downloader_adapter_ios.h @@ -2,9 +2,12 @@ #import "storage/background_downloading/downloader_subscriber_adapter_ios.h" -#include "storage/background_downloading/downloader_queue_ios.hpp" +#include "storage/background_downloading/downloader_queue.hpp" #include "storage/map_files_downloader_with_ping.hpp" +#include +#include + namespace storage { class BackgroundDownloaderAdapter : public MapFilesDownloaderWithPing @@ -27,7 +30,7 @@ class BackgroundDownloaderAdapter : public MapFilesDownloaderWithPing void DownloadFromAnyUrl(CountryId const & countryId, std::string const & downloadPath, std::vector const & urls); - BackgroundDownloaderQueue m_queue; + BackgroundDownloaderQueue> m_queue; SubscriberAdapter * m_subscriberAdapter; }; } // namespace storage diff --git a/storage/background_downloading/downloader_adapter_ios.mm b/storage/background_downloading/downloader_adapter_ios.mm index 5cc6a76b779..1f85df2042c 100644 --- a/storage/background_downloading/downloader_adapter_ios.mm +++ b/storage/background_downloading/downloader_adapter_ios.mm @@ -42,7 +42,7 @@ @implementation NSError (ToDownloaderError) return; BackgroundDownloader * downloader = [BackgroundDownloader sharedBackgroundMapDownloader]; - auto const taskIdentifier = m_queue.GetTaskIdByCountryId(countryId); + auto const taskIdentifier = m_queue.GetTaskInfoForCountryId(countryId); if (taskIdentifier) [downloader cancelTaskWithIdentifier:*taskIdentifier]; m_queue.Remove(countryId); @@ -124,7 +124,7 @@ @implementation NSError (ToDownloaderError) BackgroundDownloader * downloader = [BackgroundDownloader sharedBackgroundMapDownloader]; NSUInteger taskId = [downloader downloadWithUrl:url completion:onFinish progress:onProgress]; - m_queue.SetTaskIdForCountryId(countryId, taskId); + m_queue.SetTaskInfoForCountryId(countryId, taskId); } std::unique_ptr GetDownloader() diff --git a/storage/background_downloading/downloader_queue.hpp b/storage/background_downloading/downloader_queue.hpp new file mode 100644 index 00000000000..2247c851c44 --- /dev/null +++ b/storage/background_downloading/downloader_queue.hpp @@ -0,0 +1,87 @@ +#pragma once + +#include "storage/downloader_queue_interface.hpp" +#include "storage/queued_country.hpp" +#include "storage/storage_defines.hpp" + +#include +#include +#include +#include +#include + +namespace storage +{ +template +class BackgroundDownloaderQueue : public QueueInterface +{ +public: + bool IsEmpty() const override + { + return m_queue.empty(); + } + + bool Contains(CountryId const & country) const override + { + return m_queue.find(country) != m_queue.cend(); + } + + void ForEachCountry(ForEachCountryFunction const & fn) const override + { + for (auto const & item : m_queue) + { + fn(item.second.m_queuedCountry); + } + } + + void Append(QueuedCountry && country) + { + auto const countryId = country.GetCountryId(); + auto const result = m_queue.emplace(countryId, std::move(country)); + result.first->second.m_queuedCountry.OnCountryInQueue(); + } + + void SetTaskInfoForCountryId(CountryId const & countryId, TaskInfoType && taskInfo) + { + auto const it = m_queue.find(countryId); + CHECK(it != m_queue.cend(), ()); + + it->second.m_taskInfo = std::move(taskInfo); + } + + TaskInfoType const & GetTaskInfoForCountryId(CountryId const & countryId) const + { + auto const it = m_queue.find(countryId); + if (it == m_queue.cend()) + return {}; + + return it->second.m_taskInfo; + } + + QueuedCountry & GetCountryById(CountryId const & countryId) + { + return m_queue.at(countryId).m_queuedCountry; + } + + void Remove(CountryId const & countryId) + { + m_queue.erase(countryId); + } + + void Clear() + { + m_queue.clear(); + } + +private: + struct TaskData + { + explicit TaskData(QueuedCountry && country) : m_queuedCountry(std::move(country)) {} + + QueuedCountry m_queuedCountry; + TaskInfoType m_taskInfo; + }; + + std::unordered_map m_queue; +}; +} // namespace storage diff --git a/storage/background_downloading/downloader_queue_ios.cpp b/storage/background_downloading/downloader_queue_ios.cpp deleted file mode 100644 index 3796bafa09e..00000000000 --- a/storage/background_downloading/downloader_queue_ios.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#import "storage/background_downloading/downloader_queue_ios.hpp" - -#include - -namespace storage -{ -bool BackgroundDownloaderQueue::IsEmpty() const -{ - return m_queue.empty(); -} - -bool BackgroundDownloaderQueue::Contains(CountryId const & country) const -{ - return m_queue.find(country) != m_queue.cend(); -} - -void BackgroundDownloaderQueue::ForEachCountry(ForEachCountryFunction const & fn) const -{ - for (auto const & item : m_queue) - { - fn(item.second.m_queuedCountry); - } -} - -void BackgroundDownloaderQueue::SetTaskIdForCountryId(CountryId const & country, uint64_t taskId) -{ - auto const it = m_queue.find(country); - CHECK(it != m_queue.cend(), ()); - - it->second.m_taskId = taskId; -} - -std::optional BackgroundDownloaderQueue::GetTaskIdByCountryId(CountryId const & country) const -{ - auto const it = m_queue.find(country); - if (it == m_queue.cend()) - return {}; - - return it->second.m_taskId; -} - -QueuedCountry & BackgroundDownloaderQueue::GetCountryById(CountryId const & countryId) -{ - return m_queue.at(countryId).m_queuedCountry; -} - -void BackgroundDownloaderQueue::Remove(CountryId const & countryId) -{ - m_queue.erase(countryId); -} - -void BackgroundDownloaderQueue::Clear() -{ - m_queue.clear(); -} -} // namespace storage diff --git a/storage/background_downloading/downloader_queue_ios.hpp b/storage/background_downloading/downloader_queue_ios.hpp deleted file mode 100644 index 241ec24a154..00000000000 --- a/storage/background_downloading/downloader_queue_ios.hpp +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once - -#include "storage/downloader_queue_interface.hpp" -#include "storage/queued_country.hpp" -#include "storage/storage_defines.hpp" - -#include -#include -#include -#include - -namespace storage -{ -class BackgroundDownloaderQueue : public QueueInterface -{ -public: - bool IsEmpty() const override; - bool Contains(CountryId const & country) const override; - void ForEachCountry(ForEachCountryFunction const & fn) const override; - - void Append(QueuedCountry && country) - { - auto const countryId = country.GetCountryId(); - auto const result = m_queue.emplace(countryId, std::move(country)); - result.first->second.m_queuedCountry.OnCountryInQueue(); - } - - void SetTaskIdForCountryId(CountryId const & countryId, uint64_t taskId); - std::optional GetTaskIdByCountryId(CountryId const & countryId) const; - - QueuedCountry & GetCountryById(CountryId const & countryId); - - void Remove(CountryId const & country); - void Clear(); - -private: - struct TaskData - { - explicit TaskData(QueuedCountry && country) : m_queuedCountry(std::move(country)) {} - - QueuedCountry m_queuedCountry; - std::optional m_taskId; - }; - - std::unordered_map m_queue; -}; -} // namespace storage diff --git a/xcode/storage/storage.xcodeproj/project.pbxproj b/xcode/storage/storage.xcodeproj/project.pbxproj index 41e98097143..b3d1db37f92 100644 --- a/xcode/storage/storage.xcodeproj/project.pbxproj +++ b/xcode/storage/storage.xcodeproj/project.pbxproj @@ -25,6 +25,7 @@ 39B9682223E1AC5F00D3B8E3 /* libge0.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 39B9682123E1AC5F00D3B8E3 /* libge0.a */; }; 39B9682B23E1AD9F00D3B8E3 /* libge0.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 39B9682123E1AC5F00D3B8E3 /* libge0.a */; }; 3D2D57B725ADCEEB0002D8E3 /* downloader_queue_interface.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3D2D57B625ADCEEB0002D8E3 /* downloader_queue_interface.hpp */; }; + 3D2D57BB25B6E8550002D8E3 /* downloader_queue.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3D2D57BA25B6E8550002D8E3 /* downloader_queue.hpp */; }; 3D497EA123292706000FB57D /* map_files_downloader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3D497E9E23292706000FB57D /* map_files_downloader.cpp */; }; 3D497EA223292706000FB57D /* map_files_downloader_with_ping.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3D497E9F23292706000FB57D /* map_files_downloader_with_ping.hpp */; }; 3D497EA323292706000FB57D /* map_files_downloader_with_ping.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3D497EA023292706000FB57D /* map_files_downloader_with_ping.cpp */; }; @@ -41,11 +42,9 @@ 3DFACF40243C985A00A29A94 /* downloader_queue_universal.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3DFACF3C243C985A00A29A94 /* downloader_queue_universal.hpp */; }; 3DFACF41243C985A00A29A94 /* downloader_queue_universal.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3DFACF3D243C985A00A29A94 /* downloader_queue_universal.cpp */; }; 3DFACF4D243CB1AB00A29A94 /* downloader_subscriber_adapter_ios.h in Headers */ = {isa = PBXBuildFile; fileRef = 3DFACF45243CB1AB00A29A94 /* downloader_subscriber_adapter_ios.h */; }; - 3DFACF4E243CB1AB00A29A94 /* downloader_queue_ios.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3DFACF46243CB1AB00A29A94 /* downloader_queue_ios.hpp */; }; 3DFACF4F243CB1AB00A29A94 /* downloader_adapter_ios.h in Headers */ = {isa = PBXBuildFile; fileRef = 3DFACF47243CB1AB00A29A94 /* downloader_adapter_ios.h */; }; 3DFACF51243CB1AB00A29A94 /* downloader_adapter_ios.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3DFACF49243CB1AB00A29A94 /* downloader_adapter_ios.mm */; }; 3DFACF52243CB1AB00A29A94 /* downloader_subscriber_adapter_ios.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3DFACF4A243CB1AB00A29A94 /* downloader_subscriber_adapter_ios.mm */; }; - 3DFACF53243CB1AB00A29A94 /* downloader_queue_ios.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3DFACF4B243CB1AB00A29A94 /* downloader_queue_ios.cpp */; }; 401ECED41F56C50900DFDF76 /* country_parent_getter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 401ECED21F56C50900DFDF76 /* country_parent_getter.cpp */; }; 401ECED51F56C50900DFDF76 /* country_parent_getter.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 401ECED31F56C50900DFDF76 /* country_parent_getter.hpp */; }; 402873422295A91F0036AA1C /* country_tree.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 4028733F2295A91E0036AA1C /* country_tree.hpp */; }; @@ -249,6 +248,7 @@ 3971141D229D89F4003915E5 /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; }; 39B9682123E1AC5F00D3B8E3 /* libge0.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = libge0.a; sourceTree = BUILT_PRODUCTS_DIR; }; 3D2D57B625ADCEEB0002D8E3 /* downloader_queue_interface.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = downloader_queue_interface.hpp; sourceTree = ""; }; + 3D2D57BA25B6E8550002D8E3 /* downloader_queue.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = downloader_queue.hpp; sourceTree = ""; }; 3D497E9E23292706000FB57D /* map_files_downloader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = map_files_downloader.cpp; sourceTree = ""; }; 3D497E9F23292706000FB57D /* map_files_downloader_with_ping.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = map_files_downloader_with_ping.hpp; sourceTree = ""; }; 3D497EA023292706000FB57D /* map_files_downloader_with_ping.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = map_files_downloader_with_ping.cpp; sourceTree = ""; }; @@ -265,11 +265,9 @@ 3DFACF3C243C985A00A29A94 /* downloader_queue_universal.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = downloader_queue_universal.hpp; sourceTree = ""; }; 3DFACF3D243C985A00A29A94 /* downloader_queue_universal.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = downloader_queue_universal.cpp; sourceTree = ""; }; 3DFACF45243CB1AB00A29A94 /* downloader_subscriber_adapter_ios.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = downloader_subscriber_adapter_ios.h; sourceTree = ""; }; - 3DFACF46243CB1AB00A29A94 /* downloader_queue_ios.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = downloader_queue_ios.hpp; sourceTree = ""; }; 3DFACF47243CB1AB00A29A94 /* downloader_adapter_ios.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = downloader_adapter_ios.h; sourceTree = ""; }; 3DFACF49243CB1AB00A29A94 /* downloader_adapter_ios.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = downloader_adapter_ios.mm; sourceTree = ""; }; 3DFACF4A243CB1AB00A29A94 /* downloader_subscriber_adapter_ios.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = downloader_subscriber_adapter_ios.mm; sourceTree = ""; }; - 3DFACF4B243CB1AB00A29A94 /* downloader_queue_ios.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = downloader_queue_ios.cpp; sourceTree = ""; }; 401ECED21F56C50900DFDF76 /* country_parent_getter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = country_parent_getter.cpp; sourceTree = ""; }; 401ECED31F56C50900DFDF76 /* country_parent_getter.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = country_parent_getter.hpp; sourceTree = ""; }; 4028733F2295A91E0036AA1C /* country_tree.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = country_tree.hpp; sourceTree = ""; }; @@ -539,12 +537,11 @@ 3DFACF43243CB1AB00A29A94 /* background_downloading */ = { isa = PBXGroup; children = ( + 3D2D57BA25B6E8550002D8E3 /* downloader_queue.hpp */, 3DFACF45243CB1AB00A29A94 /* downloader_subscriber_adapter_ios.h */, - 3DFACF46243CB1AB00A29A94 /* downloader_queue_ios.hpp */, 3DFACF47243CB1AB00A29A94 /* downloader_adapter_ios.h */, 3DFACF49243CB1AB00A29A94 /* downloader_adapter_ios.mm */, 3DFACF4A243CB1AB00A29A94 /* downloader_subscriber_adapter_ios.mm */, - 3DFACF4B243CB1AB00A29A94 /* downloader_queue_ios.cpp */, ); path = background_downloading; sourceTree = ""; @@ -737,6 +734,7 @@ 674125201B4C05FA00A3E828 /* map_files_downloader.hpp in Headers */, 56D8CB9A1CAC17A80003F420 /* test_defines.hpp in Headers */, F68CC5C01F38B967007527C7 /* diff_types.hpp in Headers */, + 3D2D57BB25B6E8550002D8E3 /* downloader_queue.hpp in Headers */, 3DFACF3F243C985A00A29A94 /* downloader.hpp in Headers */, 402AD2E424A200F600DE5CB1 /* country_tree_helpers.hpp in Headers */, 3D2D57B725ADCEEB0002D8E3 /* downloader_queue_interface.hpp in Headers */, @@ -746,7 +744,6 @@ 3DFACF40243C985A00A29A94 /* downloader_queue_universal.hpp in Headers */, 67BE1DC61CD2180D00572709 /* downloading_policy.hpp in Headers */, 675343191A3F5A2600A0A8C3 /* storage_defines.hpp in Headers */, - 3DFACF4E243CB1AB00A29A94 /* downloader_queue_ios.hpp in Headers */, 3DF528E12386B966000ED0D5 /* apply_diff.hpp in Headers */, 67247FD41C60BA8A00EDE56A /* task_runner.hpp in Headers */, 56D0E4801F8E40340084B18C /* routing_helpers.hpp in Headers */, @@ -948,7 +945,6 @@ 402AD2E524A200F600DE5CB1 /* country_tree_helpers.cpp in Sources */, 402873432295A91F0036AA1C /* country_tree.cpp in Sources */, F6BC312C2034366100F677FE /* pinger.cpp in Sources */, - 3DFACF53243CB1AB00A29A94 /* downloader_queue_ios.cpp in Sources */, 34C9BCFC1C6CCF85000DC38D /* country_name_getter.cpp in Sources */, 3DF528E32386B966000ED0D5 /* diffs_data_source.cpp in Sources */, );