001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.rest.model;
021
022import com.fasterxml.jackson.annotation.JsonAnyGetter;
023import com.fasterxml.jackson.annotation.JsonAnySetter;
024import java.io.Serializable;
025import java.util.LinkedHashMap;
026import java.util.Map;
027import javax.xml.bind.annotation.XmlAnyAttribute;
028import javax.xml.bind.annotation.XmlAttribute;
029import javax.xml.bind.annotation.XmlRootElement;
030import javax.xml.namespace.QName;
031import org.apache.hadoop.hbase.HConstants;
032import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
033import org.apache.yetus.audience.InterfaceAudience;
034
035/**
036 * Representation of a column family schema.
037 * 
038 * <pre>
039 * &lt;complexType name="ColumnSchema"&gt;
040 *   &lt;attribute name="name" type="string"&gt;&lt;/attribute&gt;
041 *   &lt;anyAttribute&gt;&lt;/anyAttribute&gt;
042 * &lt;/complexType&gt;
043 * </pre>
044 */
045@XmlRootElement(name="ColumnSchema")
046@InterfaceAudience.Private
047public class ColumnSchemaModel implements Serializable {
048  private static final long serialVersionUID = 1L;
049  private static QName BLOCKCACHE = new QName(ColumnFamilyDescriptorBuilder.BLOCKCACHE);
050  private static QName BLOCKSIZE = new QName(ColumnFamilyDescriptorBuilder.BLOCKSIZE);
051  private static QName BLOOMFILTER = new QName(ColumnFamilyDescriptorBuilder.BLOOMFILTER);
052  private static QName COMPRESSION = new QName(ColumnFamilyDescriptorBuilder.COMPRESSION);
053  private static QName IN_MEMORY = new QName(HConstants.IN_MEMORY);
054  private static QName TTL = new QName(ColumnFamilyDescriptorBuilder.TTL);
055  private static QName VERSIONS = new QName(HConstants.VERSIONS);
056
057  private String name;
058  private Map<QName,Object> attrs = new LinkedHashMap<>();
059
060  /**
061   * Default constructor
062   */
063  public ColumnSchemaModel() {}
064
065  /**
066   * Add an attribute to the column family schema
067   * @param name the attribute name
068   * @param value the attribute value
069   */
070  @JsonAnySetter
071  public void addAttribute(String name, Object value) {
072    attrs.put(new QName(name), value);
073  }
074
075  /**
076   * @param name the attribute name
077   * @return the attribute value
078   */
079  public String getAttribute(String name) {
080    Object o = attrs.get(new QName(name));
081    return o != null ? o.toString(): null;
082  }
083
084  /**
085   * @return the column name
086   */
087  @XmlAttribute
088  public String getName() {
089    return name;
090  }
091
092  /**
093   * @return the map for holding unspecified (user) attributes
094   */
095  @XmlAnyAttribute
096  @JsonAnyGetter
097  public Map<QName,Object> getAny() {
098    return attrs;
099  }
100
101  /**
102   * @param name the table name
103   */
104  public void setName(String name) {
105    this.name = name;
106  }
107
108  /* (non-Javadoc)
109   * @see java.lang.Object#toString()
110   */
111  @Override
112  public String toString() {
113    StringBuilder sb = new StringBuilder();
114    sb.append("{ NAME => '");
115    sb.append(name);
116    sb.append('\'');
117    for (Map.Entry<QName,Object> e: attrs.entrySet()) {
118      sb.append(", ");
119      sb.append(e.getKey().getLocalPart());
120      sb.append(" => '");
121      sb.append(e.getValue().toString());
122      sb.append('\'');
123    }
124    sb.append(" }");
125    return sb.toString();
126  }
127
128  // getters and setters for common schema attributes
129
130  // cannot be standard bean type getters and setters, otherwise this would
131  // confuse JAXB
132
133  /**
134   * @return true if the BLOCKCACHE attribute is present and true
135   */
136  public boolean __getBlockcache() {
137    Object o = attrs.get(BLOCKCACHE);
138    return o != null ? Boolean.parseBoolean(o.toString()) :
139      ColumnFamilyDescriptorBuilder.DEFAULT_BLOCKCACHE;
140  }
141
142  /**
143   * @return the value of the BLOCKSIZE attribute or its default if it is unset
144   */
145  public int __getBlocksize() {
146    Object o = attrs.get(BLOCKSIZE);
147    return o != null ? Integer.parseInt(o.toString()) :
148      ColumnFamilyDescriptorBuilder.DEFAULT_BLOCKSIZE;
149  }
150
151  /**
152   * @return the value of the BLOOMFILTER attribute or its default if unset
153   */
154  public String __getBloomfilter() {
155    Object o = attrs.get(BLOOMFILTER);
156    return o != null ? o.toString() : ColumnFamilyDescriptorBuilder.DEFAULT_BLOOMFILTER.name();
157  }
158
159  /**
160   * @return the value of the COMPRESSION attribute or its default if unset
161   */
162  public String __getCompression() {
163    Object o = attrs.get(COMPRESSION);
164    return o != null ? o.toString() : ColumnFamilyDescriptorBuilder.DEFAULT_COMPRESSION.name();
165  }
166
167  /**
168   * @return true if the IN_MEMORY attribute is present and true
169   */
170  public boolean __getInMemory() {
171    Object o = attrs.get(IN_MEMORY);
172    return o != null ? 
173      Boolean.parseBoolean(o.toString()) : ColumnFamilyDescriptorBuilder.DEFAULT_IN_MEMORY;
174  }
175
176  /**
177   * @return the value of the TTL attribute or its default if it is unset
178   */
179  public int __getTTL() {
180    Object o = attrs.get(TTL);
181    return o != null ? Integer.parseInt(o.toString()) : ColumnFamilyDescriptorBuilder.DEFAULT_TTL;
182  }
183
184  /**
185   * @return the value of the VERSIONS attribute or its default if it is unset
186   */
187  public int __getVersions() {
188    Object o = attrs.get(VERSIONS);
189    return o != null ? Integer.parseInt(o.toString()) :
190      ColumnFamilyDescriptorBuilder.DEFAULT_MAX_VERSIONS;
191  }
192
193  /**
194   * @param value the desired value of the BLOCKSIZE attribute
195   */
196  public void __setBlocksize(int value) {
197    attrs.put(BLOCKSIZE, Integer.toString(value));
198  }
199
200  /**
201   * @param value the desired value of the BLOCKCACHE attribute
202   */
203  public void __setBlockcache(boolean value) {
204    attrs.put(BLOCKCACHE, Boolean.toString(value));
205  }
206
207  public void __setBloomfilter(String value) {
208    attrs.put(BLOOMFILTER, value);
209  }
210
211  /**
212   * @param value the desired value of the COMPRESSION attribute
213   */
214  public void __setCompression(String value) {
215    attrs.put(COMPRESSION, value); 
216  }
217
218  /**
219   * @param value the desired value of the IN_MEMORY attribute
220   */
221  public void __setInMemory(boolean value) {
222    attrs.put(IN_MEMORY, Boolean.toString(value));
223  }
224
225  /**
226   * @param value the desired value of the TTL attribute
227   */
228  public void __setTTL(int value) {
229    attrs.put(TTL, Integer.toString(value));
230  }
231
232  /**
233   * @param value the desired value of the VERSIONS attribute
234   */
235  public void __setVersions(int value) {
236    attrs.put(VERSIONS, Integer.toString(value));
237  }
238}