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.IOException; 021import java.io.Serializable; 022import java.util.ArrayList; 023import java.util.List; 024import javax.xml.bind.annotation.XmlAttribute; 025import javax.xml.bind.annotation.XmlElement; 026import javax.xml.bind.annotation.XmlRootElement; 027import org.apache.hadoop.hbase.rest.ProtobufMessageHandler; 028import org.apache.yetus.audience.InterfaceAudience; 029 030import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations; 031 032import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 033import org.apache.hadoop.hbase.shaded.rest.protobuf.generated.TableInfoMessage.TableInfo; 034 035/** 036 * Representation of a list of table regions. 037 * 038 * <pre> 039 * <complexType name="TableInfo"> 040 * <sequence> 041 * <element name="region" type="tns:TableRegion" 042 * maxOccurs="unbounded" minOccurs="1"></element> 043 * </sequence> 044 * <attribute name="name" type="string"></attribute> 045 * </complexType> 046 * </pre> 047 */ 048@XmlRootElement(name = "TableInfo") 049@InterfaceAudience.Private 050public class TableInfoModel implements Serializable, ProtobufMessageHandler { 051 private static final long serialVersionUID = 1L; 052 053 private String name; 054 private List<TableRegionModel> regions = new ArrayList<>(); 055 056 /** 057 * Default constructor 058 */ 059 public TableInfoModel() { 060 } 061 062 /** 063 * Constructor 064 */ 065 public TableInfoModel(String name) { 066 this.name = name; 067 } 068 069 /** 070 * Add a region model to the list 071 * @param region the region 072 */ 073 public void add(TableRegionModel region) { 074 regions.add(region); 075 } 076 077 /** 078 * @param index the index 079 * @return the region model 080 */ 081 public TableRegionModel get(int index) { 082 return regions.get(index); 083 } 084 085 /** Returns the table name */ 086 @XmlAttribute 087 public String getName() { 088 return name; 089 } 090 091 /** Returns the regions */ 092 @XmlElement(name = "Region") 093 public List<TableRegionModel> getRegions() { 094 return regions; 095 } 096 097 /** 098 * @param name the table name 099 */ 100 public void setName(String name) { 101 this.name = name; 102 } 103 104 /** 105 * @param regions the regions to set 106 */ 107 public void setRegions(List<TableRegionModel> regions) { 108 this.regions = regions; 109 } 110 111 /* 112 * (non-Javadoc) 113 * @see java.lang.Object#toString() 114 */ 115 @Override 116 public String toString() { 117 StringBuilder sb = new StringBuilder(); 118 for (TableRegionModel aRegion : regions) { 119 sb.append(aRegion.toString()); 120 sb.append('\n'); 121 } 122 return sb.toString(); 123 } 124 125 @Override 126 public byte[] createProtobufOutput() { 127 TableInfo.Builder builder = TableInfo.newBuilder(); 128 builder.setName(name); 129 for (TableRegionModel aRegion : regions) { 130 TableInfo.Region.Builder regionBuilder = TableInfo.Region.newBuilder(); 131 regionBuilder.setName(aRegion.getName()); 132 regionBuilder.setId(aRegion.getId()); 133 regionBuilder.setStartKey(UnsafeByteOperations.unsafeWrap(aRegion.getStartKey())); 134 regionBuilder.setEndKey(UnsafeByteOperations.unsafeWrap(aRegion.getEndKey())); 135 regionBuilder.setLocation(aRegion.getLocation()); 136 builder.addRegions(regionBuilder); 137 } 138 return builder.build().toByteArray(); 139 } 140 141 @Override 142 public ProtobufMessageHandler getObjectFromMessage(byte[] message) throws IOException { 143 TableInfo.Builder builder = TableInfo.newBuilder(); 144 ProtobufUtil.mergeFrom(builder, message); 145 setName(builder.getName()); 146 for (TableInfo.Region region : builder.getRegionsList()) { 147 add( 148 new TableRegionModel(builder.getName(), region.getId(), region.getStartKey().toByteArray(), 149 region.getEndKey().toByteArray(), region.getLocation())); 150 } 151 return this; 152 } 153}