Skip to content

Commit 818a595

Browse files
authored
Use range in subdir iteration (#3934)
1 parent 9ad464b commit 818a595

File tree

2 files changed

+166
-54
lines changed

2 files changed

+166
-54
lines changed

libmamba/include/mamba/core/subdirdata.hpp

+149-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#ifndef MAMBA_CORE_SUBDIRDATA_HPP
88
#define MAMBA_CORE_SUBDIRDATA_HPP
99

10+
#include <algorithm>
1011
#include <memory>
1112
#include <string>
1213

@@ -131,8 +132,21 @@ namespace mamba
131132
* The result can be inspected with the input subdirs methods, such as
132133
* @ref valid_cache_found, @ref valid_json_cache_path etc.
133134
*/
135+
template <typename SubdirIter1, typename SubdirIter2>
134136
[[nodiscard]] static auto download_required_indexes(
135-
std::vector<SubdirData>& subdirs,
137+
SubdirIter1 subdirs_first,
138+
SubdirIter2 subdirs_last,
139+
const SubdirParams& subdir_params,
140+
const specs::AuthenticationDataBase& auth_info,
141+
const download::mirror_map& mirrors,
142+
const download::Options& download_options,
143+
const download::RemoteFetchParams& remote_fetch_params,
144+
download::Monitor* check_monitor = nullptr,
145+
download::Monitor* download_monitor = nullptr
146+
) -> expected_t<void>;
147+
template <typename Subdirs>
148+
[[nodiscard]] static auto download_required_indexes(
149+
Subdirs& subdirs,
136150
const SubdirParams& subdir_params,
137151
const specs::AuthenticationDataBase& auth_info,
138152
const download::mirror_map& mirrors,
@@ -196,6 +210,10 @@ namespace mamba
196210

197211
[[nodiscard]] auto repodata_url_path() const -> std::string;
198212

213+
/**************************************************
214+
* Implementation details of SubdirData::create *
215+
**************************************************/
216+
199217
void load(
200218
const MultiPackageCache& caches,
201219
ChannelContext& channel_context,
@@ -209,12 +227,33 @@ namespace mamba
209227
const specs::Channel& channel
210228
);
211229

212-
auto build_check_requests(const SubdirParams& params) -> download::MultiRequest;
213-
auto build_index_request() -> download::Request;
230+
/*********************************************************************
231+
* Implementation details of SubdirData::download_required_indexes *
232+
*********************************************************************/
214233

215234
auto use_existing_cache() -> expected_t<void>;
216235
auto finalize_transfer(SubdirMetadata::HttpMetadata http_data) -> expected_t<void>;
217236
void refresh_last_write_time(const fs::u8path& json_file, const fs::u8path& solv_file);
237+
238+
template <typename First, typename End>
239+
static auto
240+
build_all_check_requests(First subdirs_first, End subdirs_last, const SubdirParams& params)
241+
-> download::MultiRequest;
242+
auto build_check_requests(const SubdirParams& params) -> download::MultiRequest;
243+
244+
template <typename First, typename End>
245+
static auto build_all_index_requests(First subdirs_first, End subdirs_last)
246+
-> download::MultiRequest;
247+
auto build_index_request() -> download::Request;
248+
249+
[[nodiscard]] static auto download_requests(
250+
download::MultiRequest index_requests,
251+
const specs::AuthenticationDataBase& auth_info,
252+
const download::mirror_map& mirrors,
253+
const download::Options& download_options,
254+
const download::RemoteFetchParams& remote_fetch_params,
255+
download::Monitor* download_monitor
256+
) -> expected_t<void>;
218257
};
219258

220259
/**
@@ -243,5 +282,112 @@ namespace mamba
243282
*/
244283
auto create_cache_dir(const fs::u8path& cache_path) -> std::string;
245284

285+
/**********************************
286+
* Implementation of Subdirdata *
287+
**********************************/
288+
289+
template <typename SubdirIter1, typename SubdirIter2>
290+
auto SubdirData::download_required_indexes(
291+
SubdirIter1 subdirs_first,
292+
SubdirIter2 subdirs_last,
293+
const SubdirParams& subdir_params,
294+
const specs::AuthenticationDataBase& auth_info,
295+
const download::mirror_map& mirrors,
296+
const download::Options& download_options,
297+
const download::RemoteFetchParams& remote_fetch_params,
298+
download::Monitor* check_monitor,
299+
download::Monitor* download_monitor
300+
) -> expected_t<void>
301+
{
302+
auto result = download_requests(
303+
build_all_check_requests(subdirs_first, subdirs_last, subdir_params),
304+
auth_info,
305+
mirrors,
306+
download_options,
307+
remote_fetch_params,
308+
check_monitor
309+
);
310+
311+
// Allow to continue if failed checks, unless asked to stop.
312+
constexpr auto is_interrupted = [](const auto& e)
313+
{ return e.error_code() == mamba_error_code::user_interrupted; };
314+
if (!result.has_value() && result.map_error(is_interrupted).error())
315+
{
316+
return result;
317+
}
318+
319+
// TODO load local channels even when offline if (!ctx.offline)
320+
if (subdir_params.offline)
321+
{
322+
return expected_t<void>();
323+
}
324+
325+
return download_requests(
326+
build_all_index_requests(subdirs_first, subdirs_last),
327+
auth_info,
328+
mirrors,
329+
download_options,
330+
remote_fetch_params,
331+
download_monitor
332+
);
333+
}
334+
335+
template <typename Subdirs>
336+
auto SubdirData::download_required_indexes(
337+
Subdirs& subdirs,
338+
const SubdirParams& subdir_params,
339+
const specs::AuthenticationDataBase& auth_info,
340+
const download::mirror_map& mirrors,
341+
const download::Options& download_options,
342+
const download::RemoteFetchParams& remote_fetch_params,
343+
download::Monitor* check_monitor,
344+
download::Monitor* download_monitor
345+
) -> expected_t<void>
346+
{
347+
return download_required_indexes(
348+
subdirs.begin(),
349+
subdirs.end(),
350+
subdir_params,
351+
auth_info,
352+
mirrors,
353+
download_options,
354+
remote_fetch_params,
355+
check_monitor,
356+
download_monitor
357+
);
358+
}
359+
360+
template <typename First, typename End>
361+
auto
362+
SubdirData::build_all_check_requests(First subdirs_first, End subdirs_last, const SubdirParams& params)
363+
-> download::MultiRequest
364+
{
365+
download::MultiRequest requests;
366+
for (; subdirs_first != subdirs_last; ++subdirs_first)
367+
{
368+
if (!subdirs_first->valid_cache_found())
369+
{
370+
auto check_list = subdirs_first->build_check_requests(params);
371+
std::move(check_list.begin(), check_list.end(), std::back_inserter(requests));
372+
}
373+
}
374+
return requests;
375+
}
376+
377+
template <typename First, typename End>
378+
auto SubdirData::build_all_index_requests(First subdirs_first, End subdirs_last)
379+
-> download::MultiRequest
380+
{
381+
download::MultiRequest requests;
382+
for (; subdirs_first != subdirs_last; ++subdirs_first)
383+
{
384+
if (!subdirs_first->valid_cache_found())
385+
{
386+
requests.push_back(subdirs_first->build_index_request());
387+
}
388+
}
389+
return requests;
390+
}
391+
246392
}
247393
#endif

libmamba/src/core/subdirdata.cpp

+17-51
Original file line numberDiff line numberDiff line change
@@ -521,67 +521,33 @@ namespace mamba
521521
return make_unexpected("Cache not loaded", mamba_error_code::cache_not_loaded);
522522
}
523523

524-
expected_t<void> SubdirData::download_required_indexes(
525-
std::vector<SubdirData>& subdirs,
526-
const SubdirParams& subdir_params,
524+
auto SubdirData::download_requests(
525+
download::MultiRequest requests,
527526
const specs::AuthenticationDataBase& auth_info,
528527
const download::mirror_map& mirrors,
529528
const download::Options& download_options,
530529
const download::RemoteFetchParams& remote_fetch_params,
531-
download::Monitor* check_monitor,
532-
download::Monitor* download_monitor
533-
)
530+
download::Monitor* monitor
531+
) -> expected_t<void>
534532
{
535-
download::MultiRequest check_requests;
536-
for (auto& subdir : subdirs)
533+
try
537534
{
538-
if (!subdir.valid_cache_found())
539-
{
540-
download::MultiRequest check_list = subdir.build_check_requests(subdir_params);
541-
std::move(check_list.begin(), check_list.end(), std::back_inserter(check_requests));
542-
}
535+
download::download(
536+
std::move(requests),
537+
mirrors,
538+
remote_fetch_params,
539+
auth_info,
540+
download_options,
541+
monitor
542+
);
543543
}
544-
download::download(
545-
std::move(check_requests),
546-
mirrors,
547-
remote_fetch_params,
548-
auth_info,
549-
download_options,
550-
check_monitor
551-
);
552-
553-
if (is_sig_interrupted())
544+
catch (const std::runtime_error& e)
554545
{
555-
return make_unexpected("Interrupted by user", mamba_error_code::user_interrupted);
546+
return make_unexpected(e.what(), mamba_error_code::repodata_not_loaded);
556547
}
557-
558-
// TODO load local channels even when offline if (!ctx.offline)
559-
if (!subdir_params.offline)
548+
if (is_sig_interrupted())
560549
{
561-
download::MultiRequest index_requests;
562-
for (auto& subdir : subdirs)
563-
{
564-
if (!subdir.valid_cache_found())
565-
{
566-
index_requests.push_back(subdir.build_index_request());
567-
}
568-
}
569-
570-
try
571-
{
572-
download::download(
573-
std::move(index_requests),
574-
mirrors,
575-
remote_fetch_params,
576-
auth_info,
577-
download_options,
578-
download_monitor
579-
);
580-
}
581-
catch (const std::runtime_error& e)
582-
{
583-
return make_unexpected(e.what(), mamba_error_code::repodata_not_loaded);
584-
}
550+
return make_unexpected("Interrupted by user", mamba_error_code::user_interrupted);
585551
}
586552

587553
return expected_t<void>();

0 commit comments

Comments
 (0)