You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by us...@apache.org on 2023/05/17 06:47:56 UTC

[lucene] branch main updated: Seal `IndexReader` and `IndexReaderContext` (#12296)

This is an automated email from the ASF dual-hosted git repository.

uschindler pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/main by this push:
     new 0c6e8aec67c Seal `IndexReader` and `IndexReaderContext` (#12296)
0c6e8aec67c is described below

commit 0c6e8aec67c375f9a2f934d28ac3f4ceb5f0ebb9
Author: Petr Portnov | PROgrm_JARvis <mr...@gmail.com>
AuthorDate: Wed May 17 09:47:47 2023 +0300

    Seal `IndexReader` and `IndexReaderContext` (#12296)
---
 lucene/CHANGES.txt                                           |  4 ++++
 .../src/java/org/apache/lucene/index/CompositeReader.java    |  2 +-
 .../core/src/java/org/apache/lucene/index/IndexReader.java   |  8 ++------
 .../src/java/org/apache/lucene/index/IndexReaderContext.java | 12 +++++-------
 lucene/core/src/java/org/apache/lucene/index/LeafReader.java |  2 +-
 5 files changed, 13 insertions(+), 15 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 308e28f27cd..57c255853db 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -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
 ---------------------
 
diff --git a/lucene/core/src/java/org/apache/lucene/index/CompositeReader.java b/lucene/core/src/java/org/apache/lucene/index/CompositeReader.java
index ccc454c5994..4b35ba6cd7c 100644
--- a/lucene/core/src/java/org/apache/lucene/index/CompositeReader.java
+++ b/lucene/core/src/java/org/apache/lucene/index/CompositeReader.java
@@ -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
 
diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexReader.java b/lucene/core/src/java/org/apache/lucene/index/IndexReader.java
index 2a3c6dbead2..0ed2d1e71b6 100644
--- a/lucene/core/src/java/org/apache/lucene/index/IndexReader.java
+++ b/lucene/core/src/java/org/apache/lucene/index/IndexReader.java
@@ -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
diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexReaderContext.java b/lucene/core/src/java/org/apache/lucene/index/IndexReaderContext.java
index fbc59e3d1ca..a23d5015ff2 100644
--- a/lucene/core/src/java/org/apache/lucene/index/IndexReaderContext.java
+++ b/lucene/core/src/java/org/apache/lucene/index/IndexReaderContext.java
@@ -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;
diff --git a/lucene/core/src/java/org/apache/lucene/index/LeafReader.java b/lucene/core/src/java/org/apache/lucene/index/LeafReader.java
index 767874cc87d..b339a7b1f4b 100644
--- a/lucene/core/src/java/org/apache/lucene/index/LeafReader.java
+++ b/lucene/core/src/java/org/apache/lucene/index/LeafReader.java
@@ -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);