001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.rest.model;
021
022import java.io.Serializable;
023
024import javax.xml.bind.annotation.XmlAttribute;
025import javax.xml.bind.annotation.XmlRootElement;
026
027import org.apache.yetus.audience.InterfaceAudience;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.HRegionInfo;
030import org.apache.hadoop.hbase.util.Bytes;
031
032/**
033 * Representation of a region of a table and its current location on the
034 * storage cluster.
035 * 
036 * <pre>
037 * &lt;complexType name="TableRegion"&gt;
038 *   &lt;attribute name="name" type="string"&gt;&lt;/attribute&gt;
039 *   &lt;attribute name="id" type="int"&gt;&lt;/attribute&gt;
040 *   &lt;attribute name="startKey" type="base64Binary"&gt;&lt;/attribute&gt;
041 *   &lt;attribute name="endKey" type="base64Binary"&gt;&lt;/attribute&gt;
042 *   &lt;attribute name="location" type="string"&gt;&lt;/attribute&gt;
043 *  &lt;/complexType&gt;
044 * </pre>
045 */
046@XmlRootElement(name="Region")
047@InterfaceAudience.Private
048public class TableRegionModel implements Serializable {
049
050  private static final long serialVersionUID = 1L;
051
052  private String table;
053  private long id;
054  private byte[] startKey; 
055  private byte[] endKey;
056  private String location;
057
058  /**
059   * Constructor
060   */
061  public TableRegionModel() {}
062
063  /**
064   * Constructor
065   * @param table the table name
066   * @param id the encoded id of the region
067   * @param startKey the start key of the region
068   * @param endKey the end key of the region
069   */
070  public TableRegionModel(String table, long id, byte[] startKey,
071      byte[] endKey) {
072    this(table, id, startKey, endKey, null);
073  }
074
075  /**
076   * Constructor
077   * @param table the table name
078   * @param id the encoded id of the region
079   * @param startKey the start key of the region
080   * @param endKey the end key of the region
081   * @param location the name and port of the region server hosting the region
082   */
083  public TableRegionModel(String table, long id, byte[] startKey,
084      byte[] endKey, String location) {
085    this.table = table;
086    this.id = id;
087    this.startKey = startKey;
088    this.endKey = endKey;
089    this.location = location;
090  }
091
092  /**
093   * @return the region name
094   */
095  @XmlAttribute
096  public String getName() {
097    byte [] tableNameAsBytes = Bytes.toBytes(this.table);
098    TableName tableName = TableName.valueOf(tableNameAsBytes);
099    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}