View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import org.apache.hadoop.hbase.exceptions.DeserializationException;
24  import org.apache.hadoop.hbase.io.compress.Compression;
25  import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
26  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
27  import org.apache.hadoop.hbase.regionserver.BloomType;
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.apache.hadoop.hbase.util.PrettyPrinter;
30  import org.apache.hadoop.hbase.testclassification.SmallTests;
31  import org.apache.hadoop.hbase.util.BuilderStyleTest;
32  import org.junit.experimental.categories.Category;
33  import org.junit.Test;
34  
35  /** Tests the HColumnDescriptor with appropriate arguments */
36  @Category(SmallTests.class)
37  public class TestHColumnDescriptor {
38    @Test
39    public void testPb() throws DeserializationException {
40      HColumnDescriptor hcd = new HColumnDescriptor(
41        HTableDescriptor.META_TABLEDESC.getColumnFamilies()[0]);
42      final int v = 123;
43      hcd.setBlocksize(v);
44      hcd.setTimeToLive(v);
45      hcd.setBlockCacheEnabled(!HColumnDescriptor.DEFAULT_BLOCKCACHE);
46      hcd.setValue("a", "b");
47      hcd.setMaxVersions(v);
48      assertEquals(v, hcd.getMaxVersions());
49      hcd.setMinVersions(v);
50      assertEquals(v, hcd.getMinVersions());
51      hcd.setKeepDeletedCells(KeepDeletedCells.TRUE);
52      hcd.setInMemory(!HColumnDescriptor.DEFAULT_IN_MEMORY);
53      boolean inmemory = hcd.isInMemory();
54      hcd.setScope(v);
55      hcd.setDataBlockEncoding(DataBlockEncoding.FAST_DIFF);
56      hcd.setBloomFilterType(BloomType.ROW);
57      hcd.setCompressionType(Algorithm.SNAPPY);
58      hcd.setMobEnabled(true);
59      hcd.setMobThreshold(1000L);
60  
61  
62      byte [] bytes = hcd.toByteArray();
63      HColumnDescriptor deserializedHcd = HColumnDescriptor.parseFrom(bytes);
64      assertTrue(hcd.equals(deserializedHcd));
65      assertEquals(v, hcd.getBlocksize());
66      assertEquals(v, hcd.getTimeToLive());
67      assertEquals(hcd.getValue("a"), deserializedHcd.getValue("a"));
68      assertEquals(hcd.getMaxVersions(), deserializedHcd.getMaxVersions());
69      assertEquals(hcd.getMinVersions(), deserializedHcd.getMinVersions());
70      assertEquals(hcd.getKeepDeletedCells(), deserializedHcd.getKeepDeletedCells());
71      assertEquals(inmemory, deserializedHcd.isInMemory());
72      assertEquals(hcd.getScope(), deserializedHcd.getScope());
73      assertTrue(deserializedHcd.getCompressionType().equals(Compression.Algorithm.SNAPPY));
74      assertTrue(deserializedHcd.getDataBlockEncoding().equals(DataBlockEncoding.FAST_DIFF));
75      assertTrue(deserializedHcd.getBloomFilterType().equals(BloomType.ROW));
76      assertEquals(hcd.isMobEnabled(), deserializedHcd.isMobEnabled());
77      assertEquals(hcd.getMobThreshold(), deserializedHcd.getMobThreshold());
78    }
79  
80    @Test
81    /** Tests HColumnDescriptor with empty familyName*/
82    public void testHColumnDescriptorShouldThrowIAEWhenFamiliyNameEmpty()
83        throws Exception {
84      try {
85        new HColumnDescriptor("".getBytes());
86      } catch (IllegalArgumentException e) {
87        assertEquals("Family name can not be empty", e.getLocalizedMessage());
88      }
89    }
90  
91    /**
92     * Test that we add and remove strings from configuration properly.
93     */
94    @Test
95    public void testAddGetRemoveConfiguration() throws Exception {
96      HColumnDescriptor desc = new HColumnDescriptor("foo");
97      String key = "Some";
98      String value = "value";
99      desc.setConfiguration(key, value);
100     assertEquals(value, desc.getConfigurationValue(key));
101     desc.removeConfiguration(key);
102     assertEquals(null, desc.getConfigurationValue(key));
103   }
104 
105   @Test
106   public void testMobValuesInHColumnDescriptorShouldReadable() {
107     boolean isMob = true;
108     long threshold = 1000;
109     String isMobString = PrettyPrinter.format(Bytes.toStringBinary(Bytes.toBytes(isMob)),
110             HColumnDescriptor.getUnit(HColumnDescriptor.IS_MOB));
111     String thresholdString = PrettyPrinter.format(Bytes.toStringBinary(Bytes.toBytes(threshold)),
112             HColumnDescriptor.getUnit(HColumnDescriptor.MOB_THRESHOLD));
113     assertEquals(String.valueOf(isMob), isMobString);
114     assertEquals(String.valueOf(threshold), thresholdString);
115   }
116 
117   @Test
118   public void testClassMethodsAreBuilderStyle() {
119     /* HColumnDescriptor should have a builder style setup where setXXX/addXXX methods
120      * can be chainable together:
121      * . For example:
122      * HColumnDescriptor hcd
123      *   = new HColumnDescriptor()
124      *     .setFoo(foo)
125      *     .setBar(bar)
126      *     .setBuz(buz)
127      *
128      * This test ensures that all methods starting with "set" returns the declaring object
129      */
130 
131     BuilderStyleTest.assertClassesAreBuilderStyle(HColumnDescriptor.class);
132   }
133 }