Thrift generation for Java

Summary: Java works, benchmark roundtrip at around 3ms, so right in between C++ and PHP


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664775 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/java/src/protocol/TBinaryProtocol.java b/lib/java/src/protocol/TBinaryProtocol.java
index a759005..d90dece 100644
--- a/lib/java/src/protocol/TBinaryProtocol.java
+++ b/lib/java/src/protocol/TBinaryProtocol.java
@@ -9,13 +9,23 @@
  * @author Mark Slee <mcslee@facebook.com>
  */
 public class TBinaryProtocol implements TProtocol {
+
+  public void writeMessageBegin(TTransport out, TMessage message) throws TException {
+    writeString(out, message.name);
+    writeByte(out, message.type);
+    writeI32(out, message.seqid);
+  }
+
+  public void writeMessageEnd(TTransport out) throws TException {}
+
+
   public void writeStructBegin(TTransport out, TStruct struct) throws TException {}
 
   public void writeStructEnd(TTransport out) throws TException {}
 
   public void writeFieldBegin(TTransport out, TField field) throws TException {
     writeByte(out, field.type);
-    writeI32(out, field.id);
+    writeI16(out, field.id);
   }
 
   public void writeFieldEnd(TTransport out) throws TException {}
@@ -46,14 +56,21 @@
 
   public void writeSetEnd(TTransport out) throws TException {}
 
+  public void writeBool(TTransport out, boolean b) throws TException {
+    writeByte(out, b ? (byte)1 : (byte)0);
+  }
+
   byte[] bout = new byte[1];
   public void writeByte(TTransport out, byte b) throws TException {
     bout[0] = b;
     out.write(bout, 0, 1);
   }
 
-  public void writeU32(TTransport out, int u32) throws TException {
-    writeI32(out, u32);
+  byte[] i16out = new byte[2];
+  public void writeI16(TTransport out, short i16) throws TException {
+    i16out[0] = (byte)(0xff & (i16 >> 8));
+    i16out[1] = (byte)(0xff & (i16));
+    out.write(i16out, 0, 2);
   }
 
   byte[] i32out = new byte[4];
@@ -65,10 +82,6 @@
     out.write(i32out, 0, 4);
   }
 
-  public void writeU64(TTransport out, long u64) throws TException {
-    writeI64(out, u64);
-  }
-
   byte[] i64out = new byte[8];
   public void writeI64(TTransport out, long i64) throws TException {
     i64out[0] = (byte)(0xff & (i64 >> 56));
@@ -92,6 +105,16 @@
    * Reading methods.
    */
 
+  public TMessage readMessageBegin(TTransport in) throws TException {
+    TMessage message = new TMessage();
+    message.name = readString(in);
+    message.type = readByte(in);
+    message.seqid = readI32(in);
+    return message;
+  }
+
+  public void readMessageEnd(TTransport in) throws TException {}
+
   public TStruct readStructBegin(TTransport in) throws TException {
     return new TStruct();
   }
@@ -102,7 +125,7 @@
     TField field = new TField();
     field.type = readByte(in);
     if (field.type != TType.STOP) {
-      field.id = readI32(in);
+      field.id = readI16(in);
     }
     return field;
   }
@@ -137,14 +160,23 @@
 
   public void readSetEnd(TTransport in) throws TException {}
 
+  public boolean readBool(TTransport in) throws TException {
+    return (readByte(in) == 1);
+  }
+
   byte[] bin = new byte[1];
   public byte readByte(TTransport in) throws TException {
     in.readAll(bin, 0, 1);
     return bin[0];
   }
 
-  public int readU32(TTransport in) throws TException {
-    return readI32(in);
+  byte[] i16rd = new byte[2];
+  public short readI16(TTransport in) throws TException {
+    in.readAll(i16rd, 0, 2);
+    return
+      (short)
+      (((i16rd[0] & 0xff) << 8) |
+       ((i16rd[1] & 0xff)));
   }
 
   byte[] i32rd = new byte[4];
@@ -156,11 +188,7 @@
       ((i32rd[2] & 0xff) <<  8) |
       ((i32rd[3] & 0xff));
   }
-
-  public long readU64(TTransport in) throws TException {
-    return readI64(in);
-  }
-  
+ 
   byte[] i64rd = new byte[8];
   public long readI64(TTransport in) throws TException {
     in.readAll(i64rd, 0, 8);
diff --git a/lib/java/src/protocol/TField.java b/lib/java/src/protocol/TField.java
index 50570fb..d32d708 100644
--- a/lib/java/src/protocol/TField.java
+++ b/lib/java/src/protocol/TField.java
@@ -8,7 +8,7 @@
 public class TField {
   public TField() {}
 
-  public TField(String n, byte t, int i) {
+  public TField(String n, byte t, short i) {
     name = n;
     type = t;
     id = i;
@@ -16,5 +16,5 @@
 
   public String name = "";
   public byte   type = TType.STOP;
-  public int    id   = 0;
+  public short  id   = 0;
 }
diff --git a/lib/java/src/protocol/TMessage.java b/lib/java/src/protocol/TMessage.java
new file mode 100644
index 0000000..92f4f8b
--- /dev/null
+++ b/lib/java/src/protocol/TMessage.java
@@ -0,0 +1,20 @@
+package com.facebook.thrift.protocol;
+
+/**
+ * Helper class that encapsulates struct metadata.
+ *
+ * @author Mark Slee <mcslee@facebook.com>
+ */
+public class TMessage {
+  public TMessage() {}
+
+  public TMessage(String n, byte t, int s) {
+    name = n;
+    type = t;
+    seqid = s;
+  }
+
+  public String name = "";
+  public byte type;
+  public int seqid;
+}
diff --git a/lib/java/src/protocol/TMessageType.java b/lib/java/src/protocol/TMessageType.java
new file mode 100644
index 0000000..905f99a
--- /dev/null
+++ b/lib/java/src/protocol/TMessageType.java
@@ -0,0 +1,11 @@
+package com.facebook.thrift.protocol;
+
+/**
+ * Message type constants in the Thrift protocol.
+ *
+ * @author Mark Slee <mcslee@facebook.com>
+ */
+public final class TMessageType {
+  public static final byte CALL  = 1;
+  public static final byte REPLY = 2;
+}
diff --git a/lib/java/src/protocol/TProtocol.java b/lib/java/src/protocol/TProtocol.java
index a60becb..69fe885 100644
--- a/lib/java/src/protocol/TProtocol.java
+++ b/lib/java/src/protocol/TProtocol.java
@@ -14,6 +14,11 @@
    * Writing methods.
    */
 
+  public void writeMessageBegin(TTransport out,
+                                TMessage   message) throws TException;
+
+  public void writeMessageEnd  (TTransport out)     throws TException;
+  
   public void writeStructBegin (TTransport out,
                                 TStruct    struct)  throws TException;
 
@@ -41,18 +46,18 @@
 
   public void writeSetEnd      (TTransport out)     throws TException;
 
+  public void writeBool        (TTransport out,
+                                boolean    b)       throws TException;
+
   public void writeByte        (TTransport out,
                                 byte       b)       throws TException;
 
-  public void writeU32         (TTransport out,
-                                int        u32)     throws TException;
+  public void writeI16         (TTransport out,
+                                short      i16)     throws TException;
 
   public void writeI32         (TTransport out,
                                 int        i32)     throws TException;
 
-  public void writeU64         (TTransport out,
-                                long       u64)     throws TException;
-
   public void writeI64         (TTransport out,
                                 long       i64)     throws TException;
 
@@ -63,36 +68,40 @@
    * Reading methods.
    */
 
-  public TStruct readStructBegin  (TTransport in)  throws TException;
+  public TMessage readMessageBegin (TTransport in)  throws TException;
 
-  public void    readStructEnd    (TTransport in)  throws TException;
+  public void     readMessageEnd   (TTransport in)  throws TException;
 
-  public TField  readFieldBegin   (TTransport in)  throws TException;
+  public TStruct  readStructBegin  (TTransport in)  throws TException;
+
+  public void     readStructEnd    (TTransport in)  throws TException;
+
+  public TField   readFieldBegin   (TTransport in)  throws TException;
   
-  public void    readFieldEnd     (TTransport in)  throws TException;
+  public void     readFieldEnd     (TTransport in)  throws TException;
  
-  public TMap    readMapBegin     (TTransport in)  throws TException;
+  public TMap     readMapBegin     (TTransport in)  throws TException;
 
-  public void    readMapEnd       (TTransport in)  throws TException;
+  public void     readMapEnd       (TTransport in)  throws TException;
 
-  public TList   readListBegin    (TTransport in)  throws TException;
+  public TList    readListBegin    (TTransport in)  throws TException;
 
-  public void    readListEnd      (TTransport in)  throws TException;
+  public void     readListEnd      (TTransport in)  throws TException;
 
-  public TSet    readSetBegin     (TTransport in)  throws TException;
+  public TSet     readSetBegin     (TTransport in)  throws TException;
 
-  public void    readSetEnd       (TTransport in)  throws TException;
+  public void     readSetEnd       (TTransport in)  throws TException;
 
-  public byte    readByte         (TTransport in)  throws TException;
+  public boolean  readBool         (TTransport in)  throws TException;
 
-  public int     readU32          (TTransport in)  throws TException;
+  public byte     readByte         (TTransport in)  throws TException;
 
-  public int     readI32          (TTransport in)  throws TException;
+  public short    readI16          (TTransport in)  throws TException;
 
-  public long    readU64          (TTransport in)  throws TException;
-  
-  public long    readI64          (TTransport in)  throws TException;
+  public int      readI32          (TTransport in)  throws TException;
+ 
+  public long     readI64          (TTransport in)  throws TException;
 
-  public String  readString       (TTransport in)  throws TException;
+  public String   readString       (TTransport in)  throws TException;
 
 }
diff --git a/lib/java/src/protocol/TProtocolUtil.java b/lib/java/src/protocol/TProtocolUtil.java
index b8e5afe..5223639 100644
--- a/lib/java/src/protocol/TProtocolUtil.java
+++ b/lib/java/src/protocol/TProtocolUtil.java
@@ -14,16 +14,22 @@
     throws TException {
 
     switch (type) {
+    case TType.BOOL:
+      {
+        prot.readBool(in);
+      }
     case TType.BYTE:
       {
         prot.readByte(in);
       }
-    case TType.U32:
+    case TType.I16:
+      {
+        prot.readI16(in);
+      }
     case TType.I32:
       {
         prot.readI32(in);
       }
-    case TType.U64:
     case TType.I64:
       {
         prot.readI64(in);
diff --git a/lib/java/src/protocol/TString.java b/lib/java/src/protocol/TString.java
deleted file mode 100644
index 04fcc1d..0000000
--- a/lib/java/src/protocol/TString.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package com.facebook.thrift.protocol;
-
-/**
- * Wrapper around String so that you can pass this object to a function and
- * have it set the internal string value.
- *
- * @author Mark Slee <mcslee@facebook.com>
- */
-public class TString {
-  public TString() {}
-
-  public TString(String v) {
-    value = v;
-  }
-
-  public String value = "";
-}
diff --git a/lib/java/src/protocol/TType.java b/lib/java/src/protocol/TType.java
index 96f06d3..37b53bd 100644
--- a/lib/java/src/protocol/TType.java
+++ b/lib/java/src/protocol/TType.java
@@ -6,17 +6,16 @@
  * @author Mark Slee <mcslee@facebook.com>
  */
 public final class TType {
-  public static final byte STOP   = 1;
-  public static final byte BYTE   = 2;
-  public static final byte U16    = 3;
-  public static final byte I16    = 4;
-  public static final byte U32    = 5;
-  public static final byte I32    = 6;
-  public static final byte U64    = 7;
-  public static final byte I64    = 8;
-  public static final byte STRING = 9;
-  public static final byte STRUCT = 10;
-  public static final byte MAP    = 11;
-  public static final byte SET    = 12;
-  public static final byte LIST   = 13;
+  public static final byte STOP   = 0;
+  public static final byte VOID   = 1;
+  public static final byte BOOL   = 2;
+  public static final byte BYTE   = 3;
+  public static final byte I16    = 6;
+  public static final byte I32    = 8;
+  public static final byte I64    = 10;
+  public static final byte STRING = 11;
+  public static final byte STRUCT = 12;
+  public static final byte MAP    = 13;
+  public static final byte SET    = 14;
+  public static final byte LIST   = 15;
 }