/*
 * Licensed to Crate.IO GmbH ("Crate") under one or more contributor
 * license agreements.  See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.  Crate licenses
 * this file to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.  You may
 * obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *
 * However, if you have executed another commercial license agreement
 * with Crate these terms will supersede the license and you may use the
 * software solely pursuant to the terms of the relevant commercial agreement.
 */

package io.crate.testing;

import io.crate.core.collections.Row;
import io.crate.operation.RowUpstream;
import io.crate.operation.projectors.RowReceiver;
import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException;

import java.util.Iterator;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;

public class RowSender implements Runnable, RowUpstream {

    private final RowReceiver downstream;
    private final AtomicBoolean paused = new AtomicBoolean(false);
    private final Executor executor;
    private volatile boolean pendingPause = false;

    private volatile int numPauses = 0;
    private volatile int numResumes = 0;
    private Iterator<Row> iterator;

    public RowSender(Iterable<Row> rows, RowReceiver rowReceiver, Executor executor) {
        this.executor = executor;
        downstream = rowReceiver;
        rowReceiver.setUpstream(this);
        iterator = rows.iterator();
    }

    @Override
    public void run() {
        while (iterator.hasNext()) {
            final boolean wantsMore = downstream.setNextRow(iterator.next());
            if (!wantsMore) {
                break;
            }
            if (processPause()) return;
        }
        downstream.finish();
   }

    private boolean processPause() {
        if (pendingPause) {
            numPauses++;
            paused.set(true);
            pendingPause = false;
            return true;
        }
        return false;
    }

    @Override
    public void pause() {
        pendingPause = true;
    }

    @Override
    public void resume(boolean async) {
        pendingPause = false;
        if (paused.compareAndSet(true, false)) {
            numResumes++;
            if (async) {
                try {
                    executor.execute(this);
                } catch (EsRejectedExecutionException | RejectedExecutionException e) {
                    run();
                }
            } else {
                run();
            }
        }
    }

    public int numPauses() {
        return numPauses;
    }

    public int numResumes() {
        return numResumes;
    }
}

@@ -1142,537 +1142,308 @@
 ion.
-projectors.RowReceiver;%0Aimport org.elasticsearch.common.util.concurrent.EsRejectedExecutionException;%0A%0Aimport java.util.Iterator;%0Aimport java.util.concurrent.Executor;%0Aimport java.util.concurrent.RejectedExecutionException;%0Aimport java.util.concurrent.atomic.AtomicBoolean;%0A%0Apublic class RowSender implements Runnable, RowUpstream %7B%0A%0A    private final RowReceiver downstream;%0A    private final AtomicBoolean paused = new AtomicBoolean(false);%0A    private final Executor executor;%0A    private volatile boolean pendingPause = false
+collect.collectors.TopRowUpstream;%0Aimport io.crate.operation.projectors.RowReceiver;%0A%0Aimport java.util.Iterator;%0Aimport java.util.concurrent.Executor;%0A%0Apublic class RowSender implements Runnable, RowUpstream %7B%0A%0A    private final RowReceiver downstream;%0A    private final TopRowUpstream topRowUpstream
 ;%0A%0A 
@@ -1658,31 +1658,31 @@
     
-this.executor = executo
+downstream = rowReceive
 r;%0A 
@@ -1680,36 +1680,40 @@
 ceiver;%0A        
-down
+topRowUp
 stream = rowRece
@@ -1701,35 +1701,216 @@
 wUpstream = 
-rowReceiver
+new TopRowUpstream(executor, new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                numResumes++;%0A                RowSender.this.run();%0A            %7D%0A        %7D)
 ;%0A        ro
@@ -2335,28 +2335,44 @@
     if (
-pending
+topRowUpstream.should
 Pause
+()
 ) %7B%0A    
@@ -2408,58 +2408,39 @@
     
-paused.set(true);%0A            pendingPause = false
+topRowUpstream.pauseProcessed()
 ;%0A  
@@ -2553,27 +2553,31 @@
     
-pendingPause = true
+topRowUpstream.pause();
 ;%0A  
@@ -2647,396 +2647,37 @@
     
-pendingPause = false;%0A        if (paused.compareAndSet(true, false)) %7B%0A            numResumes++;%0A            if (async) %7B%0A                try %7B%0A                    executor.execute(this);%0A                %7D catch (EsRejectedExecutionException %7C RejectedExecutionException e) %7B%0A                    run();%0A                %7D%0A            %7D else %7B%0A                run();%0A            %7D%0A        %7D
+topRowUpstream.resume(async);
 %0A   

use TopRowUpstream in RowSender