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