commit 5e59c803eb3f75c19b9fb4bc58fd3b9b258dd65d Author: Michele Catasta Date: Mon Jun 7 18:57:09 2010 +0200 If a native codec is not found, now HBase gracefully falls-back to NONE. Few minor esthetic changes + new test. diff --git src/main/java/org/apache/hadoop/hbase/io/hfile/Compression.java src/main/java/org/apache/hadoop/hbase/io/hfile/Compression.java index 3a429c0..ffeac2f 100644 --- src/main/java/org/apache/hadoop/hbase/io/hfile/Compression.java +++ src/main/java/org/apache/hadoop/hbase/io/hfile/Compression.java @@ -81,15 +81,7 @@ public final class Compression { @Override CompressionCodec getCodec() { if (lzoCodec == null) { - Configuration conf = new Configuration(); - conf.setBoolean("hadoop.native.lib", true); - try { - Class externalCodec = - ClassLoader.getSystemClassLoader().loadClass("com.hadoop.compression.lzo.LzoCodec"); - lzoCodec = (CompressionCodec) ReflectionUtils.newInstance(externalCodec, conf); - } catch (ClassNotFoundException e) { - throw new RuntimeException(e); - } + lzoCodec = getNativeCodec("com.hadoop.compression.lzo.LzoCodec"); } return lzoCodec; } @@ -200,8 +192,7 @@ public final class Compression { if (compressor.finished()) { // Somebody returns the compressor to CodecPool but is still using // it. - LOG - .warn("Compressor obtained from CodecPool is already finished()"); + LOG.warn("Compressor obtained from CodecPool is already finished()"); // throw new AssertionError( // "Compressor obtained from CodecPool is already finished()"); } @@ -227,7 +218,7 @@ public final class Compression { // Somebody returns the decompressor to CodecPool but is still using // it. LOG - .warn("Deompressor obtained from CodecPool is already finished()"); + .warn("Decompressor obtained from CodecPool is already finished()"); // throw new AssertionError( // "Decompressor obtained from CodecPool is already finished()"); } @@ -248,19 +239,16 @@ public final class Compression { public String getName() { return compressName; } + } public static Algorithm getCompressionAlgorithmByName(String compressName) { - Algorithm[] algos = Algorithm.class.getEnumConstants(); - - for (Algorithm a : algos) { - if (a.getName().equals(compressName)) { - return a; - } + try { + return Algorithm.valueOf(compressName.toUpperCase()); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException( + "Unsupported compression algorithm name: " + compressName); } - - throw new IllegalArgumentException( - "Unsupported compression algorithm name: " + compressName); } static String[] getSupportedAlgorithms() { @@ -274,4 +262,20 @@ public final class Compression { return ret; } + + public static CompressionCodec getNativeCodec(String clazzName) { + CompressionCodec codec = null; + Configuration conf = new Configuration(); + conf.setBoolean("hadoop.native.lib", true); + try { + Class codecClazz = + ClassLoader.getSystemClassLoader().loadClass(clazzName); + codec = (CompressionCodec) ReflectionUtils.newInstance(codecClazz, conf); + } catch (ClassNotFoundException e) { + String codecName = clazzName.substring(clazzName.lastIndexOf(".") + 1); + LOG.warn(codecName + " has not been found. Check if Hadoop GPL compression " + + "library is in the classpath.\nFalling back to NONE compression..."); + } + return codec; + } } diff --git src/test/java/org/apache/hadoop/hbase/io/hfile/TestCompression.java src/test/java/org/apache/hadoop/hbase/io/hfile/TestCompression.java new file mode 100644 index 0000000..40b4a2b --- /dev/null +++ src/test/java/org/apache/hadoop/hbase/io/hfile/TestCompression.java @@ -0,0 +1,23 @@ +package org.apache.hadoop.hbase.io.hfile; + +import org.junit.Test; +import static org.junit.Assert.*; + + +public class TestCompression { + + @Test + public void testGetNativeCodec() { + // checking if it returns null when trying to load a non-existing codec + assertNull(Compression.getNativeCodec("foo.bar.AwesomeNativeCodec")); + // GzipCodec is included in Hadoop, thus it should always return a CompressionCodec + assertNotNull(Compression.getNativeCodec("org.apache.hadoop.io.compress.GzipCodec")); + } + + @Test(expected=IllegalArgumentException.class) + public void testGetCompressionAlgorithmByName() { + assertEquals("none", Compression.getCompressionAlgorithmByName("none").getName()); + // now throwing an exception... + Compression.getCompressionAlgorithmByName("foobarCodec"); + } +}