From 5042fb660c773b238b5f2f089f85185d81756b21 Mon Sep 17 00:00:00 2001 From: Sergey Yershov Date: Tue, 26 Apr 2016 13:39:31 +0300 Subject: [PATCH] [downloader] Calculate space required for updating mwm's --- storage/storage.cpp | 2 + storage/storage.hpp | 4 +- storage/storage_helpers.cpp | 9 ++++ storage/storage_helpers.hpp | 2 + storage/storage_tests/storage_tests.cpp | 70 +++++++++++++++++++++++-- 5 files changed, 82 insertions(+), 5 deletions(-) diff --git a/storage/storage.cpp b/storage/storage.cpp index c54020b8ad..239c9d9680 100644 --- a/storage/storage.cpp +++ b/storage/storage.cpp @@ -1559,6 +1559,8 @@ bool Storage::GetUpdateInfo(TCountryId const & countryId, UpdateInfo & updateInf return; updateInfo.m_numberOfMwmFilesToUpdate += 1; // It's not a group mwm. updateInfo.m_totalUpdateSizeInBytes += descendantNode.Value().GetSubtreeMwmSizeBytes(); + TLocalAndRemoteSize sizes = CountrySizeInBytes(descendantNode.Value().Name(), MapOptions::MapWithCarRouting); + updateInfo.m_sizeDifference += static_cast(sizes.second) - static_cast(sizes.first); }; TCountryTreeNode const * const node = m_countries.FindFirst(countryId); diff --git a/storage/storage.hpp b/storage/storage.hpp index 53bf3fb3ec..a9383d9bb2 100644 --- a/storage/storage.hpp +++ b/storage/storage.hpp @@ -302,10 +302,12 @@ public: /// \brief Information for "Update all mwms" button. struct UpdateInfo { - UpdateInfo() : m_numberOfMwmFilesToUpdate(0), m_totalUpdateSizeInBytes(0) {} + UpdateInfo() : m_numberOfMwmFilesToUpdate(0), m_totalUpdateSizeInBytes(0), m_sizeDifference(0) {} TMwmCounter m_numberOfMwmFilesToUpdate; TMwmSize m_totalUpdateSizeInBytes; + // Difference size in bytes between before update and after update. + int64_t m_sizeDifference; }; struct StatusCallback diff --git a/storage/storage_helpers.cpp b/storage/storage_helpers.cpp index 48cc8e723c..d19568c4c6 100644 --- a/storage/storage_helpers.cpp +++ b/storage/storage_helpers.cpp @@ -36,6 +36,15 @@ bool IsEnoughSpaceForDownload(TCountryId const & countryId, Storage const & stor return IsEnoughSpaceForDownload(nodeAttrs.m_mwmSize - nodeAttrs.m_localMwmSize); } +bool IsEnoughSpaceForUpdate(TCountryId const & countryId, Storage const & storage) +{ + Storage::UpdateInfo updateInfo; + + storage.GetUpdateInfo(countryId, updateInfo); + TMwmSize spaceNeedForUpdate = updateInfo.m_sizeDifference > 0 ? updateInfo.m_sizeDifference : 0; + return IsEnoughSpaceForDownload(spaceNeedForUpdate); +} + m2::RectD CalcLimitRect(TCountryId const & countryId, Storage const & storage, CountryInfoGetter const & countryInfoGetter) diff --git a/storage/storage_helpers.hpp b/storage/storage_helpers.hpp index 8eb7cac1d6..d0593742c9 100644 --- a/storage/storage_helpers.hpp +++ b/storage/storage_helpers.hpp @@ -26,6 +26,8 @@ bool IsDownloadFailed(Status status); bool IsEnoughSpaceForDownload(TMwmSize size); bool IsEnoughSpaceForDownload(TCountryId const & countryId, Storage const & storage); +bool IsEnoughSpaceForUpdate(TCountryId const & countryId, Storage const & storage); + /// \brief Calculates limit rect for |countryId| (expandable or not). /// \returns bounding box in mercator coordinates. diff --git a/storage/storage_tests/storage_tests.cpp b/storage/storage_tests/storage_tests.cpp index 2fc2a610e9..06a49e0973 100644 --- a/storage/storage_tests/storage_tests.cpp +++ b/storage/storage_tests/storage_tests.cpp @@ -50,8 +50,6 @@ namespace storage { namespace { -string const kMapTestDir = "map-tests"; - using TLocalFilePtr = shared_ptr; string const kSingleMwmCountriesTxt = string(R"({ @@ -69,6 +67,28 @@ string const kSingleMwmCountriesTxt = string(R"({ "Georgia", "Russia", "Europe" ] }, + { + "id": "OutdatedCountry1", + "s": 50, + "old": [ + "NormalCountry" + ], + "affiliations": + [ + "TestFiles", "Miracle", "Ganimed" + ] + }, + { + "id": "OutdatedCountry2", + "s": 1000, + "old": [ + "NormalCountry" + ], + "affiliations": + [ + "TestFiles", "Miracle", "Ganimed" + ] + }, { "id": "Algeria", "g": [ @@ -1352,10 +1372,51 @@ UNIT_TEST(StorageTest_GetNodeAttrsSingleMwm) UNIT_TEST(StorageTest_GetUpdateInfoSingleMwm) { + string const kFakeMwmDir = "1"; + WritableDirChanger writableDirChanger(kMapTestDir); + + string const kFakeMwmPath = my::JoinFoldersToPath(GetPlatform().WritableDir(), kFakeMwmDir); + GetPlatform().MkDir(kFakeMwmPath); + + // First 150 bytes of http://direct.mapswithme.com/direct/160315/Norway_Bouvet Island.mwm + unsigned char const kMwmData[] = + {0xA5,0x35,0x00,0x00,0x00,0x00,0x00,0x00,0x4D,0x57,0x4D,0x00,0x07,0xAA,0xBB + ,0x9B,0xB7,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x1B,0x82,0x8C,0xD8,0x9C,0xFB + ,0xB4,0xA0,0x0C,0x80,0x80,0x80,0x80,0x80,0x80,0xC0,0x18,0xAA,0xD5,0xAA,0xD5 + ,0xAA,0xD5,0x8A,0x1A,0x04,0x0A,0x0C,0x0E,0x11,0x00,0x04,0x07,0x40,0x13,0xF0 + ,0x00,0x00,0x00,0x00,0x0F,0x21,0xBE,0x01,0x6A,0xF0,0xB5,0x90,0xE3,0xB2,0x98 + ,0x02,0x00,0x00,0x00,0x00,0x0C,0x40,0xF9,0x01,0xF0,0xD1,0x01,0xC1,0x03,0xE2 + ,0x04,0x95,0x05,0x9D,0x01,0x48,0x5D,0x8F,0x01,0x80,0x42,0x6F,0x75,0x76,0x65 + ,0x74,0xC3,0xB8,0x79,0x61,0x86,0x42,0x6F,0x75,0x76,0x65,0x74,0x69,0x6E,0x73 + ,0x65,0x6C,0x81,0x42,0x6F,0x75,0x76,0x65,0x74,0x20,0x49,0x73,0x6C,0x61,0x6E + ,0x64,0x95,0x49,0x73,0x6C,0x61,0x20,0x42,0x6F,0x75,0x76,0x65,0x74,0x83,0xC3 + }; + static_assert(150 == sizeof(kMwmData), "Incorrect data size"); + + { + FileWriter file(my::JoinFoldersToPath(GetPlatform().WritableDir(), "OutdatedCountry1.mwm")); + file.Write(kMwmData, sizeof(kMwmData)); + } + { + FileWriter file(my::JoinFoldersToPath(GetPlatform().WritableDir(), "OutdatedCountry2.mwm")); + file.Write(kMwmData, sizeof(kMwmData)); + } + Storage storage(kSingleMwmCountriesTxt, make_unique()); + storage.RegisterAllLocalMaps(); Storage::UpdateInfo updateInfo; + storage.GetUpdateInfo("OutdatedCountry1", updateInfo); + TEST_EQUAL(updateInfo.m_numberOfMwmFilesToUpdate, 1, ()); + TEST_EQUAL(updateInfo.m_totalUpdateSizeInBytes, 50, ()); + TEST_EQUAL(updateInfo.m_sizeDifference, -100, ()); + + storage.GetUpdateInfo("OutdatedCountry2", updateInfo); + TEST_EQUAL(updateInfo.m_numberOfMwmFilesToUpdate, 1, ()); + TEST_EQUAL(updateInfo.m_totalUpdateSizeInBytes, 1000, ()); + TEST_EQUAL(updateInfo.m_sizeDifference, 850, ()); + storage.GetUpdateInfo("Abkhazia", updateInfo); TEST_EQUAL(updateInfo.m_numberOfMwmFilesToUpdate, 0, ()); TEST_EQUAL(updateInfo.m_totalUpdateSizeInBytes, 0, ()); @@ -1369,8 +1430,9 @@ UNIT_TEST(StorageTest_GetUpdateInfoSingleMwm) TEST_EQUAL(updateInfo.m_totalUpdateSizeInBytes, 0, ()); storage.GetUpdateInfo(storage.GetRootId(), updateInfo); - TEST_EQUAL(updateInfo.m_numberOfMwmFilesToUpdate, 0, ()); - TEST_EQUAL(updateInfo.m_totalUpdateSizeInBytes, 0, ()); + TEST_EQUAL(updateInfo.m_numberOfMwmFilesToUpdate, 2, ()); + TEST_EQUAL(updateInfo.m_totalUpdateSizeInBytes, 1050, ()); + TEST_EQUAL(updateInfo.m_sizeDifference, 750, ()); } UNIT_TEST(StorageTest_ParseStatus)