PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/serde/src/java/org/apache/hadoop/hive/serde2/lazy/objectinspector/LazyObjectInspectorFactory.java

#
Java | 141 lines | 99 code | 13 blank | 29 comment | 8 complexity | 406ddeaa99174f79ee110d1a245f5120 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.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. ArrayList<Object> signature = new ArrayList<Object>();
  44. signature.add(structFieldNames);
  45. signature.add(structFieldObjectInspectors);
  46. signature.add(Byte.valueOf(separator));
  47. signature.add(nullSequence.toString());
  48. signature.add(Boolean.valueOf(lastColumnTakesRest));
  49. signature.add(Boolean.valueOf(escaped));
  50. signature.add(Byte.valueOf(escapeChar));
  51. LazySimpleStructObjectInspector result = cachedLazySimpleStructObjectInspector
  52. .get(signature);
  53. if (result == null) {
  54. result = new LazySimpleStructObjectInspector(structFieldNames,
  55. structFieldObjectInspectors, separator, nullSequence,
  56. lastColumnTakesRest, escaped, escapeChar);
  57. cachedLazySimpleStructObjectInspector.put(signature, result);
  58. }
  59. return result;
  60. }
  61. static HashMap<ArrayList<Object>, LazyListObjectInspector> cachedLazySimpleListObjectInspector = new HashMap<ArrayList<Object>, LazyListObjectInspector>();
  62. public static LazyListObjectInspector getLazySimpleListObjectInspector(
  63. ObjectInspector listElementObjectInspector, byte separator,
  64. Text nullSequence, boolean escaped, byte escapeChar) {
  65. ArrayList<Object> signature = new ArrayList<Object>();
  66. signature.add(listElementObjectInspector);
  67. signature.add(Byte.valueOf(separator));
  68. signature.add(nullSequence.toString());
  69. signature.add(Boolean.valueOf(escaped));
  70. signature.add(Byte.valueOf(escapeChar));
  71. LazyListObjectInspector result = cachedLazySimpleListObjectInspector
  72. .get(signature);
  73. if (result == null) {
  74. result = new LazyListObjectInspector(listElementObjectInspector,
  75. separator, nullSequence, escaped, escapeChar);
  76. cachedLazySimpleListObjectInspector.put(signature, result);
  77. }
  78. return result;
  79. }
  80. static HashMap<ArrayList<Object>, LazyMapObjectInspector> cachedLazySimpleMapObjectInspector = new HashMap<ArrayList<Object>, LazyMapObjectInspector>();
  81. public static LazyMapObjectInspector getLazySimpleMapObjectInspector(
  82. ObjectInspector mapKeyObjectInspector,
  83. ObjectInspector mapValueObjectInspector, byte itemSeparator,
  84. byte keyValueSeparator, Text nullSequence, boolean escaped,
  85. byte escapeChar) {
  86. ArrayList<Object> signature = new ArrayList<Object>();
  87. signature.add(mapKeyObjectInspector);
  88. signature.add(mapValueObjectInspector);
  89. signature.add(Byte.valueOf(itemSeparator));
  90. signature.add(Byte.valueOf(keyValueSeparator));
  91. signature.add(nullSequence.toString());
  92. signature.add(Boolean.valueOf(escaped));
  93. signature.add(Byte.valueOf(escapeChar));
  94. LazyMapObjectInspector result = cachedLazySimpleMapObjectInspector
  95. .get(signature);
  96. if (result == null) {
  97. result = new LazyMapObjectInspector(mapKeyObjectInspector,
  98. mapValueObjectInspector, itemSeparator, keyValueSeparator,
  99. nullSequence, escaped, escapeChar);
  100. cachedLazySimpleMapObjectInspector.put(signature, result);
  101. }
  102. return result;
  103. }
  104. static HashMap<List<Object>, LazyUnionObjectInspector>
  105. cachedLazyUnionObjectInspector =
  106. new HashMap<List<Object>, LazyUnionObjectInspector>();
  107. public static LazyUnionObjectInspector getLazyUnionObjectInspector(
  108. List<ObjectInspector> ois, byte separator, Text nullSequence,
  109. boolean escaped, byte escapeChar) {
  110. List<Object> signature = new ArrayList<Object>();
  111. signature.add(ois);
  112. signature.add(Byte.valueOf(separator));
  113. signature.add(nullSequence.toString());
  114. signature.add(Boolean.valueOf(escaped));
  115. signature.add(Byte.valueOf(escapeChar));
  116. LazyUnionObjectInspector result = cachedLazyUnionObjectInspector
  117. .get(signature);
  118. if (result == null) {
  119. result = new LazyUnionObjectInspector(ois, separator,
  120. nullSequence, escaped, escapeChar);
  121. cachedLazyUnionObjectInspector.put(signature, result);
  122. }
  123. return result;
  124. }
  125. private LazyObjectInspectorFactory() {
  126. // prevent instantiation
  127. }
  128. }