PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 129 lines | 78 code | 17 blank | 34 comment | 7 complexity | e073af46019f3fefce4a43c0b78193a3 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.HashSet;
  21. import java.util.Set;
  22. import org.apache.hadoop.hive.serde2.SerDeException;
  23. import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector;
  24. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
  25. import org.apache.thrift.TException;
  26. import org.apache.thrift.protocol.TProtocol;
  27. import org.apache.thrift.protocol.TSet;
  28. import org.apache.thrift.protocol.TType;
  29. /**
  30. * DynamicSerDeTypeSet.
  31. *
  32. */
  33. public class DynamicSerDeTypeSet extends DynamicSerDeTypeBase {
  34. // production is: set<FieldType()>
  35. private static final int FD_TYPE = 0;
  36. public DynamicSerDeTypeSet(int i) {
  37. super(i);
  38. }
  39. public DynamicSerDeTypeSet(thrift_grammar p, int i) {
  40. super(p, i);
  41. }
  42. // returns Set<?>
  43. @Override
  44. public Class getRealType() {
  45. try {
  46. Class c = getElementType().getRealType();
  47. Object o = c.newInstance();
  48. Set<?> l = Collections.singleton(o);
  49. return l.getClass();
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. throw new RuntimeException(e);
  53. }
  54. }
  55. public DynamicSerDeTypeBase getElementType() {
  56. return ((DynamicSerDeFieldType) jjtGetChild(FD_TYPE)).getMyType();
  57. }
  58. @Override
  59. public String toString() {
  60. return "set<" + getElementType().toString() + ">";
  61. }
  62. @Override
  63. public byte getType() {
  64. return TType.SET;
  65. }
  66. /**
  67. * NOTE: Set is not supported by Hive yet.
  68. */
  69. @Override
  70. public Object deserialize(Object reuse, TProtocol iprot)
  71. throws SerDeException, TException, IllegalAccessException {
  72. TSet theset = iprot.readSetBegin();
  73. if (theset == null) {
  74. return null;
  75. }
  76. Set<Object> result;
  77. if (reuse != null) {
  78. result = (Set<Object>) reuse;
  79. result.clear();
  80. } else {
  81. result = new HashSet<Object>();
  82. }
  83. for (int i = 0; i < theset.size; i++) {
  84. Object elem = getElementType().deserialize(null, iprot);
  85. result.add(elem);
  86. }
  87. // in theory, the below call isn't needed in non thrift_mode, but let's not
  88. // get too crazy
  89. iprot.readSetEnd();
  90. return result;
  91. }
  92. /**
  93. * NOTE: Set is not supported by Hive yet. The code uses ListObjectInspector
  94. * right now. We need to change it to SetObjectInspector when that is done.
  95. */
  96. TSet tset = null;
  97. @Override
  98. public void serialize(Object o, ObjectInspector oi, TProtocol oprot)
  99. throws TException, SerDeException, NoSuchFieldException, IllegalAccessException {
  100. ListObjectInspector loi = (ListObjectInspector) oi;
  101. Set<Object> set = (Set<Object>) o;
  102. DynamicSerDeTypeBase mt = getElementType();
  103. tset = new TSet(mt.getType(), set.size());
  104. oprot.writeSetBegin(tset);
  105. for (Object element : set) {
  106. mt.serialize(element, loi.getListElementObjectInspector(), oprot);
  107. }
  108. // in theory, the below call isn't needed in non thrift_mode, but let's not
  109. // get too crazy
  110. oprot.writeSetEnd();
  111. }
  112. }