westonpace commented on a change in pull request #9533:
URL: https://github.com/apache/arrow/pull/9533#discussion_r592263787



##########
File path: cpp/src/arrow/util/async_generator.h
##########
@@ -177,6 +179,94 @@ class TransformingGenerator {
   std::shared_ptr<TransformingGeneratorState> state_;
 };
 
+template <typename T>
+class SerialReadaheadGenerator {
+ public:
+  SerialReadaheadGenerator(AsyncGenerator<T> source_generator, int 
max_readahead)
+      : state_(std::make_shared<State>(std::move(source_generator), 
max_readahead)) {}
+
+  Future<T> operator()() {
+    if (state_->first) {
+      // Lazy generator, need to wait for the first ask to prime the pump
+      state_->first = false;
+      auto next = state_->source();
+      return next.Then(Callback{state_});
+    }
+
+    // This generator is not async-reentrant.  We won't be called until the 
last
+    // future finished so we know there is something in the queue
+    auto finished = state_->finished.load();
+    if (finished && state_->readahead_queue.IsEmpty()) {
+      return Future<T>::MakeFinished(IterationTraits<T>::End());
+    }
+
+    auto next_ptr = state_->readahead_queue.FrontPtr();
+    auto next = std::move(**next_ptr);
+    state_->readahead_queue.PopFront();
+
+    auto last_available = state_->spaces_available.fetch_add(1);
+    if (last_available == 0 && !finished) {
+      // Reader idled out, we need to restart it

Review comment:
       So there are two ends to this operator, the consumer and the producer.  
The consumer pulls items off the readahead queue while the producer is putting 
items on the readahead queue.  When the producer adds an item they check to see 
if they filled up the queue.  If they did then they stop (and it will need to 
be restarted here).  If they did not then they call State::Pump and start 
reading the next item.
   
   If the producer stopped and the consumer later grabs an item off the queue 
then it needs to start the process up again and call State::Pump which is what 
is happening here.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to