/*
 * Licensed 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.
 */
package com.facebook.presto.operator.exchange;

import com.facebook.presto.common.Page;
import com.facebook.presto.operator.PartitionFunction;
import com.facebook.presto.operator.exchange.PageReference.PageReleasedListener;
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Ints;
import com.google.common.util.concurrent.ListenableFuture;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;

import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

import static java.util.Objects.requireNonNull;

class PartitioningExchanger
        implements LocalExchanger
{
    private final List<Consumer<PageReference>> buffers;
    private final LocalExchangeMemoryManager memoryManager;
    private final PartitionFunction partitionFunction;
    private final int[] partitioningChannels;
    private final Optional<Integer> hashChannel;
    private final IntArrayList[] partitionAssignments;
    private final PageReleasedListener onPageReleased;

    public PartitioningExchanger(
            List<Consumer<PageReference>> partitions,
            LocalExchangeMemoryManager memoryManager,
            PartitionFunction partitionFunction,
            List<Integer> partitioningChannels,
            Optional<Integer> hashChannel)
    {
        this.buffers = ImmutableList.copyOf(requireNonNull(partitions, "partitions is null"));
        this.memoryManager = requireNonNull(memoryManager, "memoryManager is null");
        this.partitionFunction = requireNonNull(partitionFunction, "partitionFunction is null");
        this.partitioningChannels = Ints.toArray(requireNonNull(partitioningChannels, "partitioningChannels is null"));
        this.hashChannel = requireNonNull(hashChannel, "hashChannel is null");
        this.onPageReleased = PageReleasedListener.forLocalExchangeMemoryManager(memoryManager);

        partitionAssignments = new IntArrayList[partitions.size()];
        for (int i = 0; i < partitionAssignments.length; i++) {
            partitionAssignments[i] = new IntArrayList();
        }
    }

    @Override
    public synchronized void accept(Page page)
    {
        // reset the assignment lists
        for (IntList partitionAssignment : partitionAssignments) {
            partitionAssignment.clear();
        }

        // assign each row to a partition
        Page partitioningChannelsPage = extractPartitioningChannels(page);
        for (int position = 0; position < partitioningChannelsPage.getPositionCount(); position++) {
            int partition = partitionFunction.getPartition(partitioningChannelsPage, position);
            partitionAssignments[partition].add(position);
        }

        // build a page for each partition
        for (int partition = 0; partition < buffers.size(); partition++) {
            IntArrayList positions = partitionAssignments[partition];
            if (!positions.isEmpty()) {
                Page pageSplit = page.copyPositions(positions.elements(), 0, positions.size());
                memoryManager.updateMemoryUsage(pageSplit.getRetainedSizeInBytes());
                buffers.get(partition).accept(new PageReference(pageSplit, 1, onPageReleased));
            }
        }
    }

    private Page extractPartitioningChannels(Page inputPage)
    {
        // hash value is pre-computed, only needs to extract that channel
        if (hashChannel.isPresent()) {
            return new Page(inputPage.getBlock(hashChannel.get()));
        }

        // extract partitioning channels
        return inputPage.extractChannels(partitioningChannels);
    }

    @Override
    public ListenableFuture<?> waitForWriting()
    {
        return memoryManager.getNotFullFuture();
    }
}

@@ -979,51 +979,8 @@
 ist;
-%0Aimport it.unimi.dsi.fastutil.ints.IntList;
 %0A%0Aim
@@ -1417,38 +1417,94 @@
 nal 
-Optional%3CInteger%3E hashChannel;
+int hashChannel; // when %3E= 0, this is the precomputed raw hash column to partition on
 %0A   
@@ -2365,32 +2365,43 @@
 hannel is null%22)
+.orElse(-1)
 ;%0A        this.o
@@ -2720,21 +2720,8 @@
 lic 
-synchronized 
 void
@@ -2760,270 +2760,360 @@
  // 
-reset the assignment lists%0A        for (IntList partitionAssignment : partitionAssignments) %7B
+extract the partitioning channel before entering the critical section%0A        partitionPage(page, extractPartitioningChannels(page));
 %0A    
+%7D%0A%0A
     
-    partitionAssignment.clear();%0A        %7D%0A%0A        // assign each row to a partition%0A        Page partitioningChannelsPage = extractPartitioningChannels(page);
+private synchronized void partitionPage(Page page, Page partitioningChannelsPage)%0A    %7B%0A        // assign each row to a partition. The assignments lists are all expected to cleared by the previous iterations
 %0A   
@@ -3467,22 +3467,35 @@
 n %3C 
-buffers.size()
+partitionAssignments.length
 ; pa
@@ -3594,29 +3594,205 @@
    i
-f (!positions.isEmpty
+nt partitionSize = positions.size();%0A            if (partitionSize == 0) %7B%0A                continue;%0A            %7D%0A            Page pageSplit;%0A            if (partitionSize == page.getPositionCount
 ()) 
@@ -3813,92 +3813,346 @@
     
-Page pageSplit = page.copyPositions(positions.elements(), 0, positions.size());%0A    
+pageSplit = page; // entire page will be sent to this partition, no copies necessary%0A            %7D%0A            else %7B%0A                pageSplit = page.copyPositions(positions.elements(), 0, partitionSize);%0A            %7D%0A            // clear the assigned positions list for the next iteration to start empty%0A            positions.clear();%0A
     
@@ -4224,28 +4224,24 @@
 InBytes());%0A
-    
             
@@ -4320,30 +4320,16 @@
 ased));%0A
-            %7D%0A
         
@@ -4505,20 +4505,13 @@
 nnel
-.isPresent()
+ %3E= 0
 ) %7B%0A
@@ -4533,17 +4533,8 @@
 urn 
-new Page(
 inpu
@@ -4543,16 +4543,22 @@
 age.
-getBlock
+extractChannel
 (has
@@ -4569,15 +4569,8 @@
 nnel
-.get())
 );%0A 

Improve PartitioningExchanger performance