Apache Lucene open-source search software
Migrate more classes to records. (#13772)
Adrien Grand2f7da75b7d1f
diff --git a/lucene/MIGRATE.md b/lucene/MIGRATE.mdindex b155e7d087f..8cf9c299b29 100644--- a/lucene/MIGRATE.md+++ b/lucene/MIGRATE.md@@ -194,6 +194,7 @@ access the members using method calls instead of field accesses. Affected classe- `IOContext`, `MergeInfo`, and `FlushInfo` (GITHUB#13205)- `BooleanClause` (GITHUB#13261)- `TotalHits` (GITHUB#13762)+- `TermAndVector` (GITHUB#13772)- Many basic Lucene classes, including `CollectionStatistics`, `TermStatistics` and `LeafMetadata` (GITHUB#13328)### Boolean flags on IOContext replaced with a new ReadAdvice enum.diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/word2vec/Word2VecModel.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/word2vec/Word2VecModel.javaindex f43d33ca274..8189de4dd6c 100644--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/word2vec/Word2VecModel.java+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/word2vec/Word2VecModel.java@@ -56,25 +56,25 @@ public class Word2VecModel implements RandomAccessVectorValues.Floats {}public void addTermAndVector(TermAndVector modelEntry) {- modelEntry.normalizeVector();+ modelEntry = modelEntry.normalizeVector();this.termsAndVectors[loadedCount++] = modelEntry;- this.word2Vec.add(modelEntry.getTerm());+ this.word2Vec.add(modelEntry.term());}@Overridepublic float[] vectorValue(int targetOrd) {- return termsAndVectors[targetOrd].getVector();+ return termsAndVectors[targetOrd].vector();}public float[] vectorValue(BytesRef term) {int termOrd = this.word2Vec.find(term);if (termOrd < 0) return null;TermAndVector entry = this.termsAndVectors[termOrd];- return (entry == null) ? null : entry.getVector();+ return (entry == null) ? null : entry.vector();}public BytesRef termValue(int targetOrd) {- return termsAndVectors[targetOrd].getTerm();+ return termsAndVectors[targetOrd].term();}@Overridediff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/synonym/word2vec/TestWord2VecSynonymProvider.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/synonym/word2vec/TestWord2VecSynonymProvider.javaindex 228da4ede14..1d36d82a302 100644--- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/synonym/word2vec/TestWord2VecSynonymProvider.java+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/synonym/word2vec/TestWord2VecSynonymProvider.java@@ -120,8 +120,8 @@ public class TestWord2VecSynonymProvider extends LuceneTestCase {@Testpublic void normalizedVector_shouldReturnModule1() {TermAndVector synonymTerm = new TermAndVector(new BytesRef("a"), new float[] {10, 10});- synonymTerm.normalizeVector();- float[] vector = synonymTerm.getVector();+ synonymTerm = synonymTerm.normalizeVector();+ float[] vector = synonymTerm.vector();float len = 0;for (int i = 0; i < vector.length; i++) {len += vector[i] * vector[i];diff --git a/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldDocValuesFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldDocValuesFormat.javaindex 6723f530aae..9350c016f67 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldDocValuesFormat.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldDocValuesFormat.java@@ -78,10 +78,7 @@ public abstract class PerFieldDocValuesFormat extends DocValuesFormat {return new FieldsWriter(state);}- static class ConsumerAndSuffix implements Closeable {- DocValuesConsumer consumer;- int suffix;-+ record ConsumerAndSuffix(DocValuesConsumer consumer, int suffix) implements Closeable {@Overridepublic void close() throws IOException {consumer.close();@@ -222,10 +219,10 @@ public abstract class PerFieldDocValuesFormat extends DocValuesFormat {final String segmentSuffix =getFullSegmentSuffix(segmentWriteState.segmentSuffix, getSuffix(formatName, Integer.toString(suffix)));- consumer = new ConsumerAndSuffix();- consumer.consumer =- format.fieldsConsumer(new SegmentWriteState(segmentWriteState, segmentSuffix));- consumer.suffix = suffix;+ consumer =+ new ConsumerAndSuffix(+ format.fieldsConsumer(new SegmentWriteState(segmentWriteState, segmentSuffix)),+ suffix);formats.put(format, consumer);} else {// we've already seen this format, so just grab its suffixdiff --git a/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldPostingsFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldPostingsFormat.javaindex 61749a09284..182bed5797c 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldPostingsFormat.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldPostingsFormat.java@@ -79,22 +79,13 @@ public abstract class PerFieldPostingsFormat extends PostingsFormat {super(PER_FIELD_NAME);}- /** Group of fields written by one PostingsFormat */- static class FieldsGroup {- final List<String> fields;- final int suffix;-- /**- * Custom SegmentWriteState for this group of fields, with the segmentSuffix uniqueified for- * this PostingsFormat- */- final SegmentWriteState state;-- private FieldsGroup(List<String> fields, int suffix, SegmentWriteState state) {- this.fields = fields;- this.suffix = suffix;- this.state = state;- }+ /**+ * Group of fields written by one PostingsFormat+ *+ * @param state Custom SegmentWriteState for this group of fields, with the segmentSuffix+ * uniqueified for this PostingsFormat+ */+ record FieldsGroup(List<String> fields, int suffix, SegmentWriteState state) {static class Builder {final Set<String> fields;diff --git a/lucene/core/src/java/org/apache/lucene/util/TermAndVector.java b/lucene/core/src/java/org/apache/lucene/util/TermAndVector.javaindex 1ade19a1980..af544c591a3 100644--- a/lucene/core/src/java/org/apache/lucene/util/TermAndVector.java+++ b/lucene/core/src/java/org/apache/lucene/util/TermAndVector.java@@ -24,37 +24,17 @@ import java.util.Locale;** @lucene.experimental*/-public class TermAndVector {-- private final BytesRef term;- private final float[] vector;-- public TermAndVector(BytesRef term, float[] vector) {- this.term = term;- this.vector = vector;- }-- public BytesRef getTerm() {- return this.term;- }-- public float[] getVector() {- return this.vector;- }+public record TermAndVector(BytesRef term, float[] vector) {public int size() {return vector.length;}- public void normalizeVector() {- float vectorLength = 0;- for (int i = 0; i < vector.length; i++) {- vectorLength += vector[i] * vector[i];- }- vectorLength = (float) Math.sqrt(vectorLength);- for (int i = 0; i < vector.length; i++) {- vector[i] /= vectorLength;- }+ /** Return a {@link TermAndVector} whose vector is normalized according to the L2 norm. */+ public TermAndVector normalizeVector() {+ float[] vector = this.vector.clone();+ VectorUtil.l2normalize(vector);+ return new TermAndVector(term, vector);}@Override