/serde/src/java/org/apache/hadoop/hive/serde2/lazy/objectinspector/LazyObjectInspectorFactory.java

https://github.com/lobotomyme/hive · Java · 154 lines · 111 code · 14 blank · 29 comment · 10 complexity · 719fb749c022c457129c3fc2f724bdce 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. package org.apache.hadoop.hive.serde2.lazy.objectinspector;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
  23. import org.apache.hadoop.io.Text;
  24. /**
  25. * ObjectInspectorFactory is the primary way to create new ObjectInspector
  26. * instances.
  27. *
  28. * SerDe classes should call the static functions in this library to create an
  29. * ObjectInspector to return to the caller of SerDe2.getObjectInspector().
  30. *
  31. * The reason of having caches here is that ObjectInspectors do not have an
  32. * internal state - so ObjectInspectors with the same construction parameters
  33. * should result in exactly the same ObjectInspector.
  34. */
  35. public final class LazyObjectInspectorFactory {
  36. static HashMap<ArrayList<Object>, LazySimpleStructObjectInspector> cachedLazySimpleStructObjectInspector =
  37. new HashMap<ArrayList<Object>, LazySimpleStructObjectInspector>();
  38. public static LazySimpleStructObjectInspector getLazySimpleStructObjectInspector(
  39. List<String> structFieldNames,
  40. List<ObjectInspector> structFieldObjectInspectors, byte separator,
  41. Text nullSequence, boolean lastColumnTakesRest, boolean escaped,
  42. byte escapeChar) {
  43. return getLazySimpleStructObjectInspector(structFieldNames,
  44. structFieldObjectInspectors, null, separator, nullSequence,
  45. lastColumnTakesRest, escaped, escapeChar);
  46. }
  47. public static LazySimpleStructObjectInspector getLazySimpleStructObjectInspector(
  48. List<String> structFieldNames,
  49. List<ObjectInspector> structFieldObjectInspectors, List<String> structFieldComments,
  50. byte separator, Text nullSequence, boolean lastColumnTakesRest,
  51. boolean escaped,byte escapeChar) {
  52. ArrayList<Object> signature = new ArrayList<Object>();
  53. signature.add(structFieldNames);
  54. signature.add(structFieldObjectInspectors);
  55. signature.add(Byte.valueOf(separator));
  56. signature.add(nullSequence.toString());
  57. signature.add(Boolean.valueOf(lastColumnTakesRest));
  58. signature.add(Boolean.valueOf(escaped));
  59. signature.add(Byte.valueOf(escapeChar));
  60. if(structFieldComments != null) {
  61. signature.add(structFieldComments);
  62. }
  63. LazySimpleStructObjectInspector result = cachedLazySimpleStructObjectInspector
  64. .get(signature);
  65. if (result == null) {
  66. result = new LazySimpleStructObjectInspector(structFieldNames,
  67. structFieldObjectInspectors, structFieldComments, separator,
  68. nullSequence, lastColumnTakesRest, escaped, escapeChar);
  69. cachedLazySimpleStructObjectInspector.put(signature, result);
  70. }
  71. return result;
  72. }
  73. static HashMap<ArrayList<Object>, LazyListObjectInspector> cachedLazySimpleListObjectInspector = new HashMap<ArrayList<Object>, LazyListObjectInspector>();
  74. public static LazyListObjectInspector getLazySimpleListObjectInspector(
  75. ObjectInspector listElementObjectInspector, byte separator,
  76. Text nullSequence, boolean escaped, byte escapeChar) {
  77. ArrayList<Object> signature = new ArrayList<Object>();
  78. signature.add(listElementObjectInspector);
  79. signature.add(Byte.valueOf(separator));
  80. signature.add(nullSequence.toString());
  81. signature.add(Boolean.valueOf(escaped));
  82. signature.add(Byte.valueOf(escapeChar));
  83. LazyListObjectInspector result = cachedLazySimpleListObjectInspector
  84. .get(signature);
  85. if (result == null) {
  86. result = new LazyListObjectInspector(listElementObjectInspector,
  87. separator, nullSequence, escaped, escapeChar);
  88. cachedLazySimpleListObjectInspector.put(signature, result);
  89. }
  90. return result;
  91. }
  92. static HashMap<ArrayList<Object>, LazyMapObjectInspector> cachedLazySimpleMapObjectInspector = new HashMap<ArrayList<Object>, LazyMapObjectInspector>();
  93. public static LazyMapObjectInspector getLazySimpleMapObjectInspector(
  94. ObjectInspector mapKeyObjectInspector,
  95. ObjectInspector mapValueObjectInspector, byte itemSeparator,
  96. byte keyValueSeparator, Text nullSequence, boolean escaped,
  97. byte escapeChar) {
  98. ArrayList<Object> signature = new ArrayList<Object>();
  99. signature.add(mapKeyObjectInspector);
  100. signature.add(mapValueObjectInspector);
  101. signature.add(Byte.valueOf(itemSeparator));
  102. signature.add(Byte.valueOf(keyValueSeparator));
  103. signature.add(nullSequence.toString());
  104. signature.add(Boolean.valueOf(escaped));
  105. signature.add(Byte.valueOf(escapeChar));
  106. LazyMapObjectInspector result = cachedLazySimpleMapObjectInspector
  107. .get(signature);
  108. if (result == null) {
  109. result = new LazyMapObjectInspector(mapKeyObjectInspector,
  110. mapValueObjectInspector, itemSeparator, keyValueSeparator,
  111. nullSequence, escaped, escapeChar);
  112. cachedLazySimpleMapObjectInspector.put(signature, result);
  113. }
  114. return result;
  115. }
  116. static HashMap<List<Object>, LazyUnionObjectInspector>
  117. cachedLazyUnionObjectInspector =
  118. new HashMap<List<Object>, LazyUnionObjectInspector>();
  119. public static LazyUnionObjectInspector getLazyUnionObjectInspector(
  120. List<ObjectInspector> ois, byte separator, Text nullSequence,
  121. boolean escaped, byte escapeChar) {
  122. List<Object> signature = new ArrayList<Object>();
  123. signature.add(ois);
  124. signature.add(Byte.valueOf(separator));
  125. signature.add(nullSequence.toString());
  126. signature.add(Boolean.valueOf(escaped));
  127. signature.add(Byte.valueOf(escapeChar));
  128. LazyUnionObjectInspector result = cachedLazyUnionObjectInspector
  129. .get(signature);
  130. if (result == null) {
  131. result = new LazyUnionObjectInspector(ois, separator,
  132. nullSequence, escaped, escapeChar);
  133. cachedLazyUnionObjectInspector.put(signature, result);
  134. }
  135. return result;
  136. }
  137. private LazyObjectInspectorFactory() {
  138. // prevent instantiation
  139. }
  140. }