PageRenderTime 66ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/release-0.0.0-rc0/hive/external/ql/src/java/org/apache/hadoop/hive/ql/exec/persistence/MapJoinObjectKey.java

#
Java | 160 lines | 102 code | 24 blank | 34 comment | 26 complexity | 6bd62ebd3d202d85a8ef733eb44f7fce 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.ql.exec.persistence;
  19. import java.io.IOException;
  20. import java.io.ObjectInput;
  21. import java.io.ObjectOutput;
  22. import java.util.ArrayList;
  23. import org.apache.hadoop.hive.ql.exec.MapJoinMetaData;
  24. import org.apache.hadoop.hive.ql.exec.HashTableSinkOperator.HashTableSinkObjectCtx;
  25. import org.apache.hadoop.hive.serde2.SerDeException;
  26. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils;
  27. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils.ObjectInspectorCopyOption;
  28. import org.apache.hadoop.io.Writable;
  29. /**
  30. * Map Join Object used for both key.
  31. */
  32. public class MapJoinObjectKey extends AbstractMapJoinKey {
  33. protected transient Object[] obj;
  34. public MapJoinObjectKey() {
  35. }
  36. /**
  37. * @param metadataTag
  38. * @param obj
  39. */
  40. public MapJoinObjectKey(Object[] obj) {
  41. this.obj = obj;
  42. }
  43. @Override
  44. public boolean equals(Object o) {
  45. if (o instanceof MapJoinObjectKey) {
  46. MapJoinObjectKey mObj = (MapJoinObjectKey) o;
  47. Object[] mObjArray = mObj.getObj();
  48. if ((obj == null) && (mObjArray == null)) {
  49. return true;
  50. }
  51. if ((obj != null) && (mObjArray != null)) {
  52. if (obj.length == mObjArray.length) {
  53. for (int i = 0; i < obj.length; i++) {
  54. if (!obj[i].equals(mObjArray[i])) {
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. }
  61. }
  62. return false;
  63. }
  64. @Override
  65. public int hashCode() {
  66. int hashCode;
  67. if (obj == null) {
  68. hashCode = metadataTag;
  69. } else {
  70. hashCode = 1;
  71. for (int i = 0; i < obj.length; i++) {
  72. Object o = obj[i];
  73. hashCode = 31 * hashCode + (o == null ? 0 : o.hashCode());
  74. }
  75. }
  76. return hashCode;
  77. }
  78. @Override
  79. public void readExternal(ObjectInput in) throws IOException,
  80. ClassNotFoundException {
  81. try {
  82. // get the tableDesc from the map stored in the mapjoin operator
  83. HashTableSinkObjectCtx ctx = MapJoinMetaData.get(
  84. Integer.valueOf(metadataTag));
  85. Writable val = ctx.getSerDe().getSerializedClass().newInstance();
  86. val.readFields(in);
  87. ArrayList<Object> list = (ArrayList<Object>) ObjectInspectorUtils.copyToStandardObject(ctx
  88. .getSerDe().deserialize(val), ctx.getSerDe().getObjectInspector(),
  89. ObjectInspectorCopyOption.WRITABLE);
  90. if(list == null){
  91. obj = new ArrayList(0).toArray();
  92. }else{
  93. obj = list.toArray();
  94. }
  95. } catch (Exception e) {
  96. throw new IOException(e);
  97. }
  98. }
  99. @Override
  100. public void writeExternal(ObjectOutput out) throws IOException {
  101. try {
  102. // get the tableDesc from the map stored in the mapjoin operator
  103. HashTableSinkObjectCtx ctx = MapJoinMetaData.get(
  104. Integer.valueOf(metadataTag));
  105. // Different processing for key and value
  106. Writable outVal = ctx.getSerDe().serialize(obj, ctx.getStandardOI());
  107. outVal.write(out);
  108. } catch (SerDeException e) {
  109. throw new IOException(e);
  110. }
  111. }
  112. /**
  113. * @return the obj
  114. */
  115. public Object[] getObj() {
  116. return obj;
  117. }
  118. /**
  119. * @param obj
  120. * the obj to set
  121. */
  122. public void setObj(Object[] obj) {
  123. this.obj = obj;
  124. }
  125. @Override
  126. public boolean hasAnyNulls(){
  127. if (obj != null && obj.length> 0) {
  128. for (Object k : obj) {
  129. if (k == null) {
  130. return true;
  131. }
  132. }
  133. }
  134. return false;
  135. }
  136. }