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.classification.InterfaceAudience;
27  import org.apache.hadoop.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  @Deprecated
37  @InterfaceAudience.Public
38  @InterfaceStability.Stable
39  public class TableSplit implements InputSplit, Comparable<TableSplit> {
40    private TableName m_tableName;
41    private byte [] m_startRow;
42    private byte [] m_endRow;
43    private String m_regionLocation;
44  
45    /** default constructor */
46    public TableSplit() {
47      this((TableName)null, HConstants.EMPTY_BYTE_ARRAY,
48        HConstants.EMPTY_BYTE_ARRAY, "");
49    }
50  
51    /**
52     * Constructor
53     * @param tableName
54     * @param startRow
55     * @param endRow
56     * @param location
57     */
58    public TableSplit(TableName tableName, byte [] startRow, byte [] endRow,
59        final String location) {
60      this.m_tableName = tableName;
61      this.m_startRow = startRow;
62      this.m_endRow = endRow;
63      this.m_regionLocation = location;
64    }
65  
66    public TableSplit(byte [] tableName, byte [] startRow, byte [] endRow,
67        final String location) {
68      this(TableName.valueOf(tableName), startRow, endRow,
69        location);
70    }
71  
72    /** @return table name */
73    public TableName getTable() {
74      return this.m_tableName;
75    }
76  
77    /** @return table name */
78     public byte [] getTableName() {
79       return this.m_tableName.getName();
80     }
81  
82    /** @return starting row key */
83    public byte [] getStartRow() {
84      return this.m_startRow;
85    }
86  
87    /** @return end row key */
88    public byte [] getEndRow() {
89      return this.m_endRow;
90    }
91  
92    /** @return the region's hostname */
93    public String getRegionLocation() {
94      return this.m_regionLocation;
95    }
96  
97    public String[] getLocations() {
98      return new String[] {this.m_regionLocation};
99    }
100 
101   public long getLength() {
102     // Not clear how to obtain this... seems to be used only for sorting splits
103     return 0;
104   }
105 
106   public void readFields(DataInput in) throws IOException {
107     this.m_tableName = TableName.valueOf(Bytes.readByteArray(in));
108     this.m_startRow = Bytes.readByteArray(in);
109     this.m_endRow = Bytes.readByteArray(in);
110     this.m_regionLocation = Bytes.toString(Bytes.readByteArray(in));
111   }
112 
113   public void write(DataOutput out) throws IOException {
114     Bytes.writeByteArray(out, this.m_tableName.getName());
115     Bytes.writeByteArray(out, this.m_startRow);
116     Bytes.writeByteArray(out, this.m_endRow);
117     Bytes.writeByteArray(out, Bytes.toBytes(this.m_regionLocation));
118   }
119 
120   @Override
121   public String toString() {
122     return m_regionLocation + ":" +
123       Bytes.toStringBinary(m_startRow) + "," + Bytes.toStringBinary(m_endRow);
124   }
125 
126   @Override
127   public int compareTo(TableSplit o) {
128     return Bytes.compareTo(getStartRow(), o.getStartRow());
129   }
130 
131   @Override
132   public boolean equals(Object o) {
133     if (o == null || !(o instanceof TableSplit)) {
134       return false;
135     }
136     TableSplit other = (TableSplit)o;
137     return m_tableName.equals(other.m_tableName) &&
138       Bytes.equals(m_startRow, other.m_startRow) &&
139       Bytes.equals(m_endRow, other.m_endRow) &&
140       m_regionLocation.equals(other.m_regionLocation);
141   }
142 
143   @Override
144   public int hashCode() {
145     int result = m_tableName != null ? m_tableName.hashCode() : 0;
146     result = 31 * result + Arrays.hashCode(m_startRow);
147     result = 31 * result + Arrays.hashCode(m_endRow);
148     result = 31 * result + (m_regionLocation != null ? m_regionLocation.hashCode() : 0);
149     return result;
150   }
151 }