View Javadoc

1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.rest.model;
22  
23  import java.io.Serializable;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import javax.xml.bind.annotation.XmlAnyAttribute;
28  import javax.xml.bind.annotation.XmlAttribute;
29  import javax.xml.bind.annotation.XmlRootElement;
30  import javax.xml.namespace.QName;
31  
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  public class ColumnSchemaModel implements Serializable {
47    private static final long serialVersionUID = 1L;
48    private static QName BLOCKCACHE = new QName(HColumnDescriptor.BLOCKCACHE);
49    private static QName BLOCKSIZE = new QName(HColumnDescriptor.BLOCKSIZE);
50    private static QName BLOOMFILTER = new QName(HColumnDescriptor.BLOOMFILTER);
51    private static QName COMPRESSION = new QName(HColumnDescriptor.COMPRESSION);
52    private static QName IN_MEMORY = new QName(HConstants.IN_MEMORY);
53    private static QName TTL = new QName(HColumnDescriptor.TTL);
54    private static QName VERSIONS = new QName(HConstants.VERSIONS);
55  
56    private String name;
57    private Map<QName,Object> attrs = new HashMap<QName,Object>();
58  
59    /**
60     * Default constructor
61     */
62    public ColumnSchemaModel() {}
63  
64    /**
65     * Add an attribute to the column family schema
66     * @param name the attribute name
67     * @param value the attribute value
68     */
69    public void addAttribute(String name, Object value) {
70      attrs.put(new QName(name), value);
71    }
72  
73    /**
74     * @param name the attribute name
75     * @return the attribute value
76     */
77    public String getAttribute(String name) {
78      Object o = attrs.get(new QName(name));
79      return o != null ? o.toString(): null;
80    }
81  
82    /**
83     * @return the column name
84     */
85    @XmlAttribute
86    public String getName() {
87      return name;
88    }
89  
90    /**
91     * @return the map for holding unspecified (user) attributes
92     */
93    @XmlAnyAttribute
94    public Map<QName,Object> getAny() {
95      return attrs;
96    }
97  
98    /**
99     * @param name the table name
100    */
101   public void setName(String name) {
102     this.name = name;
103   }
104 
105   /* (non-Javadoc)
106    * @see java.lang.Object#toString()
107    */
108   @Override
109   public String toString() {
110     StringBuilder sb = new StringBuilder();
111     sb.append("{ NAME => '");
112     sb.append(name);
113     sb.append('\'');
114     for (Map.Entry<QName,Object> e: attrs.entrySet()) {
115       sb.append(", ");
116       sb.append(e.getKey().getLocalPart());
117       sb.append(" => '");
118       sb.append(e.getValue().toString());
119       sb.append('\'');
120     }
121     sb.append(" }");
122     return sb.toString();
123   }
124 
125   // getters and setters for common schema attributes
126 
127   // cannot be standard bean type getters and setters, otherwise this would
128   // confuse JAXB
129 
130   /**
131    * @return true if the BLOCKCACHE attribute is present and true
132    */
133   public boolean __getBlockcache() {
134     Object o = attrs.get(BLOCKCACHE);
135     return o != null ? 
136       Boolean.valueOf(o.toString()) : HColumnDescriptor.DEFAULT_BLOCKCACHE;
137   }
138 
139   /**
140    * @return the value of the BLOCKSIZE attribute or its default if it is unset
141    */
142   public int __getBlocksize() {
143     Object o = attrs.get(BLOCKSIZE);
144     return o != null ? 
145       Integer.valueOf(o.toString()) : HColumnDescriptor.DEFAULT_BLOCKSIZE;
146   }
147 
148   /**
149    * @return the value of the BLOOMFILTER attribute or its default if unset
150    */
151   public String __getBloomfilter() {
152     Object o = attrs.get(BLOOMFILTER);
153     return o != null ? o.toString() : HColumnDescriptor.DEFAULT_BLOOMFILTER;
154   }
155 
156   /**
157    * @return the value of the COMPRESSION attribute or its default if unset
158    */
159   public String __getCompression() {
160     Object o = attrs.get(COMPRESSION);
161     return o != null ? o.toString() : HColumnDescriptor.DEFAULT_COMPRESSION;
162   }
163 
164   /**
165    * @return true if the IN_MEMORY attribute is present and true
166    */
167   public boolean __getInMemory() {
168     Object o = attrs.get(IN_MEMORY);
169     return o != null ? 
170       Boolean.valueOf(o.toString()) : HColumnDescriptor.DEFAULT_IN_MEMORY;
171   }
172 
173   /**
174    * @return the value of the TTL attribute or its default if it is unset
175    */
176   public int __getTTL() {
177     Object o = attrs.get(TTL);
178     return o != null ? 
179       Integer.valueOf(o.toString()) : HColumnDescriptor.DEFAULT_TTL;
180   }
181 
182   /**
183    * @return the value of the VERSIONS attribute or its default if it is unset
184    */
185   public int __getVersions() {
186     Object o = attrs.get(VERSIONS);
187     return o != null ? 
188       Integer.valueOf(o.toString()) : HColumnDescriptor.DEFAULT_VERSIONS;
189   }
190 
191   /**
192    * @param value the desired value of the BLOCKSIZE attribute
193    */
194   public void __setBlocksize(int value) {
195     attrs.put(BLOCKSIZE, Integer.toString(value));
196   }
197 
198   /**
199    * @param value the desired value of the BLOCKCACHE attribute
200    */
201   public void __setBlockcache(boolean value) {
202     attrs.put(BLOCKCACHE, Boolean.toString(value));
203   }
204 
205   public void __setBloomfilter(String value) {
206     attrs.put(BLOOMFILTER, value);
207   }
208 
209   /**
210    * @param value the desired value of the COMPRESSION attribute
211    */
212   public void __setCompression(String value) {
213     attrs.put(COMPRESSION, value); 
214   }
215 
216   /**
217    * @param value the desired value of the IN_MEMORY attribute
218    */
219   public void __setInMemory(boolean value) {
220     attrs.put(IN_MEMORY, Boolean.toString(value));
221   }
222 
223   /**
224    * @param value the desired value of the TTL attribute
225    */
226   public void __setTTL(int value) {
227     attrs.put(TTL, Integer.toString(value));
228   }
229 
230   /**
231    * @param value the desired value of the VERSIONS attribute
232    */
233   public void __setVersions(int value) {
234     attrs.put(VERSIONS, Integer.toString(value));
235   }
236 }