Skip to content

Apache Lucene open-source search software

Remove incorrect/expensive use of ServiceLoader for choosing random format (#13428)

Uwe Schindlerb1189adeb38d
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/KnnVectorsFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/KnnVectorsFormat.java
index ca8ebd5b475..ad6e4aba607 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/KnnVectorsFormat.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/KnnVectorsFormat.java
@@ -18,6 +18,7 @@
package org.apache.lucene.codecs;
import java.io.IOException;
+import java.util.Set;
import org.apache.lucene.index.ByteVectorValues;
import org.apache.lucene.index.FloatVectorValues;
import org.apache.lucene.index.SegmentReadState;
@@ -86,6 +87,11 @@ public abstract class KnnVectorsFormat implements NamedSPILoader.NamedSPI {
return Holder.getLoader().lookup(name);
}
+ /** returns a list of all available format names */
+ public static Set<String> availableKnnVectorsFormats() {
+ return Holder.getLoader().availableServices();
+ }
+
/** Returns a {@link KnnVectorsWriter} to write the vectors to the index. */
public abstract KnnVectorsWriter fieldsWriter(SegmentWriteState state) throws IOException;
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java
index 594a19cc66a..f4795771c9b 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java
@@ -89,7 +89,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
-import java.util.ServiceLoader;
import java.util.Set;
import java.util.TimeZone;
import java.util.TreeSet;
@@ -3215,18 +3214,21 @@ public abstract class LuceneTestCase extends Assert {
return it;
}
- protected KnnVectorsFormat randomVectorFormat(VectorEncoding vectorEncoding) {
- ServiceLoader<KnnVectorsFormat> formats = java.util.ServiceLoader.load(KnnVectorsFormat.class);
- List<KnnVectorsFormat> availableFormats = new ArrayList<>();
- for (KnnVectorsFormat f : formats) {
- if (f.getName().equals(HnswBitVectorsFormat.NAME)) {
- if (vectorEncoding.equals(VectorEncoding.BYTE)) {
- availableFormats.add(f);
- }
- } else {
- availableFormats.add(f);
- }
+ private static boolean supportsVectorEncoding(
+ KnnVectorsFormat format, VectorEncoding vectorEncoding) {
+ if (format instanceof HnswBitVectorsFormat) {
+ // special case, this only supports BYTE
+ return vectorEncoding == VectorEncoding.BYTE;
}
+ return true;
+ }
+
+ protected static KnnVectorsFormat randomVectorFormat(VectorEncoding vectorEncoding) {
+ List<KnnVectorsFormat> availableFormats =
+ KnnVectorsFormat.availableKnnVectorsFormats().stream()
+ .map(KnnVectorsFormat::forName)
+ .filter(format -> supportsVectorEncoding(format, vectorEncoding))
+ .toList();
return RandomPicks.randomFrom(random(), availableFormats);
}
}