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