Skip to content

Apache Lucene open-source search software

Add `@SuppressAssertingFormats` annotation for fine-grained control over `AssertingCodec` formats (#15468)

Prudhvi Godithi31366b0b4381
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 6dda8cfa2e3..dce771a04d6 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -191,6 +191,8 @@ New Features
* GITHUB#15415: Add fallback support to Lucene104ScalarQuantizedVectorsFormat getFloatVectorValues when there are
no full-precision vectors present (Pulkit Gupta)
+ * GITHUB#15468: Add support for `@SuppressAssertingFormats` annotation for fine-grained control over `AssertingCodec` formats (Prudhvi Godithi)
+
Improvements
---------------------
* GITHUB#15148: Add support uint8 distance and allow 8 bit scalar quantization (Trevor McCulloch)
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingCodec.java b/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingCodec.java
index 0aded31855e..7ff8d9ab4a9 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingCodec.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingCodec.java
@@ -16,6 +16,9 @@
*/
package org.apache.lucene.tests.codecs.asserting;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.Set;
import org.apache.lucene.codecs.DocValuesFormat;
import org.apache.lucene.codecs.FilterCodec;
import org.apache.lucene.codecs.KnnVectorsFormat;
@@ -33,6 +36,28 @@ import org.apache.lucene.tests.util.TestUtil;
/** Acts like the default codec but with additional asserts. */
public class AssertingCodec extends FilterCodec {
+ /** Enum representing asserting format types that can be suppressed. */
+ public enum Format {
+ STORED_FIELDS,
+ TERM_VECTORS,
+ NORMS,
+ LIVE_DOCS,
+ POINTS,
+ KNN_VECTORS
+ }
+
+ private static volatile Set<Format> suppressedFormats = Collections.emptySet();
+
+ /** Set the formats to suppress. */
+ public static void setSuppressedFormats(Set<Format> formats) {
+ suppressedFormats =
+ formats == null || formats.isEmpty() ? Collections.emptySet() : EnumSet.copyOf(formats);
+ }
+
+ private static boolean isSuppressed(Format format) {
+ return suppressedFormats.contains(format);
+ }
+
static void assertThread(String object, Thread creationThread) {
if (creationThread != Thread.currentThread()) {
throw new AssertionError(
@@ -90,12 +115,12 @@ public class AssertingCodec extends FilterCodec {
@Override
public TermVectorsFormat termVectorsFormat() {
- return vectors;
+ return isSuppressed(Format.TERM_VECTORS) ? delegate.termVectorsFormat() : vectors;
}
@Override
public StoredFieldsFormat storedFieldsFormat() {
- return storedFields;
+ return isSuppressed(Format.STORED_FIELDS) ? delegate.storedFieldsFormat() : storedFields;
}
@Override
@@ -105,22 +130,22 @@ public class AssertingCodec extends FilterCodec {
@Override
public NormsFormat normsFormat() {
- return norms;
+ return isSuppressed(Format.NORMS) ? delegate.normsFormat() : norms;
}
@Override
public LiveDocsFormat liveDocsFormat() {
- return liveDocs;
+ return isSuppressed(Format.LIVE_DOCS) ? delegate.liveDocsFormat() : liveDocs;
}
@Override
public PointsFormat pointsFormat() {
- return pointsFormat;
+ return isSuppressed(Format.POINTS) ? delegate.pointsFormat() : pointsFormat;
}
@Override
public KnnVectorsFormat knnVectorsFormat() {
- return knnVectorsFormat;
+ return isSuppressed(Format.KNN_VECTORS) ? delegate.knnVectorsFormat() : knnVectorsFormat;
}
@Override
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 6c3ab79687e..f754ec67621 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
@@ -171,6 +171,7 @@ import org.apache.lucene.store.MergeInfo;
import org.apache.lucene.store.NRTCachingDirectory;
import org.apache.lucene.store.ReadOnceHint;
import org.apache.lucene.tests.analysis.MockAnalyzer;
+import org.apache.lucene.tests.codecs.asserting.AssertingCodec;
import org.apache.lucene.tests.index.AlcoholicMergePolicy;
import org.apache.lucene.tests.index.AssertingDirectoryReader;
import org.apache.lucene.tests.index.AssertingLeafReader;
@@ -350,6 +351,18 @@ public abstract class LuceneTestCase extends Assert {
String[] value();
}
+ /**
+ * Annotation for test classes that should avoid specific asserting formats within the Asserting
+ * codec (e.g., AssertingStoredFieldsFormat) while keeping other asserting formats active.
+ */
+ @Documented
+ @Inherited
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target(ElementType.TYPE)
+ public @interface SuppressAssertingFormats {
+ AssertingCodec.Format[] value();
+ }
+
/**
* Annotation for test classes that should avoid mock filesystem types (because they test a bug
* that only happens on linux, for example).
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java b/lucene/test-framework/src/java/org/apache/lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java
index e4c0625e7c5..f7b4fdc4819 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java
@@ -31,6 +31,7 @@ import com.carrotsearch.randomizedtesting.RandomizedContext;
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
import java.io.PrintStream;
import java.util.Arrays;
+import java.util.EnumSet;
import java.util.HashSet;
import java.util.Locale;
import java.util.Random;
@@ -51,6 +52,7 @@ import org.apache.lucene.tests.index.RandomCodec;
import org.apache.lucene.tests.search.similarities.AssertingSimilarity;
import org.apache.lucene.tests.search.similarities.RandomSimilarity;
import org.apache.lucene.tests.util.LuceneTestCase.LiveIWCFlushMode;
+import org.apache.lucene.tests.util.LuceneTestCase.SuppressAssertingFormats;
import org.apache.lucene.tests.util.LuceneTestCase.SuppressCodecs;
import org.apache.lucene.util.InfoStream;
import org.apache.lucene.util.PrintStreamInfoStream;
@@ -76,6 +78,11 @@ final class TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule {
*/
HashSet<String> avoidCodecs;
+ /**
+ * @see SuppressAssertingFormats
+ */
+ EnumSet<AssertingCodec.Format> avoidAssertingFormats;
+
static class ThreadNameFixingPrintStreamInfoStream extends PrintStreamInfoStream {
public ThreadNameFixingPrintStreamInfoStream(PrintStream out) {
super(out);
@@ -127,6 +134,13 @@ final class TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule {
avoidCodecs.addAll(Arrays.asList(a.value()));
}
+ avoidAssertingFormats = EnumSet.noneOf(AssertingCodec.Format.class);
+ if (targetClass.isAnnotationPresent(SuppressAssertingFormats.class)) {
+ SuppressAssertingFormats a = targetClass.getAnnotation(SuppressAssertingFormats.class);
+ avoidAssertingFormats.addAll(Arrays.asList(a.value()));
+ }
+ AssertingCodec.setSuppressedFormats(avoidAssertingFormats);
+
savedCodec = Codec.getDefault();
int randomVal = random.nextInt(11);
if ("default".equals(TEST_CODEC)) {