| 554 | : state_(std::make_shared<State>(std::move(source_generator), max_readahead)) {} |
| 555 | |
| 556 | Future<T> operator()() { |
| 557 | if (state_->first_) { |
| 558 | // Lazy generator, need to wait for the first ask to prime the pump |
| 559 | state_->first_ = false; |
| 560 | auto next = state_->source_(); |
| 561 | return next.Then(Callback{state_}, ErrCallback{state_}); |
| 562 | } |
| 563 | |
| 564 | // This generator is not async-reentrant. We won't be called until the last |
| 565 | // future finished so we know there is something in the queue |
| 566 | auto finished = state_->finished_.load(); |
| 567 | if (finished && state_->readahead_queue_.IsEmpty()) { |
| 568 | return AsyncGeneratorEnd<T>(); |
| 569 | } |
| 570 | |
| 571 | std::shared_ptr<Future<T>> next; |
| 572 | if (!state_->readahead_queue_.Read(next)) { |
| 573 | return Status::UnknownError("Could not read from readahead_queue"); |
| 574 | } |
| 575 | |
| 576 | auto last_available = state_->spaces_available_.fetch_add(1); |
| 577 | if (last_available == 0 && !finished) { |
| 578 | // Reader idled out, we need to restart it |
| 579 | ARROW_RETURN_NOT_OK(state_->Pump(state_)); |
| 580 | } |
| 581 | return *next; |
| 582 | } |
| 583 | |
| 584 | private: |
| 585 | struct State { |
nothing calls this directly
no test coverage detected