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