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