View Javadoc

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