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.HConstants;
030import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
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(ColumnFamilyDescriptorBuilder.BLOCKCACHE);
048  private static QName BLOCKSIZE = new QName(ColumnFamilyDescriptorBuilder.BLOCKSIZE);
049  private static QName BLOOMFILTER = new QName(ColumnFamilyDescriptorBuilder.BLOOMFILTER);
050  private static QName COMPRESSION = new QName(ColumnFamilyDescriptorBuilder.COMPRESSION);
051  private static QName IN_MEMORY = new QName(HConstants.IN_MEMORY);
052  private static QName TTL = new QName(ColumnFamilyDescriptorBuilder.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
133      ? Boolean.parseBoolean(o.toString())
134      : ColumnFamilyDescriptorBuilder.DEFAULT_BLOCKCACHE;
135  }
136
137  /** Returns the value of the BLOCKSIZE attribute or its default if it is unset */
138  public int __getBlocksize() {
139    Object o = attrs.get(BLOCKSIZE);
140    return o != null
141      ? Integer.parseInt(o.toString())
142      : ColumnFamilyDescriptorBuilder.DEFAULT_BLOCKSIZE;
143  }
144
145  /** Returns the value of the BLOOMFILTER attribute or its default if unset */
146  public String __getBloomfilter() {
147    Object o = attrs.get(BLOOMFILTER);
148    return o != null ? o.toString() : ColumnFamilyDescriptorBuilder.DEFAULT_BLOOMFILTER.name();
149  }
150
151  /** Returns the value of the COMPRESSION attribute or its default if unset */
152  public String __getCompression() {
153    Object o = attrs.get(COMPRESSION);
154    return o != null ? o.toString() : ColumnFamilyDescriptorBuilder.DEFAULT_COMPRESSION.name();
155  }
156
157  /** Returns true if the IN_MEMORY attribute is present and true */
158  public boolean __getInMemory() {
159    Object o = attrs.get(IN_MEMORY);
160    return o != null
161      ? Boolean.parseBoolean(o.toString())
162      : ColumnFamilyDescriptorBuilder.DEFAULT_IN_MEMORY;
163  }
164
165  /** Returns the value of the TTL attribute or its default if it is unset */
166  public int __getTTL() {
167    Object o = attrs.get(TTL);
168    return o != null ? Integer.parseInt(o.toString()) : ColumnFamilyDescriptorBuilder.DEFAULT_TTL;
169  }
170
171  /** Returns the value of the VERSIONS attribute or its default if it is unset */
172  public int __getVersions() {
173    Object o = attrs.get(VERSIONS);
174    return o != null
175      ? Integer.parseInt(o.toString())
176      : ColumnFamilyDescriptorBuilder.DEFAULT_MAX_VERSIONS;
177  }
178
179  /**
180   * @param value the desired value of the BLOCKSIZE attribute
181   */
182  public void __setBlocksize(int value) {
183    attrs.put(BLOCKSIZE, Integer.toString(value));
184  }
185
186  /**
187   * @param value the desired value of the BLOCKCACHE attribute
188   */
189  public void __setBlockcache(boolean value) {
190    attrs.put(BLOCKCACHE, Boolean.toString(value));
191  }
192
193  public void __setBloomfilter(String value) {
194    attrs.put(BLOOMFILTER, value);
195  }
196
197  /**
198   * @param value the desired value of the COMPRESSION attribute
199   */
200  public void __setCompression(String value) {
201    attrs.put(COMPRESSION, value);
202  }
203
204  /**
205   * @param value the desired value of the IN_MEMORY attribute
206   */
207  public void __setInMemory(boolean value) {
208    attrs.put(IN_MEMORY, Boolean.toString(value));
209  }
210
211  /**
212   * @param value the desired value of the TTL attribute
213   */
214  public void __setTTL(int value) {
215    attrs.put(TTL, Integer.toString(value));
216  }
217
218  /**
219   * @param value the desired value of the VERSIONS attribute
220   */
221  public void __setVersions(int value) {
222    attrs.put(VERSIONS, Integer.toString(value));
223  }
224}