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