View Javadoc

1   /**
2    * Copyright 2007 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.mapreduce;
21  
22  import java.io.DataInput;
23  import java.io.DataOutput;
24  import java.io.IOException;
25  import java.util.Arrays;
26  
27  import org.apache.hadoop.hbase.HConstants;
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.apache.hadoop.io.Writable;
30  import org.apache.hadoop.mapreduce.InputSplit;
31  
32  /**
33   * A table split corresponds to a key range (low, high). All references to row
34   * below refer to the key of the row.
35   */
36  public class TableSplit extends InputSplit
37  implements Writable, Comparable<TableSplit> {
38  
39    private byte [] tableName;
40    private byte [] startRow;
41    private byte [] endRow;
42    private String regionLocation;
43  
44    /** Default constructor. */
45    public TableSplit() {
46      this(HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY,
47        HConstants.EMPTY_BYTE_ARRAY, "");
48    }
49  
50    /**
51     * Creates a new instance while assigning all variables.
52     *
53     * @param tableName  The name of the current table.
54     * @param startRow  The start row of the split.
55     * @param endRow  The end row of the split.
56     * @param location  The location of the region.
57     */
58    public TableSplit(byte [] tableName, byte [] startRow, byte [] endRow,
59        final String location) {
60      this.tableName = tableName;
61      this.startRow = startRow;
62      this.endRow = endRow;
63      this.regionLocation = location;
64    }
65  
66    /**
67     * Returns the table name.
68     *
69     * @return The table name.
70     */
71    public byte [] getTableName() {
72      return tableName;
73    }
74  
75    /**
76     * Returns the start row.
77     *
78     * @return The start row.
79     */
80    public byte [] getStartRow() {
81      return startRow;
82    }
83  
84    /**
85     * Returns the end row.
86     *
87     * @return The end row.
88     */
89    public byte [] getEndRow() {
90      return endRow;
91    }
92  
93    /**
94     * Returns the region location.
95     *
96     * @return The region's location.
97     */
98    public String getRegionLocation() {
99      return regionLocation;
100   }
101 
102   /**
103    * Returns the region's location as an array.
104    *
105    * @return The array containing the region location.
106    * @see org.apache.hadoop.mapreduce.InputSplit#getLocations()
107    */
108   @Override
109   public String[] getLocations() {
110     return new String[] {regionLocation};
111   }
112 
113   /**
114    * Returns the length of the split.
115    *
116    * @return The length of the split.
117    * @see org.apache.hadoop.mapreduce.InputSplit#getLength()
118    */
119   @Override
120   public long getLength() {
121     // Not clear how to obtain this... seems to be used only for sorting splits
122     return 0;
123   }
124 
125   /**
126    * Reads the values of each field.
127    *
128    * @param in  The input to read from.
129    * @throws IOException When reading the input fails.
130    */
131   @Override
132   public void readFields(DataInput in) throws IOException {
133     tableName = Bytes.readByteArray(in);
134     startRow = Bytes.readByteArray(in);
135     endRow = Bytes.readByteArray(in);
136     regionLocation = Bytes.toString(Bytes.readByteArray(in));
137   }
138 
139   /**
140    * Writes the field values to the output.
141    *
142    * @param out  The output to write to.
143    * @throws IOException When writing the values to the output fails.
144    */
145   @Override
146   public void write(DataOutput out) throws IOException {
147     Bytes.writeByteArray(out, tableName);
148     Bytes.writeByteArray(out, startRow);
149     Bytes.writeByteArray(out, endRow);
150     Bytes.writeByteArray(out, Bytes.toBytes(regionLocation));
151   }
152 
153   /**
154    * Returns the details about this instance as a string.
155    *
156    * @return The values of this instance as a string.
157    * @see java.lang.Object#toString()
158    */
159   @Override
160   public String toString() {
161     return regionLocation + ":" +
162       Bytes.toStringBinary(startRow) + "," + Bytes.toStringBinary(endRow);
163   }
164 
165   /**
166    * Compares this split against the given one.
167    *
168    * @param split  The split to compare to.
169    * @return The result of the comparison.
170    * @see java.lang.Comparable#compareTo(java.lang.Object)
171    */
172   @Override
173   public int compareTo(TableSplit split) {
174     return Bytes.compareTo(getStartRow(), split.getStartRow());
175   }
176 
177   @Override
178   public boolean equals(Object o) {
179     if (o == null || !(o instanceof TableSplit)) {
180       return false;
181     }
182     return Bytes.equals(tableName, ((TableSplit)o).tableName) &&
183       Bytes.equals(startRow, ((TableSplit)o).startRow) &&
184       Bytes.equals(endRow, ((TableSplit)o).endRow) &&
185       regionLocation.equals(((TableSplit)o).regionLocation);
186   }
187 
188     @Override
189     public int hashCode() {
190       int result = tableName != null ? Arrays.hashCode(tableName) : 0;
191       result = 31 * result + (startRow != null ? Arrays.hashCode(startRow) : 0);
192       result = 31 * result + (endRow != null ? Arrays.hashCode(endRow) : 0);
193       result = 31 * result + (regionLocation != null ? regionLocation.hashCode() : 0);
194       return result;
195     }
196 }