You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by cu...@apache.org on 2014/11/26 20:16:47 UTC

svn commit: r1641892 - in /avro/trunk: ./ lang/java/thrift/src/main/java/org/apache/avro/thrift/ lang/java/thrift/src/test/java/org/apache/avro/thrift/ lang/java/thrift/src/test/java/org/apache/avro/thrift/test/ lang/java/thrift/src/test/thrift/

Author: cutting
Date: Wed Nov 26 19:16:47 2014
New Revision: 1641892

URL: http://svn.apache.org/r1641892
Log:
AVRO-1564. Java: Fix handling of optional byte field in Thrift.  Contributed by Michael Pershyn.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/thrift/src/main/java/org/apache/avro/thrift/ThriftData.java
    avro/trunk/lang/java/thrift/src/test/java/org/apache/avro/thrift/TestThrift.java
    avro/trunk/lang/java/thrift/src/test/java/org/apache/avro/thrift/test/Test.java
    avro/trunk/lang/java/thrift/src/test/thrift/test.thrift

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1641892&r1=1641891&r2=1641892&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Wed Nov 26 19:16:47 2014
@@ -78,6 +78,9 @@ Trunk (not yet released)
     AVRO-1596. Java: Cannot read past corrupted block in Avro data file.
     (tomwhite)
 
+    AVRO-1564. Java: Fix handling of optional byte field in Thrift.
+    (Michael Pershyn via cutting)
+
 Avro 1.7.7 (23 July 2014)
 
   NEW FEATURES

Modified: avro/trunk/lang/java/thrift/src/main/java/org/apache/avro/thrift/ThriftData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/thrift/src/main/java/org/apache/avro/thrift/ThriftData.java?rev=1641892&r1=1641891&r2=1641892&view=diff
==============================================================================
--- avro/trunk/lang/java/thrift/src/main/java/org/apache/avro/thrift/ThriftData.java (original)
+++ avro/trunk/lang/java/thrift/src/main/java/org/apache/avro/thrift/ThriftData.java Wed Nov 26 19:16:47 2014
@@ -118,6 +118,11 @@ public class ThriftData extends GenericD
     // to avro INT for thrift's optional fields
     if (datum instanceof Short)
       return Schema.Type.INT.getName();
+    // support implicit conversion from thrift's byte
+    // to avro INT for thrift's optional fields
+    if (datum instanceof Byte)
+      return Schema.Type.INT.getName();
+
     return super.getSchemaName(datum);
   }
 

Modified: avro/trunk/lang/java/thrift/src/test/java/org/apache/avro/thrift/TestThrift.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/thrift/src/test/java/org/apache/avro/thrift/TestThrift.java?rev=1641892&r1=1641891&r2=1641892&view=diff
==============================================================================
--- avro/trunk/lang/java/thrift/src/test/java/org/apache/avro/thrift/TestThrift.java (original)
+++ avro/trunk/lang/java/thrift/src/test/java/org/apache/avro/thrift/TestThrift.java Wed Nov 26 19:16:47 2014
@@ -66,7 +66,9 @@ public class TestThrift {
     Test test = new Test();
     test.setBoolField(true);
     test.setByteField((byte)2);
+    test.setByteOptionalField((byte)4);
     test.setI16Field((short)3);
+    test.setI16OptionalField((short)15);
     test.setI64Field(5L);
     test.setDoubleField(2.0);
 
@@ -75,7 +77,6 @@ public class TestThrift {
     check(test);
   }
 
-
   private void check(Test test) throws Exception {
 
     ByteArrayOutputStream bao = new ByteArrayOutputStream();
@@ -83,7 +84,7 @@ public class TestThrift {
     Encoder e = EncoderFactory.get().binaryEncoder(bao, null);
     w.write(test, e);
     e.flush();
-    
+
     Object o = new ThriftDatumReader<Test>(Test.class).read
       (null,
        DecoderFactory.get().createBinaryDecoder

Modified: avro/trunk/lang/java/thrift/src/test/java/org/apache/avro/thrift/test/Test.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/thrift/src/test/java/org/apache/avro/thrift/test/Test.java?rev=1641892&r1=1641891&r2=1641892&view=diff
==============================================================================
--- avro/trunk/lang/java/thrift/src/test/java/org/apache/avro/thrift/test/Test.java (original)
+++ avro/trunk/lang/java/thrift/src/test/java/org/apache/avro/thrift/test/Test.java Wed Nov 26 19:16:47 2014
@@ -37,6 +37,7 @@ public class Test implements org.apache.
 
   private static final org.apache.thrift.protocol.TField BOOL_FIELD_FIELD_DESC = new org.apache.thrift.protocol.TField("boolField", org.apache.thrift.protocol.TType.BOOL, (short)1);
   private static final org.apache.thrift.protocol.TField BYTE_FIELD_FIELD_DESC = new org.apache.thrift.protocol.TField("byteField", org.apache.thrift.protocol.TType.BYTE, (short)2);
+  private static final org.apache.thrift.protocol.TField BYTE_OPTIONAL_FIELD_FIELD_DESC = new org.apache.thrift.protocol.TField("byteOptionalField", org.apache.thrift.protocol.TType.BYTE, (short)16);
   private static final org.apache.thrift.protocol.TField I16_FIELD_FIELD_DESC = new org.apache.thrift.protocol.TField("i16Field", org.apache.thrift.protocol.TType.I16, (short)3);
   private static final org.apache.thrift.protocol.TField I16_OPTIONAL_FIELD_FIELD_DESC = new org.apache.thrift.protocol.TField("i16OptionalField", org.apache.thrift.protocol.TType.I16, (short)15);
   private static final org.apache.thrift.protocol.TField I32_FIELD_FIELD_DESC = new org.apache.thrift.protocol.TField("i32Field", org.apache.thrift.protocol.TType.I32, (short)4);
@@ -59,6 +60,7 @@ public class Test implements org.apache.
 
   private boolean boolField; // required
   private byte byteField; // required
+  private byte byteOptionalField; // optional
   private short i16Field; // required
   private short i16OptionalField; // optional
   private int i32Field; // optional
@@ -77,6 +79,7 @@ public class Test implements org.apache.
   public enum _Fields implements org.apache.thrift.TFieldIdEnum {
     BOOL_FIELD((short)1, "boolField"),
     BYTE_FIELD((short)2, "byteField"),
+    BYTE_OPTIONAL_FIELD((short)16, "byteOptionalField"),
     I16_FIELD((short)3, "i16Field"),
     I16_OPTIONAL_FIELD((short)15, "i16OptionalField"),
     I32_FIELD((short)4, "i32Field"),
@@ -112,6 +115,8 @@ public class Test implements org.apache.
           return BOOL_FIELD;
         case 2: // BYTE_FIELD
           return BYTE_FIELD;
+        case 16: // BYTE_OPTIONAL_FIELD
+          return BYTE_OPTIONAL_FIELD;
         case 3: // I16_FIELD
           return I16_FIELD;
         case 15: // I16_OPTIONAL_FIELD
@@ -180,13 +185,14 @@ public class Test implements org.apache.
   // isset id assignments
   private static final int __BOOLFIELD_ISSET_ID = 0;
   private static final int __BYTEFIELD_ISSET_ID = 1;
-  private static final int __I16FIELD_ISSET_ID = 2;
-  private static final int __I16OPTIONALFIELD_ISSET_ID = 3;
-  private static final int __I32FIELD_ISSET_ID = 4;
-  private static final int __I64FIELD_ISSET_ID = 5;
-  private static final int __DOUBLEFIELD_ISSET_ID = 6;
+  private static final int __BYTEOPTIONALFIELD_ISSET_ID = 2;
+  private static final int __I16FIELD_ISSET_ID = 3;
+  private static final int __I16OPTIONALFIELD_ISSET_ID = 4;
+  private static final int __I32FIELD_ISSET_ID = 5;
+  private static final int __I64FIELD_ISSET_ID = 6;
+  private static final int __DOUBLEFIELD_ISSET_ID = 7;
   private byte __isset_bitfield = 0;
-  private _Fields optionals[] = {_Fields.I16_OPTIONAL_FIELD,_Fields.I32_FIELD,_Fields.BINARY_FIELD};
+  private _Fields optionals[] = {_Fields.BYTE_OPTIONAL_FIELD,_Fields.I16_OPTIONAL_FIELD,_Fields.I32_FIELD,_Fields.BINARY_FIELD};
   public static final Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> metaDataMap;
   static {
     Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> tmpMap = new EnumMap<_Fields, org.apache.thrift.meta_data.FieldMetaData>(_Fields.class);
@@ -194,6 +200,8 @@ public class Test implements org.apache.
         new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.BOOL)));
     tmpMap.put(_Fields.BYTE_FIELD, new org.apache.thrift.meta_data.FieldMetaData("byteField", org.apache.thrift.TFieldRequirementType.DEFAULT, 
         new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.BYTE)));
+    tmpMap.put(_Fields.BYTE_OPTIONAL_FIELD, new org.apache.thrift.meta_data.FieldMetaData("byteOptionalField", org.apache.thrift.TFieldRequirementType.OPTIONAL, 
+        new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.BYTE)));
     tmpMap.put(_Fields.I16_FIELD, new org.apache.thrift.meta_data.FieldMetaData("i16Field", org.apache.thrift.TFieldRequirementType.DEFAULT, 
         new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.I16)));
     tmpMap.put(_Fields.I16_OPTIONAL_FIELD, new org.apache.thrift.meta_data.FieldMetaData("i16OptionalField", org.apache.thrift.TFieldRequirementType.OPTIONAL, 
@@ -272,6 +280,7 @@ public class Test implements org.apache.
     __isset_bitfield = other.__isset_bitfield;
     this.boolField = other.boolField;
     this.byteField = other.byteField;
+    this.byteOptionalField = other.byteOptionalField;
     this.i16Field = other.i16Field;
     this.i16OptionalField = other.i16OptionalField;
     this.i32Field = other.i32Field;
@@ -317,6 +326,8 @@ public class Test implements org.apache.
     this.boolField = false;
     setByteFieldIsSet(false);
     this.byteField = 0;
+    setByteOptionalFieldIsSet(false);
+    this.byteOptionalField = 0;
     setI16FieldIsSet(false);
     this.i16Field = 0;
     setI16OptionalFieldIsSet(false);
@@ -381,6 +392,28 @@ public class Test implements org.apache.
     __isset_bitfield = EncodingUtils.setBit(__isset_bitfield, __BYTEFIELD_ISSET_ID, value);
   }
 
+  public byte getByteOptionalField() {
+    return this.byteOptionalField;
+  }
+
+  public void setByteOptionalField(byte byteOptionalField) {
+    this.byteOptionalField = byteOptionalField;
+    setByteOptionalFieldIsSet(true);
+  }
+
+  public void unsetByteOptionalField() {
+    __isset_bitfield = EncodingUtils.clearBit(__isset_bitfield, __BYTEOPTIONALFIELD_ISSET_ID);
+  }
+
+  /** Returns true if field byteOptionalField is set (has been assigned a value) and false otherwise */
+  public boolean isSetByteOptionalField() {
+    return EncodingUtils.testBit(__isset_bitfield, __BYTEOPTIONALFIELD_ISSET_ID);
+  }
+
+  public void setByteOptionalFieldIsSet(boolean value) {
+    __isset_bitfield = EncodingUtils.setBit(__isset_bitfield, __BYTEOPTIONALFIELD_ISSET_ID, value);
+  }
+
   public short getI16Field() {
     return this.i16Field;
   }
@@ -751,6 +784,14 @@ public class Test implements org.apache.
       }
       break;
 
+    case BYTE_OPTIONAL_FIELD:
+      if (value == null) {
+        unsetByteOptionalField();
+      } else {
+        setByteOptionalField((Byte)value);
+      }
+      break;
+
     case I16_FIELD:
       if (value == null) {
         unsetI16Field();
@@ -866,6 +907,9 @@ public class Test implements org.apache.
     case BYTE_FIELD:
       return Byte.valueOf(getByteField());
 
+    case BYTE_OPTIONAL_FIELD:
+      return Byte.valueOf(getByteOptionalField());
+
     case I16_FIELD:
       return Short.valueOf(getI16Field());
 
@@ -920,6 +964,8 @@ public class Test implements org.apache.
       return isSetBoolField();
     case BYTE_FIELD:
       return isSetByteField();
+    case BYTE_OPTIONAL_FIELD:
+      return isSetByteOptionalField();
     case I16_FIELD:
       return isSetI16Field();
     case I16_OPTIONAL_FIELD:
@@ -981,6 +1027,15 @@ public class Test implements org.apache.
         return false;
     }
 
+    boolean this_present_byteOptionalField = true && this.isSetByteOptionalField();
+    boolean that_present_byteOptionalField = true && that.isSetByteOptionalField();
+    if (this_present_byteOptionalField || that_present_byteOptionalField) {
+      if (!(this_present_byteOptionalField && that_present_byteOptionalField))
+        return false;
+      if (this.byteOptionalField != that.byteOptionalField)
+        return false;
+    }
+
     boolean this_present_i16Field = true;
     boolean that_present_i16Field = true;
     if (this_present_i16Field || that_present_i16Field) {
@@ -1134,6 +1189,16 @@ public class Test implements org.apache.
         return lastComparison;
       }
     }
+    lastComparison = Boolean.valueOf(isSetByteOptionalField()).compareTo(other.isSetByteOptionalField());
+    if (lastComparison != 0) {
+      return lastComparison;
+    }
+    if (isSetByteOptionalField()) {
+      lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.byteOptionalField, other.byteOptionalField);
+      if (lastComparison != 0) {
+        return lastComparison;
+      }
+    }
     lastComparison = Boolean.valueOf(isSetI16Field()).compareTo(other.isSetI16Field());
     if (lastComparison != 0) {
       return lastComparison;
@@ -1291,6 +1356,12 @@ public class Test implements org.apache.
     sb.append("byteField:");
     sb.append(this.byteField);
     first = false;
+    if (isSetByteOptionalField()) {
+      if (!first) sb.append(", ");
+      sb.append("byteOptionalField:");
+      sb.append(this.byteOptionalField);
+      first = false;
+    }
     if (!first) sb.append(", ");
     sb.append("i16Field:");
     sb.append(this.i16Field);
@@ -1445,6 +1516,14 @@ public class Test implements org.apache.
               org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
             }
             break;
+          case 16: // BYTE_OPTIONAL_FIELD
+            if (schemeField.type == org.apache.thrift.protocol.TType.BYTE) {
+              struct.byteOptionalField = iprot.readByte();
+              struct.setByteOptionalFieldIsSet(true);
+            } else { 
+              org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
+            }
+            break;
           case 3: // I16_FIELD
             if (schemeField.type == org.apache.thrift.protocol.TType.I16) {
               struct.i16Field = iprot.readI16();
@@ -1685,6 +1764,11 @@ public class Test implements org.apache.
         oprot.writeI16(struct.i16OptionalField);
         oprot.writeFieldEnd();
       }
+      if (struct.isSetByteOptionalField()) {
+        oprot.writeFieldBegin(BYTE_OPTIONAL_FIELD_FIELD_DESC);
+        oprot.writeByte(struct.byteOptionalField);
+        oprot.writeFieldEnd();
+      }
       oprot.writeFieldStop();
       oprot.writeStructEnd();
     }
@@ -1709,52 +1793,58 @@ public class Test implements org.apache.
       if (struct.isSetByteField()) {
         optionals.set(1);
       }
-      if (struct.isSetI16Field()) {
+      if (struct.isSetByteOptionalField()) {
         optionals.set(2);
       }
-      if (struct.isSetI16OptionalField()) {
+      if (struct.isSetI16Field()) {
         optionals.set(3);
       }
-      if (struct.isSetI32Field()) {
+      if (struct.isSetI16OptionalField()) {
         optionals.set(4);
       }
-      if (struct.isSetI64Field()) {
+      if (struct.isSetI32Field()) {
         optionals.set(5);
       }
-      if (struct.isSetDoubleField()) {
+      if (struct.isSetI64Field()) {
         optionals.set(6);
       }
-      if (struct.isSetStringField()) {
+      if (struct.isSetDoubleField()) {
         optionals.set(7);
       }
-      if (struct.isSetBinaryField()) {
+      if (struct.isSetStringField()) {
         optionals.set(8);
       }
-      if (struct.isSetMapField()) {
+      if (struct.isSetBinaryField()) {
         optionals.set(9);
       }
-      if (struct.isSetListField()) {
+      if (struct.isSetMapField()) {
         optionals.set(10);
       }
-      if (struct.isSetSetField()) {
+      if (struct.isSetListField()) {
         optionals.set(11);
       }
-      if (struct.isSetEnumField()) {
+      if (struct.isSetSetField()) {
         optionals.set(12);
       }
-      if (struct.isSetStructField()) {
+      if (struct.isSetEnumField()) {
         optionals.set(13);
       }
-      if (struct.isSetFooOrBar()) {
+      if (struct.isSetStructField()) {
         optionals.set(14);
       }
-      oprot.writeBitSet(optionals, 15);
+      if (struct.isSetFooOrBar()) {
+        optionals.set(15);
+      }
+      oprot.writeBitSet(optionals, 16);
       if (struct.isSetBoolField()) {
         oprot.writeBool(struct.boolField);
       }
       if (struct.isSetByteField()) {
         oprot.writeByte(struct.byteField);
       }
+      if (struct.isSetByteOptionalField()) {
+        oprot.writeByte(struct.byteOptionalField);
+      }
       if (struct.isSetI16Field()) {
         oprot.writeI16(struct.i16Field);
       }
@@ -1818,7 +1908,7 @@ public class Test implements org.apache.
     @Override
     public void read(org.apache.thrift.protocol.TProtocol prot, Test struct) throws org.apache.thrift.TException {
       TTupleProtocol iprot = (TTupleProtocol) prot;
-      BitSet incoming = iprot.readBitSet(15);
+      BitSet incoming = iprot.readBitSet(16);
       if (incoming.get(0)) {
         struct.boolField = iprot.readBool();
         struct.setBoolFieldIsSet(true);
@@ -1828,34 +1918,38 @@ public class Test implements org.apache.
         struct.setByteFieldIsSet(true);
       }
       if (incoming.get(2)) {
+        struct.byteOptionalField = iprot.readByte();
+        struct.setByteOptionalFieldIsSet(true);
+      }
+      if (incoming.get(3)) {
         struct.i16Field = iprot.readI16();
         struct.setI16FieldIsSet(true);
       }
-      if (incoming.get(3)) {
+      if (incoming.get(4)) {
         struct.i16OptionalField = iprot.readI16();
         struct.setI16OptionalFieldIsSet(true);
       }
-      if (incoming.get(4)) {
+      if (incoming.get(5)) {
         struct.i32Field = iprot.readI32();
         struct.setI32FieldIsSet(true);
       }
-      if (incoming.get(5)) {
+      if (incoming.get(6)) {
         struct.i64Field = iprot.readI64();
         struct.setI64FieldIsSet(true);
       }
-      if (incoming.get(6)) {
+      if (incoming.get(7)) {
         struct.doubleField = iprot.readDouble();
         struct.setDoubleFieldIsSet(true);
       }
-      if (incoming.get(7)) {
+      if (incoming.get(8)) {
         struct.stringField = iprot.readString();
         struct.setStringFieldIsSet(true);
       }
-      if (incoming.get(8)) {
+      if (incoming.get(9)) {
         struct.binaryField = iprot.readBinary();
         struct.setBinaryFieldIsSet(true);
       }
-      if (incoming.get(9)) {
+      if (incoming.get(10)) {
         {
           org.apache.thrift.protocol.TMap _map16 = new org.apache.thrift.protocol.TMap(org.apache.thrift.protocol.TType.STRING, org.apache.thrift.protocol.TType.I32, iprot.readI32());
           struct.mapField = new HashMap<String,Integer>(2*_map16.size);
@@ -1870,7 +1964,7 @@ public class Test implements org.apache.
         }
         struct.setMapFieldIsSet(true);
       }
-      if (incoming.get(10)) {
+      if (incoming.get(11)) {
         {
           org.apache.thrift.protocol.TList _list20 = new org.apache.thrift.protocol.TList(org.apache.thrift.protocol.TType.I32, iprot.readI32());
           struct.listField = new ArrayList<Integer>(_list20.size);
@@ -1883,7 +1977,7 @@ public class Test implements org.apache.
         }
         struct.setListFieldIsSet(true);
       }
-      if (incoming.get(11)) {
+      if (incoming.get(12)) {
         {
           org.apache.thrift.protocol.TSet _set23 = new org.apache.thrift.protocol.TSet(org.apache.thrift.protocol.TType.I32, iprot.readI32());
           struct.setField = new HashSet<Integer>(2*_set23.size);
@@ -1896,16 +1990,16 @@ public class Test implements org.apache.
         }
         struct.setSetFieldIsSet(true);
       }
-      if (incoming.get(12)) {
+      if (incoming.get(13)) {
         struct.enumField = E.findByValue(iprot.readI32());
         struct.setEnumFieldIsSet(true);
       }
-      if (incoming.get(13)) {
+      if (incoming.get(14)) {
         struct.structField = new Nested();
         struct.structField.read(iprot);
         struct.setStructFieldIsSet(true);
       }
-      if (incoming.get(14)) {
+      if (incoming.get(15)) {
         struct.fooOrBar = new FooOrBar();
         struct.fooOrBar.read(iprot);
         struct.setFooOrBarIsSet(true);

Modified: avro/trunk/lang/java/thrift/src/test/thrift/test.thrift
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/thrift/src/test/thrift/test.thrift?rev=1641892&r1=1641891&r2=1641892&view=diff
==============================================================================
--- avro/trunk/lang/java/thrift/src/test/thrift/test.thrift (original)
+++ avro/trunk/lang/java/thrift/src/test/thrift/test.thrift Wed Nov 26 19:16:47 2014
@@ -38,6 +38,7 @@ union FooOrBar {
 struct Test {
   1: bool boolField
   2: byte byteField
+ 16: optional byte byteOptionalField
   3: i16 i16Field
  15: optional i16 i16OptionalField
   4: optional i32 i32Field