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  
20  package org.apache.hadoop.hbase.rest.model;
21  
22  import java.io.Serializable;
23  
24  import javax.xml.bind.annotation.XmlAttribute;
25  import javax.xml.bind.annotation.XmlRootElement;
26  
27  import org.apache.hadoop.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.HRegionInfo;
30  import org.apache.hadoop.hbase.HTableDescriptor;
31  import org.apache.hadoop.hbase.util.Bytes;
32  
33  /**
34   * Representation of a region of a table and its current location on the
35   * storage cluster.
36   * 
37   * <pre>
38   * &lt;complexType name="TableRegion"&gt;
39   *   &lt;attribute name="name" type="string"&gt;&lt;/attribute&gt;
40   *   &lt;attribute name="id" type="int"&gt;&lt;/attribute&gt;
41   *   &lt;attribute name="startKey" type="base64Binary"&gt;&lt;/attribute&gt;
42   *   &lt;attribute name="endKey" type="base64Binary"&gt;&lt;/attribute&gt;
43   *   &lt;attribute name="location" type="string"&gt;&lt;/attribute&gt;
44   *  &lt;/complexType&gt;
45   * </pre>
46   */
47  @XmlRootElement(name="Region")
48  @InterfaceAudience.Private
49  public class TableRegionModel implements Serializable {
50  
51    private static final long serialVersionUID = 1L;
52  
53    private String table;
54    private long id;
55    private byte[] startKey; 
56    private byte[] endKey;
57    private String location;
58  
59    /**
60     * Constructor
61     */
62    public TableRegionModel() {}
63  
64    /**
65     * Constructor
66     * @param table the table name
67     * @param id the encoded id of the region
68     * @param startKey the start key of the region
69     * @param endKey the end key of the region
70     */
71    public TableRegionModel(String table, long id, byte[] startKey,
72        byte[] endKey) {
73      this(table, id, startKey, endKey, null);
74    }
75  
76    /**
77     * Constructor
78     * @param table the table name
79     * @param id the encoded id of the region
80     * @param startKey the start key of the region
81     * @param endKey the end key of the region
82     * @param location the name and port of the region server hosting the region
83     */
84    public TableRegionModel(String table, long id, byte[] startKey,
85        byte[] endKey, String location) {
86      this.table = table;
87      this.id = id;
88      this.startKey = startKey;
89      this.endKey = endKey;
90      this.location = location;
91    }
92  
93    /**
94     * @return the region name
95     */
96    @XmlAttribute
97    public String getName() {
98      byte [] tableNameAsBytes = Bytes.toBytes(this.table);
99      byte [] nameAsBytes = HRegionInfo.createRegionName(
100         TableName.valueOf(tableNameAsBytes),
101       this.startKey, this.id,
102       !HTableDescriptor.isSystemTable(TableName.valueOf(tableNameAsBytes)));
103     return Bytes.toString(nameAsBytes);
104   }
105 
106   /**
107    * @return the encoded region id
108    */
109   @XmlAttribute 
110   public long getId() {
111     return id;
112   }
113 
114   /**
115    * @return the start key
116    */
117   @XmlAttribute 
118   public byte[] getStartKey() {
119     return startKey;
120   }
121 
122   /**
123    * @return the end key
124    */
125   @XmlAttribute 
126   public byte[] getEndKey() {
127     return endKey;
128   }
129 
130   /**
131    * @return the name and port of the region server hosting the region
132    */
133   @XmlAttribute 
134   public String getLocation() {
135     return location;
136   }
137 
138   /**
139    * @param name region printable name
140    */
141   public void setName(String name) {
142     String split[] = name.split(",");
143     this.table = split[0];
144     this.startKey = Bytes.toBytes(split[1]);
145     String tail = split[2];
146     split = tail.split("\\.");
147     id = Long.valueOf(split[0]);
148   }
149 
150   /**
151    * @param id the region's encoded id
152    */
153   public void setId(long id) {
154     this.id = id;
155   }
156 
157   /**
158    * @param startKey the start key
159    */
160   public void setStartKey(byte[] startKey) {
161     this.startKey = startKey;
162   }
163 
164   /**
165    * @param endKey the end key
166    */
167   public void setEndKey(byte[] endKey) {
168     this.endKey = endKey;
169   }
170 
171   /**
172    * @param location the name and port of the region server hosting the region
173    */
174   public void setLocation(String location) {
175     this.location = location;
176   }
177 
178   /* (non-Javadoc)
179    * @see java.lang.Object#toString()
180    */
181   @Override
182   public String toString() {
183     StringBuilder sb = new StringBuilder();
184     sb.append(getName());
185     sb.append(" [\n  id=");
186     sb.append(id);
187     sb.append("\n  startKey='");
188     sb.append(Bytes.toString(startKey));
189     sb.append("'\n  endKey='");
190     sb.append(Bytes.toString(endKey));
191     if (location != null) {
192       sb.append("'\n  location='");
193       sb.append(location);
194     }
195     sb.append("'\n]\n");
196     return sb.toString();
197   }
198 }