Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert more classes to record classes #13328

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

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.

public float[] getVector() {
return this.vector;
}
public record TermAndVector(BytesRef term, float[] vector) {
Copy link
Contributor

Choose a reason for hiding this comment

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

this class has the problem that it supplies a method to normalize the inner vector. This is against immutability of records. It is possible to do this, but feels bad.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense! I changed it back to class in the new revision

this.format = format;
this.bitsPerValue = bitsPerValue;
}
public record FormatAndBits(Format format, int bitsPerValue) {

@Override
public String toString() {
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need toString(), this one can be autogenerated, just the output is different.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed this now

@@ -116,7 +105,8 @@ public CollectionStatistics(
*
* @return field's name, not {@code null}
*/
public final String field() {
Copy link
Contributor

Choose a reason for hiding this comment

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

we can remove all accessor methods and add the documentation into @param on the record declaration.

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 also changed some more records I could find to be as leaner as possible.

@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.

this.score = score;
}
private record Weighted<T extends Comparable<T>>(T word, int score)
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

public int hashCode() {
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

* @param term the term
* @param boost the boost
*/
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);
}

@@ -68,16 +63,6 @@ public int hashCode() {
hash = (int) (hash ^ endOrd);
return hash;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

remove hashCode, too ^^

public final BytesRef maxTerm;

public FieldMetaData(
private record FieldMetaData(
Copy link
Contributor

Choose a reason for hiding this comment

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

make this the default ctor (see above and remove the field assignments), just keep asserts or other checks

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

This PR has not had activity in the past 2 weeks, labeling it as stale. If the PR is waiting for review, notify the [email protected] 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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants