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 com.google.protobuf.HBaseZeroCopyByteString;
32  import org.apache.hadoop.classification.InterfaceAudience;
33  import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
34  import org.apache.hadoop.hbase.rest.protobuf.generated.TableInfoMessage.TableInfo;
35  
36  /**
37   * Representation of a list of table regions. 
38   * 
39   * <pre>
40   * &lt;complexType name="TableInfo"&gt;
41   *   &lt;sequence&gt;
42   *     &lt;element name="region" type="tns:TableRegion" 
43   *       maxOccurs="unbounded" minOccurs="1"&gt;&lt;/element&gt;
44   *   &lt;/sequence&gt;
45   *   &lt;attribute name="name" type="string"&gt;&lt;/attribute&gt;
46   * &lt;/complexType&gt;
47   * </pre>
48   */
49  @XmlRootElement(name="TableInfo")
50  @InterfaceAudience.Private
51  public class TableInfoModel implements Serializable, ProtobufMessageHandler {
52    private static final long serialVersionUID = 1L;
53  
54    private String name;
55    private List<TableRegionModel> regions = new ArrayList<TableRegionModel>();
56  
57    /**
58     * Default constructor
59     */
60    public TableInfoModel() {}
61  
62    /**
63     * Constructor
64     * @param name
65     */
66    public TableInfoModel(String name) {
67      this.name = name;
68    }
69  
70    /**
71     * Add a region model to the list
72     * @param region the region
73     */
74    public void add(TableRegionModel region) {
75      regions.add(region);
76    }
77  
78    /**
79     * @param index the index
80     * @return the region model
81     */
82    public TableRegionModel get(int index) {
83      return regions.get(index);
84    }
85  
86    /**
87     * @return the table name
88     */
89    @XmlAttribute
90    public String getName() {
91      return name;
92    }
93  
94    /**
95     * @return the regions
96     */
97    @XmlElement(name="Region")
98    public List<TableRegionModel> getRegions() {
99      return regions;
100   }
101 
102   /**
103    * @param name the table name
104    */
105   public void setName(String name) {
106     this.name = name;
107   }
108 
109   /**
110    * @param regions the regions to set
111    */
112   public void setRegions(List<TableRegionModel> regions) {
113     this.regions = regions;
114   }
115 
116   /* (non-Javadoc)
117    * @see java.lang.Object#toString()
118    */
119   @Override
120   public String toString() {
121     StringBuilder sb = new StringBuilder();
122     for(TableRegionModel aRegion : regions) {
123       sb.append(aRegion.toString());
124       sb.append('\n');
125     }
126     return sb.toString();
127   }
128 
129   @Override
130   public byte[] createProtobufOutput() {
131     TableInfo.Builder builder = TableInfo.newBuilder();
132     builder.setName(name);
133     for (TableRegionModel aRegion: regions) {
134       TableInfo.Region.Builder regionBuilder = TableInfo.Region.newBuilder();
135       regionBuilder.setName(aRegion.getName());
136       regionBuilder.setId(aRegion.getId());
137       regionBuilder.setStartKey(HBaseZeroCopyByteString.wrap(aRegion.getStartKey()));
138       regionBuilder.setEndKey(HBaseZeroCopyByteString.wrap(aRegion.getEndKey()));
139       regionBuilder.setLocation(aRegion.getLocation());
140       builder.addRegions(regionBuilder);
141     }
142     return builder.build().toByteArray();
143   }
144 
145   @Override
146   public ProtobufMessageHandler getObjectFromMessage(byte[] message) 
147       throws IOException {
148     TableInfo.Builder builder = TableInfo.newBuilder();
149     builder.mergeFrom(message);
150     setName(builder.getName());
151     for (TableInfo.Region region: builder.getRegionsList()) {
152       add(new TableRegionModel(builder.getName(), region.getId(), 
153           region.getStartKey().toByteArray(),
154           region.getEndKey().toByteArray(),
155           region.getLocation()));
156     }
157     return this;
158   }
159 }