001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.rest.model; 019 020import java.io.Serializable; 021import javax.xml.bind.annotation.XmlAttribute; 022import javax.xml.bind.annotation.XmlRootElement; 023import org.apache.hadoop.hbase.TableName; 024import org.apache.hadoop.hbase.client.RegionInfo; 025import org.apache.hadoop.hbase.util.Bytes; 026import org.apache.yetus.audience.InterfaceAudience; 027 028/** 029 * Representation of a region of a table and its current location on the storage cluster. 030 * 031 * <pre> 032 * <complexType name="TableRegion"> 033 * <attribute name="name" type="string"></attribute> 034 * <attribute name="id" type="int"></attribute> 035 * <attribute name="startKey" type="base64Binary"></attribute> 036 * <attribute name="endKey" type="base64Binary"></attribute> 037 * <attribute name="location" type="string"></attribute> 038 * </complexType> 039 * </pre> 040 */ 041@XmlRootElement(name = "Region") 042@InterfaceAudience.Private 043public class TableRegionModel implements Serializable { 044 045 private static final long serialVersionUID = 1L; 046 047 private String table; 048 private long id; 049 private byte[] startKey; 050 private byte[] endKey; 051 private String location; 052 053 /** 054 * Constructor 055 */ 056 public TableRegionModel() { 057 } 058 059 /** 060 * Constructor 061 * @param table the table name 062 * @param id the encoded id of the region 063 * @param startKey the start key of the region 064 * @param endKey the end key of the region 065 */ 066 public TableRegionModel(String table, long id, byte[] startKey, byte[] endKey) { 067 this(table, id, startKey, endKey, null); 068 } 069 070 /** 071 * Constructor 072 * @param table the table name 073 * @param id the encoded id of the region 074 * @param startKey the start key of the region 075 * @param endKey the end key of the region 076 * @param location the name and port of the region server hosting the region 077 */ 078 public TableRegionModel(String table, long id, byte[] startKey, byte[] endKey, String location) { 079 this.table = table; 080 this.id = id; 081 this.startKey = startKey; 082 this.endKey = endKey; 083 this.location = location; 084 } 085 086 /** Returns the region name */ 087 @XmlAttribute 088 public String getName() { 089 byte[] tableNameAsBytes = Bytes.toBytes(this.table); 090 TableName tableName = TableName.valueOf(tableNameAsBytes); 091 byte[] nameAsBytes = 092 RegionInfo.createRegionName(tableName, this.startKey, this.id, !tableName.isSystemTable()); 093 return Bytes.toString(nameAsBytes); 094 } 095 096 /** Returns the encoded region id */ 097 @XmlAttribute 098 public long getId() { 099 return id; 100 } 101 102 /** Returns the start key */ 103 @XmlAttribute 104 public byte[] getStartKey() { 105 return startKey; 106 } 107 108 /** Returns the end key */ 109 @XmlAttribute 110 public byte[] getEndKey() { 111 return endKey; 112 } 113 114 /** Returns the name and port of the region server hosting the region */ 115 @XmlAttribute 116 public String getLocation() { 117 return location; 118 } 119 120 /** 121 * @param name region printable name 122 */ 123 public void setName(String name) { 124 String split[] = name.split(","); 125 this.table = split[0]; 126 this.startKey = Bytes.toBytes(split[1]); 127 String tail = split[2]; 128 split = tail.split("\\."); 129 id = Long.parseLong(split[0]); 130 } 131 132 /** 133 * @param id the region's encoded id 134 */ 135 public void setId(long id) { 136 this.id = id; 137 } 138 139 /** 140 * @param startKey the start key 141 */ 142 public void setStartKey(byte[] startKey) { 143 this.startKey = startKey; 144 } 145 146 /** 147 * @param endKey the end key 148 */ 149 public void setEndKey(byte[] endKey) { 150 this.endKey = endKey; 151 } 152 153 /** 154 * @param location the name and port of the region server hosting the region 155 */ 156 public void setLocation(String location) { 157 this.location = location; 158 } 159 160 /* 161 * (non-Javadoc) 162 * @see java.lang.Object#toString() 163 */ 164 @Override 165 public String toString() { 166 StringBuilder sb = new StringBuilder(); 167 sb.append(getName()); 168 sb.append(" [\n id="); 169 sb.append(id); 170 sb.append("\n startKey='"); 171 sb.append(Bytes.toString(startKey)); 172 sb.append("'\n endKey='"); 173 sb.append(Bytes.toString(endKey)); 174 if (location != null) { 175 sb.append("'\n location='"); 176 sb.append(location); 177 } 178 sb.append("'\n]\n"); 179 return sb.toString(); 180 } 181}