Seal `IndexReader` and `IndexReaderContext` (#12296)

This commit is contained in:
Petr Portnov | PROgrm_JARvis 2023-05-17 09:47:47 +03:00 committed by GitHub
parent f53eb28af0
commit 0c6e8aec67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 15 deletions

View File

@ -47,6 +47,10 @@ API Changes
* GITHUB#12107: Remove deprecated KnnVectorField, KnnVectorQuery, VectorValues and
LeafReader#getVectorValues. (Luca Cavanna)
* GITHUB#12296: Make IndexReader and IndexReaderContext classes explicitly sealed.
They have already been runtime-checked to only be implemented by the specific classes
so this is effectively a non-breaking change.
New Features
---------------------

View File

@ -44,7 +44,7 @@ import org.apache.lucene.store.Directory;
* synchronization, you should <b>not</b> synchronize on the <code>IndexReader</code> instance; use
* your own (non-Lucene) objects instead.
*/
public abstract class CompositeReader extends IndexReader {
public abstract non-sealed class CompositeReader extends IndexReader {
private volatile CompositeReaderContext readerContext = null; // lazy init

View File

@ -63,17 +63,13 @@ import org.apache.lucene.store.AlreadyClosedException;
* synchronization, you should <b>not</b> synchronize on the <code>IndexReader</code> instance; use
* your own (non-Lucene) objects instead.
*/
public abstract class IndexReader implements Closeable {
public abstract sealed class IndexReader implements Closeable permits CompositeReader, LeafReader {
private boolean closed = false;
private boolean closedByChild = false;
private final AtomicInteger refCount = new AtomicInteger(1);
IndexReader() {
if (!(this instanceof CompositeReader || this instanceof LeafReader))
throw new Error(
"IndexReader should never be directly extended, subclass LeafReader or CompositeReader instead.");
}
IndexReader() {}
/**
* A utility class that gives hooks in order to help build a cache based on the data that is

View File

@ -22,17 +22,17 @@ import java.util.List;
* A struct like class that represents a hierarchical relationship between {@link IndexReader}
* instances.
*/
public abstract class IndexReaderContext {
public abstract sealed class IndexReaderContext permits CompositeReaderContext, LeafReaderContext {
/** The reader context for this reader's immediate parent, or null if none */
public final CompositeReaderContext parent;
/**
* <code>true</code> if this context struct represents the top level reader within the
* hierarchical context
* {@code true} if this context struct represents the top level reader within the hierarchical
* context
*/
public final boolean isTopLevel;
/** the doc base for this reader in the parent, <code>0</code> if parent is null */
/** the doc base for this reader in the parent, {@code 0} if parent is null */
public final int docBaseInParent;
/** the ord for this reader in the parent, <code>0</code> if parent is null */
/** the ord for this reader in the parent, {@code 0} if parent is null */
public final int ordInParent;
// An object that uniquely identifies this context without referencing
@ -41,8 +41,6 @@ public abstract class IndexReaderContext {
final Object identity = new Object();
IndexReaderContext(CompositeReaderContext parent, int ordInParent, int docBaseInParent) {
if (!(this instanceof CompositeReaderContext || this instanceof LeafReaderContext))
throw new Error("This class should never be extended by custom code!");
this.parent = parent;
this.docBaseInParent = docBaseInParent;
this.ordInParent = ordInParent;

View File

@ -41,7 +41,7 @@ import org.apache.lucene.util.Bits;
* synchronization, you should <b>not</b> synchronize on the <code>IndexReader</code> instance; use
* your own (non-Lucene) objects instead.
*/
public abstract class LeafReader extends IndexReader {
public abstract non-sealed class LeafReader extends IndexReader {
private final LeafReaderContext readerContext = new LeafReaderContext(this);