View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.mapred;
20  
21  import java.io.DataInput;
22  import java.io.DataOutput;
23  import java.io.IOException;
24  import java.util.Arrays;
25  
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.classification.InterfaceStability;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.apache.hadoop.mapred.InputSplit;
32  
33  /**
34   * A table split corresponds to a key range [low, high)
35   */
36  @InterfaceAudience.Public
37  @InterfaceStability.Stable
38  public class TableSplit implements InputSplit, Comparable<TableSplit> {
39    private TableName m_tableName;
40    private byte [] m_startRow;
41    private byte [] m_endRow;
42    private String m_regionLocation;
43  
44    /** default constructor */
45    public TableSplit() {
46      this((TableName)null, HConstants.EMPTY_BYTE_ARRAY,
47        HConstants.EMPTY_BYTE_ARRAY, "");
48    }
49  
50    /**
51     * Constructor
52     * @param tableName
53     * @param startRow
54     * @param endRow
55     * @param location
56     */
57    public TableSplit(TableName tableName, byte [] startRow, byte [] endRow,
58        final String location) {
59      this.m_tableName = tableName;
60      this.m_startRow = startRow;
61      this.m_endRow = endRow;
62      this.m_regionLocation = location;
63    }
64  
65    public TableSplit(byte [] tableName, byte [] startRow, byte [] endRow,
66        final String location) {
67      this(TableName.valueOf(tableName), startRow, endRow,
68        location);
69    }
70  
71    /** @return table name */
72    public TableName getTable() {
73      return this.m_tableName;
74    }
75  
76    /** @return table name */
77     public byte [] getTableName() {
78       return this.m_tableName.getName();
79     }
80  
81    /** @return starting row key */
82    public byte [] getStartRow() {
83      return this.m_startRow;
84    }
85  
86    /** @return end row key */
87    public byte [] getEndRow() {
88      return this.m_endRow;
89    }
90  
91    /** @return the region's hostname */
92    public String getRegionLocation() {
93      return this.m_regionLocation;
94    }
95  
96    public String[] getLocations() {
97      return new String[] {this.m_regionLocation};
98    }
99  
100   public long getLength() {
101     // Not clear how to obtain this... seems to be used only for sorting splits
102     return 0;
103   }
104 
105   public void readFields(DataInput in) throws IOException {
106     this.m_tableName = TableName.valueOf(Bytes.readByteArray(in));
107     this.m_startRow = Bytes.readByteArray(in);
108     this.m_endRow = Bytes.readByteArray(in);
109     this.m_regionLocation = Bytes.toString(Bytes.readByteArray(in));
110   }
111 
112   public void write(DataOutput out) throws IOException {
113     Bytes.writeByteArray(out, this.m_tableName.getName());
114     Bytes.writeByteArray(out, this.m_startRow);
115     Bytes.writeByteArray(out, this.m_endRow);
116     Bytes.writeByteArray(out, Bytes.toBytes(this.m_regionLocation));
117   }
118 
119   @Override
120   public String toString() {
121       StringBuilder sb = new StringBuilder();
122       sb.append("HBase table split(");
123       sb.append("table name: ").append(m_tableName);
124       sb.append(", start row: ").append(Bytes.toStringBinary(m_startRow));
125       sb.append(", end row: ").append(Bytes.toStringBinary(m_endRow));
126       sb.append(", region location: ").append(m_regionLocation);
127       sb.append(")");
128       return sb.toString();
129   }
130 
131   @Override
132   public int compareTo(TableSplit o) {
133     return Bytes.compareTo(getStartRow(), o.getStartRow());
134   }
135 
136   @Override
137   public boolean equals(Object o) {
138     if (o == null || !(o instanceof TableSplit)) {
139       return false;
140     }
141     TableSplit other = (TableSplit)o;
142     return m_tableName.equals(other.m_tableName) &&
143       Bytes.equals(m_startRow, other.m_startRow) &&
144       Bytes.equals(m_endRow, other.m_endRow) &&
145       m_regionLocation.equals(other.m_regionLocation);
146   }
147 
148   @Override
149   public int hashCode() {
150     int result = m_tableName != null ? m_tableName.hashCode() : 0;
151     result = 31 * result + Arrays.hashCode(m_startRow);
152     result = 31 * result + Arrays.hashCode(m_endRow);
153     result = 31 * result + (m_regionLocation != null ? m_regionLocation.hashCode() : 0);
154     return result;
155   }
156 }