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  
25  import org.apache.hadoop.classification.InterfaceAudience;
26  import org.apache.hadoop.classification.InterfaceStability;
27  import org.apache.hadoop.hbase.TableName;
28  import org.apache.hadoop.hbase.HConstants;
29  import org.apache.hadoop.hbase.util.Bytes;
30  import org.apache.hadoop.mapred.InputSplit;
31  
32  /**
33   * A table split corresponds to a key range [low, high)
34   */
35  @Deprecated
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     return m_regionLocation + ":" +
122       Bytes.toStringBinary(m_startRow) + "," + Bytes.toStringBinary(m_endRow);
123   }
124 
125   @Override
126   public int compareTo(TableSplit o) {
127     return Bytes.compareTo(getStartRow(), o.getStartRow());
128   }
129 
130   @Override
131   public boolean equals(Object o) {
132     if (o == null || !(o instanceof TableSplit)) {
133       return false;
134     }
135     TableSplit other = (TableSplit)o;
136     return m_tableName.equals(other.m_tableName) &&
137       Bytes.equals(m_startRow, other.m_startRow) &&
138       Bytes.equals(m_endRow, other.m_endRow) &&
139       m_regionLocation.equals(other.m_regionLocation);
140   }
141 }