Skip to content

Conversation

@shubhamvishu
Copy link
Contributor

Description

  • This PR addresses Find more classes in main branch that can be converted to record classes #13207 to convert more classes on main branch to record classes on main (Lucene 10 only).
  • It moves a lot of data classes(120 to be precise) to use record classes that were flagged suitable in my IDE and seemed good candidate in general.
  • I left a couple of them like TotalHits and SynonymMap as the PR is already very big and including those were leading to a lot more changes. Maybe we could do those as a separate PR.

 
Raising this PR since all the tests are passing(./gradlew test) but renderJavadoc task is complaining about missing java docs on some record classes(converted in this PR) which I see has the javadocs already eg: TermStats, ReaderSlice. I'm not sure why its flagging those incorrectly or if maybe I'm missing something.

@uschindler
Copy link
Contributor

uschindler commented Apr 30, 2024

Hi, you can merge main into your branch and hopefully checks pass. For some packages like the "codecs" one, the new code requires you to also document all record components with @param.

@shubhamvishu
Copy link
Contributor Author

I addressed your comments in the new revision and all the checks are passing now after your fix. Thanks!

@uschindler
Copy link
Contributor

Thanks, looks fine. I have no time to do another closer review, please give me some time to proceed.

Copy link
Contributor

@uschindler uschindler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went through the first few files and have some changes:

  • there were some hashCodes revoed but equals survied, please remove both
  • some toString may be obsolete, too (unless the exact formatting is important)
  • some ctors with exact same parameers like default can be rewritten to use the default syntax and remove the field assignments

I will proceed later on the weekend with other classes.

implements Comparable<Weighted<T>> {

@Override
public boolean equals(Object o) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs to be removed, too

return Objects.hash(word, score);
}

@Override
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's keep that one, as the toString output is different from default

*/
public record TermAndBoost(BytesRef term, float boost) {
/** Creates a new TermAndBoost */
public TermAndBoost(BytesRef term, float boost) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be converted to the generic ctor without parameters:

public TermAndBoost {
  term = BytesRef.deepCopyOf(term);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!.....I changed others such occurrences that I could find as well

record Cell(
PointTree index, int readerIndex, byte[] minPacked, byte[] maxPacked, double distanceSortKey)
implements Comparable<Cell> {
Cell(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove that ctor and replace by default ctor and only copy minPacked and maxPacked (see above).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@github-actions
Copy link
Contributor

This PR has not had activity in the past 2 weeks, labeling it as stale. If the PR is waiting for review, notify the dev@lucene.apache.org list. Thank you for your contribution!

@github-actions github-actions bot added the Stale label May 17, 2024
@uschindler
Copy link
Contributor

Hi @shubhamvishu,
can you check my comments? The changes here look fine, please fix the remaining issues.
Uwe

@github-actions github-actions bot removed the Stale label May 19, 2024
@shubhamvishu
Copy link
Contributor Author

shubhamvishu commented May 20, 2024

Hi @uschindler , sorry I was on vacation until last week, so this PR stalled. I'll take a look at the comments today or tomorrow.

Update : Got stuck with some other work. Will take a look at this sometime this week.

@shubhamvishu
Copy link
Contributor Author

Hi @uschindler , thanks for the review. I took another pass at the changes and pushed some commits addressing your comments. Please have a look when you get a chance. Thanks!

@shubhamvishu shubhamvishu requested a review from uschindler June 11, 2024 20:42
@github-actions
Copy link
Contributor

This PR has not had activity in the past 2 weeks, labeling it as stale. If the PR is waiting for review, notify the dev@lucene.apache.org list. Thank you for your contribution!

@github-actions github-actions bot added the Stale label Jun 26, 2024
@shubhamvishu
Copy link
Contributor Author

Hi @uschindler, Could you please review the current code changes once you get some time and if it looks good maybe we can move this forward?

@github-actions github-actions bot removed the Stale label Jul 5, 2024
@github-actions
Copy link
Contributor

This PR has not had activity in the past 2 weeks, labeling it as stale. If the PR is waiting for review, notify the dev@lucene.apache.org list. Thank you for your contribution!

@github-actions github-actions bot added the Stale label Jul 19, 2024
@stefanvodita stefanvodita added the blocker A severe issue that should be resolved before the released specified in its Milestone. label Sep 9, 2024
@stefanvodita
Copy link
Contributor

Added the blocker label to draw attention to this PR for the Lucene 10 release.

@shubhamvishu
Copy link
Contributor Author

Hi @uschindler @mikemccand @jpountz , I fixed the recent merge conflicts and looking for reviews as 10.0 release is blocked on this change. This PR has been attracting merge conflicts frequently since its touching 200+ files, so hoping we could close this sooner. Let me know if there is any concerns here(I'm up for quick followups). Thanks!

Copy link
Contributor

@jpountz jpountz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left some minor comments but the change looks good to me in general. Can you add an entry to lucene/MIGRATE.txt that explains that TopDocs, CollectionStatistics, TermStatistics and LeafMetadata were converted to records? I'm only considering mentioning these because these are the most user-facing APIs that are migrated to a record from what I can tell (please let me know if you think I'm missing an important one).

@Override
public String toString() {
return "TwoLongs:" + first + "," + second;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll get a different toString() but I think it's fine in this case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, its fine in this case.

if (o == null || getClass() != o.getClass()) return false;
FieldAndWeight that = (FieldAndWeight) o;
return Float.compare(that.weight, weight) == 0 && Objects.equals(field, that.field);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you note remove this equals override because the record would behave differently? (I tried to quickly look up what record does wrt equality for floats and doubles but couldn't find it, will look later)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, I think its a leftover.I'll change it. Found similar one TermAndBoost in SynonymQuery also.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I tried to quickly look up what record does wrt equality for floats and doubles but couldn't find it, will look later)

I found one mention of floating point numbers in the JEP for records:

In addition, for all record classes the implicitly declared equals method is implemented so that it is reflexive and that it behaves consistently with hashCode for record classes that have floating point components. Again, explicitly declared equals and hashCode methods should behave similarly.

I'm not sure I understand that paragraph :)

Copy link
Contributor

@uschindler uschindler Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
The equals methods for floats is generated by invokedynamic at runtime using a bootstrapping:

https://github.com/openjdk/jdk/blob/9785e19f3f87306cabc26a862d35b89d41cfef93/src/java.base/share/classes/java/lang/runtime/ObjectMethods.java#L382

The class file of a record only contains a stub equals/hashcode/to string which delegates to an invokedynamic call passing the class which allows the bootstrapped to get all record components and construct a method handle for the whole method implementation.

When a record component is a float, the autogenerated impl using this impl to handle floats. It compares them using this code:

https://github.com/openjdk/jdk/blob/9785e19f3f87306cabc26a862d35b89d41cfef93/src/java.base/share/classes/java/lang/runtime/ObjectMethods.java#L157

The method handles used by the code generator are saved in a map:

https://github.com/openjdk/jdk/blob/9785e19f3f87306cabc26a862d35b89d41cfef93/src/java.base/share/classes/java/lang/runtime/ObjectMethods.java#L101

This is equivalent to the already existing code. So please remove all custom equals methods.

Unfortunately in the merged PR there are some more. Please remove them in a followup.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I'll check and followup with removing others I could find.

this.bits = bits;
this.length = length;
}
record FixedBits(long[] bits, int length) implements Bits {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't really fit my mental model for a record, maybe undo the refactoring on this class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

@github-actions github-actions bot removed the Stale label Sep 10, 2024
@shubhamvishu
Copy link
Contributor Author

@jpountz I addressed your comments and added a CHANGES, MIGRATE entry now. Thanks for reviewing!

Copy link
Member

@mikemccand mikemccand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for persisting here @shubhamvishu -- this is a massive rote refactoring change. We should rather bias towards quickly merging such changes since they pick up conflicts so quickly. I just left some minor feedback about wording in CHANGES and MIGRATE, else +1 to merge.

argument with a `FacetsCollectorManager` and update the return type to include both `TopDocs` results as well as
facets results. (Luca Cavanna)

* GITHUB#13328: Convert CollectionStatistics, TermStatistics and LeafMetadata etc. to record classes. (Shubham Chaudhary)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe reword to Convert many basic Lucene classes to record classes, including X, Y, and Z? It's not just these three classes, if I'm reading the PR correctly, and the etc. should not have so much power ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks better indeed. I'll reword it.


- `IOContext`, `MergeInfo`, and `FlushInfo` (GITHUB#13205)
- `BooleanClause` (GITHUB#13261)
- `CollectionStatistics`, `TermStatistics` and `LeafMetadata` (GITHUB#13328)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And maybe change this wording too (same as CHANGES.txt)? I think @jpountz asked for TopDocs to also be mentioned?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll reword. I haven't changed TopDocs in this PR and hence I didn't include it. Maybe some confusion(prolly because there are just too many classes changed here)

if (o == null || getClass() != o.getClass()) return false;
FieldAndWeight that = (FieldAndWeight) o;
return Float.compare(that.weight, weight) == 0 && Objects.equals(field, that.field);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I tried to quickly look up what record does wrt equality for floats and doubles but couldn't find it, will look later)

I found one mention of floating point numbers in the JEP for records:

In addition, for all record classes the implicitly declared equals method is implemented so that it is reflexive and that it behaves consistently with hashCode for record classes that have floating point components. Again, explicitly declared equals and hashCode methods should behave similarly.

I'm not sure I understand that paragraph :)

@jpountz jpountz merged commit 942065c into apache:main Sep 10, 2024
@stefanvodita
Copy link
Contributor

I think we've run into a conflict with #13689. @jpountz - should I fix that or are you already doing it?

@jpountz
Copy link
Contributor

jpountz commented Sep 10, 2024

@javanna just fixed it, thanks @javanna !

Copy link
Contributor

@uschindler uschindler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is one as symmetry in removal of equals/hashcode.

Please fix this in a new PR.

return docBase == result.docBase && Float.compare(result.score, score) == 0;
}

@Override
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also remove the equals, see this comment: #13328 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To string is also useless here. Please remove, too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll fix it in a followup PR. I could only find this instance in the classes changed to records where we have removable custom equals/toString but I'll take another pass to be sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened a followup PR #13769. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocker A severe issue that should be resolved before the released specified in its Milestone.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants