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;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import org.apache.hadoop.hbase.exceptions.DeserializationException;
024import org.apache.hadoop.hbase.exceptions.HBaseException;
025import org.apache.hadoop.hbase.io.compress.Compression;
026import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
027import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
028import org.apache.hadoop.hbase.regionserver.BloomType;
029import org.apache.hadoop.hbase.testclassification.MiscTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.apache.hadoop.hbase.util.BuilderStyleTest;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.apache.hadoop.hbase.util.PrettyPrinter;
034import org.junit.Assert;
035import org.junit.ClassRule;
036import org.junit.Rule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039import org.junit.rules.ExpectedException;
040
041/**
042 * Tests the HColumnDescriptor with appropriate arguments.
043 *
044 * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0 together with
045 *            {@link HColumnDescriptor}.
046 */
047@Category({MiscTests.class, SmallTests.class})
048@Deprecated
049public class TestHColumnDescriptor {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053      HBaseClassTestRule.forClass(TestHColumnDescriptor.class);
054
055  @Rule
056  public ExpectedException expectedEx = ExpectedException.none();
057  @Test
058  public void testPb() throws DeserializationException {
059    HColumnDescriptor hcd = new HColumnDescriptor(
060        new HColumnDescriptor(HConstants.CATALOG_FAMILY)
061            .setInMemory(true)
062            .setScope(HConstants.REPLICATION_SCOPE_LOCAL)
063            .setBloomFilterType(BloomType.NONE)
064            .setCacheDataInL1(true));
065    final int v = 123;
066    hcd.setBlocksize(v);
067    hcd.setTimeToLive(v);
068    hcd.setBlockCacheEnabled(!HColumnDescriptor.DEFAULT_BLOCKCACHE);
069    hcd.setValue("a", "b");
070    hcd.setMaxVersions(v);
071    assertEquals(v, hcd.getMaxVersions());
072    hcd.setMinVersions(v);
073    assertEquals(v, hcd.getMinVersions());
074    hcd.setKeepDeletedCells(KeepDeletedCells.TRUE);
075    hcd.setInMemory(!HColumnDescriptor.DEFAULT_IN_MEMORY);
076    boolean inmemory = hcd.isInMemory();
077    hcd.setScope(v);
078    hcd.setDataBlockEncoding(DataBlockEncoding.FAST_DIFF);
079    hcd.setBloomFilterType(BloomType.ROW);
080    hcd.setCompressionType(Algorithm.SNAPPY);
081    hcd.setMobEnabled(true);
082    hcd.setMobThreshold(1000L);
083    hcd.setDFSReplication((short) v);
084
085    byte [] bytes = hcd.toByteArray();
086    HColumnDescriptor deserializedHcd = HColumnDescriptor.parseFrom(bytes);
087    assertTrue(hcd.equals(deserializedHcd));
088    assertEquals(v, hcd.getBlocksize());
089    assertEquals(v, hcd.getTimeToLive());
090    assertEquals(v, hcd.getScope());
091    assertEquals(hcd.getValue("a"), deserializedHcd.getValue("a"));
092    assertEquals(hcd.getMaxVersions(), deserializedHcd.getMaxVersions());
093    assertEquals(hcd.getMinVersions(), deserializedHcd.getMinVersions());
094    assertEquals(hcd.getKeepDeletedCells(), deserializedHcd.getKeepDeletedCells());
095    assertEquals(inmemory, deserializedHcd.isInMemory());
096    assertEquals(hcd.getScope(), deserializedHcd.getScope());
097    assertTrue(deserializedHcd.getCompressionType().equals(Compression.Algorithm.SNAPPY));
098    assertTrue(deserializedHcd.getDataBlockEncoding().equals(DataBlockEncoding.FAST_DIFF));
099    assertTrue(deserializedHcd.getBloomFilterType().equals(BloomType.ROW));
100    assertEquals(hcd.isMobEnabled(), deserializedHcd.isMobEnabled());
101    assertEquals(hcd.getMobThreshold(), deserializedHcd.getMobThreshold());
102    assertEquals(v, deserializedHcd.getDFSReplication());
103  }
104
105  /**
106   * Tests HColumnDescriptor with empty familyName
107   */
108  @Test
109  public void testHColumnDescriptorShouldThrowIAEWhenFamilyNameEmpty() throws Exception {
110    expectedEx.expect(IllegalArgumentException.class);
111    expectedEx.expectMessage("Column Family name can not be empty");
112    new HColumnDescriptor(Bytes.toBytes(""));
113  }
114
115  /**
116   * Test that we add and remove strings from configuration properly.
117   */
118  @Test
119  public void testAddGetRemoveConfiguration() throws Exception {
120    HColumnDescriptor desc = new HColumnDescriptor("foo");
121    String key = "Some";
122    String value = "value";
123    desc.setConfiguration(key, value);
124    assertEquals(value, desc.getConfigurationValue(key));
125    desc.removeConfiguration(key);
126    assertEquals(null, desc.getConfigurationValue(key));
127  }
128
129  @Test
130  public void testMobValuesInHColumnDescriptorShouldReadable() {
131    boolean isMob = true;
132    long threshold = 1000;
133    String policy = "weekly";
134    // We unify the format of all values saved in the descriptor.
135    // Each value is stored as bytes of string.
136    String isMobString = PrettyPrinter.format(String.valueOf(isMob),
137            HColumnDescriptor.getUnit(HColumnDescriptor.IS_MOB));
138    String thresholdString = PrettyPrinter.format(String.valueOf(threshold),
139            HColumnDescriptor.getUnit(HColumnDescriptor.MOB_THRESHOLD));
140    String policyString = PrettyPrinter.format(Bytes.toStringBinary(Bytes.toBytes(policy)),
141        HColumnDescriptor.getUnit(HColumnDescriptor.MOB_COMPACT_PARTITION_POLICY));
142    assertEquals(String.valueOf(isMob), isMobString);
143    assertEquals(String.valueOf(threshold), thresholdString);
144    assertEquals(String.valueOf(policy), policyString);
145  }
146
147  @Test
148  public void testClassMethodsAreBuilderStyle() {
149    /* HColumnDescriptor should have a builder style setup where setXXX/addXXX methods
150     * can be chainable together:
151     * . For example:
152     * HColumnDescriptor hcd
153     *   = new HColumnDescriptor()
154     *     .setFoo(foo)
155     *     .setBar(bar)
156     *     .setBuz(buz)
157     *
158     * This test ensures that all methods starting with "set" returns the declaring object
159     */
160
161    BuilderStyleTest.assertClassesAreBuilderStyle(HColumnDescriptor.class);
162  }
163
164  @Test
165  public void testSetTimeToLive() throws HBaseException {
166    String ttl;
167    HColumnDescriptor desc = new HColumnDescriptor("foo");
168
169    ttl = "50000";
170    desc.setTimeToLive(ttl);
171    Assert.assertEquals(50000, desc.getTimeToLive());
172
173    ttl = "50000 seconds";
174    desc.setTimeToLive(ttl);
175    Assert.assertEquals(50000, desc.getTimeToLive());
176
177    ttl = "";
178    desc.setTimeToLive(ttl);
179    Assert.assertEquals(0, desc.getTimeToLive());
180
181    ttl = "FOREVER";
182    desc.setTimeToLive(ttl);
183    Assert.assertEquals(HConstants.FOREVER, desc.getTimeToLive());
184
185    ttl = "1 HOUR 10 minutes 1 second";
186    desc.setTimeToLive(ttl);
187    Assert.assertEquals(4201, desc.getTimeToLive());
188
189    ttl = "500 Days 23 HOURS";
190    desc.setTimeToLive(ttl);
191    Assert.assertEquals(43282800, desc.getTimeToLive());
192
193    ttl = "43282800 SECONDS (500 Days 23 hours)";
194    desc.setTimeToLive(ttl);
195    Assert.assertEquals(43282800, desc.getTimeToLive());
196  }
197}