Author: hairong Date: Thu Oct 14 20:35:53 2010 New Revision: 1022697 URL: http://svn.apache.org/viewvc?rev=1022697&view=rev Log: HADOOP-6996. Allow CodecFactory to return a codec object given a codec' class name. Contributed by Hairong Kuang.
Modified: hadoop/common/trunk/CHANGES.txt hadoop/common/trunk/src/java/org/apache/hadoop/io/compress/CompressionCodecFactory.java hadoop/common/trunk/src/test/core/org/apache/hadoop/io/compress/TestCodecFactory.java Modified: hadoop/common/trunk/CHANGES.txt URL: http://svn.apache.org/viewvc/hadoop/common/trunk/CHANGES.txt?rev=1022697&r1=1022696&r2=1022697&view=diff ============================================================================== --- hadoop/common/trunk/CHANGES.txt (original) +++ hadoop/common/trunk/CHANGES.txt Thu Oct 14 20:35:53 2010 @@ -35,6 +35,9 @@ Trunk (unreleased changes) HADOOP-6889. Make RPC to have an option to timeout. (hairong) + HADOOP-6996. Allow CodecFactory to return a codec object given a codec' + class name. (hairong) + IMPROVEMENTS HADOOP-6644. util.Shell getGROUPS_FOR_USER_COMMAND method name Modified: hadoop/common/trunk/src/java/org/apache/hadoop/io/compress/CompressionCodecFactory.java URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/java/org/apache/hadoop/io/compress/CompressionCodecFactory.java?rev=1022697&r1=1022696&r2=1022697&view=diff ============================================================================== --- hadoop/common/trunk/src/java/org/apache/hadoop/io/compress/CompressionCodecFactory.java (original) +++ hadoop/common/trunk/src/java/org/apache/hadoop/io/compress/CompressionCodecFactory.java Thu Oct 14 20:35:53 2010 @@ -44,9 +44,15 @@ public class CompressionCodecFactory { */ private SortedMap<String, CompressionCodec> codecs = null; + /** + * A map from class names to the codecs + */ + private HashMap<String, CompressionCodec> codecsByClassName = null; + private void addCodec(CompressionCodec codec) { String suffix = codec.getDefaultExtension(); codecs.put(new StringBuilder(suffix).reverse().toString(), codec); + codecsByClassName.put(codec.getClass().getCanonicalName(), codec); } /** @@ -135,6 +141,7 @@ public class CompressionCodecFactory { */ public CompressionCodecFactory(Configuration conf) { codecs = new TreeMap<String, CompressionCodec>(); + codecsByClassName = new HashMap<String, CompressionCodec>(); List<Class<? extends CompressionCodec>> codecClasses = getCodecClasses(conf); if (codecClasses == null) { addCodec(new GzipCodec()); @@ -172,6 +179,18 @@ public class CompressionCodecFactory { } /** + * Find the relevant compression codec for the codec's canonical class name. + * @param classname the canonical class name of the codec + * @return the codec object + */ + public CompressionCodec getCodecByClassName(String classname) { + if (codecsByClassName == null) { + return null; + } + return codecsByClassName.get(classname); + } + + /** * Removes a suffix from a filename, if it has it. * @param filename the filename to strip * @param suffix the suffix to remove Modified: hadoop/common/trunk/src/test/core/org/apache/hadoop/io/compress/TestCodecFactory.java URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/test/core/org/apache/hadoop/io/compress/TestCodecFactory.java?rev=1022697&r1=1022696&r2=1022697&view=diff ============================================================================== --- hadoop/common/trunk/src/test/core/org/apache/hadoop/io/compress/TestCodecFactory.java (original) +++ hadoop/common/trunk/src/test/core/org/apache/hadoop/io/compress/TestCodecFactory.java Thu Oct 14 20:35:53 2010 @@ -124,28 +124,60 @@ public class TestCodecFactory extends Te new CompressionCodecFactory(new Configuration()); CompressionCodec codec = factory.getCodec(new Path("/tmp/foo.bar")); assertEquals("default factory foo codec", null, codec); + codec = factory.getCodecByClassName(BarCodec.class.getCanonicalName()); + assertEquals("default factory foo codec", null, codec); + codec = factory.getCodec(new Path("/tmp/foo.gz")); checkCodec("default factory for .gz", GzipCodec.class, codec); + codec = factory.getCodecByClassName(GzipCodec.class.getCanonicalName()); + checkCodec("default factory for gzip codec", GzipCodec.class, codec); + codec = factory.getCodec(new Path("/tmp/foo.bz2")); checkCodec("default factory for .bz2", BZip2Codec.class, codec); + codec = factory.getCodecByClassName(BZip2Codec.class.getCanonicalName()); + checkCodec("default factory for bzip2 codec", BZip2Codec.class, codec); + factory = setClasses(new Class[0]); codec = factory.getCodec(new Path("/tmp/foo.bar")); assertEquals("empty codec bar codec", null, codec); + codec = factory.getCodecByClassName(BarCodec.class.getCanonicalName()); + assertEquals("empty codec bar codec", null, codec); + codec = factory.getCodec(new Path("/tmp/foo.gz")); assertEquals("empty codec gz codec", null, codec); + codec = factory.getCodecByClassName(GzipCodec.class.getCanonicalName()); + assertEquals("empty codec gz codec", null, codec); + codec = factory.getCodec(new Path("/tmp/foo.bz2")); - assertEquals("default factory for .bz2", null, codec); + assertEquals("empty factory for .bz2", null, codec); + codec = factory.getCodecByClassName(BZip2Codec.class.getCanonicalName()); + assertEquals("empty factory for bzip2 codec", null, codec); + factory = setClasses(new Class[]{BarCodec.class, FooCodec.class, FooBarCodec.class}); codec = factory.getCodec(new Path("/tmp/.foo.bar.gz")); assertEquals("full factory gz codec", null, codec); + codec = factory.getCodecByClassName(GzipCodec.class.getCanonicalName()); + assertEquals("full codec gz codec", null, codec); + codec = factory.getCodec(new Path("/tmp/foo.bz2")); - assertEquals("default factory for .bz2", null, codec); + assertEquals("full factory for .bz2", null, codec); + codec = factory.getCodecByClassName(BZip2Codec.class.getCanonicalName()); + assertEquals("full codec bzip2 codec", null, codec); + codec = factory.getCodec(new Path("/tmp/foo.bar")); checkCodec("full factory bar codec", BarCodec.class, codec); + codec = factory.getCodecByClassName(BarCodec.class.getCanonicalName()); + checkCodec("full factory bar codec", BarCodec.class, codec); + codec = factory.getCodec(new Path("/tmp/foo/baz.foo.bar")); checkCodec("full factory foo bar codec", FooBarCodec.class, codec); + codec = factory.getCodecByClassName(FooBarCodec.class.getCanonicalName()); + checkCodec("full factory foo bar codec", FooBarCodec.class, codec); + codec = factory.getCodec(new Path("/tmp/foo.foo")); checkCodec("full factory foo codec", FooCodec.class, codec); + codec = factory.getCodecByClassName(FooCodec.class.getCanonicalName()); + checkCodec("full factory foo codec", FooCodec.class, codec); } }