Package io.airlift.drift.protocol
Class TCompactProtocol
java.lang.Object
io.airlift.drift.protocol.TCompactProtocol
- All Implemented Interfaces:
TProtocol,TProtocolReader,TProtocolWriter
TCompactProtocol2 is the Java implementation of the compact protocol specified
in THRIFT-110. The fundamental approach to reducing the overhead of
structures is a) use variable-length integers all over the place and b) make
use of unused bits wherever possible. Your savings will obviously vary
based on the specific makeup of your structs, but in general, the more
fields, nested structures, short strings and collections, and low-value i32
and i64 fields you have, the more benefit you'll see.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionRead a byte[] from the wire.booleanreadBool()Read a boolean off the wire.bytereadByte()Read a single byte off the wire.doubleNo magic here - just read a double off the wire.Read a field header off the wire.voidfloatNo magic here - just read a float off the wire.shortreadI16()Read an i16 from the wire as a zigzag varint.intreadI32()Read an i32 from the wire as a zigzag varint.longreadI64()Read an i64 from the wire as a zigzag varint.Read a list header off the wire.voidRead a map header off the wire.voidRead a message header.voidRead a set header off the wire.voidReads a byte[] (via readBinary), and then UTF-8 decodes it.Read a struct begin.voidDoesn't actually consume any wire data, just removes the last field for this struct from the field stack.voidwriteBinary(ByteBuffer value) Write a byte array, using a varint for the size.voidwriteBool(boolean value) Write a boolean value.voidwriteByte(byte value) Write a byte.voidwriteDouble(double value) Write a double to the wire as 8 bytes.voidwriteFieldBegin(TField field) Write a field header containing the field id and field type.voidvoidWrite the STOP symbol so we know there are no more fields in this struct.voidwriteFloat(float value) Write a float to the wire as 4 bytes.voidwriteI16(short value) Write an I16 as a zigzag varint.voidwriteI32(int value) Write an i32 as a zigzag varint.voidwriteI64(long value) Write an i64 as a zigzag varint.voidwriteListBegin(TList list) Write a list header.voidvoidwriteMapBegin(TMap map) Write a map header.voidvoidwriteMessageBegin(TMessage message) Write a message header to the wire.voidvoidwriteSetBegin(TSet set) Write a set header.voidvoidwriteString(String value) Write a string to the wire with a varint size preceding.voidwriteStructBegin(TStruct struct) Write a struct begin.voidWrite a struct end.
-
Constructor Details
-
TCompactProtocol
Create a TCompactProtocol.- Parameters:
transport- the TTransport object to read from or write to.
-
-
Method Details
-
writeMessageBegin
Write a message header to the wire. Compact Protocol messages contain the protocol version so we can migrate forwards in the future if need be.- Specified by:
writeMessageBeginin interfaceTProtocolWriter- Throws:
TException
-
writeStructBegin
Write a struct begin. This doesn't actually put anything on the wire. We use it as an opportunity to put special placeholder markers on the field stack so we can get the field id deltas correct.- Specified by:
writeStructBeginin interfaceTProtocolWriter
-
writeStructEnd
public void writeStructEnd()Write a struct end. This doesn't actually put anything on the wire. We use this as an opportunity to pop the last field from the current struct off of the field stack.- Specified by:
writeStructEndin interfaceTProtocolWriter
-
writeFieldBegin
Write a field header containing the field id and field type. If the difference between the current field id and the last one is small (< 15), then the field id will be encoded in the 4 MSB as a delta. Otherwise, the field id will follow the type header as a zigzag varint.- Specified by:
writeFieldBeginin interfaceTProtocolWriter- Throws:
TException
-
writeFieldStop
Write the STOP symbol so we know there are no more fields in this struct.- Specified by:
writeFieldStopin interfaceTProtocolWriter- Throws:
TException
-
writeMapBegin
Write a map header. If the map is empty, omit the key and value type headers, as we don't need any additional information to skip it.- Specified by:
writeMapBeginin interfaceTProtocolWriter- Throws:
TException
-
writeListBegin
Write a list header.- Specified by:
writeListBeginin interfaceTProtocolWriter- Throws:
TException
-
writeSetBegin
Write a set header.- Specified by:
writeSetBeginin interfaceTProtocolWriter- Throws:
TException
-
writeBool
Write a boolean value. Potentially, this could be a boolean field, in which case the field header info isn't written yet. If so, decide what the right type header is for the value and then write the field header. Otherwise, write a single byte.- Specified by:
writeBoolin interfaceTProtocolWriter- Throws:
TException
-
writeByte
Write a byte. Nothing to see here!- Specified by:
writeBytein interfaceTProtocolWriter- Throws:
TException
-
writeI16
Write an I16 as a zigzag varint.- Specified by:
writeI16in interfaceTProtocolWriter- Throws:
TException
-
writeI32
Write an i32 as a zigzag varint.- Specified by:
writeI32in interfaceTProtocolWriter- Throws:
TException
-
writeI64
Write an i64 as a zigzag varint.- Specified by:
writeI64in interfaceTProtocolWriter- Throws:
TException
-
writeFloat
Write a float to the wire as 4 bytes.- Specified by:
writeFloatin interfaceTProtocolWriter- Throws:
TException
-
writeDouble
Write a double to the wire as 8 bytes.- Specified by:
writeDoublein interfaceTProtocolWriter- Throws:
TException
-
writeString
Write a string to the wire with a varint size preceding.- Specified by:
writeStringin interfaceTProtocolWriter- Throws:
TException
-
writeBinary
Write a byte array, using a varint for the size.- Specified by:
writeBinaryin interfaceTProtocolWriter- Throws:
TException
-
writeMessageEnd
public void writeMessageEnd()- Specified by:
writeMessageEndin interfaceTProtocolWriter
-
writeMapEnd
public void writeMapEnd()- Specified by:
writeMapEndin interfaceTProtocolWriter
-
writeListEnd
public void writeListEnd()- Specified by:
writeListEndin interfaceTProtocolWriter
-
writeSetEnd
public void writeSetEnd()- Specified by:
writeSetEndin interfaceTProtocolWriter
-
writeFieldEnd
public void writeFieldEnd()- Specified by:
writeFieldEndin interfaceTProtocolWriter
-
readMessageBegin
Read a message header.- Specified by:
readMessageBeginin interfaceTProtocolReader- Throws:
TException
-
readStructBegin
Read a struct begin. There's nothing on the wire for this, but it is our opportunity to push a new struct begin marker onto the field stack.- Specified by:
readStructBeginin interfaceTProtocolReader
-
readStructEnd
public void readStructEnd()Doesn't actually consume any wire data, just removes the last field for this struct from the field stack.- Specified by:
readStructEndin interfaceTProtocolReader
-
readFieldBegin
Read a field header off the wire.- Specified by:
readFieldBeginin interfaceTProtocolReader- Throws:
TException
-
readMapBegin
Read a map header off the wire. If the size is zero, skip reading the key and value type. This means that 0-length maps will yield TMaps without the "correct" types.- Specified by:
readMapBeginin interfaceTProtocolReader- Throws:
TException
-
readListBegin
Read a list header off the wire. If the list size is 0-14, the size will be packed into the element type header. If it's a longer list, the 4 MSB of the element type header will be 0xF, and a varint will follow with the true size.- Specified by:
readListBeginin interfaceTProtocolReader- Throws:
TException
-
readSetBegin
Read a set header off the wire. If the set size is 0-14, the size will be packed into the element type header. If it's a longer set, the 4 MSB of the element type header will be 0xF, and a varint will follow with the true size.- Specified by:
readSetBeginin interfaceTProtocolReader- Throws:
TException
-
readBool
Read a boolean off the wire. If this is a boolean field, the value should already have been read during readFieldBegin, so we'll just consume the pre-stored value. Otherwise, read a byte.- Specified by:
readBoolin interfaceTProtocolReader- Throws:
TException
-
readByte
Read a single byte off the wire. Nothing interesting here.- Specified by:
readBytein interfaceTProtocolReader- Throws:
TException
-
readI16
Read an i16 from the wire as a zigzag varint.- Specified by:
readI16in interfaceTProtocolReader- Throws:
TException
-
readI32
Read an i32 from the wire as a zigzag varint.- Specified by:
readI32in interfaceTProtocolReader- Throws:
TException
-
readI64
Read an i64 from the wire as a zigzag varint.- Specified by:
readI64in interfaceTProtocolReader- Throws:
TException
-
readFloat
No magic here - just read a float off the wire.- Specified by:
readFloatin interfaceTProtocolReader- Throws:
TException
-
readDouble
No magic here - just read a double off the wire.- Specified by:
readDoublein interfaceTProtocolReader- Throws:
TException
-
readString
Reads a byte[] (via readBinary), and then UTF-8 decodes it.- Specified by:
readStringin interfaceTProtocolReader- Throws:
TException
-
readBinary
Read a byte[] from the wire.- Specified by:
readBinaryin interfaceTProtocolReader- Throws:
TException
-
readMessageEnd
public void readMessageEnd()- Specified by:
readMessageEndin interfaceTProtocolReader
-
readFieldEnd
public void readFieldEnd()- Specified by:
readFieldEndin interfaceTProtocolReader
-
readMapEnd
public void readMapEnd()- Specified by:
readMapEndin interfaceTProtocolReader
-
readListEnd
public void readListEnd()- Specified by:
readListEndin interfaceTProtocolReader
-
readSetEnd
public void readSetEnd()- Specified by:
readSetEndin interfaceTProtocolReader
-