Skip to content

Commit

Permalink
[storage] background downloader queue is unified
Browse files Browse the repository at this point in the history
  • Loading branch information
Arsentiy Milchakov authored and alexzatsepin committed Mar 30, 2021
1 parent 573b670 commit 1384b48
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 116 deletions.
7 changes: 6 additions & 1 deletion storage/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions storage/background_downloading/downloader_adapter_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cstdint>
#include <optional>

namespace storage
{
class BackgroundDownloaderAdapter : public MapFilesDownloaderWithPing
Expand All @@ -27,7 +30,7 @@ class BackgroundDownloaderAdapter : public MapFilesDownloaderWithPing
void DownloadFromAnyUrl(CountryId const & countryId, std::string const & downloadPath,
std::vector<std::string> const & urls);

BackgroundDownloaderQueue m_queue;
BackgroundDownloaderQueue<std::optional<uint64_t>> m_queue;
SubscriberAdapter * m_subscriberAdapter;
};
} // namespace storage
4 changes: 2 additions & 2 deletions storage/background_downloading/downloader_adapter_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<MapFilesDownloader> GetDownloader()
Expand Down
87 changes: 87 additions & 0 deletions storage/background_downloading/downloader_queue.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#pragma once

#include "storage/downloader_queue_interface.hpp"
#include "storage/queued_country.hpp"
#include "storage/storage_defines.hpp"

#include <cstdint>
#include <optional>
#include <string>
#include <unordered_map>
#include <utility>

namespace storage
{
template <typename TaskInfoType>
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<CountryId, TaskData> m_queue;
};
} // namespace storage
56 changes: 0 additions & 56 deletions storage/background_downloading/downloader_queue_ios.cpp

This file was deleted.

47 changes: 0 additions & 47 deletions storage/background_downloading/downloader_queue_ios.hpp

This file was deleted.

12 changes: 4 additions & 8 deletions xcode/storage/storage.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand All @@ -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 */; };
Expand Down Expand Up @@ -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 = "<group>"; };
3D2D57BA25B6E8550002D8E3 /* downloader_queue.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = downloader_queue.hpp; sourceTree = "<group>"; };
3D497E9E23292706000FB57D /* map_files_downloader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = map_files_downloader.cpp; sourceTree = "<group>"; };
3D497E9F23292706000FB57D /* map_files_downloader_with_ping.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = map_files_downloader_with_ping.hpp; sourceTree = "<group>"; };
3D497EA023292706000FB57D /* map_files_downloader_with_ping.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = map_files_downloader_with_ping.cpp; sourceTree = "<group>"; };
Expand All @@ -265,11 +265,9 @@
3DFACF3C243C985A00A29A94 /* downloader_queue_universal.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = downloader_queue_universal.hpp; sourceTree = "<group>"; };
3DFACF3D243C985A00A29A94 /* downloader_queue_universal.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = downloader_queue_universal.cpp; sourceTree = "<group>"; };
3DFACF45243CB1AB00A29A94 /* downloader_subscriber_adapter_ios.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = downloader_subscriber_adapter_ios.h; sourceTree = "<group>"; };
3DFACF46243CB1AB00A29A94 /* downloader_queue_ios.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = downloader_queue_ios.hpp; sourceTree = "<group>"; };
3DFACF47243CB1AB00A29A94 /* downloader_adapter_ios.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = downloader_adapter_ios.h; sourceTree = "<group>"; };
3DFACF49243CB1AB00A29A94 /* downloader_adapter_ios.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = downloader_adapter_ios.mm; sourceTree = "<group>"; };
3DFACF4A243CB1AB00A29A94 /* downloader_subscriber_adapter_ios.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = downloader_subscriber_adapter_ios.mm; sourceTree = "<group>"; };
3DFACF4B243CB1AB00A29A94 /* downloader_queue_ios.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = downloader_queue_ios.cpp; sourceTree = "<group>"; };
401ECED21F56C50900DFDF76 /* country_parent_getter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = country_parent_getter.cpp; sourceTree = "<group>"; };
401ECED31F56C50900DFDF76 /* country_parent_getter.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = country_parent_getter.hpp; sourceTree = "<group>"; };
4028733F2295A91E0036AA1C /* country_tree.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = country_tree.hpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 = "<group>";
Expand Down Expand Up @@ -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 */,
Expand All @@ -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 */,
Expand Down Expand Up @@ -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 */,
);
Expand Down

0 comments on commit 1384b48

Please sign in to comment.