proxygen
folly::fibers::AtomicBatchDispatcher< InputT, ResultT >::DispatchBaton< InputT, ResultT > Struct Template Reference

#include <AtomicBatchDispatcher-inl.h>

Classes

struct  Entry
 

Public Member Functions

 DispatchBaton (DispatchFunctionT &&dispatchFunction)
 
 ~DispatchBaton ()
 
void reserve (size_t numEntries)
 
void setExceptionWrapper (folly::exception_wrapper &&exWrapper)
 
void setExpectedCount (size_t expectedCount)
 
Future< ResultT > getFutureResult (InputT &&input, size_t sequenceNumber)
 

Private Member Functions

void setExceptionResults (const folly::exception_wrapper &exceptionWrapper)
 
void setExceptionResults (std::exception_ptr eptr)
 
template<typename TException >
void setExceptionResults (const TException &ex, std::exception_ptr eptr=std::exception_ptr())
 
void fulfillPromises ()
 

Private Attributes

size_t expectedCount_
 
DispatchFunctionT dispatchFunction_
 
std::vector< folly::Optional< Entry > > optEntries_
 
folly::exception_wrapper exceptionWrapper_
 

Detailed Description

template<typename InputT, typename ResultT>
template<typename InputT, typename ResultT>
struct folly::fibers::AtomicBatchDispatcher< InputT, ResultT >::DispatchBaton< InputT, ResultT >

Definition at line 20 of file AtomicBatchDispatcher-inl.h.

Constructor & Destructor Documentation

template<typename InputT, typename ResultT>
template<typename InputT , typename ResultT >
folly::fibers::AtomicBatchDispatcher< InputT, ResultT >::DispatchBaton< InputT, ResultT >::DispatchBaton ( DispatchFunctionT &&  dispatchFunction)
inline
template<typename InputT, typename ResultT>
template<typename InputT , typename ResultT >
folly::fibers::AtomicBatchDispatcher< InputT, ResultT >::DispatchBaton< InputT, ResultT >::~DispatchBaton ( )
inline

Member Function Documentation

template<typename InputT, typename ResultT>
template<typename InputT , typename ResultT >
void folly::fibers::AtomicBatchDispatcher< InputT, ResultT >::DispatchBaton< InputT, ResultT >::fulfillPromises ( )
inlineprivate

Definition at line 75 of file AtomicBatchDispatcher-inl.h.

References folly::fibers::detail::createABDTokenNotDispatchedExMsg(), folly::fibers::detail::createUnexpectedNumResultsABDUsageExMsg(), i, and folly::gen::move.

75  {
76  try {
77  // If an error message is set, set all promises to exception with message
78  if (exceptionWrapper_) {
80  }
81 
82  // Validate entries count same as expectedCount_
83  assert(
84  optEntries_.size() == expectedCount_ ||
85  !"Entries vector did not have expected size");
86  std::vector<size_t> vecTokensNotDispatched;
87  for (size_t i = 0; i < expectedCount_; ++i) {
88  if (!optEntries_[i]) {
89  vecTokensNotDispatched.push_back(i);
90  }
91  }
92  if (!vecTokensNotDispatched.empty()) {
93  return setExceptionResults(ABDTokenNotDispatchedException(
94  detail::createABDTokenNotDispatchedExMsg(vecTokensNotDispatched)));
95  }
96 
97  // Create the inputs vector
98  std::vector<InputT> inputs;
99  inputs.reserve(expectedCount_);
100  for (auto& optEntry : optEntries_) {
101  inputs.emplace_back(std::move(optEntry->input));
102  }
103 
104  // Call the user provided batch dispatch function to get all results
105  // and make sure that we have the expected number of results returned
106  auto results = dispatchFunction_(std::move(inputs));
107  if (results.size() != expectedCount_) {
108  return setExceptionResults(
110  expectedCount_, results.size())));
111  }
112 
113  // Fulfill the promises with the results from the batch dispatch
114  for (size_t i = 0; i < expectedCount_; ++i) {
115  optEntries_[i]->promise.setValue(std::move(results[i]));
116  }
117  } catch (const std::exception& ex) {
118  // Set exceptions thrown when executing the user provided dispatch func
119  return setExceptionResults(ex, std::current_exception());
120  } catch (...) {
121  // Set exceptions thrown when executing the user provided dispatch func
122  return setExceptionResults(std::current_exception());
123  }
124  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::string createABDTokenNotDispatchedExMsg(const std::vector< size_t > &vecTokensNotDispatched)
void setExceptionResults(const folly::exception_wrapper &exceptionWrapper)
std::string createUnexpectedNumResultsABDUsageExMsg(size_t numExpectedResults, size_t numActualResults)
template<typename InputT, typename ResultT>
template<typename InputT , typename ResultT >
Future<ResultT> folly::fibers::AtomicBatchDispatcher< InputT, ResultT >::DispatchBaton< InputT, ResultT >::getFutureResult ( InputT &&  input,
size_t  sequenceNumber 
)
inline

Definition at line 42 of file AtomicBatchDispatcher-inl.h.

References folly::gen::move.

42  {
43  if (sequenceNumber >= optEntries_.size()) {
44  optEntries_.resize(sequenceNumber + 1);
45  }
46  folly::Optional<Entry>& optEntry = optEntries_[sequenceNumber];
47  assert(!optEntry || !"Multiple inputs have the same token sequence number");
48  optEntry = Entry(std::move(input));
49  return optEntry->promise.getFuture();
50  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
Future< T > getFuture()
Definition: Promise-inl.h:97
template<typename InputT, typename ResultT>
template<typename InputT , typename ResultT >
void folly::fibers::AtomicBatchDispatcher< InputT, ResultT >::DispatchBaton< InputT, ResultT >::reserve ( size_t  numEntries)
inline

Definition at line 28 of file AtomicBatchDispatcher-inl.h.

28  {
29  optEntries_.reserve(numEntries);
30  }
template<typename InputT, typename ResultT>
template<typename InputT , typename ResultT >
void folly::fibers::AtomicBatchDispatcher< InputT, ResultT >::DispatchBaton< InputT, ResultT >::setExceptionResults ( const folly::exception_wrapper exceptionWrapper)
inlineprivate

Definition at line 53 of file AtomicBatchDispatcher-inl.h.

53  {
54  for (auto& optEntry : optEntries_) {
55  if (optEntry) {
56  optEntry->promise.setException(exceptionWrapper);
57  }
58  }
59  }
template<typename InputT, typename ResultT>
template<typename InputT , typename ResultT >
void folly::fibers::AtomicBatchDispatcher< InputT, ResultT >::DispatchBaton< InputT, ResultT >::setExceptionResults ( std::exception_ptr  eptr)
inlineprivate

Definition at line 61 of file AtomicBatchDispatcher-inl.h.

61  {
62  auto exceptionWrapper = exception_wrapper(eptr);
63  return setExceptionResults(exceptionWrapper);
64  }
void setExceptionResults(const folly::exception_wrapper &exceptionWrapper)
template<typename InputT, typename ResultT>
template<typename InputT , typename ResultT >
template<typename TException >
void folly::fibers::AtomicBatchDispatcher< InputT, ResultT >::DispatchBaton< InputT, ResultT >::setExceptionResults ( const TException &  ex,
std::exception_ptr  eptr = std::exception_ptr() 
)
inlineprivate

Definition at line 67 of file AtomicBatchDispatcher-inl.h.

69  {
70  auto exceptionWrapper =
71  eptr ? exception_wrapper(eptr, ex) : exception_wrapper(ex);
72  return setExceptionResults(exceptionWrapper);
73  }
void setExceptionResults(const folly::exception_wrapper &exceptionWrapper)
template<typename InputT, typename ResultT>
template<typename InputT , typename ResultT >
void folly::fibers::AtomicBatchDispatcher< InputT, ResultT >::DispatchBaton< InputT, ResultT >::setExceptionWrapper ( folly::exception_wrapper &&  exWrapper)
inline

Definition at line 32 of file AtomicBatchDispatcher-inl.h.

References folly::gen::move.

32  {
33  exceptionWrapper_ = std::move(exWrapper);
34  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
template<typename InputT, typename ResultT>
template<typename InputT , typename ResultT >
void folly::fibers::AtomicBatchDispatcher< InputT, ResultT >::DispatchBaton< InputT, ResultT >::setExpectedCount ( size_t  expectedCount)
inline

Definition at line 36 of file AtomicBatchDispatcher-inl.h.

36  {
37  assert(expectedCount_ == 0 || !"expectedCount_ being set more than once");
38  expectedCount_ = expectedCount;
40  }

Member Data Documentation

template<typename InputT, typename ResultT>
template<typename InputT , typename ResultT >
DispatchFunctionT folly::fibers::AtomicBatchDispatcher< InputT, ResultT >::DispatchBaton< InputT, ResultT >::dispatchFunction_
private

Definition at line 143 of file AtomicBatchDispatcher-inl.h.

template<typename InputT, typename ResultT>
template<typename InputT , typename ResultT >
folly::exception_wrapper folly::fibers::AtomicBatchDispatcher< InputT, ResultT >::DispatchBaton< InputT, ResultT >::exceptionWrapper_
private

Definition at line 145 of file AtomicBatchDispatcher-inl.h.

template<typename InputT, typename ResultT>
template<typename InputT , typename ResultT >
size_t folly::fibers::AtomicBatchDispatcher< InputT, ResultT >::DispatchBaton< InputT, ResultT >::expectedCount_
private

Definition at line 142 of file AtomicBatchDispatcher-inl.h.

template<typename InputT, typename ResultT>
template<typename InputT , typename ResultT >
std::vector<folly::Optional<Entry> > folly::fibers::AtomicBatchDispatcher< InputT, ResultT >::DispatchBaton< InputT, ResultT >::optEntries_
private

Definition at line 144 of file AtomicBatchDispatcher-inl.h.


The documentation for this struct was generated from the following file: