PageRenderTime 42ms CodeModel.GetById 11ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 1ms

/tags/release-0.0.0-rc0/hive/external/serde/src/java/org/apache/hadoop/hive/serde2/dynamic_type/DynamicSerDeTypeMap.java

#
Java | 162 lines | 111 code | 23 blank | 28 comment | 12 complexity | 505b5a6250ca73d70aba6d2b523f3558 MD5 | raw file
  1/**
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18
 19package org.apache.hadoop.hive.serde2.dynamic_type;
 20
 21import java.util.Collections;
 22import java.util.HashMap;
 23import java.util.Map;
 24
 25import org.apache.hadoop.hive.serde2.SerDeException;
 26import org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector;
 27import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
 28import org.apache.hadoop.hive.serde2.thrift.WriteNullsProtocol;
 29import org.apache.thrift.TException;
 30import org.apache.thrift.protocol.TMap;
 31import org.apache.thrift.protocol.TProtocol;
 32import org.apache.thrift.protocol.TType;
 33
 34/**
 35 * DynamicSerDeTypeMap.
 36 *
 37 */
 38public class DynamicSerDeTypeMap extends DynamicSerDeTypeBase {
 39
 40  @Override
 41  public boolean isPrimitive() {
 42    return false;
 43  }
 44
 45  @Override
 46  public boolean isMap() {
 47    return true;
 48  }
 49
 50  // production is: Map<FieldType(),FieldType()>
 51
 52  private final byte FD_KEYTYPE = 0;
 53  private final byte FD_VALUETYPE = 1;
 54
 55  // returns Map<?,?>
 56  @Override
 57  public Class getRealType() {
 58    try {
 59      Class c = getKeyType().getRealType();
 60      Class c2 = getValueType().getRealType();
 61      Object o = c.newInstance();
 62      Object o2 = c2.newInstance();
 63      Map<?, ?> l = Collections.singletonMap(o, o2);
 64      return l.getClass();
 65    } catch (Exception e) {
 66      e.printStackTrace();
 67      throw new RuntimeException(e);
 68    }
 69  }
 70
 71  public DynamicSerDeTypeMap(int i) {
 72    super(i);
 73  }
 74
 75  public DynamicSerDeTypeMap(thrift_grammar p, int i) {
 76    super(p, i);
 77  }
 78
 79  public DynamicSerDeTypeBase getKeyType() {
 80    return ((DynamicSerDeFieldType) jjtGetChild(FD_KEYTYPE)).getMyType();
 81  }
 82
 83  public DynamicSerDeTypeBase getValueType() {
 84    return ((DynamicSerDeFieldType) jjtGetChild(FD_VALUETYPE)).getMyType();
 85  }
 86
 87  @Override
 88  public String toString() {
 89    return "map<" + getKeyType().toString() + "," + getValueType().toString()
 90        + ">";
 91  }
 92
 93  @Override
 94  public Map<Object, Object> deserialize(Object reuse, TProtocol iprot)
 95      throws SerDeException, TException, IllegalAccessException {
 96    HashMap<Object, Object> deserializeReuse;
 97    if (reuse != null) {
 98      deserializeReuse = (HashMap<Object, Object>) reuse;
 99      deserializeReuse.clear();
100    } else {
101      deserializeReuse = new HashMap<Object, Object>();
102    }
103    TMap themap = iprot.readMapBegin();
104    if (themap == null) {
105      return null;
106    }
107    // themap might be reused by the Protocol.
108    int mapSize = themap.size;
109    for (int i = 0; i < mapSize; i++) {
110      Object key = getKeyType().deserialize(null, iprot);
111      Object value = getValueType().deserialize(null, iprot);
112      deserializeReuse.put(key, value);
113    }
114
115    // in theory, the below call isn't needed in non thrift_mode, but let's not
116    // get too crazy
117    iprot.readMapEnd();
118    return deserializeReuse;
119  }
120
121  TMap serializeMap = null;
122
123  @Override
124  public void serialize(Object o, ObjectInspector oi, TProtocol oprot)
125      throws TException, SerDeException, NoSuchFieldException, IllegalAccessException {
126    DynamicSerDeTypeBase keyType = getKeyType();
127    DynamicSerDeTypeBase valueType = getValueType();
128
129    WriteNullsProtocol nullProtocol =
130        (oprot instanceof WriteNullsProtocol) ? (WriteNullsProtocol) oprot : null;
131
132    assert (oi.getCategory() == ObjectInspector.Category.MAP);
133    MapObjectInspector moi = (MapObjectInspector) oi;
134    ObjectInspector koi = moi.getMapKeyObjectInspector();
135    ObjectInspector voi = moi.getMapValueObjectInspector();
136
137    Map<?, ?> map = moi.getMap(o);
138    serializeMap = new TMap(keyType.getType(), valueType.getType(), map.size());
139    oprot.writeMapBegin(serializeMap);
140
141    for (Object element : map.entrySet()) {
142      Map.Entry it = (Map.Entry) element;
143      Object key = it.getKey();
144      Object value = it.getValue();
145      keyType.serialize(key, koi, oprot);
146      if (value == null) {
147        assert (nullProtocol != null);
148        nullProtocol.writeNull();
149      } else {
150        valueType.serialize(value, voi, oprot);
151      }
152    }
153    // in theory, the below call isn't needed in non thrift_mode, but let's not
154    // get too crazy
155    oprot.writeMapEnd();
156  }
157
158  @Override
159  public byte getType() {
160    return TType.MAP;
161  }
162};