PageRenderTime 55ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/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
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. package org.apache.hadoop.hive.serde2.dynamic_type;
  19. import java.util.Collections;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import org.apache.hadoop.hive.serde2.SerDeException;
  23. import org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector;
  24. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
  25. import org.apache.hadoop.hive.serde2.thrift.WriteNullsProtocol;
  26. import org.apache.thrift.TException;
  27. import org.apache.thrift.protocol.TMap;
  28. import org.apache.thrift.protocol.TProtocol;
  29. import org.apache.thrift.protocol.TType;
  30. /**
  31. * DynamicSerDeTypeMap.
  32. *
  33. */
  34. public class DynamicSerDeTypeMap extends DynamicSerDeTypeBase {
  35. @Override
  36. public boolean isPrimitive() {
  37. return false;
  38. }
  39. @Override
  40. public boolean isMap() {
  41. return true;
  42. }
  43. // production is: Map<FieldType(),FieldType()>
  44. private final byte FD_KEYTYPE = 0;
  45. private final byte FD_VALUETYPE = 1;
  46. // returns Map<?,?>
  47. @Override
  48. public Class getRealType() {
  49. try {
  50. Class c = getKeyType().getRealType();
  51. Class c2 = getValueType().getRealType();
  52. Object o = c.newInstance();
  53. Object o2 = c2.newInstance();
  54. Map<?, ?> l = Collections.singletonMap(o, o2);
  55. return l.getClass();
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. throw new RuntimeException(e);
  59. }
  60. }
  61. public DynamicSerDeTypeMap(int i) {
  62. super(i);
  63. }
  64. public DynamicSerDeTypeMap(thrift_grammar p, int i) {
  65. super(p, i);
  66. }
  67. public DynamicSerDeTypeBase getKeyType() {
  68. return ((DynamicSerDeFieldType) jjtGetChild(FD_KEYTYPE)).getMyType();
  69. }
  70. public DynamicSerDeTypeBase getValueType() {
  71. return ((DynamicSerDeFieldType) jjtGetChild(FD_VALUETYPE)).getMyType();
  72. }
  73. @Override
  74. public String toString() {
  75. return "map<" + getKeyType().toString() + "," + getValueType().toString()
  76. + ">";
  77. }
  78. @Override
  79. public Map<Object, Object> deserialize(Object reuse, TProtocol iprot)
  80. throws SerDeException, TException, IllegalAccessException {
  81. HashMap<Object, Object> deserializeReuse;
  82. if (reuse != null) {
  83. deserializeReuse = (HashMap<Object, Object>) reuse;
  84. deserializeReuse.clear();
  85. } else {
  86. deserializeReuse = new HashMap<Object, Object>();
  87. }
  88. TMap themap = iprot.readMapBegin();
  89. if (themap == null) {
  90. return null;
  91. }
  92. // themap might be reused by the Protocol.
  93. int mapSize = themap.size;
  94. for (int i = 0; i < mapSize; i++) {
  95. Object key = getKeyType().deserialize(null, iprot);
  96. Object value = getValueType().deserialize(null, iprot);
  97. deserializeReuse.put(key, value);
  98. }
  99. // in theory, the below call isn't needed in non thrift_mode, but let's not
  100. // get too crazy
  101. iprot.readMapEnd();
  102. return deserializeReuse;
  103. }
  104. TMap serializeMap = null;
  105. @Override
  106. public void serialize(Object o, ObjectInspector oi, TProtocol oprot)
  107. throws TException, SerDeException, NoSuchFieldException, IllegalAccessException {
  108. DynamicSerDeTypeBase keyType = getKeyType();
  109. DynamicSerDeTypeBase valueType = getValueType();
  110. WriteNullsProtocol nullProtocol =
  111. (oprot instanceof WriteNullsProtocol) ? (WriteNullsProtocol) oprot : null;
  112. assert (oi.getCategory() == ObjectInspector.Category.MAP);
  113. MapObjectInspector moi = (MapObjectInspector) oi;
  114. ObjectInspector koi = moi.getMapKeyObjectInspector();
  115. ObjectInspector voi = moi.getMapValueObjectInspector();
  116. Map<?, ?> map = moi.getMap(o);
  117. serializeMap = new TMap(keyType.getType(), valueType.getType(), map.size());
  118. oprot.writeMapBegin(serializeMap);
  119. for (Object element : map.entrySet()) {
  120. Map.Entry it = (Map.Entry) element;
  121. Object key = it.getKey();
  122. Object value = it.getValue();
  123. keyType.serialize(key, koi, oprot);
  124. if (value == null) {
  125. assert (nullProtocol != null);
  126. nullProtocol.writeNull();
  127. } else {
  128. valueType.serialize(value, voi, oprot);
  129. }
  130. }
  131. // in theory, the below call isn't needed in non thrift_mode, but let's not
  132. // get too crazy
  133. oprot.writeMapEnd();
  134. }
  135. @Override
  136. public byte getType() {
  137. return TType.MAP;
  138. }
  139. };