[ 
https://issues.apache.org/jira/browse/THRIFT-5483?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17463543#comment-17463543
 ] 

Tian Jiang edited comment on THRIFT-5483 at 12/22/21, 2:51 AM:
---------------------------------------------------------------

Thanks for your patient reply. This case is quite common in Java, but I 
understand that Java is not the only supported language by thrift and it is not 
very good to change the grammar for one language support (maybe other languages 
also encounter the same problem).

Still, there is another more light-weighted way to do this. I think it is 
practical to add a compiling option to disable generating comparator and hash 
code. For example, when I use:
`thrift --gen java --noJavaComparator myfile.thrift`
The generated Java classes will not include comparators and hashcode, so it is 
safer to use them in indexed data structures like HashMaps.

I would like to implement it and contribute, but I am mainly a Java programmer 
and not so familiar with CPP projects.


was (Author: jt2594838):
This case is quite common in Java, but I understand that Java is not the only 
supported language by thrift and it is not very good to change the grammar for 
one language support (maybe other languages also encounter the same problem).

Still, there is another more light-weighted way to do this. I think it is 
practical to add a compiling option to disable generating comparator and hash 
code. For example, when I use:
`thrift --gen java --noJavaComparator myfile.thrift`
The generated Java classes will not include comparators and hashcode, so it is 
safer to use them in indexed data structures like HashMaps.

I would like to implement it and contribute, but I am mainly a Java programmer 
and not so familiar with CPP projects.

> Support customized comparator in Java
> -------------------------------------
>
>                 Key: THRIFT-5483
>                 URL: https://issues.apache.org/jira/browse/THRIFT-5483
>             Project: Thrift
>          Issue Type: Improvement
>            Reporter: Tian Jiang
>            Priority: Major
>
> The generated comparator (and hash code) of Thrift Structs compares two 
> objects by all of their fields, which may cause trouble when used in hash 
> structures.
> For example, I define a struct like:
> {code:thrift}
> struct Person{
>   1: required i64 id
>   2: required string phoneNumber
> }
> {code}
> The id of a Person is final, while its phoneNumber can be changed.
> Then I use it as a key in a map to record a Person's supervisors:
> {code:java}
> Map<Person, Person> supervisorMap;
> {code}
> Apparently, as the generated comparator of Person will use both id and 
> phoneNumber for comparison and hash if I change the phoneNumber of a Person, 
> I can no longer get it from the map as its hash value changes and it will be 
> distributed to another hash slot.
> Therefore, I wish I could specify the fields that are to be used in 
> comparator and hash code generation. One preferable grammar may be like:
> {code:thrift}
> struct Person{
>   1: required comparable i64 id
>   2: required string phoneNumber
> }
> {code}
> Then the generated comparator and hash code will only use fields marked with 
> `comparable`, and if no fields are marked with comparable, Java's default 
> comparison and hashcode (using object address) will be used. 



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to