/*
 * Licensed to 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.operation.projectors;

import io.crate.data.Row;
import io.crate.data.Row1;

import java.util.function.Consumer;

class SysUpdateProjector extends AbstractProjector {

    private final Consumer<Object> rowWriter;
    private long rowCount = 0;

    SysUpdateProjector(Consumer<Object> rowWriter)  {
        this.rowWriter = rowWriter;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Result setNextRow(Row row) {
        Object id = row.get(0);
        rowWriter.accept(id);
        rowCount++;
        return Result.CONTINUE;
    }

    @Override
    public void finish(RepeatHandle repeatHandle) {
        downstream.setNextRow(new Row1(rowCount));
        downstream.finish(RepeatHandle.UNSUPPORTED);
    }

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

@@ -1057,40 +1057,195 @@
 ata.
-Row;%0Aimport io.crate.data.Row1;%0A
+BatchIteratorProjector;%0Aimport io.crate.data.CollectingBatchIterator;%0Aimport io.crate.data.Row;%0Aimport io.crate.data.Row1;%0A%0Aimport javax.annotation.Nullable;%0Aimport java.util.Collections;
 %0Aimp
@@ -1276,16 +1276,51 @@
 onsumer;
+%0Aimport java.util.stream.Collector;
 %0A%0Aclass 
@@ -1416,39 +1416,8 @@
 ter;
-%0A    private long rowCount = 0;
 %0A%0A  
@@ -1612,61 +1612,184 @@
     
-Object id = row.get(0
+throw new UnsupportedOperationException(%22SysUpdateProjector should be used as BatchIteratorProjector%22
 );%0A    
+%7D%0A%0A
     
-rowWriter.accept(id);
+@Override%0A    public void finish(RepeatHandle repeatHandle) %7B
 %0A   
@@ -1797,172 +1797,629 @@
     
+th
 row
-Count++;%0A        return Result.CONTINUE;%0A    %7D%0A%0A    @Override%0A    public void finish(RepeatHandle repeatHandle) %7B%0A        downstream.setNextRow(new Row1(
+ new UnsupportedOperationException(%22SysUpdateProjector should be used as BatchIteratorProjector%22);%0A    %7D%0A%0A    @Override%0A    public void fail(Throwable throwable) %7B%0A        throw new UnsupportedOperationException(%22SysUpdateProjector should be used as BatchIteratorProjector%22);%0A    %7D%0A%0A    @Nullable%0A    @Override%0A    public BatchIteratorProjector asProjector() %7B%0A        return bi -%3E CollectingBatchIterator.newInstance(bi,%0A            Collector.of(%0A                () -%3E new State(rowWriter),%0A                (state, row) -%3E %7B%0A                    state.rowWriter.accept(row.get(0));%0A                    state.
 rowCount
 ));%0A
@@ -2414,18 +2414,18 @@
 rowCount
-))
+++
 ;%0A      
@@ -2430,153 +2430,425 @@
     
-downstream.finish(RepeatHandle.UNSUPPORTED);%0A    %7D%0A%0A    @Override%0A    public void fail(Throwable throwable) %7B%0A        downstream.fail(throwable);
+        %7D,%0A                (state1, state2) -%3E %7B throw new UnsupportedOperationException(%22Combine not supported%22); %7D,%0A                state -%3E Collections.singletonList(new Row1(state.rowCount))%0A            ), 1);%0A    %7D%0A%0A    private static class State %7B%0A        final Consumer%3CObject%3E rowWriter;%0A        long rowCount = 0;%0A%0A        State(Consumer%3CObject%3E rowWriter) %7B%0A            this.rowWriter = rowWriter;%0A        %7D
 %0A   

Add BatchIteratorProjector implementation to SysUpdateProjector