PageRenderTime 16ms CodeModel.GetById 7ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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