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