PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 165 lines | 83 code | 16 blank | 66 comment | 19 complexity | e122e3f1b6b0cdb25fcef66ba1b166e7 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;
  19. import org.apache.hadoop.hive.serde2.lazy.objectinspector.LazyUnionObjectInspector;
  20. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
  21. import org.apache.hadoop.io.Text;
  22. /**
  23. * LazyObject for storing a union. The field of a union can be primitive or
  24. * non-primitive.
  25. *
  26. */
  27. public class LazyUnion extends
  28. LazyNonPrimitive<LazyUnionObjectInspector> {
  29. /**
  30. * Whether the data is already parsed or not.
  31. */
  32. private boolean parsed;
  33. /**
  34. * The start position of union field. Only valid when the data is parsed.
  35. */
  36. private int startPosition;
  37. /**
  38. * The object of the union.
  39. */
  40. private LazyObject<? extends ObjectInspector> field;
  41. /**
  42. * Tag of the Union
  43. */
  44. private byte tag;
  45. /**
  46. * Whether init() has been called on the field or not.
  47. */
  48. private boolean fieldInited = false;
  49. /**
  50. * Construct a LazyUnion object with the ObjectInspector.
  51. */
  52. public LazyUnion(LazyUnionObjectInspector oi) {
  53. super(oi);
  54. }
  55. /**
  56. * Set the row data for this LazyUnion.
  57. *
  58. * @see LazyObject#init(ByteArrayRef, int, int)
  59. */
  60. @Override
  61. public void init(ByteArrayRef bytes, int start, int length) {
  62. super.init(bytes, start, length);
  63. parsed = false;
  64. }
  65. /**
  66. * Parse the byte[] and fill each field.
  67. */
  68. @SuppressWarnings("unchecked")
  69. private void parse() {
  70. byte separator = oi.getSeparator();
  71. boolean isEscaped = oi.isEscaped();
  72. byte escapeChar = oi.getEscapeChar();
  73. boolean tagStarted = false;
  74. boolean tagParsed = false;
  75. int tagStart = -1;
  76. int tagEnd = -1;
  77. int unionByteEnd = start + length;
  78. int fieldByteEnd = start;
  79. byte[] bytes = this.bytes.getData();
  80. // Go through all bytes in the byte[]
  81. while (fieldByteEnd < unionByteEnd) {
  82. if (bytes[fieldByteEnd] != separator) {
  83. if (isEscaped && bytes[fieldByteEnd] == escapeChar
  84. && fieldByteEnd + 1 < unionByteEnd) {
  85. // ignore the char after escape_char
  86. fieldByteEnd += 1;
  87. } else {
  88. if (!tagStarted) {
  89. tagStart = fieldByteEnd;
  90. tagStarted = true;
  91. }
  92. }
  93. } else { // (bytes[fieldByteEnd] == separator)
  94. if (!tagParsed) {
  95. // Reached the end of the tag
  96. tagEnd = fieldByteEnd - 1;
  97. startPosition = fieldByteEnd + 1;
  98. tagParsed = true;
  99. }
  100. }
  101. fieldByteEnd++;
  102. }
  103. tag = LazyByte.parseByte(bytes, tagStart, (tagEnd - tagStart) + 1);
  104. field = LazyFactory.createLazyObject(oi.getObjectInspectors().get(tag));
  105. fieldInited = false;
  106. parsed = true;
  107. }
  108. /**
  109. * Get the field out of the row without checking parsed.
  110. *
  111. * @return The value of the field
  112. */
  113. private Object uncheckedGetField() {
  114. Text nullSequence = oi.getNullSequence();
  115. int fieldLength = start + length - startPosition;
  116. if (fieldLength != 0 && fieldLength == nullSequence.getLength() &&
  117. LazyUtils.compare(bytes.getData(), startPosition, fieldLength,
  118. nullSequence.getBytes(), 0, nullSequence.getLength()) == 0) {
  119. return null;
  120. }
  121. if (!fieldInited) {
  122. fieldInited = true;
  123. field.init(bytes, startPosition, fieldLength);
  124. }
  125. return field.getObject();
  126. }
  127. /**
  128. * Get the field out of the union.
  129. *
  130. * @return The field as a LazyObject
  131. */
  132. public Object getField() {
  133. if (!parsed) {
  134. parse();
  135. }
  136. return uncheckedGetField();
  137. }
  138. /**
  139. * Get the tag of the union
  140. *
  141. * @return The tag byte
  142. */
  143. public byte getTag() {
  144. if (!parsed) {
  145. parse();
  146. }
  147. return tag;
  148. }
  149. }