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