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.assertNull;
022import static org.junit.Assert.assertTrue;
023
024import java.util.Map;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HColumnDescriptor;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.KeepDeletedCells;
029import org.apache.hadoop.hbase.exceptions.DeserializationException;
030import org.apache.hadoop.hbase.exceptions.HBaseException;
031import org.apache.hadoop.hbase.io.compress.Compression;
032import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
033import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
034import org.apache.hadoop.hbase.regionserver.BloomType;
035import org.apache.hadoop.hbase.testclassification.MiscTests;
036import org.apache.hadoop.hbase.testclassification.SmallTests;
037import org.apache.hadoop.hbase.util.BuilderStyleTest;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.apache.hadoop.hbase.util.PrettyPrinter;
040import org.junit.Assert;
041import org.junit.ClassRule;
042import org.junit.Rule;
043import org.junit.Test;
044import org.junit.experimental.categories.Category;
045import org.junit.rules.ExpectedException;
046
047@Category({ MiscTests.class, SmallTests.class })
048public class TestColumnFamilyDescriptorBuilder {
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051    HBaseClassTestRule.forClass(TestColumnFamilyDescriptorBuilder.class);
052
053  @Rule
054  public ExpectedException expectedEx = ExpectedException.none();
055
056  @Test
057  public void testBuilder() throws DeserializationException {
058    ColumnFamilyDescriptorBuilder builder =
059      ColumnFamilyDescriptorBuilder.newBuilder(HConstants.CATALOG_FAMILY).setInMemory(true)
060        .setScope(HConstants.REPLICATION_SCOPE_LOCAL).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(
088      Bytes.equals(hcd.getValue(Bytes.toBytes("a")), 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    /*
148     * HColumnDescriptor should have a builder style setup where setXXX/addXXX methods can be
149     * chainable together: . For example: HColumnDescriptor hcd = new HColumnDescriptor()
150     * .setFoo(foo) .setBar(bar) .setBuz(buz) This test ensures that all methods starting with "set"
151     * returns the declaring object
152     */
153
154    BuilderStyleTest.assertClassesAreBuilderStyle(ColumnFamilyDescriptorBuilder.class);
155  }
156
157  @Test
158  public void testSetTimeToLive() throws HBaseException {
159    String ttl;
160    ColumnFamilyDescriptorBuilder builder =
161      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("foo"));
162
163    ttl = "50000";
164    builder.setTimeToLive(ttl);
165    Assert.assertEquals(50000, builder.build().getTimeToLive());
166
167    ttl = "50000 seconds";
168    builder.setTimeToLive(ttl);
169    Assert.assertEquals(50000, builder.build().getTimeToLive());
170
171    ttl = "";
172    builder.setTimeToLive(ttl);
173    Assert.assertEquals(0, builder.build().getTimeToLive());
174
175    ttl = "FOREVER";
176    builder.setTimeToLive(ttl);
177    Assert.assertEquals(HConstants.FOREVER, builder.build().getTimeToLive());
178
179    ttl = "1 HOUR 10 minutes 1 second";
180    builder.setTimeToLive(ttl);
181    Assert.assertEquals(4201, builder.build().getTimeToLive());
182
183    ttl = "500 Days 23 HOURS";
184    builder.setTimeToLive(ttl);
185    Assert.assertEquals(43282800, builder.build().getTimeToLive());
186
187    ttl = "43282800 SECONDS (500 Days 23 hours)";
188    builder.setTimeToLive(ttl);
189    Assert.assertEquals(43282800, builder.build().getTimeToLive());
190  }
191
192  @Test
193  public void testSetBlocksize() throws HBaseException {
194    String blocksize;
195    ColumnFamilyDescriptorBuilder builder =
196      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("foo"));
197
198    blocksize = "131072";
199    builder.setBlocksize(blocksize);
200    assertEquals(131072, builder.build().getBlocksize());
201
202    blocksize = "100KB";
203    builder.setBlocksize(blocksize);
204    assertEquals(102400, builder.build().getBlocksize());
205
206    blocksize = "1MB";
207    builder.setBlocksize(blocksize);
208    assertEquals(1048576, builder.build().getBlocksize());
209
210    // ignore case
211    blocksize = "64kb 512B";
212    builder.setBlocksize(blocksize);
213    assertEquals(66048, builder.build().getBlocksize());
214
215    blocksize = "66048 B (64KB 512B)";
216    builder.setBlocksize(blocksize);
217    assertEquals(66048, builder.build().getBlocksize());
218  }
219
220  /**
221   * Test for verifying the ColumnFamilyDescriptorBuilder's default values so that backward
222   * compatibility with hbase-1.x can be mantained (see HBASE-24981).
223   */
224  @Test
225  public void testDefaultBuilder() {
226    final Map<String, String> defaultValueMap = ColumnFamilyDescriptorBuilder.getDefaultValues();
227    assertEquals(defaultValueMap.size(), 11);
228    assertEquals(defaultValueMap.get(ColumnFamilyDescriptorBuilder.BLOOMFILTER),
229      BloomType.ROW.toString());
230    assertEquals(defaultValueMap.get(ColumnFamilyDescriptorBuilder.REPLICATION_SCOPE), "0");
231    assertEquals(defaultValueMap.get(ColumnFamilyDescriptorBuilder.MAX_VERSIONS), "1");
232    assertEquals(defaultValueMap.get(ColumnFamilyDescriptorBuilder.MIN_VERSIONS), "0");
233    assertEquals(defaultValueMap.get(ColumnFamilyDescriptorBuilder.COMPRESSION),
234      Compression.Algorithm.NONE.toString());
235    assertEquals(defaultValueMap.get(ColumnFamilyDescriptorBuilder.TTL),
236      Integer.toString(Integer.MAX_VALUE));
237    assertEquals(defaultValueMap.get(ColumnFamilyDescriptorBuilder.BLOCKSIZE),
238      Integer.toString(64 * 1024));
239    assertEquals(defaultValueMap.get(ColumnFamilyDescriptorBuilder.IN_MEMORY),
240      Boolean.toString(false));
241    assertEquals(defaultValueMap.get(ColumnFamilyDescriptorBuilder.BLOCKCACHE),
242      Boolean.toString(true));
243    assertEquals(defaultValueMap.get(ColumnFamilyDescriptorBuilder.KEEP_DELETED_CELLS),
244      KeepDeletedCells.FALSE.toString());
245    assertEquals(defaultValueMap.get(ColumnFamilyDescriptorBuilder.DATA_BLOCK_ENCODING),
246      DataBlockEncoding.NONE.toString());
247  }
248
249  @Test
250  public void testSetEmptyValue() {
251    ColumnFamilyDescriptorBuilder builder =
252      ColumnFamilyDescriptorBuilder.newBuilder(HConstants.CATALOG_FAMILY);
253    String testConf = "TestConfiguration";
254    String testValue = "TestValue";
255    // test set value
256    builder.setValue(testValue, "2");
257    assertEquals("2", Bytes.toString(builder.build().getValue(Bytes.toBytes(testValue))));
258    builder.setValue(testValue, "");
259    assertNull(builder.build().getValue(Bytes.toBytes(testValue)));
260
261    // test set configuration
262    builder.setConfiguration(testConf, "1");
263    assertEquals("1", builder.build().getConfigurationValue(testConf));
264    builder.setConfiguration(testConf, "");
265    assertNull(builder.build().getConfigurationValue(testConf));
266  }
267}