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