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.type;
 
 
 import java.util.List;
 import java.util.Map;
 
 import static com.facebook.presto.type.TypeUtils.appendToBlockBuilder;
 import static com.facebook.presto.type.TypeUtils.buildStructuralSlice;
 import static com.facebook.presto.type.TypeUtils.checkElementNotNull;
 import static com.facebook.presto.type.TypeUtils.hashPosition;
 import static com.facebook.presto.type.TypeUtils.parameterizedTypeName;
 import static com.facebook.presto.type.TypeUtils.readStructuralBlock;
 import static com.google.common.base.Preconditions.checkArgument;
 
 public class MapType
         extends AbstractVariableWidthType
 {
     private final Type keyType;
     private final Type valueType;
     private static final String MAP_NULL_ELEMENT_MSG = "MAP comparison not supported for null value elements";
 
     public MapType(Type keyTypeType valueType)
     {
         super(parameterizedTypeName("map"keyType.getTypeSignature(), valueType.getTypeSignature()), Slice.class);
         checkArgument(keyType.isComparable(), "key type must be comparable");
         this. = keyType;
         this. = valueType;
     }
 
     public static Slice toStackRepresentation(Map<?, ?> valueType keyTypeType valueType)
     {
         BlockBuilder blockBuilder = new VariableWidthBlockBuilder(new BlockBuilderStatus(), 1024);
         for (Map.Entry<?, ?> entry : value.entrySet()) {
             appendToBlockBuilder(keyTypeentry.getKey(), blockBuilder);
             appendToBlockBuilder(valueTypeentry.getValue(), blockBuilder);
         }
         return buildStructuralSlice(blockBuilder);
     }
 
     public Type getKeyType()
     {
         return ;
     }
 
     public Type getValueType()
     {
         return ;
     }
 
     @Override
     public boolean isComparable()
     {
         return .isComparable();
     }
 
     @Override
     public int hash(Block blockint position)
     {
         Slice mapSlice = getSlice(blockposition);
         Block mapBlock = readStructuralBlock(mapSlice);
         int result = 0;
 
         for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
             result += hashPosition(mapBlocki);
             result += hashPosition(mapBlocki + 1);
         }
         return result;
     }
 
     @Override
     public boolean equalTo(Block leftBlockint leftPositionBlock rightBlockint rightPosition)
     {
         Slice leftSlice = getSlice(leftBlockleftPosition);
         Slice rightSlice = getSlice(rightBlockrightPosition);
         Block leftMapBlock = readStructuralBlock(leftSlice);
        Block rightMapBlock = readStructuralBlock(rightSlice);
        if (leftMapBlock.getPositionCount() != rightMapBlock.getPositionCount()) {
            return false;
        }
        Map<KeyWrapperIntegerwrappedLeftMap = new HashMap<>();
        for (int position = 0; position < leftMapBlock.getPositionCount(); position += 2) {
            wrappedLeftMap.put(new KeyWrapper(leftMapBlockposition), position + 1);
        }
        for (int position = 0; position < rightMapBlock.getPositionCount(); position += 2) {
            KeyWrapper key = new KeyWrapper(rightMapBlockposition);
            Integer leftValuePosition = wrappedLeftMap.get(key);
            if (leftValuePosition == null) {
                return false;
            }
            int rightValuePosition = position + 1;
            checkElementNotNull(leftMapBlock.isNull(leftValuePosition), );
            checkElementNotNull(rightMapBlock.isNull(rightValuePosition), );
            if (!.equalTo(leftMapBlockleftValuePositionrightMapBlockrightValuePosition)) {
                return false;
            }
        }
        return true;
    }
    private static final class KeyWrapper
    {
        private final Type type;
        private final Block block;
        private final int position;
        public KeyWrapper(Type typeBlock blockint position)
        {
            this. = type;
            this. = block;
            this. = position;
        }
        public Block getBlock()
        {
            return this.;
        }
        public int getPosition()
        {
            return this.;
        }
        @Override
        public int hashCode()
        {
            return .hash();
        }
        @Override
        public boolean equals(Object obj)
        {
            if (obj == null || !getClass().equals(obj.getClass())) {
                return false;
            }
            KeyWrapper other = (KeyWrapperobj;
            return .equalTo(this.this.other.getBlock(), other.getPosition());
        }
    }
    @Override
    public Object getObjectValue(ConnectorSession sessionBlock blockint position)
    {
        if (block.isNull(position)) {
            return null;
        }
        Slice slice = block.getSlice(position, 0, block.getLength(position));
        Block mapBlock = readStructuralBlock(slice);
        Map<ObjectObjectmap = new HashMap<>();
        for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
            map.put(.getObjectValue(sessionmapBlocki), .getObjectValue(sessionmapBlocki + 1));
        }
        return Collections.unmodifiableMap(map);
    }
    @Override
    public void appendTo(Block blockint positionBlockBuilder blockBuilder)
    {
        if (block.isNull(position)) {
            blockBuilder.appendNull();
        }
        else {
            block.writeBytesTo(position, 0, block.getLength(position), blockBuilder);
            blockBuilder.closeEntry();
        }
    }
    @Override
    public Slice getSlice(Block blockint position)
    {
        return block.getSlice(position, 0, block.getLength(position));
    }
    @Override
    public void writeSlice(BlockBuilder blockBuilderSlice value)
    {
        writeSlice(blockBuildervalue, 0, value.length());
    }
    @Override
    public void writeSlice(BlockBuilder blockBuilderSlice valueint offsetint length)
    {
        blockBuilder.writeBytes(valueoffsetlength).closeEntry();
    }
    @Override
    public List<TypegetTypeParameters()
    {
        return ImmutableList.of(getKeyType(), getValueType());
    }
    @Override
    public String getDisplayName()
    {
        return "map<" + .getDisplayName() + ", " + .getDisplayName() + ">";
    }
New to GrepCode? Check out our FAQ X