From 075d39e705512aedbc76c976633dc1a8102f9643 Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Wed, 10 May 2017 13:36:21 +0300 Subject: [PATCH] Calculating correct value of max mwm size. --- .../jni/com/mapswithme/maps/MapManager.cpp | 2 +- iphone/Maps/Core/Storage/MWMStorage.mm | 6 +++--- map/framework.cpp | 3 ++- storage/storage.cpp | 21 ++++++++++++++++++- storage/storage.hpp | 6 ++++++ storage/storage_helpers.cpp | 10 ++++----- storage/storage_helpers.hpp | 4 +--- 7 files changed, 38 insertions(+), 14 deletions(-) diff --git a/android/jni/com/mapswithme/maps/MapManager.cpp b/android/jni/com/mapswithme/maps/MapManager.cpp index 2d2dc59b22..c001e45910 100644 --- a/android/jni/com/mapswithme/maps/MapManager.cpp +++ b/android/jni/com/mapswithme/maps/MapManager.cpp @@ -103,7 +103,7 @@ Java_com_mapswithme_maps_downloader_MapManager_nativeHasSpaceForMigration(JNIEnv JNIEXPORT jboolean JNICALL Java_com_mapswithme_maps_downloader_MapManager_nativeHasSpaceToDownloadAmount(JNIEnv * env, jclass clazz, jlong bytes) { - return IsEnoughSpaceForDownload(bytes); + return IsEnoughSpaceForDownload(bytes, GetStorage().GetMaxMwmSizeBytes()); } // static boolean nativeHasSpaceToDownloadCountry(String root); diff --git a/iphone/Maps/Core/Storage/MWMStorage.mm b/iphone/Maps/Core/Storage/MWMStorage.mm index 5d7a4da171..25b2e108fc 100644 --- a/iphone/Maps/Core/Storage/MWMStorage.mm +++ b/iphone/Maps/Core/Storage/MWMStorage.mm @@ -81,7 +81,8 @@ using namespace storage; } + (void)downloadNodes:(TCountriesVec const &)countryIds onSuccess:(MWMVoidBlock)onSuccess { - TMwmSize requiredSize = accumulate(countryIds.begin(), countryIds.end(), kMaxMwmSizeBytes, + auto & s = GetFramework().GetStorage(); + TMwmSize requiredSize = accumulate(countryIds.begin(), countryIds.end(), s.GetMaxMwmSizeBytes(), [](size_t const & size, TCountryId const & countryId) { NodeAttrs nodeAttrs; @@ -90,8 +91,7 @@ using namespace storage; }); if (storage::IsEnoughSpaceForDownload(requiredSize)) { - [self checkConnectionAndPerformAction:[countryIds, onSuccess] { - auto & s = GetFramework().GetStorage(); + [self checkConnectionAndPerformAction:[countryIds, onSuccess, &s] { for (auto const & countryId : countryIds) s.DownloadNode(countryId); if (onSuccess) diff --git a/map/framework.cpp b/map/framework.cpp index 6e35a2c343..625f883779 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -341,7 +341,8 @@ void Framework::StopLocationFollow() bool Framework::IsEnoughSpaceForMigrate() const { - return GetPlatform().GetWritableStorageStatus(kMaxMwmSizeBytes) == Platform::TStorageStatus::STORAGE_OK; + return GetPlatform().GetWritableStorageStatus(GetStorage().GetMaxMwmSizeBytes()) == + Platform::TStorageStatus::STORAGE_OK; } TCountryId Framework::PreMigrate(ms::LatLon const & position, diff --git a/storage/storage.cpp b/storage/storage.cpp index 515b0ce6dc..8b874ec683 100644 --- a/storage/storage.cpp +++ b/storage/storage.cpp @@ -114,18 +114,24 @@ Storage::Storage(string const & pathToCountriesFile /* = COUNTRIES_FILE */, , m_currentSlotId(0) , m_dataDir(dataDir) , m_downloadMapOnTheMap(nullptr) + , m_maxMwmSizeBytes(0) { SetLocale(languages::GetCurrentTwine()); LoadCountriesFile(pathToCountriesFile, m_dataDir); + CalMaxMwmSizeBytes(); } Storage::Storage(string const & referenceCountriesTxtJsonForTesting, unique_ptr mapDownloaderForTesting) - : m_downloader(move(mapDownloaderForTesting)), m_currentSlotId(0), m_downloadMapOnTheMap(nullptr) + : m_downloader(move(mapDownloaderForTesting)) + , m_currentSlotId(0) + , m_downloadMapOnTheMap(nullptr) + , m_maxMwmSizeBytes(0) { m_currentVersion = LoadCountries(referenceCountriesTxtJsonForTesting, m_countries, m_affiliations); CHECK_LESS_OR_EQUAL(0, m_currentVersion, ("Can't load test countries file")); + CalMaxMwmSizeBytes(); } void Storage::Init(TUpdateCallback const & didDownload, TDeleteCallback const & willDelete) @@ -1310,6 +1316,19 @@ bool Storage::IsDisputed(TCountryTreeNode const & node) const return found.size() > 1; } +void Storage::CalMaxMwmSizeBytes() +{ + m_maxMwmSizeBytes = 0; + m_countries.GetRoot().ForEachInSubtree([&](TCountryTree::Node const & node) { + if (node.ChildrenCount() == 0) + { + TMwmSize mwmSizeBytes = node.Value().GetSubtreeMwmSizeBytes(); + if (mwmSizeBytes > m_maxMwmSizeBytes) + m_maxMwmSizeBytes = mwmSizeBytes; + } + }); +} + StatusAndError Storage::GetNodeStatusInfo( TCountryTreeNode const & node, vector> & disputedTerritories, bool isDisputedTerritoriesCounted) const diff --git a/storage/storage.hpp b/storage/storage.hpp index 57e9c8a92f..cf753e040f 100644 --- a/storage/storage.hpp +++ b/storage/storage.hpp @@ -240,6 +240,8 @@ private: // Note. |m_affiliations| is empty in case of countries_obsolete.txt. TMappingAffiliations m_affiliations; + TMwmSize m_maxMwmSizeBytes; + ThreadChecker m_threadChecker; void DownloadNextCountryFromQueue(); @@ -532,6 +534,8 @@ public: /// Sets and gets locale, which is used to get localized counries names void SetLocale(string const & locale); string GetLocale() const; + + TMwmSize GetMaxMwmSizeBytes() const { return m_maxMwmSizeBytes; } // for testing: void SetDownloaderForTesting(unique_ptr && downloader); @@ -635,6 +639,8 @@ private: void ForEachAncestorExceptForTheRoot(vector const & nodes, ToDo && toDo) const; /// Returns true if |node.Value().Name()| is a disputed territory and false otherwise. bool IsDisputed(TCountryTreeNode const & node) const; + + void CalMaxMwmSizeBytes(); }; void GetQueuedCountries(Storage::TQueue const & queue, TCountriesSet & resultCountries); diff --git a/storage/storage_helpers.cpp b/storage/storage_helpers.cpp index 4764c89681..a636902b67 100644 --- a/storage/storage_helpers.cpp +++ b/storage/storage_helpers.cpp @@ -20,12 +20,12 @@ bool IsDownloadFailed(Status status) status == Status::EUnknown; } -bool IsEnoughSpaceForDownload(TMwmSize size) +bool IsEnoughSpaceForDownload(TMwmSize mwmSize, TMwmSize maxMwmSize) { - // Mwm size is less than kMaxMwmSizeBytes. In case of map update at first we download updated map + // Mwm size is less than |maxMwmSize|. In case of map update at first we download updated map // and only after that we do delete the obsolete map. So in such a case we might need up to // kMaxMwmSizeBytes of extra space. - return GetPlatform().GetWritableStorageStatus(size + kMaxMwmSizeBytes) == + return GetPlatform().GetWritableStorageStatus(mwmSize + maxMwmSize) == Platform::TStorageStatus::STORAGE_OK; } @@ -35,7 +35,7 @@ bool IsEnoughSpaceForDownload(TCountryId const & countryId, Storage const & stor storage.GetNodeAttrs(countryId, nodeAttrs); return IsEnoughSpaceForDownload(nodeAttrs.m_mwmSize > nodeAttrs.m_localMwmSize ? nodeAttrs.m_mwmSize - nodeAttrs.m_localMwmSize - : 0); + : 0, storage.GetMaxMwmSizeBytes()); } bool IsEnoughSpaceForUpdate(TCountryId const & countryId, Storage const & storage) @@ -44,7 +44,7 @@ bool IsEnoughSpaceForUpdate(TCountryId const & countryId, Storage const & storag storage.GetUpdateInfo(countryId, updateInfo); TMwmSize spaceNeedForUpdate = updateInfo.m_sizeDifference > 0 ? updateInfo.m_sizeDifference : 0; - return IsEnoughSpaceForDownload(spaceNeedForUpdate); + return IsEnoughSpaceForDownload(spaceNeedForUpdate, storage.GetMaxMwmSizeBytes()); } m2::RectD CalcLimitRect(TCountryId const & countryId, diff --git a/storage/storage_helpers.hpp b/storage/storage_helpers.hpp index d0593742c9..edf85bd0ec 100644 --- a/storage/storage_helpers.hpp +++ b/storage/storage_helpers.hpp @@ -13,8 +13,6 @@ namespace storage class CountryInfoGetter; class Storage; -TMwmSize constexpr kMaxMwmSizeBytes = 100 /*Mb*/ * 1024 * 1024; - /// \returns true if |position| is covered by a downloaded mwms and false otherwise. /// \note |position| has coordinates in mercator. /// \note This method takes into acount only maps enumerated in countries.txt. @@ -24,7 +22,7 @@ bool IsPointCoveredByDownloadedMaps(m2::PointD const & position, bool IsDownloadFailed(Status status); -bool IsEnoughSpaceForDownload(TMwmSize size); +bool IsEnoughSpaceForDownload(TMwmSize mwmSize, TMwmSize maxMwmSize); bool IsEnoughSpaceForDownload(TCountryId const & countryId, Storage const & storage); bool IsEnoughSpaceForUpdate(TCountryId const & countryId, Storage const & storage);