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 java.util.Set;
 
 import static com.facebook.presto.operator.GroupByHash.createGroupByHash;
 import static com.facebook.presto.operator.index.IndexSnapshot.UNLOADED_INDEX_KEY;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 
         implements RecordSet
 {
     private final List<Typetypes;
     private final List<PageAndPositionspageAndPositions;
 
     public UnloadedIndexKeyRecordSet(IndexSnapshot existingSnapshotSet<IntegerchannelsForDistinctList<TypetypesList<UpdateRequestrequests)
     {
         checkNotNull(existingSnapshot"existingSnapshot is null");
         this. = ImmutableList.copyOf(checkNotNull(types"types is null"));
         checkNotNull(requests"requests is null");
 
         int[] distinctChannels = Ints.toArray(channelsForDistinct);
         int[] normalizedDistinctChannels = new int[distinctChannels.length];
         List<TypedistinctChannelTypes = new ArrayList<>(distinctChannels.length);
         for (int i = 0; i < distinctChannels.lengthi++) {
             normalizedDistinctChannels[i] = i;
             distinctChannelTypes.add(types.get(distinctChannels[i]));
         }
 
         ImmutableList.Builder<PageAndPositionsbuilder = ImmutableList.builder();
         GroupByHash groupByHash = createGroupByHash(distinctChannelTypesnormalizedDistinctChannels, Optional.<Integer>empty(), Optional.empty(), 10_000);
         for (UpdateRequest request : requests) {
             Page page = request.getPage();
             Block[] blocks = page.getBlocks();
 
             Block[] distinctBlocks = new Block[distinctChannels.length];
             for (int i = 0; i < distinctBlocks.lengthi++) {
                 distinctBlocks[i] = blocks[distinctChannels[i]];
             }
 
             // Move through the positions while advancing the cursors in lockstep
             GroupByIdBlock groupIds = groupByHash.getGroupIds(new Page(distinctBlocks));
             int positionCount = blocks[0].getPositionCount();
             long nextDistinctId = -1;
             checkArgument(groupIds.getGroupCount() <= .);
             IntList positions = new IntArrayList((intgroupIds.getGroupCount());
             for (int position = 0; position < positionCountposition++) {
                 // We are reading ahead in the cursors, so we need to filter any nulls since they can not join
                 if (!containsNullValue(positionblocks)) {
                     // Only include the key if it is not already in the index
                     if (existingSnapshot.getJoinPosition(positionpage) == ) {
                         // Only add the position if we have not seen this tuple before (based on the distinct channels)
                         long groupId = groupIds.getGroupId(position);
                         if (nextDistinctId < groupId) {
                             nextDistinctId = groupId;
                             positions.add(position);
                         }
                     }
                 }
             }
 
             if (!positions.isEmpty()) {
                 builder.add(new PageAndPositions(requestpositions));
             }
         }
 
          = builder.build();
    }
    @Override
    public List<TypegetColumnTypes()
    {
        return ;
    }
    @Override
    {
        return new UnloadedIndexKeyRecordCursor();
    }
    private static boolean containsNullValue(int positionBlock... blocks)
    {
        for (Block block : blocks) {
            if (block.isNull(position)) {
                return true;
            }
        }
        return false;
    }
    public static class UnloadedIndexKeyRecordCursor
            implements RecordCursor
    {
        private final List<Typetypes;
        private final Iterator<PageAndPositionspageAndPositionsIterator;
        private Block[] blocks;
        private Page page;
        private IntListIterator positionIterator;
        private int position;
        public UnloadedIndexKeyRecordCursor(List<TypetypesList<PageAndPositionspageAndPositions)
        {
            this. = ImmutableList.copyOf(checkNotNull(types"types is null"));
            this. = checkNotNull(pageAndPositions"pageAndPositions is null").iterator();
            this. = new Block[types.size()];
        }
        @Override
        public long getTotalBytes()
        {
            return 0;
        }
        @Override
        public long getCompletedBytes()
        {
            return 0;
        }
        @Override
        public long getReadTimeNanos()
        {
            return 0;
        }
        @Override
        public Type getType(int field)
        {
            return .get(field);
        }
        @Override
        public boolean advanceNextPosition()
        {
            while ( == null || !.hasNext()) {
                if (!.hasNext()) {
                    return false;
                }
                PageAndPositions pageAndPositions = .next();
                 = pageAndPositions.getUpdateRequest().getPage();
                 = .getBlocks();
                checkState(.size() == .);
                 = pageAndPositions.getPositions().iterator();
            }
             = .nextInt();
            return true;
        }
        public Block[] getBlocks()
        {
            return ;
        }
        public Page getPage()
        {
            return ;
        }
        public int getPosition()
        {
            return ;
        }
        @Override
        public boolean getBoolean(int field)
        {
            return .get(field).getBoolean([field], );
        }
        @Override
        public long getLong(int field)
        {
            return .get(field).getLong([field], );
        }
        @Override
        public double getDouble(int field)
        {
            return .get(field).getDouble([field], );
        }
        @Override
        public Slice getSlice(int field)
        {
            return .get(field).getSlice([field], );
        }
        @Override
        public boolean isNull(int field)
        {
            return [field].isNull();
        }
        @Override
        public void close()
        {
            // Do nothing
        }
    }
    private static class PageAndPositions
    {
        private final UpdateRequest updateRequest;
        private final IntList positions;
        private PageAndPositions(UpdateRequest updateRequestIntList positions)
        {
            this. = checkNotNull(updateRequest"updateRequest is null");
            this. = checkNotNull(positions"positions is null");
        }
        private UpdateRequest getUpdateRequest()
        {
            return ;
        }
        private IntList getPositions()
        {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X