Skip to content

Commit

Permalink
Seal IndexReader and IndexReaderContext (apache#12296)
Browse files Browse the repository at this point in the history
  • Loading branch information
JarvisCraft authored May 17, 2023
1 parent f53eb28 commit 0c6e8ae
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 15 deletions.
4 changes: 4 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* 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

Expand Down
8 changes: 2 additions & 6 deletions lucene/core/src/java/org/apache/lucene/index/IndexReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,13 @@
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@
* 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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* 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);

Expand Down

0 comments on commit 0c6e8ae

Please sign in to comment.