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.index;
 
 
 import java.util.List;
 
 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 IndexSnapshotBuilder
 {
     private final int expectedPositions;
     private final List<TypeoutputTypes;
     private final List<TypemissingKeysTypes;
     private final List<IntegerkeyOutputChannels;
     private final Optional<IntegerkeyOutputHashChannel;
     private final List<IntegermissingKeysChannels;
 
     private final long maxMemoryInBytes;
     private final OperatorContext bogusOperatorContext;
     private PagesIndex outputPagesIndex;
     private PagesIndex missingKeysIndex;
     private LookupSource missingKeys;
 
     private final List<Pagepages = new ArrayList<>();
     private long memoryInBytes;
 
     public IndexSnapshotBuilder(List<TypeoutputTypes,
             List<IntegerkeyOutputChannels,
             Optional<IntegerkeyOutputHashChannel,
             DriverContext driverContext,
             DataSize maxMemoryInBytes,
             int expectedPositions)
     {
         checkNotNull(outputTypes"outputTypes is null");
         checkNotNull(keyOutputChannels"keyOutputChannels is null");
         checkNotNull(keyOutputHashChannel"keyOutputHashChannel is null");
         checkNotNull(driverContext"driverContext is null");
         checkNotNull(maxMemoryInBytes"maxMemoryInBytes is null");
         checkArgument(expectedPositions > 0, "expectedPositions must be greater than zero");
 
         this. = ImmutableList.copyOf(outputTypes);
         this. = expectedPositions;
         this. = ImmutableList.copyOf(keyOutputChannels);
         this. = keyOutputHashChannel;
         this. = maxMemoryInBytes.toBytes();
 
         ImmutableList.Builder<TypemissingKeysTypes = ImmutableList.builder();
         ImmutableList.Builder<IntegermissingKeysChannels = ImmutableList.builder();
         for (int i = 0; i < keyOutputChannels.size(); i++) {
             Integer keyOutputChannel = keyOutputChannels.get(i);
             missingKeysTypes.add(outputTypes.get(keyOutputChannel));
             missingKeysChannels.add(i);
         }
         this. = missingKeysTypes.build();
         this. = missingKeysChannels.build();
 
         // create a bogus operator context with unlimited memory for the pages index
         boolean cpuTimerEnabled = driverContext.getPipelineContext().getTaskContext().isCpuTimerEnabled();
         this. = new TaskContext(driverContext.getTaskId(), driverContext.getExecutor(), driverContext.getSession(), new DataSize(..), cpuTimerEnabled)
                 .addPipelineContext(truetrue)
                 .addDriverContext()
                 .addOperatorContext(0, "operator");
 
         this. = new PagesIndex(outputTypesexpectedPositions);
         this. = new PagesIndex(missingKeysTypes.build(), expectedPositions);
     }
 
     public List<TypegetOutputTypes()
     {
        return ;
    }
    public long getMemoryInBytes()
    {
        return ;
    }
    public boolean isMemoryExceeded()
    {
        return  > ;
    }
    public boolean tryAddPage(Page page)
    {
         += page.getSizeInBytes();
        if (isMemoryExceeded()) {
            return false;
        }
        .add(page);
        return true;
    }
    public IndexSnapshot createIndexSnapshot(UnloadedIndexKeyRecordSet indexKeysRecordSet)
    {
        checkArgument(indexKeysRecordSet.getColumnTypes().equals(), "indexKeysRecordSet must have same schema as missingKeys");
        checkState(!isMemoryExceeded(), "Max memory exceeded");
        for (Page page : ) {
            .addPage(page);
        }
        .clear();
        // Build a page containing the keys that produced no output rows, so in future requests can skip these keys
        PageBuilder missingKeysPageBuilder = new PageBuilder(.getTypes());
        UnloadedIndexKeyRecordCursor indexKeysRecordCursor = indexKeysRecordSet.cursor();
        while (indexKeysRecordCursor.advanceNextPosition()) {
            Block[] blocks = indexKeysRecordCursor.getBlocks();
            Page page = indexKeysRecordCursor.getPage();
            int position = indexKeysRecordCursor.getPosition();
            if (lookupSource.getJoinPosition(positionpage) < 0) {
                missingKeysPageBuilder.declarePosition();
                for (int i = 0; i < blocks.lengthi++) {
                    Block block = blocks[i];
                    Type type = indexKeysRecordCursor.getType(i);
                    type.appendTo(blockpositionmissingKeysPageBuilder.getBlockBuilder(i));
                }
            }
        }
        Page missingKeysPage = missingKeysPageBuilder.build();
         += missingKeysPage.getSizeInBytes();
        if (isMemoryExceeded()) {
            return null;
        }
        // only update missing keys if we have new missing keys
        if (!missingKeysPageBuilder.isEmpty()) {
            .addPage(missingKeysPage);
        }
        return new IndexSnapshot(lookupSource);
    }
    public void reset()
    {
         = 0;
        .clear();
    }
New to GrepCode? Check out our FAQ X