PageRenderTime 39ms CodeModel.GetById 30ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/ql/src/java/org/apache/hadoop/hive/ql/hooks/ReadEntity.java

#
Java | 197 lines | 96 code | 28 blank | 73 comment | 14 complexity | ce3efccff4a64be27f1a0f463b505c22 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.hooks;
 20
 21import java.io.Serializable;
 22import java.net.URI;
 23import java.util.Map;
 24
 25import org.apache.hadoop.hive.ql.metadata.Partition;
 26import org.apache.hadoop.hive.ql.metadata.Table;
 27
 28/**
 29 * This class encapsulates the information on the partition and tables that are
 30 * read by the query.
 31 */
 32public class ReadEntity implements Serializable {
 33
 34  private static final long serialVersionUID = 1L;
 35  
 36  /**
 37   * The table.
 38   */
 39  private Table t;
 40
 41  /**
 42   * The partition. This is null for a non partitioned table.
 43   */
 44  private Partition p;
 45
 46  /**
 47   * This is derived from t and p, but we need to serialize this field to make sure
 48   * ReadEntity.hashCode() does not need to recursively read into t and p. 
 49   */
 50  private String name;
 51  
 52  public String getName() {
 53    return name;
 54  }
 55
 56  public void setName(String name) {
 57    this.name = name;
 58  }
 59
 60  public void setP(Partition p) {
 61    this.p = p;
 62  }
 63
 64  public void setT(Table t) {
 65    this.t = t;
 66  }
 67
 68  public Partition getP() {
 69    return p;
 70  }
 71
 72  public Table getT() {
 73    return t;
 74  }
 75
 76  /**
 77   * For serialization only.
 78   */
 79  public ReadEntity() {
 80  }
 81  
 82  /**
 83   * Constructor.
 84   * 
 85   * @param t
 86   *          The Table that the query reads from.
 87   */
 88  public ReadEntity(Table t) {
 89    this.t = t;
 90    p = null;
 91    name = computeName();
 92  }
 93
 94  /**
 95   * Constructor given a partiton.
 96   * 
 97   * @param p
 98   *          The partition that the query reads from.
 99   */
100  public ReadEntity(Partition p) {
101    t = p.getTable();
102    this.p = p;
103    name = computeName();
104  }
105
106  private String computeName() {
107    if (p != null) {
108      return p.getTable().getDbName() + "@" + p.getTable().getTableName() + "@"
109          + p.getName();
110    } else {
111      return t.getDbName() + "@" + t.getTableName();
112    }
113  }
114  
115  /**
116   * Enum that tells what time of a read entity this is.
117   */
118  public static enum Type {
119    TABLE, PARTITION
120  };
121
122  /**
123   * Get the type.
124   */
125  public Type getType() {
126    return p == null ? Type.TABLE : Type.PARTITION;
127  }
128
129  /**
130   * Get the parameter map of the Entity.
131   */
132  public Map<String, String> getParameters() {
133    if (p != null) {
134      return p.getParameters();
135    } else {
136      return t.getParameters();
137    }
138  }
139
140  /**
141   * Get the location of the entity.
142   */
143  public URI getLocation() {
144    if (p != null) {
145      return p.getDataLocation();
146    } else {
147      return t.getDataLocation();
148    }
149  }
150
151  /**
152   * Get partition entity.
153   */
154  public Partition getPartition() {
155    return p;
156  }
157
158  /**
159   * Get table entity.
160   */
161  public Table getTable() {
162    return t;
163  }
164
165  /**
166   * toString function.
167   */
168  @Override
169  public String toString() {
170    return name;
171  }
172
173  /**
174   * Equals function.
175   */
176  @Override
177  public boolean equals(Object o) {
178    if (o == null) {
179      return false;
180    }
181
182    if (o instanceof ReadEntity) {
183      ReadEntity ore = (ReadEntity) o;
184      return (toString().equalsIgnoreCase(ore.toString()));
185    } else {
186      return false;
187    }
188  }
189
190  /**
191   * Hashcode function.
192   */
193  @Override
194  public int hashCode() {
195    return toString().hashCode();
196  }
197}