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