Skip to content

Commit 1539844

Browse files
committed
update after review
1 parent 58be89d commit 1539844

File tree

8 files changed

+70
-60
lines changed

8 files changed

+70
-60
lines changed

x-pack/plugin/old-lucene-versions/src/main/java/org/elasticsearch/xpack/lucene/bwc/codecs/BWCCodec.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.lucene.store.Directory;
2929
import org.apache.lucene.store.IOContext;
3030
import org.apache.lucene.util.Version;
31+
import org.elasticsearch.core.UpdateForV10;
3132
import org.elasticsearch.xpack.lucene.bwc.codecs.lucene80.BWCLucene80Codec;
3233
import org.elasticsearch.xpack.lucene.bwc.codecs.lucene84.BWCLucene84Codec;
3334
import org.elasticsearch.xpack.lucene.bwc.codecs.lucene86.BWCLucene86Codec;
@@ -97,23 +98,60 @@ public SegmentInfoFormat segmentInfoFormat() {
9798
return segmentInfosFormat;
9899
}
99100

101+
/**
102+
* This method is not supported for archive indices and older codecs and will always throw an {@link UnsupportedOperationException}.
103+
* This method is never called in practice, as we rewrite field infos to override the info about which features are present in
104+
* the index. Even if norms are present, field info lies about it.
105+
*
106+
* @return nothing, as this method always throws an exception
107+
* @throws UnsupportedOperationException always thrown to indicate that this method is not supported
108+
*/
100109
@Override
101110
public final NormsFormat normsFormat() {
102111
throw new UnsupportedOperationException();
103112
}
104113

114+
/**
115+
* This method is not supported for archive indices and older codecs and will always throw an {@link UnsupportedOperationException}.
116+
* This method is never called in practice, as we rewrite field infos to override the info about which features are present in
117+
* the index. Even if term vectors are present, field info lies about it.
118+
*
119+
* @return nothing, as this method always throws an exception
120+
* @throws UnsupportedOperationException always thrown to indicate that this method is not supported
121+
*/
105122
@Override
106123
public final TermVectorsFormat termVectorsFormat() {
107124
throw new UnsupportedOperationException();
108125
}
109126

127+
/**
128+
* This method is not supported for archive indices and older codecs and will always throw an {@link UnsupportedOperationException}.
129+
* The knn vectors can't be present because it is not supported yet in any of the lucene versions that we support for archive indices.
130+
*
131+
* @return nothing, as this method always throws an exception
132+
* @throws UnsupportedOperationException always thrown to indicate that this method is not supported
133+
*/
110134
@Override
111135
public final KnnVectorsFormat knnVectorsFormat() {
112136
throw new UnsupportedOperationException();
113137
}
114138

139+
/**
140+
* Returns the original {@link SegmentInfoFormat} used by this codec.
141+
* This method should be implemented by subclasses to provide the specific
142+
* {@link SegmentInfoFormat} that this codec is intended to use.
143+
*
144+
* @return the original {@link SegmentInfoFormat} used by this codec
145+
*/
115146
protected abstract SegmentInfoFormat originalSegmentInfoFormat();
116147

148+
/**
149+
* Returns the original {@link FieldInfosFormat} used by this codec.
150+
* This method should be implemented by subclasses to provide the specific
151+
* {@link FieldInfosFormat} that this codec is intended to use.
152+
*
153+
* @return the original {@link FieldInfosFormat} used by this codec
154+
*/
117155
protected abstract FieldInfosFormat originalFieldInfosFormat();
118156

119157
// mark all fields as no term vectors, no norms, no payloads, and no vectors.
@@ -170,8 +208,12 @@ public static SegmentInfo wrap(SegmentInfo segmentInfo) {
170208
return segmentInfo1;
171209
}
172210

173-
// Special handling for Lucene8xCodecs (which are currently bundled with Lucene)
174-
// Use BWCLucene8xCodec instead as that one extends BWCCodec (similar to all other older codecs)
211+
/**
212+
* Returns a backward-compatible codec for the given codec. If the codec is one of the known Lucene 8.x codecs,
213+
* it returns a corresponding read-only backward-compatible codec. Otherwise, it returns the original codec.
214+
* This switch if for indices created in ES 6.x, that may have some of their segments written with ES 7.x, hence Lucene 8.x.
215+
*/
216+
@UpdateForV10(owner = UpdateForV10.Owner.SEARCH_FOUNDATIONS)
175217
private static Codec getBackwardCompatibleCodec(Codec codec) {
176218
if (codec == null) return null;
177219

x-pack/plugin/old-lucene-versions/src/main/java/org/elasticsearch/xpack/lucene/bwc/codecs/lucene60/MetadataOnlyBKDReader.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ public class MetadataOnlyBKDReader extends PointValues {
4646
final long pointCount;
4747
final int docCount;
4848
final int version;
49+
final long minLeafBlockFP;
4950

5051
final int numIndexBytes;
51-
final long minLeafBlockFP;
52-
private final long indexStartPointer;
5352

5453
public MetadataOnlyBKDReader(IndexInput metaIn) throws IOException {
5554
version = CodecUtil.checkHeader(metaIn, "BKD", VERSION_START, VERSION_CURRENT);
@@ -93,7 +92,8 @@ public MetadataOnlyBKDReader(IndexInput metaIn) throws IOException {
9392
// This code has been introduced to process IndexInput created with Lucene86Codec+
9493
numIndexBytes = metaIn.readVInt();
9594
minLeafBlockFP = metaIn.readLong();
96-
indexStartPointer = metaIn.readLong();
95+
// The following fields are not used in this class, but we need to read them to advance the pointer
96+
long indexStartPointer = metaIn.readLong();
9797
}
9898

9999
@Override

x-pack/plugin/old-lucene-versions/src/main/java/org/elasticsearch/xpack/lucene/bwc/codecs/lucene80/BWCLucene80Codec.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.xpack.lucene.bwc.codecs.lucene60.Lucene60MetadataOnlyPointsFormat;
2727

2828
/**
29+
* This is a fork of {@link org.apache.lucene.backward_codecs.lucene80.Lucene80Codec}
2930
* Implements the Lucene 8.0 index format. Loaded via SPI for indices created/written with Lucene 8.0.0-8.3.0
3031
* (Elasticsearch [7.0.0-7.5.2]), mounted as archive indices in Elasticsearch 8.x / 9.x.
3132
*/
@@ -48,11 +49,7 @@ public DocValuesFormat getDocValuesFormatForField(String field) {
4849
// Needed for SPI loading
4950
@SuppressWarnings("unused")
5051
public BWCLucene80Codec() {
51-
this("BWCLucene80Codec");
52-
}
53-
54-
public BWCLucene80Codec(String name) {
55-
super(name);
52+
super("BWCLucene80Codec");
5653
this.storedFieldsFormat = new Lucene50StoredFieldsFormat(Lucene50StoredFieldsFormat.Mode.BEST_SPEED);
5754
}
5855

x-pack/plugin/old-lucene-versions/src/main/java/org/elasticsearch/xpack/lucene/bwc/codecs/lucene84/BWCLucene84Codec.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.xpack.lucene.bwc.codecs.lucene60.Lucene60MetadataOnlyPointsFormat;
2929

3030
/**
31+
* This is a fork of {@link org.apache.lucene.backward_codecs.lucene84.Lucene84Codec}
3132
* Implements the Lucene 8.4 index format. Loaded via SPI for indices created/written with Lucene 8.4.0-8.5.1
3233
* (Elasticsearch [7.6.0-7.8.1]), mounted as archive indices in Elasticsearch 8.x / 9.x.
3334
*/
@@ -58,11 +59,7 @@ public DocValuesFormat getDocValuesFormatForField(String field) {
5859
// Needed for SPI loading
5960
@SuppressWarnings("unused")
6061
public BWCLucene84Codec() {
61-
this("BWCLucene84Codec");
62-
}
63-
64-
public BWCLucene84Codec(String name) {
65-
super(name);
62+
super("BWCLucene84Codec");
6663
this.storedFieldsFormat = new Lucene50StoredFieldsFormat(Lucene50StoredFieldsFormat.Mode.BEST_SPEED);
6764
this.defaultFormat = new Lucene84PostingsFormat();
6865
this.defaultDVFormat = new Lucene80DocValuesFormat();

x-pack/plugin/old-lucene-versions/src/main/java/org/elasticsearch/xpack/lucene/bwc/codecs/lucene86/BWCLucene86Codec.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.apache.lucene.backward_codecs.lucene50.Lucene50StoredFieldsFormat;
1313
import org.apache.lucene.backward_codecs.lucene60.Lucene60FieldInfosFormat;
1414
import org.apache.lucene.backward_codecs.lucene80.Lucene80DocValuesFormat;
15-
import org.apache.lucene.backward_codecs.lucene84.Lucene84PostingsFormat;
1615
import org.apache.lucene.backward_codecs.lucene86.Lucene86SegmentInfoFormat;
1716
import org.apache.lucene.codecs.CompoundFormat;
1817
import org.apache.lucene.codecs.DocValuesFormat;
@@ -23,10 +22,10 @@
2322
import org.apache.lucene.codecs.SegmentInfoFormat;
2423
import org.apache.lucene.codecs.StoredFieldsFormat;
2524
import org.apache.lucene.codecs.perfield.PerFieldDocValuesFormat;
26-
import org.apache.lucene.codecs.perfield.PerFieldPostingsFormat;
2725
import org.elasticsearch.xpack.lucene.bwc.codecs.BWCCodec;
2826

2927
/**
28+
* This is a fork of {@link org.apache.lucene.backward_codecs.lucene86.Lucene86Codec}
3029
* Implements the Lucene 8.6 index format. Loaded via SPI for indices created/written with Lucene 8.6.0-8.6.2
3130
* (Elasticsearch [7.9.0-7.9.3]), mounted as archive indices in Elasticsearch 8.x / 9.x.
3231
*/
@@ -35,16 +34,8 @@ public class BWCLucene86Codec extends BWCCodec {
3534
private final LiveDocsFormat liveDocsFormat = new Lucene50LiveDocsFormat();
3635
private final CompoundFormat compoundFormat = new Lucene50CompoundFormat();
3736
private final PointsFormat pointsFormat = new Lucene86MetadataOnlyPointsFormat();
38-
private final PostingsFormat defaultFormat;
3937
private final DocValuesFormat defaultDVFormat;
4038

41-
private final PostingsFormat postingsFormat = new PerFieldPostingsFormat() {
42-
@Override
43-
public PostingsFormat getPostingsFormatForField(String field) {
44-
return defaultFormat;
45-
}
46-
};
47-
4839
private final DocValuesFormat docValuesFormat = new PerFieldDocValuesFormat() {
4940
@Override
5041
public DocValuesFormat getDocValuesFormatForField(String field) {
@@ -57,14 +48,8 @@ public DocValuesFormat getDocValuesFormatForField(String field) {
5748
// Needed for SPI loading
5849
@SuppressWarnings("unused")
5950
public BWCLucene86Codec() {
60-
this("BWCLucene86Codec");
61-
}
62-
63-
/** Instantiates a new codec. */
64-
public BWCLucene86Codec(String name) {
65-
super(name);
51+
super("BWCLucene86Codec");
6652
this.storedFieldsFormat = new Lucene50StoredFieldsFormat(Lucene50StoredFieldsFormat.Mode.BEST_SPEED);
67-
this.defaultFormat = new Lucene84PostingsFormat();
6853
this.defaultDVFormat = new Lucene80DocValuesFormat();
6954
}
7055

x-pack/plugin/old-lucene-versions/src/main/java/org/elasticsearch/xpack/lucene/bwc/codecs/lucene87/BWCLucene87Codec.java

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,13 @@
2727
import org.elasticsearch.xpack.lucene.bwc.codecs.BWCCodec;
2828
import org.elasticsearch.xpack.lucene.bwc.codecs.lucene86.Lucene86MetadataOnlyPointsFormat;
2929

30-
import java.util.Objects;
31-
3230
/**
31+
* This is a fork of {@link org.apache.lucene.backward_codecs.lucene87.Lucene87Codec}
3332
* Implements the Lucene 8.7 index format. Loaded via SPI for indices created/written with Lucene 8.7.0-8.11.3
3433
* (Elasticsearch [7.10.0-7-17.26]), mounted as archive indices in Elasticsearch 8.x / 9.x.
3534
*/
3635
public class BWCLucene87Codec extends BWCCodec {
3736

38-
private enum Mode {
39-
/** Trade compression ratio for retrieval speed. */
40-
BEST_SPEED(Lucene87StoredFieldsFormat.Mode.BEST_SPEED, Lucene80DocValuesFormat.Mode.BEST_SPEED),
41-
/** Trade retrieval speed for compression ratio. */
42-
BEST_COMPRESSION(Lucene87StoredFieldsFormat.Mode.BEST_COMPRESSION, Lucene80DocValuesFormat.Mode.BEST_COMPRESSION);
43-
44-
/** compression mode for stored fields */
45-
private final Lucene87StoredFieldsFormat.Mode storedMode;
46-
47-
/** compression mode for doc value fields */
48-
private final Lucene80DocValuesFormat.Mode dvMode;
49-
50-
Mode(Lucene87StoredFieldsFormat.Mode storedMode, Lucene80DocValuesFormat.Mode dvMode) {
51-
this.storedMode = Objects.requireNonNull(storedMode);
52-
this.dvMode = Objects.requireNonNull(dvMode);
53-
}
54-
}
55-
5637
private final LiveDocsFormat liveDocsFormat = new Lucene50LiveDocsFormat();
5738
private final CompoundFormat compoundFormat = new Lucene50CompoundFormat();
5839
private final PointsFormat pointsFormat = new Lucene86MetadataOnlyPointsFormat();
@@ -78,14 +59,10 @@ public DocValuesFormat getDocValuesFormatForField(String field) {
7859
// Needed for SPI loading
7960
@SuppressWarnings("unused")
8061
public BWCLucene87Codec() {
81-
this("BWCLucene87Codec", Mode.BEST_COMPRESSION);
82-
}
83-
84-
public BWCLucene87Codec(String name, Mode mode) {
85-
super(name);
86-
this.storedFieldsFormat = new Lucene87StoredFieldsFormat(mode.storedMode);
62+
super("BWCLucene87Codec");
63+
this.storedFieldsFormat = new Lucene87StoredFieldsFormat(Lucene87StoredFieldsFormat.Mode.BEST_COMPRESSION);
8764
this.defaultFormat = new Lucene84PostingsFormat();
88-
this.defaultDVFormat = new Lucene80DocValuesFormat(mode.dvMode);
65+
this.defaultDVFormat = new Lucene80DocValuesFormat(Lucene80DocValuesFormat.Mode.BEST_COMPRESSION);
8966
}
9067

9168
@Override

x-pack/plugin/old-lucene-versions/src/test/java/org/elasticsearch/xpack/lucene/bwc/codecs/BWCCodecTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import org.apache.lucene.codecs.StoredFieldsFormat;
1919
import org.elasticsearch.test.ESTestCase;
2020

21+
/**
22+
* Unit tests for the {@link BWCCodec} class.
23+
*/
2124
public class BWCCodecTests extends ESTestCase {
2225

2326
private final Codec codec;
@@ -66,14 +69,23 @@ public PointsFormat pointsFormat() {
6669
};
6770
}
6871

72+
/**
73+
* Tests that the {@link Codec#normsFormat()} method throws an {@link UnsupportedOperationException}.
74+
*/
6975
public void testNormsFormatUnsupportedOperation() {
7076
assertThrows(UnsupportedOperationException.class, codec::normsFormat);
7177
}
7278

79+
/**
80+
* Tests that the {@link Codec#termVectorsFormat()} method throws an {@link UnsupportedOperationException}.
81+
*/
7382
public void testTermVectorsFormatUnsupportedOperation() {
7483
assertThrows(UnsupportedOperationException.class, codec::termVectorsFormat);
7584
}
7685

86+
/**
87+
* Tests that the {@link Codec#knnVectorsFormat()} method throws an {@link UnsupportedOperationException}.
88+
*/
7789
public void testKnnVectorsFormatUnsupportedOperation() {
7890
assertThrows(UnsupportedOperationException.class, codec::knnVectorsFormat);
7991
}

x-pack/plugin/old-lucene-versions/src/test/java/org/elasticsearch/xpack/lucene/bwc/codecs/OldCodecsAvailableTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class OldCodecsAvailableTests extends ESTestCase {
2828
*/
2929
@UpdateForV10(owner = UpdateForV10.Owner.SEARCH_FOUNDATIONS)
3030
public void testLuceneBWCCodecsAvailable() {
31-
assertEquals("Add Lucene BWC codecs for Elasticsearch version 9", 9, Version.CURRENT.major);
31+
assertEquals("Add Lucene BWC codecs for Elasticsearch version 8", 9, Version.CURRENT.major);
3232

3333
String codecPathRegex = ".*[\\\\.](Lucene(8[0-9])Codec)";
3434
Pattern codecPathPattern = Pattern.compile(codecPathRegex);

0 commit comments

Comments
 (0)