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.facebook.presto.spi.type.BigintType.BIGINT;
 import static com.facebook.presto.spi.type.VarbinaryType.VARBINARY;
 import static com.facebook.presto.sql.planner.plan.TableWriterNode.CreateHandle;
 import static com.facebook.presto.sql.planner.plan.TableWriterNode.InsertHandle;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 
 public class TableWriterOperator
         implements Operator
 {
     public static final List<TypeTYPES = ImmutableList.<Type>of();
 
     public static class TableWriterOperatorFactory
             implements OperatorFactory
     {
         private final int operatorId;
         private final PageSinkManager pageSinkManager;
         private final WriterTarget target;
         private final List<IntegerinputChannels;
         private final Optional<IntegersampleWeightChannel;
         private boolean closed;
 
         public TableWriterOperatorFactory(int operatorIdPageSinkManager pageSinkManagerWriterTarget writerTargetList<IntegerinputChannelsOptional<IntegersampleWeightChannel)
         {
             this. = operatorId;
             this. = checkNotNull(inputChannels"inputChannels is null");
             this. = checkNotNull(pageSinkManager"pageSinkManager is null");
             checkArgument(writerTarget instanceof CreateHandle || writerTarget instanceof InsertHandle"writerTarget must be CreateHandle or InsertHandle");
             this. = checkNotNull(writerTarget"writerTarget is null");
             this. = checkNotNull(sampleWeightChannel"sampleWeightChannel is null");
         }
 
         @Override
         public List<TypegetTypes()
         {
             return ;
         }
 
         @Override
         public Operator createOperator(DriverContext driverContext)
         {
             checkState(!"Factory is already closed");
             OperatorContext context = driverContext.addOperatorContext(TableWriterOperator.class.getSimpleName());
             return new TableWriterOperator(contextcreatePageSink(), );
         }
 
         private ConnectorPageSink createPageSink()
         {
             if ( instanceof CreateHandle) {
                 return .createPageSink(((CreateHandle).getHandle());
             }
             if ( instanceof InsertHandle) {
                 return .createPageSink(((InsertHandle).getHandle());
             }
             throw new UnsupportedOperationException("Unhandled target type: " + .getClass().getName());
         }
 
         @Override
         public void close()
         {
              = true;
         }
     }
 
     private enum State
     {
         RUNNING, FINISHING, FINISHED
     }
    private final OperatorContext operatorContext;
    private final ConnectorPageSink pageSink;
    private final Optional<IntegersampleWeightChannel;
    private final List<IntegerinputChannels;
    private State state = .;
    private long rowCount;
    private boolean committed;
    private boolean closed;
    public TableWriterOperator(OperatorContext operatorContext,
            ConnectorPageSink pageSink,
            List<IntegerinputChannels,
            Optional<IntegersampleWeightChannel)
    {
        this. = checkNotNull(operatorContext"operatorContext is null");
        this. = checkNotNull(pageSink"pageSink is null");
        this. = checkNotNull(sampleWeightChannel"sampleWeightChannel is null");
        this. = checkNotNull(inputChannels"inputChannels is null");
    }
    @Override
    {
        return ;
    }
    @Override
    public List<TypegetTypes()
    {
        return ;
    }
    @Override
    public void finish()
    {
        if ( == .) {
             = .;
        }
    }
    @Override
    public boolean isFinished()
    {
        return  == .;
    }
    @Override
    public boolean needsInput()
    {
        return  == .;
    }
    @Override
    public void addInput(Page page)
    {
        checkNotNull(page"page is null");
        checkState( == ."Operator is %s");
        Block[] blocks = new Block[.size()];
        for (int outputChannel = 0; outputChannel < .size(); outputChannel++) {
            blocks[outputChannel] = page.getBlock(.get(outputChannel));
        }
        Block sampleWeightBlock = null;
        if (.isPresent()) {
            sampleWeightBlock = page.getBlock(.get());
        }
        .appendPage(new Page(blocks), sampleWeightBlock);
         += page.getPositionCount();
    }
    @Override
    public Page getOutput()
    {
        if ( != .) {
            return null;
        }
         = .;
        Collection<Slicefragments = .commit();
         = true;
        PageBuilder page = new PageBuilder();
        BlockBuilder rowsBuilder = page.getBlockBuilder(0);
        BlockBuilder fragmentBuilder = page.getBlockBuilder(1);
        // write row count
        page.declarePosition();
        .writeLong(rowsBuilder);
        fragmentBuilder.appendNull();
        // write fragments
        for (Slice fragment : fragments) {
            page.declarePosition();
            rowsBuilder.appendNull();
            .writeSlice(fragmentBuilderfragment);
        }
        return page.build();
    }
    @Override
    public void close()
            throws Exception
    {
        if (!) {
             = true;
            if (!) {
                .rollback();
            }
        }
    }
New to GrepCode? Check out our FAQ X