Search code examples
javathrift

Extra boolean set fields in java generated file using thrift


I have a thrift file point.thrift that has only two variables as follows:

struct Point {
  1: double lat,
  2: double lon
}

But when I generate the java file using,

thrift --gen java point.thrift

then the generated java code has other boolean variables, setLat and setLon. So, my Point object looks like below:

"point": {
  "lat": 12.983451879142935,
  "lon": 77.75181960314512,
  "setLat": true,
  "setLon": true
}

Is there any way these extra boolean variables can be removed from my point object because it is increasing the size of my object, which in turn will take up a lot of space when I store them. Note that this is the basic definition but the actual definition will have lots of other fields. So, storage is a concern for me.


Solution

  • The Thrift compiler generates a bitset to determine, what fields are set or not set. The background for these lies in what codesf already said: requiredness.

    In short: Thrift supports three kinds of requiredness, optional, required and a third one called "default" requiredness. The latter takes place when there is no explicit optional or required specification. The Thrift runtime libraries use these flags to determine, whether or not a particular field has been assigned a value.

    In the case of Java, the implementation obviously always generates these "isset" bits, no matter what requiredness you specify.