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.testclassification.SmallTests;
29  import org.apache.hadoop.hbase.util.BuilderStyleTest;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.apache.hadoop.hbase.util.PrettyPrinter;
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      hcd.setDFSReplication((short) v);
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.getKeepDeletedCellsAsEnum(), deserializedHcd.getKeepDeletedCellsAsEnum());
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      assertEquals(v, deserializedHcd.getDFSReplication());
79    }
80  
81    @Test
82    /** Tests HColumnDescriptor with empty familyName*/
83    public void testHColumnDescriptorShouldThrowIAEWhenFamiliyNameEmpty()
84        throws Exception {
85      try {
86        new HColumnDescriptor("".getBytes());
87      } catch (IllegalArgumentException e) {
88        assertEquals("Family name can not be empty", e.getLocalizedMessage());
89      }
90    }
91  
92    /**
93     * Test that we add and remove strings from configuration properly.
94     */
95    @Test
96    public void testAddGetRemoveConfiguration() throws Exception {
97      HColumnDescriptor desc = new HColumnDescriptor("foo");
98      String key = "Some";
99      String value = "value";
100     desc.setConfiguration(key, value);
101     assertEquals(value, desc.getConfigurationValue(key));
102     desc.removeConfiguration(key);
103     assertEquals(null, desc.getConfigurationValue(key));
104   }
105 
106   @Test
107   public void testMobValuesInHColumnDescriptorShouldReadable() {
108     boolean isMob = true;
109     long threshold = 1000;
110     String isMobString = PrettyPrinter.format(Bytes.toStringBinary(Bytes.toBytes(isMob)),
111         HColumnDescriptor.getUnit(HColumnDescriptor.IS_MOB));
112     String thresholdString = PrettyPrinter.format(Bytes.toStringBinary(Bytes.toBytes(threshold)),
113         HColumnDescriptor.getUnit(HColumnDescriptor.MOB_THRESHOLD));
114     assertEquals(String.valueOf(isMob), isMobString);
115     assertEquals(String.valueOf(threshold), thresholdString);
116   }
117 }