Skip to content

Apache Lucene open-source search software

Support `@SuppressAssertingFormats` (#15490)

Prudhvi Godithib7c213c77900
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 4a82ad0c9b9..10f891391e2 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -193,6 +193,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..c81e831a721 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,10 @@
*/
package org.apache.lucene.tests.codecs.asserting;
+import com.carrotsearch.randomizedtesting.RandomizedContext;
+import java.util.Arrays;
+import java.util.EnumSet;
+import java.util.stream.Collectors;
import org.apache.lucene.codecs.DocValuesFormat;
import org.apache.lucene.codecs.FilterCodec;
import org.apache.lucene.codecs.KnnVectorsFormat;
@@ -28,11 +32,28 @@ import org.apache.lucene.codecs.TermVectorsFormat;
import org.apache.lucene.codecs.perfield.PerFieldDocValuesFormat;
import org.apache.lucene.codecs.perfield.PerFieldKnnVectorsFormat;
import org.apache.lucene.codecs.perfield.PerFieldPostingsFormat;
+import org.apache.lucene.tests.util.LuceneTestCase;
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 final EnumSet<Format> suppressedFormats;
+
+ private boolean isSuppressed(Format format) {
+ return suppressedFormats.contains(format);
+ }
+
static void assertThread(String object, Thread creationThread) {
if (creationThread != Thread.currentThread()) {
throw new AssertionError(
@@ -81,6 +102,24 @@ public class AssertingCodec extends FilterCodec {
public AssertingCodec() {
super("Asserting", TestUtil.getDefaultCodec());
+
+ var suppressedFormats = EnumSet.noneOf(AssertingCodec.Format.class);
+ Class<?> targetClass;
+ try {
+ targetClass = RandomizedContext.current().getTargetClass();
+ } catch (IllegalStateException _) {
+ // Not under any randomized context.
+ targetClass = null;
+ }
+
+ if (targetClass != null
+ && targetClass.isAnnotationPresent(LuceneTestCase.SuppressAssertingFormats.class)) {
+ suppressedFormats.addAll(
+ Arrays.asList(
+ targetClass.getAnnotation(LuceneTestCase.SuppressAssertingFormats.class).value()));
+ }
+
+ this.suppressedFormats = suppressedFormats;
}
@Override
@@ -90,12 +129,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,27 +144,33 @@ 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
public String toString() {
- return "Asserting(" + delegate + ")";
+ return "Asserting("
+ + delegate
+ + (suppressedFormats.isEmpty()
+ ? ""
+ : ", suppressed: "
+ + suppressedFormats.stream().map(Enum::toString).collect(Collectors.joining(", ")))
+ + ")";
}
/**
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..8d1a2b9aa20 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;
@@ -365,6 +366,20 @@ public abstract class LuceneTestCase extends Assert {
String[] value();
}
+ /**
+ * Annotation for test classes that should avoid specific asserting formats within the {@link
+ * AssertingCodec} while keeping other asserting formats active.
+ *
+ * @see org.apache.lucene.tests.codecs.asserting.AssertingCodec.Format
+ */
+ @Documented
+ @Inherited
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target(ElementType.TYPE)
+ public @interface SuppressAssertingFormats {
+ AssertingCodec.Format[] value();
+ }
+
/**
* Annotation for test classes that should avoid always omit actual fsync calls from reaching the
* filesystem.
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..f5ab9b3a173 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
@@ -166,7 +166,7 @@ final class TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule {
@Override
public String toString() {
- return super.toString() + ": " + format.toString() + ", " + dvFormat.toString();
+ return super.toString() + ": " + format + ", " + dvFormat;
}
};
} else if ("SimpleText".equals(TEST_CODEC)