11

I have a structure in c++ which stores bytes like this:

struct RemoteData 
{
    /// some other fields here

    unsigned char* buf;
    int bufLen;     
};

And I need to send this data to remote service, written in C++, via thrift. I found three ways how to map this structure to thrift idl:

  1. Using container types like this:

    struct RemoteData 
    {
        1: list<BYTE> buf,
        ...
    }
    
  2. Using binary type:

    struct RemoteData 
    {
        1: binary buf,
        ...
    }
    
  3. Storing data in string type:

    struct RemoteData 
    {
        1: string buf,
        ...
    }
    

What is the best way?

Jason Aller
  • 3,391
  • 28
  • 37
  • 36
DanilaNV
  • 151
  • 2
  • 3
  • 13

1 Answers1

15

The value contained in thrift string type must be UTF8-encoded, otherwise some client won't be able to read it (Java thrift client for example).

Thrift list<byte> type will be converted into std::vector<int8_t> in c++, but in other languages it will be not so good (for example, in java it will be compiled into suboptimal List<Byte>.

The binary type is the best choice in terms of interoperability with clients in other languages and the correctness of protocol definition.

Since all 3 definitions produce messages of roughly the same size, I'd go with binary: even if you don't need interoperability with other languages now, you may need it in future.

Wildfire
  • 6,140
  • 2
  • 29
  • 50
  • 1
    But in thrift documentation http://thrift.apache.org/docs/types/ written about binary type "N.B.: This is currently a specialized form of the string type above, added to provide better interoperability with Java. The current plan-of-record is to elevate this to a base type at some point." – DanilaNV Dec 17 '12 at 05:43
  • 5
    @DanilaNV This documentation is probably outdated: Thrift binary type is as stable, convenient and widely used as other types, so I don't see why it is not in the list of basic types yet. – Wildfire Dec 17 '12 at 12:08