|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 HERE Europe B.V. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * License-Filename: LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +#pragma once |
| 21 | + |
| 22 | +#include <olp/core/client/CancellationContext.h> |
| 23 | +#include <olp/core/logging/Log.h> |
| 24 | +#include <olp/dataservice/read/Types.h> |
| 25 | + |
| 26 | +#include "Common.h" |
| 27 | +#include "ExtendedApiResponse.h" |
| 28 | +#include "ExtendedApiResponseHelpers.h" |
| 29 | + |
| 30 | +namespace olp { |
| 31 | +namespace dataservice { |
| 32 | +namespace read { |
| 33 | + |
| 34 | +using ExtendedDataResponse = ExtendedApiResponse<model::Data, client::ApiError, |
| 35 | + client::NetworkStatistics>; |
| 36 | + |
| 37 | +// Prototype of function used to download data using data handle. |
| 38 | +using DownloadFunc = std::function<ExtendedDataResponse( |
| 39 | + std::string, client::CancellationContext)>; |
| 40 | + |
| 41 | +template <typename ItemType, typename PrefetchResult> |
| 42 | +using AppendResultFunc = |
| 43 | + std::function<void(ExtendedDataResponse response, ItemType item, |
| 44 | + PrefetchResult& prefetch_result)>; |
| 45 | + |
| 46 | +template <typename ItemType, typename PrefetchResult> |
| 47 | +class DownloadItemsJob { |
| 48 | + public: |
| 49 | + DownloadItemsJob(DownloadFunc download, |
| 50 | + AppendResultFunc<ItemType, PrefetchResult> append_result, |
| 51 | + Callback<PrefetchResult> user_callback, |
| 52 | + PrefetchStatusCallback status_callback) |
| 53 | + : download_(std::move(download)), |
| 54 | + append_result_(std::move(append_result)), |
| 55 | + user_callback_(std::move(user_callback)), |
| 56 | + status_callback_(std::move(status_callback)) {} |
| 57 | + |
| 58 | + void Initialize(size_t items_count, client::NetworkStatistics statistics) { |
| 59 | + download_task_count_ = total_download_task_count_ = items_count; |
| 60 | + accumulated_statistics_ = statistics; |
| 61 | + } |
| 62 | + |
| 63 | + ExtendedDataResponse Download(const std::string& data_handle, |
| 64 | + client::CancellationContext context) { |
| 65 | + return download_(data_handle, context); |
| 66 | + } |
| 67 | + size_t GetAccumulatedBytes(const olp::client::NetworkStatistics& statistics) { |
| 68 | + // This narrow cast is necessary to avoid narrowing compiler errors like |
| 69 | + // -Wc++11-narrowing when building for 32bit targets. |
| 70 | + const auto bytes_transferred = |
| 71 | + statistics.GetBytesDownloaded() + statistics.GetBytesUploaded(); |
| 72 | + return static_cast<size_t>(bytes_transferred & |
| 73 | + std::numeric_limits<size_t>::max()); |
| 74 | + } |
| 75 | + |
| 76 | + void CompleteItem(ItemType item, ExtendedDataResponse response) { |
| 77 | + std::lock_guard<std::mutex> lock(mutex_); |
| 78 | + accumulated_statistics_ += GetNetworkStatistics(response); |
| 79 | + |
| 80 | + if (response.IsSuccessful()) { |
| 81 | + requests_succeeded_++; |
| 82 | + } else { |
| 83 | + requests_failed_++; |
| 84 | + } |
| 85 | + |
| 86 | + append_result_(response, item, prefetch_result_); |
| 87 | + |
| 88 | + if (status_callback_) { |
| 89 | + status_callback_(PrefetchStatus{ |
| 90 | + requests_succeeded_ + requests_failed_, total_download_task_count_, |
| 91 | + GetAccumulatedBytes(accumulated_statistics_)}); |
| 92 | + } |
| 93 | + |
| 94 | + if (!--download_task_count_) { |
| 95 | + OLP_SDK_LOG_DEBUG_F("DownloadItemsJob", |
| 96 | + "Download complete, succeeded=%zu, failed=%zu", |
| 97 | + requests_succeeded_, requests_failed_); |
| 98 | + |
| 99 | + OnPrefetchCompleted(std::move(prefetch_result_)); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + void OnPrefetchCompleted(Response<PrefetchResult> result) { |
| 104 | + auto prefetch_callback = std::move(user_callback_); |
| 105 | + prefetch_callback(std::move(result)); |
| 106 | + } |
| 107 | + |
| 108 | + private: |
| 109 | + DownloadFunc download_; |
| 110 | + AppendResultFunc<ItemType, PrefetchResult> append_result_; |
| 111 | + Callback<PrefetchResult> user_callback_; |
| 112 | + PrefetchStatusCallback status_callback_; |
| 113 | + size_t download_task_count_{0}; |
| 114 | + size_t total_download_task_count_{0}; |
| 115 | + size_t requests_succeeded_{0}; |
| 116 | + size_t requests_failed_{0}; |
| 117 | + client::NetworkStatistics accumulated_statistics_; |
| 118 | + PrefetchResult prefetch_result_; |
| 119 | + std::mutex mutex_; |
| 120 | +}; |
| 121 | + |
| 122 | +} // namespace read |
| 123 | +} // namespace dataservice |
| 124 | +} // namespace olp |
0 commit comments