From 9ae91337c98d872766fa1326fe22e4ae770df0b2 Mon Sep 17 00:00:00 2001 From: Arsentiy Milchakov Date: Fri, 20 Sep 2019 19:20:35 +0300 Subject: [PATCH] [storage] diffs loading is moved from diff manager --- storage/CMakeLists.txt | 4 +-- storage/diff_scheme/diff_manager.cpp | 31 +++++++------------ storage/diff_scheme/diff_manager.hpp | 16 ++-------- storage/diff_scheme/diff_scheme_checker.hpp | 15 --------- ...eme_checker.cpp => diff_scheme_loader.cpp} | 26 +++++++++++----- storage/diff_scheme/diff_scheme_loader.hpp | 19 ++++++++++++ storage/storage.cpp | 19 ++++++++---- storage/storage.hpp | 4 +-- .../storage/storage.xcodeproj/project.pbxproj | 16 +++++----- 9 files changed, 75 insertions(+), 75 deletions(-) delete mode 100644 storage/diff_scheme/diff_scheme_checker.hpp rename storage/diff_scheme/{diff_scheme_checker.cpp => diff_scheme_loader.cpp} (87%) create mode 100644 storage/diff_scheme/diff_scheme_loader.hpp diff --git a/storage/CMakeLists.txt b/storage/CMakeLists.txt index 4c72c04945..6210cc1325 100644 --- a/storage/CMakeLists.txt +++ b/storage/CMakeLists.txt @@ -19,8 +19,8 @@ set( country_tree.hpp diff_scheme/diff_manager.cpp diff_scheme/diff_manager.hpp - diff_scheme/diff_scheme_checker.cpp - diff_scheme/diff_scheme_checker.hpp + diff_scheme/diff_scheme_loader.cpp + diff_scheme/diff_scheme_loader.hpp diff_scheme/diff_types.hpp downloader_search_params.hpp downloading_policy.cpp diff --git a/storage/diff_scheme/diff_manager.cpp b/storage/diff_scheme/diff_manager.cpp index edd9d70495..2fb09fb8b5 100644 --- a/storage/diff_scheme/diff_manager.cpp +++ b/storage/diff_scheme/diff_manager.cpp @@ -1,5 +1,4 @@ #include "storage/diff_scheme/diff_manager.hpp" -#include "storage/diff_scheme/diff_scheme_checker.hpp" #include "platform/platform.hpp" @@ -25,27 +24,19 @@ namespace storage { namespace diffs { -void Manager::Load(LocalMapsInfo && info) +void Manager::Load(NameDiffInfoMap && info) { - GetPlatform().RunTask(Platform::Thread::Network, [this, info = std::move(info)] { - NameDiffInfoMap diffs = Checker::Check(info); + if (info.empty()) + { + m_status = Status::NotAvailable; - GetPlatform().RunTask(Platform::Thread::Gui, [this, diffs = std::move(diffs)] { - m_diffs = std::move(diffs); - if (m_diffs.empty()) - { - m_status = Status::NotAvailable; - - alohalytics::Stats::Instance().LogEvent("Downloader_DiffScheme_OnStart_fallback"); - } - else - { - m_status = Status::Available; - } - - m_observers.ForEach(&Observer::OnDiffStatusReceived, m_status); - }); - }); + alohalytics::Stats::Instance().LogEvent("Downloader_DiffScheme_OnStart_fallback"); + } + else + { + m_diffs = std::move(info); + m_status = Status::Available; + } } void Manager::ApplyDiff(ApplyDiffParams && p, base::Cancellable const & cancellable, diff --git a/storage/diff_scheme/diff_manager.hpp b/storage/diff_scheme/diff_manager.hpp index 1f2487670a..3bafbb0729 100644 --- a/storage/diff_scheme/diff_manager.hpp +++ b/storage/diff_scheme/diff_manager.hpp @@ -11,6 +11,7 @@ #include #include +#include #include namespace base @@ -32,14 +33,6 @@ public: LocalFilePtr m_oldMwmFile; }; - class Observer - { - public: - virtual ~Observer() = default; - - virtual void OnDiffStatusReceived(Status const status) = 0; - }; - using OnDiffApplicationFinished = std::function; // If the diff is available, sets |size| to its size and returns true. @@ -65,13 +58,9 @@ public: Status GetStatus() const; - void Load(LocalMapsInfo && info); + void Load(NameDiffInfoMap && info); void ApplyDiff(ApplyDiffParams && p, base::Cancellable const & cancellable, OnDiffApplicationFinished const & task); - - bool AddObserver(Observer & observer) { return m_observers.Add(observer); } - bool RemoveObserver(Observer const & observer) { return m_observers.Remove(observer); } - private: template bool WithNotAppliedDiff(storage::CountryId const & countryId, Fn && fn) const @@ -89,7 +78,6 @@ private: Status m_status = Status::Undefined; NameDiffInfoMap m_diffs; - base::ObserverListUnsafe m_observers; }; } // namespace diffs } // namespace storage diff --git a/storage/diff_scheme/diff_scheme_checker.hpp b/storage/diff_scheme/diff_scheme_checker.hpp deleted file mode 100644 index 2cd69dcb25..0000000000 --- a/storage/diff_scheme/diff_scheme_checker.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include "storage/diff_scheme/diff_types.hpp" - -namespace storage -{ -namespace diffs -{ -class Checker final -{ -public: - static NameDiffInfoMap Check(LocalMapsInfo const & info); -}; -} // namespace diffs -} // namespace storage diff --git a/storage/diff_scheme/diff_scheme_checker.cpp b/storage/diff_scheme/diff_scheme_loader.cpp similarity index 87% rename from storage/diff_scheme/diff_scheme_checker.cpp rename to storage/diff_scheme/diff_scheme_loader.cpp index fb606a99a3..75e6d8f551 100644 --- a/storage/diff_scheme/diff_scheme_checker.cpp +++ b/storage/diff_scheme/diff_scheme_loader.cpp @@ -1,4 +1,4 @@ -#include "storage/diff_scheme/diff_scheme_checker.hpp" +#include "storage/diff_scheme/diff_scheme_loader.hpp" #include "platform/http_client.hpp" #include "platform/platform.hpp" @@ -103,14 +103,8 @@ NameDiffInfoMap DeserializeResponse(string const & response, LocalMapsInfo::Name return diffs; } -} // namespace -namespace storage -{ -namespace diffs -{ -//static -NameDiffInfoMap Checker::Check(LocalMapsInfo const & info) +NameDiffInfoMap Load(LocalMapsInfo const & info) { if (info.m_localMaps.empty()) return NameDiffInfoMap(); @@ -138,5 +132,21 @@ NameDiffInfoMap Checker::Check(LocalMapsInfo const & info) return diffs; } +} // namespace + +namespace storage +{ +namespace diffs +{ +//static +void Loader::Load(LocalMapsInfo && info, DiffsReceivedCallback && callback) +{ + GetPlatform().RunTask(Platform::Thread::Network, [info = move(info), callback = move(callback)]() { + GetPlatform().RunTask(Platform::Thread::Gui, + [result = ::Load(info), callback = move(callback)]() mutable { + callback(move(result)); + }); + }); +} } // namespace diffs } // namespace storage diff --git a/storage/diff_scheme/diff_scheme_loader.hpp b/storage/diff_scheme/diff_scheme_loader.hpp new file mode 100644 index 0000000000..38bbabf0ad --- /dev/null +++ b/storage/diff_scheme/diff_scheme_loader.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include "storage/diff_scheme/diff_types.hpp" + +#include + +namespace storage +{ +namespace diffs +{ +using DiffsReceivedCallback = std::function; + +class Loader final +{ +public: + static void Load(LocalMapsInfo && info, DiffsReceivedCallback && callback); +}; +} // namespace diffs +} // namespace storage diff --git a/storage/storage.cpp b/storage/storage.cpp index 4009f9c29a..994eccfcc8 100644 --- a/storage/storage.cpp +++ b/storage/storage.cpp @@ -1,5 +1,6 @@ #include "storage/storage.hpp" +#include "storage/diff_scheme/diff_scheme_loader.hpp" #include "storage/http_map_files_downloader.hpp" #include "defines.hpp" @@ -32,8 +33,9 @@ using namespace downloader; using namespace generator::mwm_diff; using namespace platform; -using namespace std::chrono; using namespace std; +using namespace std::chrono; +using namespace std::placeholders; namespace storage { @@ -327,6 +329,9 @@ void Storage::RegisterAllLocalMaps(bool enableDiffs) FindAllDiffs(m_dataDir, m_notAppliedDiffs); if (enableDiffs) LoadDiffScheme(); + // Note: call order is important, diffs loading must be called first. + // Because of diffs downloading and servers list downloading + // are working on network thread, consequtive executing is guaranteed. RestoreDownloadQueue(); } @@ -900,7 +905,6 @@ void Storage::DoDownload() auto const relativeUrl = GetDownloadRelativeUrl(id, options); auto const filePath = GetFileDownloadPath(id, options); - using namespace std::placeholders; m_downloader->DownloadMapFile(relativeUrl, filePath, GetDownloadSize(queuedCountry), bind(&Storage::OnMapFileDownloadFinished, this, _1, _2), bind(&Storage::OnMapFileDownloadProgress, this, _1)); @@ -1526,8 +1530,10 @@ void Storage::LoadDiffScheme() localMapsInfo.m_localMaps.emplace(localFile->GetCountryName(), mapVersion); } - m_diffManager.AddObserver(*this); - m_diffManager.Load(move(localMapsInfo)); + diffs::Loader::Load(move(localMapsInfo), [this](diffs::NameDiffInfoMap && diffs) + { + OnDiffStatusReceived(move(diffs)); + }); } void Storage::ApplyDiff(CountryId const & countryId, function const & fn) @@ -1634,9 +1640,10 @@ void Storage::SetStartDownloadingCallback(StartDownloadingCallback const & cb) m_startDownloadingCallback = cb; } -void Storage::OnDiffStatusReceived(diffs::Status const status) +void Storage::OnDiffStatusReceived(diffs::NameDiffInfoMap && diffs) { - if (status != diffs::Status::NotAvailable) + m_diffManager.Load(move(diffs)); + if (m_diffManager.GetStatus() != diffs::Status::NotAvailable) { for (auto const & localDiff : m_notAppliedDiffs) { diff --git a/storage/storage.hpp b/storage/storage.hpp index 07a99bfd6f..49aa3e01d2 100644 --- a/storage/storage.hpp +++ b/storage/storage.hpp @@ -146,7 +146,7 @@ struct NodeStatuses // Downloading of only one mwm at a time is supported, so while the // mwm at the top of the queue is being downloaded (or updated by // applying a diff file) all other mwms have to wait. -class Storage : public diffs::Manager::Observer +class Storage { public: struct StatusCallback; @@ -710,7 +710,7 @@ private: // Should be called once on startup, downloading process should be suspended until this method // was not called. Do not call this method manually. - void OnDiffStatusReceived(diffs::Status const status) override; + void OnDiffStatusReceived(diffs::NameDiffInfoMap && diffs); }; void GetQueuedCountries(Storage::Queue const & queue, CountriesSet & resultCountries); diff --git a/xcode/storage/storage.xcodeproj/project.pbxproj b/xcode/storage/storage.xcodeproj/project.pbxproj index 3eb37bdae0..d385f035aa 100644 --- a/xcode/storage/storage.xcodeproj/project.pbxproj +++ b/xcode/storage/storage.xcodeproj/project.pbxproj @@ -28,6 +28,8 @@ 3DCD414620D80C0900143533 /* country_info_reader_light.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3DCD414420D80C0800143533 /* country_info_reader_light.hpp */; }; 3DCD414720D80C0900143533 /* country_info_reader_light.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3DCD414520D80C0900143533 /* country_info_reader_light.cpp */; }; 3DCD414920D80C1B00143533 /* lightweight_matching_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3DCD414820D80C1B00143533 /* lightweight_matching_tests.cpp */; }; + 3DD84014233A38DE0008D0ED /* diff_scheme_loader.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3DD84012233A38DE0008D0ED /* diff_scheme_loader.hpp */; }; + 3DD84015233A38DE0008D0ED /* diff_scheme_loader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3DD84013233A38DE0008D0ED /* diff_scheme_loader.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 */; }; @@ -188,8 +190,6 @@ F66D56AF1EAE383E0081E883 /* drules_proto_vehicle_clear.bin in Resources */ = {isa = PBXBuildFile; fileRef = F66D569B1EAE37160081E883 /* drules_proto_vehicle_clear.bin */; }; F66D56B01EAE383E0081E883 /* drules_proto_vehicle_clear.txt in Resources */ = {isa = PBXBuildFile; fileRef = F66D569C1EAE37160081E883 /* drules_proto_vehicle_clear.txt */; }; F66D56B21EAE385B0081E883 /* local_ads_symbols.txt in Resources */ = {isa = PBXBuildFile; fileRef = F66D56B11EAE385B0081E883 /* local_ads_symbols.txt */; }; - F68CC5BE1F38B967007527C7 /* diff_scheme_checker.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F68CC5BB1F38B967007527C7 /* diff_scheme_checker.cpp */; }; - F68CC5BF1F38B967007527C7 /* diff_scheme_checker.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F68CC5BC1F38B967007527C7 /* diff_scheme_checker.hpp */; }; F68CC5C01F38B967007527C7 /* diff_types.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F68CC5BD1F38B967007527C7 /* diff_types.hpp */; }; F6BC312B2034366100F677FE /* pinger.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F6BC31292034366100F677FE /* pinger.hpp */; }; F6BC312C2034366100F677FE /* pinger.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6BC312A2034366100F677FE /* pinger.cpp */; }; @@ -218,6 +218,8 @@ 3DCD414420D80C0800143533 /* country_info_reader_light.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = country_info_reader_light.hpp; sourceTree = ""; }; 3DCD414520D80C0900143533 /* country_info_reader_light.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = country_info_reader_light.cpp; sourceTree = ""; }; 3DCD414820D80C1B00143533 /* lightweight_matching_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lightweight_matching_tests.cpp; sourceTree = ""; }; + 3DD84012233A38DE0008D0ED /* diff_scheme_loader.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = diff_scheme_loader.hpp; path = diff_scheme/diff_scheme_loader.hpp; sourceTree = ""; }; + 3DD84013233A38DE0008D0ED /* diff_scheme_loader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = diff_scheme_loader.cpp; path = diff_scheme/diff_scheme_loader.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 = ""; }; @@ -336,8 +338,6 @@ F66D56A91EAE38030081E883 /* libtraffic.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libtraffic.a; path = ../traffic/build/Debug/libtraffic.a; sourceTree = ""; }; F66D56AB1EAE38150081E883 /* liblocal_ads.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = liblocal_ads.a; path = ../local_ads/build/Debug/liblocal_ads.a; sourceTree = ""; }; F66D56B11EAE385B0081E883 /* local_ads_symbols.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = local_ads_symbols.txt; sourceTree = ""; }; - F68CC5BB1F38B967007527C7 /* diff_scheme_checker.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = diff_scheme_checker.cpp; path = diff_scheme/diff_scheme_checker.cpp; sourceTree = ""; }; - F68CC5BC1F38B967007527C7 /* diff_scheme_checker.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = diff_scheme_checker.hpp; path = diff_scheme/diff_scheme_checker.hpp; sourceTree = ""; }; F68CC5BD1F38B967007527C7 /* diff_types.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = diff_types.hpp; path = diff_scheme/diff_types.hpp; sourceTree = ""; }; F6BC31292034366100F677FE /* pinger.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = pinger.hpp; sourceTree = ""; }; F6BC312A2034366100F677FE /* pinger.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = pinger.cpp; sourceTree = ""; }; @@ -582,6 +582,8 @@ 675342E21A3F59EF00A0A8C3 /* storage */ = { isa = PBXGroup; children = ( + 3DD84013233A38DE0008D0ED /* diff_scheme_loader.cpp */, + 3DD84012233A38DE0008D0ED /* diff_scheme_loader.hpp */, 3D497EA023292706000FB57D /* map_files_downloader_with_ping.cpp */, 3D497E9F23292706000FB57D /* map_files_downloader_with_ping.hpp */, 3D497E9E23292706000FB57D /* map_files_downloader.cpp */, @@ -598,8 +600,6 @@ 401ECED31F56C50900DFDF76 /* country_parent_getter.hpp */, 4586D0C51F48126A00DF9CE5 /* diff_manager.cpp */, 4586D0C61F48126A00DF9CE5 /* diff_manager.hpp */, - F68CC5BB1F38B967007527C7 /* diff_scheme_checker.cpp */, - F68CC5BC1F38B967007527C7 /* diff_scheme_checker.hpp */, F68CC5BD1F38B967007527C7 /* diff_types.hpp */, 67BE1DC31CD2180D00572709 /* downloading_policy.cpp */, 67BE1DC41CD2180D00572709 /* downloading_policy.hpp */, @@ -672,7 +672,6 @@ 34C9BCFD1C6CCF85000DC38D /* country_name_getter.hpp in Headers */, 6741251F1B4C05FA00A3E828 /* http_map_files_downloader.hpp in Headers */, 67BE1DC61CD2180D00572709 /* downloading_policy.hpp in Headers */, - F68CC5BF1F38B967007527C7 /* diff_scheme_checker.hpp in Headers */, 675343191A3F5A2600A0A8C3 /* storage_defines.hpp in Headers */, 67247FD41C60BA8A00EDE56A /* task_runner.hpp in Headers */, 56D0E4801F8E40340084B18C /* routing_helpers.hpp in Headers */, @@ -683,6 +682,7 @@ 6753430A1A3F5A2600A0A8C3 /* country_decl.hpp in Headers */, 67247FCF1C60BA8A00EDE56A /* fake_map_files_downloader.hpp in Headers */, 67AF4A011BC579DD0048B1ED /* country_info_getter.hpp in Headers */, + 3DD84014233A38DE0008D0ED /* diff_scheme_loader.hpp in Headers */, 3971140A229D7C6D003915E5 /* helpers.hpp in Headers */, 402873442295A91F0036AA1C /* downloader_search_params.hpp in Headers */, 674125221B4C05FA00A3E828 /* queued_country.hpp in Headers */, @@ -856,6 +856,7 @@ files = ( 56D8CB991CAC17A80003F420 /* test_defines.cpp in Sources */, 3D497EA323292706000FB57D /* map_files_downloader_with_ping.cpp in Sources */, + 3DD84015233A38DE0008D0ED /* diff_scheme_loader.cpp in Sources */, 401ECED41F56C50900DFDF76 /* country_parent_getter.cpp in Sources */, 56D0E4811F8E40340084B18C /* routing_helpers.cpp in Sources */, 3D497EA123292706000FB57D /* map_files_downloader.cpp in Sources */, @@ -864,7 +865,6 @@ 4586D0C71F48126A00DF9CE5 /* diff_manager.cpp in Sources */, 675343091A3F5A2600A0A8C3 /* country_decl.cpp in Sources */, 674125211B4C05FA00A3E828 /* queued_country.cpp in Sources */, - F68CC5BE1F38B967007527C7 /* diff_scheme_checker.cpp in Sources */, 3DCD414720D80C0900143533 /* country_info_reader_light.cpp in Sources */, 6753431A1A3F5A2600A0A8C3 /* storage.cpp in Sources */, 674125231B4C05FA00A3E828 /* storage_defines.cpp in Sources */,