Author: cutting
Date: Sat Dec 5 00:05:25 2009
New Revision: 887459
URL: http://svn.apache.org/viewvc?rev=887459&view=rev
Log:
AVRO-249. In reflection, implement Java short as an Avro int whose java-class
property is set to java.lang.Short.
Modified:
hadoop/avro/trunk/CHANGES.txt
hadoop/avro/trunk/src/java/org/apache/avro/generic/GenericDatumReader.java
hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectData.java
hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectDatumReader.java
hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectDatumWriter.java
hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificData.java
hadoop/avro/trunk/src/test/java/org/apache/avro/TestReflect.java
Modified: hadoop/avro/trunk/CHANGES.txt
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/CHANGES.txt?rev=887459&r1=887458&r2=887459&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Sat Dec 5 00:05:25 2009
@@ -120,6 +120,9 @@
AVRO-241. In Java, add a union annotation for reflection. (cutting)
+ AVRO-249. In reflection, implement Java short as an int whose
+ "java-class" property is set to java.lang.Short. (cutting)
+
OPTIMIZATIONS
AVRO-172. More efficient schema processing (massie)
Modified:
hadoop/avro/trunk/src/java/org/apache/avro/generic/GenericDatumReader.java
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/generic/GenericDatumReader.java?rev=887459&r1=887458&r2=887459&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/generic/GenericDatumReader.java
(original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/generic/GenericDatumReader.java
Sat Dec 5 00:05:25 2009
@@ -76,7 +76,7 @@
case FIXED: return readFixed(old, actual, expected, in);
case STRING: return readString(old, in);
case BYTES: return readBytes(old, in);
- case INT: return in.readInt();
+ case INT: return readInt(old, actual, expected, in);
case LONG: return in.readLong();
case FLOAT: return in.readFloat();
case DOUBLE: return in.readDouble();
@@ -413,6 +413,14 @@
return in.readBytes((ByteBuffer)old);
}
+ /** Called to read integers. Subclasses may override to use a different
+ * integer representation. By default, this calls {...@link
+ * Decoder#readInt()}.*/
+ protected Object readInt(Object old, Schema actual, Schema expected,
+ Decoder in) throws IOException {
+ return in.readInt();
+ }
+
/** Called to create byte arrays from default values. Subclasses may
* override to use a different byte array representation. By default, this
* calls {...@link ByteBuffer#wrap(byte[])}.*/
Modified: hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectData.java
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectData.java?rev=887459&r1=887458&r2=887459&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectData.java
(original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectData.java Sat Dec
5 00:05:25 2009
@@ -193,6 +193,9 @@
return
java.lang.reflect.Array.newInstance(getClass(schema.getElementType()),0).getClass();
case STRING: return String.class;
case BYTES: return BYTES_CLASS;
+ case INT:
+ if (Short.class.getName().equals(schema.getProp(CLASS_PROP)))
+ return Short.TYPE;
default:
return super.getClass(schema);
}
@@ -225,6 +228,10 @@
schema.setProp(CLASS_PROP, raw.getName());
return schema;
}
+ } else if ((type == Short.class) || (type == Short.TYPE)) {
+ Schema result = Schema.create(Schema.Type.INT);
+ result.setProp(CLASS_PROP, Short.class.getName());
+ return result;
} else if (type instanceof Class) { // Class
Class c = (Class)type;
if (c.isPrimitive() || Number.class.isAssignableFrom(c)
Modified:
hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectDatumReader.java
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectDatumReader.java?rev=887459&r1=887458&r2=887459&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectDatumReader.java
(original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectDatumReader.java
Sat Dec 5 00:05:25 2009
@@ -21,7 +21,6 @@
import java.util.Collection;
import java.util.ArrayList;
import java.lang.reflect.Array;
-import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import org.apache.avro.AvroRuntimeException;
@@ -47,10 +46,7 @@
@Override
protected void addField(Object record, String name, int position, Object o) {
try {
- Field field = ReflectData.getField(record.getClass(), name);
- if (field.getType() == Short.TYPE)
- o = ((Integer)o).shortValue(); // downgrade int to short
- field.set(record, o);
+ ReflectData.getField(record.getClass(), name).set(record, o);
} catch (IllegalAccessException e) {
throw new AvroRuntimeException(e);
}
@@ -121,5 +117,13 @@
return result;
}
-}
+ @Override
+ protected Object readInt(Object old, Schema actual,
+ Schema expected, Decoder in) throws IOException {
+ Object value = in.readInt();
+ if (Short.class.getName().equals(expected.getProp(ReflectData.CLASS_PROP)))
+ value = ((Integer)value).shortValue();
+ return value;
+ }
+}
Modified:
hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectDatumWriter.java
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectDatumWriter.java?rev=887459&r1=887458&r2=887459&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectDatumWriter.java
(original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectDatumWriter.java
Sat Dec 5 00:05:25 2009
@@ -60,10 +60,7 @@
@Override
protected Object getField(Object record, String name, int position) {
try {
- Object value = ReflectData.getField(record.getClass(), name).get(record);
- if (value instanceof Short)
- return ((Short)value).intValue(); // upgrade short to int
- return value;
+ return ReflectData.getField(record.getClass(), name).get(record);
} catch (IllegalAccessException e) {
throw new AvroRuntimeException(e);
}
@@ -102,7 +99,12 @@
out.writeBytes((byte[])datum);
}
+ @Override
+ protected void write(Schema schema, Object datum, Encoder out)
+ throws IOException {
+ if (datum instanceof Short)
+ datum = ((Short)datum).intValue();
+ super.write(schema, datum, out);
+ }
}
-
-
Modified: hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificData.java
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificData.java?rev=887459&r1=887458&r2=887459&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificData.java
(original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificData.java Sat
Dec 5 00:05:25 2009
@@ -145,8 +145,7 @@
return Schema.create(Type.STRING);
else if (type == ByteBuffer.class)
return Schema.create(Type.BYTES);
- else if ((type == Integer.class) || (type == Integer.TYPE)
- || (type == Short.TYPE))
+ else if ((type == Integer.class) || (type == Integer.TYPE))
return Schema.create(Type.INT);
else if ((type == Long.class) || (type == Long.TYPE))
return Schema.create(Type.LONG);
Modified: hadoop/avro/trunk/src/test/java/org/apache/avro/TestReflect.java
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/test/java/org/apache/avro/TestReflect.java?rev=887459&r1=887458&r2=887459&view=diff
==============================================================================
--- hadoop/avro/trunk/src/test/java/org/apache/avro/TestReflect.java (original)
+++ hadoop/avro/trunk/src/test/java/org/apache/avro/TestReflect.java Sat Dec 5
00:05:25 2009
@@ -161,10 +161,13 @@
// test inherited fields & short datatype
public static class R4 {
public short value;
+ public short[] shorts;
public boolean equals(Object o) {
if (!(o instanceof R4)) return false;
- return this.value == ((R4)o).value;
+ R4 that = (R4)o;
+ return this.value == that.value
+ && Arrays.equals(this.shorts, that.shorts);
}
}
@@ -173,6 +176,7 @@
@Test public void testR5() throws Exception {
R5 r5 = new R5();
r5.value = 1;
+ r5.shorts = new short[] {3,255,256,Short.MAX_VALUE,Short.MIN_VALUE};
checkReadWrite(r5);
}