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.Serializable;
23  import java.util.LinkedHashMap;
24  import java.util.Map;
25  
26  import javax.xml.bind.annotation.XmlAnyAttribute;
27  import javax.xml.bind.annotation.XmlAttribute;
28  import javax.xml.bind.annotation.XmlRootElement;
29  import javax.xml.namespace.QName;
30  
31  import org.apache.hadoop.hbase.classification.InterfaceAudience;
32  import org.apache.hadoop.hbase.HColumnDescriptor;
33  import org.apache.hadoop.hbase.HConstants;
34  import org.codehaus.jackson.annotate.JsonAnyGetter;
35  import org.codehaus.jackson.annotate.JsonAnySetter;
36  
37  /**
38   * Representation of a column family schema.
39   * 
40   * <pre>
41   * &lt;complexType name="ColumnSchema"&gt;
42   *   &lt;attribute name="name" type="string"&gt;&lt;/attribute&gt;
43   *   &lt;anyAttribute&gt;&lt;/anyAttribute&gt;
44   * &lt;/complexType&gt;
45   * </pre>
46   */
47  @XmlRootElement(name="ColumnSchema")
48  @InterfaceAudience.Private
49  public class ColumnSchemaModel implements Serializable {
50    private static final long serialVersionUID = 1L;
51    private static QName BLOCKCACHE = new QName(HColumnDescriptor.BLOCKCACHE);
52    private static QName BLOCKSIZE = new QName(HColumnDescriptor.BLOCKSIZE);
53    private static QName BLOOMFILTER = new QName(HColumnDescriptor.BLOOMFILTER);
54    private static QName COMPRESSION = new QName(HColumnDescriptor.COMPRESSION);
55    private static QName IN_MEMORY = new QName(HConstants.IN_MEMORY);
56    private static QName TTL = new QName(HColumnDescriptor.TTL);
57    private static QName VERSIONS = new QName(HConstants.VERSIONS);
58  
59    private String name;
60    private Map<QName,Object> attrs = new LinkedHashMap<QName,Object>();
61  
62    /**
63     * Default constructor
64     */
65    public ColumnSchemaModel() {}
66  
67    /**
68     * Add an attribute to the column family schema
69     * @param name the attribute name
70     * @param value the attribute value
71     */
72    @JsonAnySetter
73    public void addAttribute(String name, Object value) {
74      attrs.put(new QName(name), value);
75    }
76  
77    /**
78     * @param name the attribute name
79     * @return the attribute value
80     */
81    public String getAttribute(String name) {
82      Object o = attrs.get(new QName(name));
83      return o != null ? o.toString(): null;
84    }
85  
86    /**
87     * @return the column name
88     */
89    @XmlAttribute
90    public String getName() {
91      return name;
92    }
93  
94    /**
95     * @return the map for holding unspecified (user) attributes
96     */
97    @XmlAnyAttribute
98    @JsonAnyGetter
99    public Map<QName,Object> getAny() {
100     return attrs;
101   }
102 
103   /**
104    * @param name the table name
105    */
106   public void setName(String name) {
107     this.name = name;
108   }
109 
110   /* (non-Javadoc)
111    * @see java.lang.Object#toString()
112    */
113   @Override
114   public String toString() {
115     StringBuilder sb = new StringBuilder();
116     sb.append("{ NAME => '");
117     sb.append(name);
118     sb.append('\'');
119     for (Map.Entry<QName,Object> e: attrs.entrySet()) {
120       sb.append(", ");
121       sb.append(e.getKey().getLocalPart());
122       sb.append(" => '");
123       sb.append(e.getValue().toString());
124       sb.append('\'');
125     }
126     sb.append(" }");
127     return sb.toString();
128   }
129 
130   // getters and setters for common schema attributes
131 
132   // cannot be standard bean type getters and setters, otherwise this would
133   // confuse JAXB
134 
135   /**
136    * @return true if the BLOCKCACHE attribute is present and true
137    */
138   public boolean __getBlockcache() {
139     Object o = attrs.get(BLOCKCACHE);
140     return o != null ?
141       Boolean.parseBoolean(o.toString()) : HColumnDescriptor.DEFAULT_BLOCKCACHE;
142   }
143 
144   /**
145    * @return the value of the BLOCKSIZE attribute or its default if it is unset
146    */
147   public int __getBlocksize() {
148     Object o = attrs.get(BLOCKSIZE);
149     return o != null ?
150       Integer.parseInt(o.toString()) : HColumnDescriptor.DEFAULT_BLOCKSIZE;
151   }
152 
153   /**
154    * @return the value of the BLOOMFILTER attribute or its default if unset
155    */
156   public String __getBloomfilter() {
157     Object o = attrs.get(BLOOMFILTER);
158     return o != null ? o.toString() : HColumnDescriptor.DEFAULT_BLOOMFILTER;
159   }
160 
161   /**
162    * @return the value of the COMPRESSION attribute or its default if unset
163    */
164   public String __getCompression() {
165     Object o = attrs.get(COMPRESSION);
166     return o != null ? o.toString() : HColumnDescriptor.DEFAULT_COMPRESSION;
167   }
168 
169   /**
170    * @return true if the IN_MEMORY attribute is present and true
171    */
172   public boolean __getInMemory() {
173     Object o = attrs.get(IN_MEMORY);
174     return o != null ?
175       Boolean.parseBoolean(o.toString()) : HColumnDescriptor.DEFAULT_IN_MEMORY;
176   }
177 
178   /**
179    * @return the value of the TTL attribute or its default if it is unset
180    */
181   public int __getTTL() {
182     Object o = attrs.get(TTL);
183     return o != null ?
184       Integer.parseInt(o.toString()) : HColumnDescriptor.DEFAULT_TTL;
185   }
186 
187   /**
188    * @return the value of the VERSIONS attribute or its default if it is unset
189    */
190   public int __getVersions() {
191     Object o = attrs.get(VERSIONS);
192     return o != null ?
193       Integer.parseInt(o.toString()) : HColumnDescriptor.DEFAULT_VERSIONS;
194   }
195 
196   /**
197    * @param value the desired value of the BLOCKSIZE attribute
198    */
199   public void __setBlocksize(int value) {
200     attrs.put(BLOCKSIZE, Integer.toString(value));
201   }
202 
203   /**
204    * @param value the desired value of the BLOCKCACHE attribute
205    */
206   public void __setBlockcache(boolean value) {
207     attrs.put(BLOCKCACHE, Boolean.toString(value));
208   }
209 
210   public void __setBloomfilter(String value) {
211     attrs.put(BLOOMFILTER, value);
212   }
213 
214   /**
215    * @param value the desired value of the COMPRESSION attribute
216    */
217   public void __setCompression(String value) {
218     attrs.put(COMPRESSION, value); 
219   }
220 
221   /**
222    * @param value the desired value of the IN_MEMORY attribute
223    */
224   public void __setInMemory(boolean value) {
225     attrs.put(IN_MEMORY, Boolean.toString(value));
226   }
227 
228   /**
229    * @param value the desired value of the TTL attribute
230    */
231   public void __setTTL(int value) {
232     attrs.put(TTL, Integer.toString(value));
233   }
234 
235   /**
236    * @param value the desired value of the VERSIONS attribute
237    */
238   public void __setVersions(int value) {
239     attrs.put(VERSIONS, Integer.toString(value));
240   }
241 }