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