Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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;
 
 
 import java.util.List;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 
 public class OrderByOperator
         implements Operator
 {
     public static class OrderByOperatorFactory
             implements OperatorFactory
     {
         private final int operatorId;
         private final List<TypesortTypes;
         private final List<TypesourceTypes;
         private final List<IntegeroutputChannels;
         private final int expectedPositions;
         private final List<IntegersortChannels;
         private final List<SortOrdersortOrder;
         private final List<Typetypes;
         private boolean closed;
 
         public OrderByOperatorFactory(
                 int operatorId,
                 List<? extends TypesourceTypes,
                 List<IntegeroutputChannels,
                 int expectedPositions,
                 List<IntegersortChannels,
                 List<SortOrdersortOrder)
         {
             this. = operatorId;
             this. = ImmutableList.copyOf(checkNotNull(sourceTypes"sourceTypes is null"));
             ImmutableList.Builder<TypesortTypes = ImmutableList.builder();
             for (int channel : sortChannels) {
                 sortTypes.add(sourceTypes.get(channel));
             }
             this. = sortTypes.build();
             this. = checkNotNull(outputChannels"outputChannels is null");
             this. = expectedPositions;
             this. = ImmutableList.copyOf(checkNotNull(sortChannels"sortChannels is null"));
             this. = ImmutableList.copyOf(checkNotNull(sortOrder"sortOrder is null"));
 
             this. = toTypes(sourceTypesoutputChannels);
         }
 
         @Override
         public List<TypegetTypes()
         {
             return ;
         }
 
         @Override
         public Operator createOperator(DriverContext driverContext)
         {
             checkState(!"Factory is already closed");
 
             OperatorContext operatorContext = driverContext.addOperatorContext(OrderByOperator.class.getSimpleName());
             return new OrderByOperator(
                     operatorContext,
                     ,
                     ,
                     ,
                     ,
                     ,
                     );
         }
 
         @Override
         public void close()
         {
              = true;
         }
     }
 
     private enum State
     {
         NEEDS_INPUT,
         HAS_OUTPUT,
        FINISHED
    }
    private final OperatorContext operatorContext;
    private final List<TypesortTypes;
    private final List<IntegersortChannels;
    private final List<SortOrdersortOrder;
    private final int[] outputChannels;
    private final List<Typetypes;
    private final PagesIndex pageIndex;
    private final PageBuilder pageBuilder;
    private int currentPosition;
    private State state = .;
    public OrderByOperator(
            OperatorContext operatorContext,
            List<TypesourceTypes,
            List<IntegeroutputChannels,
            int expectedPositions,
            List<TypesortTypes,
            List<IntegersortChannels,
            List<SortOrdersortOrder)
    {
        this. = checkNotNull(operatorContext"operatorContext is null");
        this. = Ints.toArray(checkNotNull(outputChannels"outputChannels is null"));
        this. = toTypes(sourceTypesoutputChannels);
        this. = ImmutableList.copyOf(checkNotNull(sortTypes"sortTypes is null"));
        this. = ImmutableList.copyOf(checkNotNull(sortChannels"sortChannels is null"));
        this. = ImmutableList.copyOf(checkNotNull(sortOrder"sortOrder is null"));
        this. = new PagesIndex(sourceTypesexpectedPositions);
        this. = new PageBuilder(this.);
    }
    @Override
    {
        return ;
    }
    @Override
    public List<TypegetTypes()
    {
        return ;
    }
    @Override
    public void finish()
    {
        if ( == .) {
             = .;
            // sort the index
            .sort();
        }
    }
    @Override
    public boolean isFinished()
    {
        return  == .;
    }
    @Override
    public boolean needsInput()
    {
        return  == .;
    }
    @Override
    public void addInput(Page page)
    {
        checkState( == ."Operator is already finishing");
        checkNotNull(page"page is null");
        .addPage(page);
    }
    @Override
    public Page getOutput()
    {
        if ( != .) {
            return null;
        }
        if ( >= .getPositionCount()) {
             = .;
            return null;
        }
        // iterate through the positions sequentially until we have one full page
        .reset();
        // output the page if we have any data
        if (.isEmpty()) {
             = .;
            return null;
        }
        Page page = .build();
        return page;
    }
    private static List<TypetoTypes(List<? extends TypesourceTypesList<IntegeroutputChannels)
    {
        ImmutableList.Builder<Typetypes = ImmutableList.builder();
        for (int channel : outputChannels) {
            types.add(sourceTypes.get(channel));
        }
        return types.build();
    }
New to GrepCode? Check out our FAQ X