/*
 * Licensed to CRATE Technology 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.executor.transport.distributed;

import com.google.common.base.Throwables;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
import io.crate.Streamer;
import io.crate.core.collections.Bucket;
import io.crate.core.collections.Row;
import io.crate.executor.transport.StreamBucket;
import io.crate.jobs.ExecutionState;
import io.crate.operation.RowUpstream;
import io.crate.operation.projectors.RowReceiver;

import java.io.IOException;


public class SingleBucketBuilder implements RowReceiver {

    private final StreamBucket.Builder bucketBuilder;
    private final SettableFuture<Bucket> bucketFuture = SettableFuture.create();

    public SingleBucketBuilder(Streamer<?>[] streamers) {
        bucketBuilder = new StreamBucket.Builder(streamers);
    }

    private Bucket build() {
        try {
            return bucketBuilder.build();
        } catch (IOException e) {
            Throwables.propagate(e);
        }
        return null;
    }

    @Override
    public synchronized boolean setNextRow(Row row) {
        try {
            bucketBuilder.add(row);
        } catch (Throwable e) {
            Throwables.propagate(e);
        }
        return true;
    }

    public ListenableFuture<Bucket> result() {
        return bucketFuture;
    }

    @Override
    public void finish() {
        bucketFuture.set(build());
    }

    @Override
    public void fail(Throwable throwable) {
        bucketFuture.setException(throwable);
    }

    @Override
    public void prepare(ExecutionState executionState) {
    }

    @Override
    public void setUpstream(RowUpstream rowUpstream) {
    }
}

@@ -1860,202 +1860,8 @@
  %7D%0A%0A
-    private Bucket build() %7B%0A        try %7B%0A            return bucketBuilder.build();%0A        %7D catch (IOException e) %7B%0A            Throwables.propagate(e);%0A        %7D%0A        return null;%0A    %7D%0A%0A
     
@@ -1885,21 +1885,8 @@
 lic 
-synchronized 
 bool
@@ -2188,24 +2188,42 @@
  finish() %7B%0A
+        try %7B%0A    
         buck
@@ -2241,15 +2241,115 @@
 t(bu
-ild());
+cketBuilder.build());%0A        %7D catch (IOException e) %7B%0A            bucketFuture.setException(e);%0A        %7D
 %0A   

remove synchronization and change failure propagation