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