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  
26  import org.apache.hadoop.hbase.HConstants;
27  import org.apache.hadoop.hbase.util.Bytes;
28  import org.apache.hadoop.io.Writable;
29  import org.apache.hadoop.mapreduce.InputSplit;
30  
31  /**
32   * A table split corresponds to a key range (low, high). All references to row
33   * below refer to the key of the row.
34   */
35  public class TableSplit extends InputSplit
36  implements Writable, Comparable<TableSplit> {
37  
38    private byte [] tableName;
39    private byte [] startRow;
40    private byte [] endRow;
41    private String regionLocation;
42  
43    /** Default constructor. */
44    public TableSplit() {
45      this(HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY,
46        HConstants.EMPTY_BYTE_ARRAY, "");
47    }
48  
49    /**
50     * Creates a new instance while assigning all variables.
51     *
52     * @param tableName  The name of the current table.
53     * @param startRow  The start row of the split.
54     * @param endRow  The end row of the split.
55     * @param location  The location of the region.
56     */
57    public TableSplit(byte [] tableName, byte [] startRow, byte [] endRow,
58        final String location) {
59      this.tableName = tableName;
60      this.startRow = startRow;
61      this.endRow = endRow;
62      this.regionLocation = location;
63    }
64  
65    /**
66     * Returns the table name.
67     *
68     * @return The table name.
69     */
70    public byte [] getTableName() {
71      return tableName;
72    }
73  
74    /**
75     * Returns the start row.
76     *
77     * @return The start row.
78     */
79    public byte [] getStartRow() {
80      return startRow;
81    }
82  
83    /**
84     * Returns the end row.
85     *
86     * @return The end row.
87     */
88    public byte [] getEndRow() {
89      return endRow;
90    }
91  
92    /**
93     * Returns the region location.
94     *
95     * @return The region's location.
96     */
97    public String getRegionLocation() {
98      return regionLocation;
99    }
100 
101   /**
102    * Returns the region's location as an array.
103    *
104    * @return The array containing the region location.
105    * @see org.apache.hadoop.mapreduce.InputSplit#getLocations()
106    */
107   @Override
108   public String[] getLocations() {
109     return new String[] {regionLocation};
110   }
111 
112   /**
113    * Returns the length of the split.
114    *
115    * @return The length of the split.
116    * @see org.apache.hadoop.mapreduce.InputSplit#getLength()
117    */
118   @Override
119   public long getLength() {
120     // Not clear how to obtain this... seems to be used only for sorting splits
121     return 0;
122   }
123 
124   /**
125    * Reads the values of each field.
126    *
127    * @param in  The input to read from.
128    * @throws IOException When reading the input fails.
129    */
130   @Override
131   public void readFields(DataInput in) throws IOException {
132     tableName = Bytes.readByteArray(in);
133     startRow = Bytes.readByteArray(in);
134     endRow = Bytes.readByteArray(in);
135     regionLocation = Bytes.toString(Bytes.readByteArray(in));
136   }
137 
138   /**
139    * Writes the field values to the output.
140    *
141    * @param out  The output to write to.
142    * @throws IOException When writing the values to the output fails.
143    */
144   @Override
145   public void write(DataOutput out) throws IOException {
146     Bytes.writeByteArray(out, tableName);
147     Bytes.writeByteArray(out, startRow);
148     Bytes.writeByteArray(out, endRow);
149     Bytes.writeByteArray(out, Bytes.toBytes(regionLocation));
150   }
151 
152   /**
153    * Returns the details about this instance as a string.
154    *
155    * @return The values of this instance as a string.
156    * @see java.lang.Object#toString()
157    */
158   @Override
159   public String toString() {
160     return regionLocation + ":" +
161       Bytes.toStringBinary(startRow) + "," + Bytes.toStringBinary(endRow);
162   }
163 
164   /**
165    * Compares this split against the given one.
166    *
167    * @param split  The split to compare to.
168    * @return The result of the comparison.
169    * @see java.lang.Comparable#compareTo(java.lang.Object)
170    */
171   @Override
172   public int compareTo(TableSplit split) {
173     return Bytes.compareTo(getStartRow(), split.getStartRow());
174   }
175 
176   @Override
177   public boolean equals(Object o) {
178     if (o == null || !(o instanceof TableSplit)) {
179       return false;
180     }
181     return Bytes.equals(tableName, ((TableSplit)o).tableName) &&
182       Bytes.equals(startRow, ((TableSplit)o).startRow) &&
183       Bytes.equals(endRow, ((TableSplit)o).endRow) &&
184       regionLocation.equals(((TableSplit)o).regionLocation);
185   }
186 }