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.operator.SyntheticAddress.decodePosition;
 import static com.facebook.presto.operator.SyntheticAddress.decodeSliceIndex;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static io.airlift.slice.SizeOf.sizeOfIntArray;
 
 // This implementation assumes arrays used in the hash are always a power of 2
 public final class InMemoryJoinHash
         implements LookupSource
 {
     private final LongArrayList addresses;
     private final PagesHashStrategy pagesHashStrategy;
 
     private final int channelCount;
     private final int mask;
     private final int[] key;
     private final int[] positionLinks;
     private final List<TypehashTypes;
 
     public InMemoryJoinHash(LongArrayList addressesList<TypehashTypesPagesHashStrategy pagesHashStrategyOperatorContext operatorContext)
     {
         this. = checkNotNull(addresses"addresses is null");
         this. = ImmutableList.copyOf(checkNotNull(hashTypes"hashTypes is null"));
         this. = checkNotNull(pagesHashStrategy"pagesHashStrategy is null");
         this. = pagesHashStrategy.getChannelCount();
 
         checkNotNull(operatorContext"operatorContext is null");
 
         // reserve memory for the arrays
         int hashSize = HashCommon.arraySize(addresses.size(), 0.75f);
         operatorContext.reserveMemory(sizeOfIntArray(hashSize) + sizeOfIntArray(addresses.size()));
 
          = hashSize - 1;
          = new int[hashSize];
         Arrays.fill(, -1);
 
         this. = new int[addresses.size()];
         Arrays.fill(, -1);
 
         // index pages
         for (int position = 0; position < addresses.size(); position++) {
             int pos = getHashPosition(hashPosition(position), );
 
             // look for an empty slot or a slot containing this key
             while ([pos] != -1) {
                 int currentKey = [pos];
                 if (positionEqualsPosition(currentKeyposition)) {
                     // found a slot for this key
                     // link the new key position to the current key position
                     [position] = currentKey;
 
                     // key[pos] updated outside of this loop
                     break;
                 }
                 // increment position and mask to handler wrap around
                 pos = (pos + 1) & ;
             }
 
             [pos] = position;
         }
     }
 
     @Override
     public final int getChannelCount()
     {
         return ;
     }
 
     @Override
     public long getJoinPosition(int positionPage page)
     {
         return getJoinPosition(positionpage.hashRow(positionpage.getBlocks()));
    }
    @Override
    public long getJoinPosition(int positionPage pageint rawHash)
    {
        int pos = getHashPosition(rawHash);
        while ([pos] != -1) {
            if (positionEqualsCurrentRow([pos], positionpage.getBlocks())) {
                return [pos];
            }
            // increment position and mask to handler wrap around
            pos = (pos + 1) & ;
        }
        return -1;
    }
    @Override
    public final long getNextJoinPosition(long currentPosition)
    {
        return [Ints.checkedCast(currentPosition)];
    }
    @Override
    public void appendTo(long positionPageBuilder pageBuilderint outputChannelOffset)
    {
        long pageAddress = .getLong(Ints.checkedCast(position));
        int blockIndex = decodeSliceIndex(pageAddress);
        int blockPosition = decodePosition(pageAddress);
        .appendTo(blockIndexblockPositionpageBuilderoutputChannelOffset);
    }
    @Override
    public void close()
    {
    }
    private int hashPosition(int position)
    {
        long pageAddress = .getLong(position);
        int blockIndex = decodeSliceIndex(pageAddress);
        int blockPosition = decodePosition(pageAddress);
        return .hashPosition(blockIndexblockPosition);
    }
    private boolean positionEqualsCurrentRow(int leftPositionint rightPositionBlock... rightBlocks)
    {
        long pageAddress = .getLong(leftPosition);
        int blockIndex = decodeSliceIndex(pageAddress);
        int blockPosition = decodePosition(pageAddress);
        return .positionEqualsRow(blockIndexblockPositionrightPositionrightBlocks);
    }
    private boolean positionEqualsPosition(int leftPositionint rightPosition)
    {
        long leftPageAddress = .getLong(leftPosition);
        int leftBlockIndex = decodeSliceIndex(leftPageAddress);
        int leftBlockPosition = decodePosition(leftPageAddress);
        long rightPageAddress = .getLong(rightPosition);
        int rightBlockIndex = decodeSliceIndex(rightPageAddress);
        int rightBlockPosition = decodePosition(rightPageAddress);
        return .positionEqualsPosition(leftBlockIndexleftBlockPositionrightBlockIndexrightBlockPosition);
    }
    private static int getHashPosition(int rawHashint mask)
    {
        return ((int) XxHash64.hash(rawHash)) & mask;
    }
New to GrepCode? Check out our FAQ X