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