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 com.fasterxml.jackson.annotation.JsonAnyGetter;
23  import com.fasterxml.jackson.annotation.JsonAnySetter;
24  import com.fasterxml.jackson.annotation.JsonIgnore;
25  
26  import java.io.IOException;
27  import java.io.Serializable;
28  import java.util.ArrayList;
29  import java.util.LinkedHashMap;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Map;
33  
34  import javax.xml.bind.annotation.XmlAnyAttribute;
35  import javax.xml.bind.annotation.XmlAttribute;
36  import javax.xml.bind.annotation.XmlElement;
37  import javax.xml.bind.annotation.XmlRootElement;
38  import javax.xml.namespace.QName;
39  
40  import org.apache.hadoop.hbase.classification.InterfaceAudience;
41  import org.apache.hadoop.hbase.HColumnDescriptor;
42  import org.apache.hadoop.hbase.HConstants;
43  import org.apache.hadoop.hbase.HTableDescriptor;
44  import org.apache.hadoop.hbase.TableName;
45  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
46  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
47  import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
48  import org.apache.hadoop.hbase.rest.protobuf.generated.ColumnSchemaMessage.ColumnSchema;
49  import org.apache.hadoop.hbase.rest.protobuf.generated.TableSchemaMessage.TableSchema;
50  import org.apache.hadoop.hbase.util.Bytes;
51  
52  /**
53   * A representation of HBase table descriptors.
54   * 
55   * <pre>
56   * &lt;complexType name="TableSchema"&gt;
57   *   &lt;sequence&gt;
58   *     &lt;element name="column" type="tns:ColumnSchema" 
59   *       maxOccurs="unbounded" minOccurs="1"&gt;&lt;/element&gt;
60   *   &lt;/sequence&gt;
61   *   &lt;attribute name="name" type="string"&gt;&lt;/attribute&gt;
62   *   &lt;anyAttribute&gt;&lt;/anyAttribute&gt;
63   * &lt;/complexType&gt;
64   * </pre>
65   */
66  @XmlRootElement(name="TableSchema")
67  @InterfaceAudience.Private
68  public class TableSchemaModel implements Serializable, ProtobufMessageHandler {
69    private static final long serialVersionUID = 1L;
70    private static final QName IS_META = new QName(HTableDescriptor.IS_META);
71    private static final QName IS_ROOT = new QName(HTableDescriptor.IS_ROOT);
72    private static final QName READONLY = new QName(HTableDescriptor.READONLY);
73    private static final QName TTL = new QName(HColumnDescriptor.TTL);
74    private static final QName VERSIONS = new QName(HConstants.VERSIONS);
75    private static final QName COMPRESSION = 
76      new QName(HColumnDescriptor.COMPRESSION);
77  
78    private String name;
79    private Map<QName,Object> attrs = new LinkedHashMap<QName,Object>();
80    private List<ColumnSchemaModel> columns = new ArrayList<ColumnSchemaModel>();
81    
82    /**
83     * Default constructor.
84     */
85    public TableSchemaModel() {}
86  
87    /**
88     * Constructor
89     * @param htd the table descriptor
90     */
91    public TableSchemaModel(HTableDescriptor htd) {
92      setName(htd.getTableName().getNameAsString());
93      for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
94          htd.getValues().entrySet()) {
95        addAttribute(Bytes.toString(e.getKey().get()), 
96          Bytes.toString(e.getValue().get()));
97      }
98      for (HColumnDescriptor hcd: htd.getFamilies()) {
99        ColumnSchemaModel columnModel = new ColumnSchemaModel();
100       columnModel.setName(hcd.getNameAsString());
101       for (Map.Entry<ImmutableBytesWritable, ImmutableBytesWritable> e:
102           hcd.getValues().entrySet()) {
103         columnModel.addAttribute(Bytes.toString(e.getKey().get()), 
104             Bytes.toString(e.getValue().get()));
105       }
106       addColumnFamily(columnModel);
107     }
108   }
109 
110   /**
111    * Add an attribute to the table descriptor
112    * @param name attribute name
113    * @param value attribute value
114    */
115   @JsonAnySetter
116   public void addAttribute(String name, Object value) {
117     attrs.put(new QName(name), value);
118   }
119 
120   /**
121    * Return a table descriptor value as a string. Calls toString() on the
122    * object stored in the descriptor value map.
123    * @param name the attribute name
124    * @return the attribute value
125    */
126   public String getAttribute(String name) {
127     Object o = attrs.get(new QName(name));
128     return o != null ? o.toString() : null;
129   }
130 
131   /**
132    * Add a column family to the table descriptor
133    * @param family the column family model
134    */
135   public void addColumnFamily(ColumnSchemaModel family) {
136     columns.add(family);
137   }
138 
139   /**
140    * Retrieve the column family at the given index from the table descriptor
141    * @param index the index
142    * @return the column family model
143    */
144   public ColumnSchemaModel getColumnFamily(int index) {
145     return columns.get(index);
146   }
147 
148   /**
149    * @return the table name
150    */
151   @XmlAttribute
152   public String getName() {
153     return name;
154   }
155 
156   /**
157    * @return the map for holding unspecified (user) attributes
158    */
159   @XmlAnyAttribute
160   @JsonAnyGetter
161   public Map<QName,Object> getAny() {
162     return attrs;
163   }
164 
165   /**
166    * @return the columns
167    */
168   @XmlElement(name="ColumnSchema")
169   public List<ColumnSchemaModel> getColumns() {
170     return columns;
171   }
172 
173   /**
174    * @param name the table name
175    */
176   public void setName(String name) {
177     this.name = name;
178   }
179 
180   /**
181    * @param columns the columns to set
182    */
183   public void setColumns(List<ColumnSchemaModel> columns) {
184     this.columns = columns;
185   }
186 
187   /* (non-Javadoc)
188    * @see java.lang.Object#toString()
189    */
190   @Override
191   public String toString() {
192     StringBuilder sb = new StringBuilder();
193     sb.append("{ NAME=> '");
194     sb.append(name);
195     sb.append('\'');
196     for (Map.Entry<QName,Object> e: attrs.entrySet()) {
197       sb.append(", ");
198       sb.append(e.getKey().getLocalPart());
199       sb.append(" => '");
200       sb.append(e.getValue().toString());
201       sb.append('\'');
202     }
203     sb.append(", COLUMNS => [ ");
204     Iterator<ColumnSchemaModel> i = columns.iterator();
205     while (i.hasNext()) {
206       ColumnSchemaModel family = i.next();
207       sb.append(family.toString());
208       if (i.hasNext()) {
209         sb.append(',');
210       }
211       sb.append(' ');
212     }
213     sb.append("] }");
214     return sb.toString();
215   }
216 
217   // getters and setters for common schema attributes
218 
219   // cannot be standard bean type getters and setters, otherwise this would
220   // confuse JAXB
221 
222   /**
223    * @return true if IS_META attribute exists and is truel
224    */
225   public boolean __getIsMeta() {
226     Object o = attrs.get(IS_META);
227     return o != null ? Boolean.parseBoolean(o.toString()) : false;
228   }
229 
230   /**
231    * @return true if IS_ROOT attribute exists and is truel
232    */
233   public boolean __getIsRoot() {
234     Object o = attrs.get(IS_ROOT);
235     return o != null ? Boolean.parseBoolean(o.toString()) : false;
236   }
237 
238   /**
239    * @return true if READONLY attribute exists and is truel
240    */
241   public boolean __getReadOnly() {
242     Object o = attrs.get(READONLY);
243     return o != null ?
244       Boolean.parseBoolean(o.toString()) : HTableDescriptor.DEFAULT_READONLY;
245   }
246 
247   /**
248    * @param value desired value of IS_META attribute
249    */
250   public void __setIsMeta(boolean value) {
251     attrs.put(IS_META, Boolean.toString(value));
252   }
253 
254   /**
255    * @param value desired value of IS_ROOT attribute
256    */
257   public void __setIsRoot(boolean value) {
258     attrs.put(IS_ROOT, Boolean.toString(value));
259   }
260 
261   /**
262    * @param value desired value of READONLY attribute
263    */
264   public void __setReadOnly(boolean value) {
265     attrs.put(READONLY, Boolean.toString(value));
266   }
267 
268   @Override
269   public byte[] createProtobufOutput() {
270     TableSchema.Builder builder = TableSchema.newBuilder();
271     builder.setName(name);
272     for (Map.Entry<QName, Object> e: attrs.entrySet()) {
273       TableSchema.Attribute.Builder attrBuilder = 
274         TableSchema.Attribute.newBuilder();
275       attrBuilder.setName(e.getKey().getLocalPart());
276       attrBuilder.setValue(e.getValue().toString());
277       builder.addAttrs(attrBuilder);
278     }
279     for (ColumnSchemaModel family: columns) {
280       Map<QName, Object> familyAttrs = family.getAny();
281       ColumnSchema.Builder familyBuilder = ColumnSchema.newBuilder();
282       familyBuilder.setName(family.getName());
283       for (Map.Entry<QName, Object> e: familyAttrs.entrySet()) {
284         ColumnSchema.Attribute.Builder attrBuilder = 
285           ColumnSchema.Attribute.newBuilder();
286         attrBuilder.setName(e.getKey().getLocalPart());
287         attrBuilder.setValue(e.getValue().toString());
288         familyBuilder.addAttrs(attrBuilder);
289       }
290       if (familyAttrs.containsKey(TTL)) {
291         familyBuilder.setTtl(
292           Integer.parseInt(familyAttrs.get(TTL).toString()));
293       }
294       if (familyAttrs.containsKey(VERSIONS)) {
295         familyBuilder.setMaxVersions(
296           Integer.parseInt(familyAttrs.get(VERSIONS).toString()));
297       }
298       if (familyAttrs.containsKey(COMPRESSION)) {
299         familyBuilder.setCompression(familyAttrs.get(COMPRESSION).toString());
300       }
301       builder.addColumns(familyBuilder);
302     }
303     if (attrs.containsKey(READONLY)) {
304       builder.setReadOnly(
305         Boolean.parseBoolean(attrs.get(READONLY).toString()));
306     }
307     return builder.build().toByteArray();
308   }
309 
310   @Override
311   public ProtobufMessageHandler getObjectFromMessage(byte[] message) 
312       throws IOException {
313     TableSchema.Builder builder = TableSchema.newBuilder();
314     ProtobufUtil.mergeFrom(builder, message);
315     this.setName(builder.getName());
316     for (TableSchema.Attribute attr: builder.getAttrsList()) {
317       this.addAttribute(attr.getName(), attr.getValue());
318     }
319     if (builder.hasReadOnly()) {
320       this.addAttribute(HTableDescriptor.READONLY, builder.getReadOnly());
321     }
322     for (ColumnSchema family: builder.getColumnsList()) {
323       ColumnSchemaModel familyModel = new ColumnSchemaModel();
324       familyModel.setName(family.getName());
325       for (ColumnSchema.Attribute attr: family.getAttrsList()) {
326         familyModel.addAttribute(attr.getName(), attr.getValue());
327       }
328       if (family.hasTtl()) {
329         familyModel.addAttribute(HColumnDescriptor.TTL, family.getTtl());
330       }
331       if (family.hasMaxVersions()) {
332         familyModel.addAttribute(HConstants.VERSIONS,
333           family.getMaxVersions());
334       }
335       if (family.hasCompression()) {
336         familyModel.addAttribute(HColumnDescriptor.COMPRESSION,
337           family.getCompression());
338       }
339       this.addColumnFamily(familyModel);
340     }
341     return this;
342   }
343 
344   /**
345    * @return a table descriptor
346    */
347   @JsonIgnore
348   public HTableDescriptor getTableDescriptor() {
349     HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(getName()));
350     for (Map.Entry<QName, Object> e: getAny().entrySet()) {
351       htd.setValue(e.getKey().getLocalPart(), e.getValue().toString());
352     }
353     for (ColumnSchemaModel column: getColumns()) {
354       HColumnDescriptor hcd = new HColumnDescriptor(column.getName());
355       for (Map.Entry<QName, Object> e: column.getAny().entrySet()) {
356         hcd.setValue(e.getKey().getLocalPart(), e.getValue().toString());
357       }
358       htd.addFamily(hcd);
359     }
360     return htd;
361   }
362 
363 }