View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest.model;
21  
22  import java.io.IOException;
23  import java.io.Serializable;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import javax.xml.bind.annotation.XmlAttribute;
28  import javax.xml.bind.annotation.XmlElement;
29  import javax.xml.bind.annotation.XmlRootElement;
30  
31  import org.apache.hadoop.hbase.util.ByteStringer;
32  import org.apache.hadoop.hbase.classification.InterfaceAudience;
33  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
34  import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
35  import org.apache.hadoop.hbase.rest.protobuf.generated.TableInfoMessage.TableInfo;
36  
37  /**
38   * Representation of a list of table regions. 
39   * 
40   * <pre>
41   * &lt;complexType name="TableInfo"&gt;
42   *   &lt;sequence&gt;
43   *     &lt;element name="region" type="tns:TableRegion" 
44   *       maxOccurs="unbounded" minOccurs="1"&gt;&lt;/element&gt;
45   *   &lt;/sequence&gt;
46   *   &lt;attribute name="name" type="string"&gt;&lt;/attribute&gt;
47   * &lt;/complexType&gt;
48   * </pre>
49   */
50  @XmlRootElement(name="TableInfo")
51  @InterfaceAudience.Private
52  public class TableInfoModel implements Serializable, ProtobufMessageHandler {
53    private static final long serialVersionUID = 1L;
54  
55    private String name;
56    private List<TableRegionModel> regions = new ArrayList<TableRegionModel>();
57  
58    /**
59     * Default constructor
60     */
61    public TableInfoModel() {}
62  
63    /**
64     * Constructor
65     * @param name
66     */
67    public TableInfoModel(String name) {
68      this.name = name;
69    }
70  
71    /**
72     * Add a region model to the list
73     * @param region the region
74     */
75    public void add(TableRegionModel region) {
76      regions.add(region);
77    }
78  
79    /**
80     * @param index the index
81     * @return the region model
82     */
83    public TableRegionModel get(int index) {
84      return regions.get(index);
85    }
86  
87    /**
88     * @return the table name
89     */
90    @XmlAttribute
91    public String getName() {
92      return name;
93    }
94  
95    /**
96     * @return the regions
97     */
98    @XmlElement(name="Region")
99    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 }