HADOOP-6996. Allow CodecFactory to return a codec object given a codec' class name. Contributed by Hairong Kuang.
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1022697 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e050832c93
commit
13fcef246e
@ -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
|
||||
|
@ -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 static void setCodecClasses(Configuration conf,
|
||||
*/
|
||||
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());
|
||||
@ -171,6 +178,18 @@ public CompressionCodec getCodec(Path file) {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
@ -124,28 +124,60 @@ public static void testFinding() {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user