* @see JavaSerializationComparator
*/
-public class JavaSerialization implements Serialization {
-
+public class JavaSerialization extends SerializationBase {
+
static class JavaSerializationDeserializer
- implements Deserializer {
+ extends DeserializerBase {
private ObjectInputStream ois;
@@ -61,11 +64,16 @@ public class JavaSerialization implements Serialization {
}
}
-
- static class JavaSerializationSerializer
- implements Serializer {
+
+ static class JavaSerializationSerializer
+ extends SerializerBase {
private ObjectOutputStream oos;
+ private Map metadata;
+
+ public JavaSerializationSerializer(Map metadata) {
+ this.metadata = metadata;
+ }
public void open(OutputStream out) throws IOException {
oos = new ObjectOutputStream(out) {
@@ -75,7 +83,7 @@ public class JavaSerialization implements Serialization {
};
}
- public void serialize(Serializable object) throws IOException {
+ public void serialize(T object) throws IOException {
oos.reset(); // clear (class) back-references
oos.writeObject(object);
}
@@ -84,18 +92,55 @@ public class JavaSerialization implements Serialization {
oos.close();
}
+ @Override
+ public Map getMetadata() throws IOException {
+ return metadata;
+ }
}
- public boolean accept(Class> c) {
+ public boolean accept(Map metadata) {
+ String intendedSerializer = metadata.get(SERIALIZATION_KEY);
+ if (intendedSerializer != null &&
+ !getClass().getName().equals(intendedSerializer)) {
+ return false;
+ }
+
+ Class> c = getClassFromMetadata(metadata);
return Serializable.class.isAssignableFrom(c);
}
- public Deserializer getDeserializer(Class c) {
+ public DeserializerBase getDeserializer(
+ Map metadata) {
return new JavaSerializationDeserializer();
}
- public Serializer getSerializer(Class c) {
- return new JavaSerializationSerializer();
+ public SerializerBase getSerializer(
+ Map metadata) {
+ return new JavaSerializationSerializer(metadata);
}
+ @SuppressWarnings("unchecked")
+ @Override
+ public RawComparator getRawComparator(
+ Map metadata) {
+ Class> klazz = getClassFromMetadata(metadata);
+ if (null == klazz) {
+ throw new IllegalArgumentException(
+ "Cannot get comparator without " + SerializationBase.CLASS_KEY
+ + " set in metadata");
+ }
+
+ if (Serializable.class.isAssignableFrom(klazz)) {
+ try {
+ return (RawComparator) new JavaSerializationComparator();
+ } catch (IOException ioe) {
+ throw new IllegalArgumentException(
+ "Could not instantiate JavaSerializationComparator for type "
+ + klazz.getName(), ioe);
+ }
+ } else {
+ throw new IllegalArgumentException("Class " + klazz.getName()
+ + " is incompatible with JavaSerialization");
+ }
+ }
}
diff --git a/src/java/org/apache/hadoop/io/serializer/LegacySerialization.java b/src/java/org/apache/hadoop/io/serializer/LegacySerialization.java
index d5f0f9a8363..e97a673a32d 100644
--- a/src/java/org/apache/hadoop/io/serializer/LegacySerialization.java
+++ b/src/java/org/apache/hadoop/io/serializer/LegacySerialization.java
@@ -21,6 +21,7 @@ package org.apache.hadoop.io.serializer;
import java.util.Map;
import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.io.RawComparator;
/**
*
@@ -82,4 +83,14 @@ class LegacySerialization extends SerializationBase {
return new LegacyDeserializer(getDeserializer(c));
}
+ @Override
+ public RawComparator getRawComparator(Map metadata) {
+ // Since this method is being added to an API meant to provide legacy
+ // compatability with deprecated serializers, leaving this as an incomplete
+ // stub.
+
+ throw new UnsupportedOperationException(
+ "LegacySerialization does not provide raw comparators");
+ }
+
}
diff --git a/src/java/org/apache/hadoop/io/serializer/SerializationBase.java b/src/java/org/apache/hadoop/io/serializer/SerializationBase.java
index 01df3ef856e..7934b089fe4 100644
--- a/src/java/org/apache/hadoop/io/serializer/SerializationBase.java
+++ b/src/java/org/apache/hadoop/io/serializer/SerializationBase.java
@@ -22,6 +22,7 @@ import java.util.HashMap;
import java.util.Map;
import org.apache.hadoop.conf.Configured;
+import org.apache.hadoop.io.RawComparator;
/**
*
@@ -88,4 +89,16 @@ public abstract class SerializationBase extends Configured
throw new IllegalArgumentException(e);
}
}
+
+ /** Provide a raw comparator for the specified serializable class.
+ * Requires a serialization-specific metadata entry to name the class
+ * to compare (e.g., "Serialized-Class" for JavaSerialization and
+ * WritableSerialization).
+ * @param metadata a set of string mappings providing serialization-specific
+ * arguments that parameterize the data being serialized/compared.
+ * @return a {@link RawComparator} for the given metadata.
+ * @throws UnsupportedOperationException if it cannot instantiate a RawComparator
+ * for this given metadata.
+ */
+ public abstract RawComparator getRawComparator(Map metadata);
}
diff --git a/src/java/org/apache/hadoop/io/serializer/WritableSerialization.java b/src/java/org/apache/hadoop/io/serializer/WritableSerialization.java
index 04211a185cf..7c0dec1af13 100644
--- a/src/java/org/apache/hadoop/io/serializer/WritableSerialization.java
+++ b/src/java/org/apache/hadoop/io/serializer/WritableSerialization.java
@@ -26,8 +26,12 @@ import java.io.OutputStream;
import java.util.Map;
import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.io.RawComparator;
import org.apache.hadoop.io.Writable;
+import org.apache.hadoop.io.WritableComparable;
+import org.apache.hadoop.io.WritableComparator;
import org.apache.hadoop.util.ReflectionUtils;
+import org.apache.hadoop.util.StringUtils;
/**
* A {@link SerializationBase} for {@link Writable}s that delegates to
@@ -35,7 +39,6 @@ import org.apache.hadoop.util.ReflectionUtils;
* {@link Writable#readFields(java.io.DataInput)}.
*/
public class WritableSerialization extends SerializationBase {
-
static class WritableDeserializer extends DeserializerBase {
private Class> writableClass;
@@ -79,9 +82,25 @@ public class WritableSerialization extends SerializationBase {
private Map metadata;
private DataOutputStream dataOut;
+ private Class> serializedClass;
- public WritableSerializer(Map metadata) {
+ public WritableSerializer(Configuration conf,
+ Map metadata) {
this.metadata = metadata;
+
+ // If this metadata specifies a serialized class, memoize the
+ // class object for this.
+ String className = this.metadata.get(CLASS_KEY);
+ if (null != className) {
+ try {
+ this.serializedClass = conf.getClassByName(className);
+ } catch (ClassNotFoundException cnfe) {
+ throw new RuntimeException(cnfe);
+ }
+ } else {
+ throw new UnsupportedOperationException("the "
+ + CLASS_KEY + " metadata is missing, but is required.");
+ }
}
@Override
@@ -95,6 +114,10 @@ public class WritableSerialization extends SerializationBase {
@Override
public void serialize(Writable w) throws IOException {
+ if (serializedClass != w.getClass()) {
+ throw new IOException("Type mismatch in serialization: expected "
+ + serializedClass + "; received " + w.getClass());
+ }
w.write(dataOut);
}
@@ -112,8 +135,10 @@ public class WritableSerialization extends SerializationBase {
@Override
public boolean accept(Map metadata) {
- if (getClass().getName().equals(metadata.get(SERIALIZATION_KEY))) {
- return true;
+ String intendedSerializer = metadata.get(SERIALIZATION_KEY);
+ if (intendedSerializer != null &&
+ !getClass().getName().equals(intendedSerializer)) {
+ return false;
}
Class> c = getClassFromMetadata(metadata);
return c == null ? false : Writable.class.isAssignableFrom(c);
@@ -121,7 +146,7 @@ public class WritableSerialization extends SerializationBase {
@Override
public SerializerBase getSerializer(Map metadata) {
- return new WritableSerializer(metadata);
+ return new WritableSerializer(getConf(), metadata);
}
@Override
@@ -130,4 +155,17 @@ public class WritableSerialization extends SerializationBase {
return new WritableDeserializer(getConf(), c);
}
+ @Override
+ @SuppressWarnings("unchecked")
+ public RawComparator getRawComparator(Map metadata) {
+ Class> klazz = getClassFromMetadata(metadata);
+ if (null == klazz) {
+ throw new IllegalArgumentException(
+ "Cannot get comparator without " + SerializationBase.CLASS_KEY
+ + " set in metadata");
+ }
+
+ return (RawComparator) WritableComparator.get(
+ (Class)klazz);
+ }
}
diff --git a/src/java/org/apache/hadoop/io/serializer/avro/AvroComparator.java b/src/java/org/apache/hadoop/io/serializer/avro/AvroComparator.java
new file mode 100644
index 00000000000..2f499d067f9
--- /dev/null
+++ b/src/java/org/apache/hadoop/io/serializer/avro/AvroComparator.java
@@ -0,0 +1,48 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.io.serializer.avro;
+
+import org.apache.avro.Schema;
+import org.apache.avro.io.BinaryData;
+import org.apache.hadoop.io.RawComparator;
+
+/**
+ *
+ * A {@link RawComparator} that uses Avro to extract data from the
+ * source stream and compare their contents without explicit
+ * deserialization.
+ */
+public class AvroComparator>
+ implements RawComparator {
+
+ private final Schema schema;
+
+ public AvroComparator(final Schema s) {
+ this.schema = s;
+ }
+
+ public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
+ return BinaryData.compare(b1, s1, b2, s2, schema);
+ }
+
+ public int compare(T t1, T t2) {
+ return t1.compareTo(t2);
+ }
+
+}
diff --git a/src/java/org/apache/hadoop/io/serializer/avro/AvroGenericSerialization.java b/src/java/org/apache/hadoop/io/serializer/avro/AvroGenericSerialization.java
index 52f25aec6a7..464b5fd88d5 100644
--- a/src/java/org/apache/hadoop/io/serializer/avro/AvroGenericSerialization.java
+++ b/src/java/org/apache/hadoop/io/serializer/avro/AvroGenericSerialization.java
@@ -50,9 +50,8 @@ public class AvroGenericSerialization extends AvroSerialization