Segregates result utilities into: - result.hh - basic definitions related to results with exception containers, - result_combinators.hh - combinators for working with results in conjunction with futures, - result_loop.hh - loop-like combinators, currently has only result_parallel_for_each. The motivation for the split is: 1. In headers, usually only result.hh will be needed, so no need to force most .cc files to compile definitions from other files, 2. Less files need to be recompiled when a combinator is added to result_combinators or result_loop. As a bonus, `result_with_exception` was moved from `utils::internal` to just `utils`.
49 lines
1.4 KiB
C++
49 lines
1.4 KiB
C++
/*
|
|
* Copyright 2022-present ScyllaDB
|
|
*/
|
|
|
|
/*
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "utils/result.hh"
|
|
|
|
namespace utils {
|
|
|
|
// A version of parallel_for_each which understands results.
|
|
// In case of a failure, it returns one of the failed results.
|
|
template<typename R, typename Iterator, typename Func>
|
|
requires
|
|
ExceptionContainerResult<R>
|
|
&& std::is_void_v<typename R::value_type>
|
|
&& requires (Func f, Iterator i) { { f(*i++) } -> std::same_as<seastar::future<R>>; }
|
|
inline seastar::future<R> result_parallel_for_each(Iterator begin, Iterator end, Func&& func) {
|
|
struct result_reducer {
|
|
R res = bo::success();
|
|
void operator()(R&& r) {
|
|
if (res && !r) {
|
|
res = std::move(r);
|
|
}
|
|
}
|
|
R get() {
|
|
return std::move(res);
|
|
}
|
|
};
|
|
|
|
return seastar::map_reduce(std::move(begin), std::move(end), std::forward<Func>(func), result_reducer{});
|
|
}
|
|
|
|
// A version of parallel_for_each which understands results.
|
|
// In case of a failure, it returns one of the failed results.
|
|
template<typename R, typename Range, typename Func>
|
|
requires
|
|
ExceptionContainerResult<R>
|
|
&& std::is_void_v<typename R::value_type>
|
|
inline seastar::future<R> result_parallel_for_each(Range&& range, Func&& func) {
|
|
return result_parallel_for_each<R>(std::begin(range), std::end(range), std::forward<Func>(func));
|
|
}
|
|
|
|
}
|