Skip to content

Apache Lucene open-source search software

Use the existing IO* functional interfaces in various places rather than defining new types (#14973)

Simon Cooper47f3387e67d1
diff --git a/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java b/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java
index 49c6ef2f137..7e98e51bf69 100644
--- a/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java
+++ b/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java
@@ -87,6 +87,7 @@ import org.apache.lucene.util.BytesRefBuilder;
import org.apache.lucene.util.CommandLineUtil;
import org.apache.lucene.util.FixedBitSet;
import org.apache.lucene.util.IOBooleanSupplier;
+import org.apache.lucene.util.IOFunction;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LongBitSet;
import org.apache.lucene.util.NamedThreadFactory;
@@ -3563,11 +3564,6 @@ public final class CheckIndex implements Closeable {
return status;
}
- @FunctionalInterface
- private interface DocValuesIteratorSupplier {
- DocValuesIterator get(FieldInfo fi) throws IOException;
- }
-
private static void checkDocValueSkipper(FieldInfo fi, DocValuesSkipper skipper)
throws IOException {
String fieldName = fi.name;
@@ -3656,13 +3652,13 @@ public final class CheckIndex implements Closeable {
}
}
- private static void checkDVIterator(FieldInfo fi, DocValuesIteratorSupplier producer)
- throws IOException {
+ private static void checkDVIterator(
+ FieldInfo fi, IOFunction<FieldInfo, DocValuesIterator> producer) throws IOException {
String field = fi.name;
// Check advance
- DocValuesIterator it1 = producer.get(fi);
- DocValuesIterator it2 = producer.get(fi);
+ DocValuesIterator it1 = producer.apply(fi);
+ DocValuesIterator it2 = producer.apply(fi);
int i = 0;
for (int doc = it1.nextDoc(); ; doc = it1.nextDoc()) {
@@ -3709,8 +3705,8 @@ public final class CheckIndex implements Closeable {
}
// Check advanceExact
- it1 = producer.get(fi);
- it2 = producer.get(fi);
+ it1 = producer.apply(fi);
+ it2 = producer.apply(fi);
i = 0;
int lastDoc = -1;
for (int doc = it1.nextDoc(); doc != NO_MORE_DOCS; doc = it1.nextDoc()) {
diff --git a/lucene/core/src/java/org/apache/lucene/index/FrozenBufferedUpdates.java b/lucene/core/src/java/org/apache/lucene/index/FrozenBufferedUpdates.java
index d8b143c45f7..f5ff86d8981 100644
--- a/lucene/core/src/java/org/apache/lucene/index/FrozenBufferedUpdates.java
+++ b/lucene/core/src/java/org/apache/lucene/index/FrozenBufferedUpdates.java
@@ -33,6 +33,7 @@ import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Weight;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.IOFunction;
import org.apache.lucene.util.InfoStream;
import org.apache.lucene.util.RamUsageEstimator;
@@ -540,7 +541,7 @@ final class FrozenBufferedUpdates {
* passed in sorted order and makes sure terms and postings are reused as much as possible.
*/
static final class TermDocsIterator {
- private final TermsProvider provider;
+ private final IOFunction<String, Terms> provider;
private String field;
private TermsEnum termsEnum;
private PostingsEnum postingsEnum;
@@ -548,11 +549,6 @@ final class FrozenBufferedUpdates {
private BytesRef readerTerm;
private BytesRef lastTerm; // only set with asserts
- @FunctionalInterface
- interface TermsProvider {
- Terms terms(String field) throws IOException;
- }
-
TermDocsIterator(Fields fields, boolean sortedTerms) {
this(fields::terms, sortedTerms);
}
@@ -561,7 +557,7 @@ final class FrozenBufferedUpdates {
this(reader::terms, sortedTerms);
}
- private TermDocsIterator(TermsProvider provider, boolean sortedTerms) {
+ private TermDocsIterator(IOFunction<String, Terms> provider, boolean sortedTerms) {
this.sortedTerms = sortedTerms;
this.provider = provider;
}
@@ -570,7 +566,7 @@ final class FrozenBufferedUpdates {
if (this.field == null || this.field.equals(field) == false) {
this.field = field;
- Terms terms = provider.terms(field);
+ Terms terms = provider.apply(field);
if (terms != null) {
termsEnum = terms.iterator();
if (sortedTerms) {
diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
index 0d6f901bc77..94f4253c679 100644
--- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
@@ -290,13 +290,19 @@ public class IndexWriter
private final ReentrantLock writeDocValuesLock = new ReentrantLock();
+ /**
+ * Queue for internal atomic events. See {@link DocumentsWriter} for details. Events are executed
+ * concurrently and no order is guaranteed. Each event should only rely on the serializability
+ * within its process method. All actions that must happen before or after a certain action must
+ * be encoded inside the {@link IOConsumer#accept} implementation.
+ */
static final class EventQueue implements Closeable {
private volatile boolean closed;
// we use a semaphore here instead of simply synced methods to allow
// events to be processed concurrently by multiple threads such that all events
// for a certain thread are processed once the thread returns from IW
private final Semaphore permits = new Semaphore(Integer.MAX_VALUE);
- private final Queue<Event> queue = new ConcurrentLinkedQueue<>();
+ private final Queue<IOConsumer<IndexWriter>> queue = new ConcurrentLinkedQueue<>();
private final IndexWriter writer;
EventQueue(IndexWriter writer) {
@@ -313,7 +319,7 @@ public class IndexWriter
}
}
- boolean add(Event event) {
+ boolean add(IOConsumer<IndexWriter> event) {
acquire();
try {
return queue.add(event);
@@ -334,9 +340,9 @@ public class IndexWriter
private void processEventsInternal() throws IOException {
assert Integer.MAX_VALUE - permits.availablePermits() > 0
: "must acquire a permit before processing events";
- Event event;
+ IOConsumer<IndexWriter> event;
while ((event = queue.poll()) != null) {
- event.process(writer);
+ event.accept(writer);
}
}
@@ -5424,7 +5430,8 @@ public class IndexWriter
}
success = true;
-
+ } catch (Throwable t) {
+ throw t;
} finally {
// Readers are already closed in commitMerge if we didn't hit
// an exc:
@@ -5982,24 +5989,6 @@ public class IndexWriter
}
}
- /**
- * Interface for internal atomic events. See {@link DocumentsWriter} for details. Events are
- * executed concurrently and no order is guaranteed. Each event should only rely on the
- * serializability within its process method. All actions that must happen before or after a
- * certain action must be encoded inside the {@link #process(IndexWriter)} method.
- */
- @FunctionalInterface
- interface Event {
- /**
- * Processes the event. This method is called by the {@link IndexWriter} passed as the first
- * argument.
- *
- * @param writer the {@link IndexWriter} that executes the event.
- * @throws IOException if an {@link IOException} occurs
- */
- void process(IndexWriter writer) throws IOException;
- }
-
/**
* Anything that will add N docs to the index should reserve first to make sure it's allowed. This
* will throw {@code IllegalArgumentException} if it's not allowed.
diff --git a/lucene/core/src/java/org/apache/lucene/search/BitSetDocIdStream.java b/lucene/core/src/java/org/apache/lucene/search/BitSetDocIdStream.java
index 8c2f01e38e3..2a022069ec0 100644
--- a/lucene/core/src/java/org/apache/lucene/search/BitSetDocIdStream.java
+++ b/lucene/core/src/java/org/apache/lucene/search/BitSetDocIdStream.java
@@ -18,6 +18,7 @@ package org.apache.lucene.search;
import java.io.IOException;
import org.apache.lucene.util.FixedBitSet;
+import org.apache.lucene.util.IOIntConsumer;
import org.apache.lucene.util.MathUtil;
final class BitSetDocIdStream extends DocIdStream {
@@ -39,7 +40,7 @@ final class BitSetDocIdStream extends DocIdStream {
}
@Override
- public void forEach(int upTo, CheckedIntConsumer<IOException> consumer) throws IOException {
+ public void forEach(int upTo, IOIntConsumer consumer) throws IOException {
if (upTo > this.upTo) {
upTo = Math.min(upTo, max);
bitSet.forEach(this.upTo - offset, upTo - offset, offset, consumer);
diff --git a/lucene/core/src/java/org/apache/lucene/search/DocIdStream.java b/lucene/core/src/java/org/apache/lucene/search/DocIdStream.java
index b13e0ce6ec1..3f3a947c365 100644
--- a/lucene/core/src/java/org/apache/lucene/search/DocIdStream.java
+++ b/lucene/core/src/java/org/apache/lucene/search/DocIdStream.java
@@ -17,6 +17,7 @@
package org.apache.lucene.search;
import java.io.IOException;
+import org.apache.lucene.util.IOIntConsumer;
/**
* A stream of doc IDs. Doc IDs may be consumed at most once.
@@ -30,20 +31,19 @@ public abstract class DocIdStream {
protected DocIdStream() {}
/**
- * Iterate over doc IDs contained in this stream in order, calling the given {@link
- * CheckedIntConsumer} on them. This is a terminal operation.
+ * Iterate over doc IDs contained in this stream in order, calling the given {@link IOIntConsumer}
+ * on them. This is a terminal operation.
*/
- public void forEach(CheckedIntConsumer<IOException> consumer) throws IOException {
+ public void forEach(IOIntConsumer consumer) throws IOException {
forEach(DocIdSetIterator.NO_MORE_DOCS, consumer);
}
/**
* Iterate over doc IDs contained in this doc ID stream up to the given {@code upTo} exclusive,
- * calling the given {@link CheckedIntConsumer} on them. It is not possible to iterate these doc
- * IDs again later on.
+ * calling the given {@link IOIntConsumer} on them. It is not possible to iterate these doc IDs
+ * again later on.
*/
- public abstract void forEach(int upTo, CheckedIntConsumer<IOException> consumer)
- throws IOException;
+ public abstract void forEach(int upTo, IOIntConsumer consumer) throws IOException;
/** Count the number of entries in this stream. This is a terminal operation. */
public int count() throws IOException {
diff --git a/lucene/core/src/java/org/apache/lucene/search/RangeDocIdStream.java b/lucene/core/src/java/org/apache/lucene/search/RangeDocIdStream.java
index fc128880fc1..bfaee445f95 100644
--- a/lucene/core/src/java/org/apache/lucene/search/RangeDocIdStream.java
+++ b/lucene/core/src/java/org/apache/lucene/search/RangeDocIdStream.java
@@ -17,6 +17,7 @@
package org.apache.lucene.search;
import java.io.IOException;
+import org.apache.lucene.util.IOIntConsumer;
final class RangeDocIdStream extends DocIdStream {
@@ -37,7 +38,7 @@ final class RangeDocIdStream extends DocIdStream {
}
@Override
- public void forEach(int upTo, CheckedIntConsumer<IOException> consumer) throws IOException {
+ public void forEach(int upTo, IOIntConsumer consumer) throws IOException {
if (upTo > this.upTo) {
upTo = Math.min(upTo, max);
for (int doc = this.upTo; doc < upTo; ++doc) {
diff --git a/lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java b/lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java
index 1d8b4d16ecd..e896bff8721 100644
--- a/lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java
+++ b/lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java
@@ -19,7 +19,6 @@ package org.apache.lucene.util;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
-import org.apache.lucene.search.CheckedIntConsumer;
import org.apache.lucene.search.DocIdSet;
import org.apache.lucene.search.DocIdSetIterator;
@@ -832,8 +831,7 @@ public final class FixedBitSet extends BitSet {
* bit index and call {@code consumer} on it. This is internally used by queries that use bit sets
* as intermediate representations of their matches.
*/
- public void forEach(int from, int to, int base, CheckedIntConsumer<IOException> consumer)
- throws IOException {
+ public void forEach(int from, int to, int base, IOIntConsumer consumer) throws IOException {
Objects.checkFromToIndex(from, to, length());
// First, align `from` with a word start, ie. a multiple of Long.SIZE (64)
@@ -862,8 +860,7 @@ public final class FixedBitSet extends BitSet {
}
}
- private static void forEach(long bits, int base, CheckedIntConsumer<IOException> consumer)
- throws IOException {
+ private static void forEach(long bits, int base, IOIntConsumer consumer) throws IOException {
while (bits != 0L) {
int ntz = Long.numberOfTrailingZeros(bits);
consumer.accept(base + ntz);
diff --git a/lucene/core/src/java/org/apache/lucene/search/CheckedIntConsumer.java b/lucene/core/src/java/org/apache/lucene/util/IOIntConsumer.java
similarity index 66%
rename from lucene/core/src/java/org/apache/lucene/search/CheckedIntConsumer.java
rename to lucene/core/src/java/org/apache/lucene/util/IOIntConsumer.java
index 0e2b67084cf..4031f969ab1 100644
--- a/lucene/core/src/java/org/apache/lucene/search/CheckedIntConsumer.java
+++ b/lucene/core/src/java/org/apache/lucene/util/IOIntConsumer.java
@@ -14,18 +14,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.lucene.search;
+package org.apache.lucene.util;
-import java.util.function.IntConsumer;
+import java.io.IOException;
-/** Like {@link IntConsumer}, but may throw checked exceptions. */
+/**
+ * An IO operation with a single int input that may throw an IOException.
+ *
+ * @see java.util.function.IntConsumer
+ */
@FunctionalInterface
-public interface CheckedIntConsumer<T extends Exception> {
-
+public interface IOIntConsumer {
/**
- * Process the given value.
+ * Performs this operation on the given argument.
*
- * @see IntConsumer#accept(int)
+ * @param value the value to accept
+ * @throws IOException if producing the result throws an {@link IOException}
*/
- void accept(int value) throws T;
+ void accept(int value) throws IOException;
}
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/hnsw/TestFlatVectorScorer.java b/lucene/core/src/test/org/apache/lucene/codecs/hnsw/TestFlatVectorScorer.java
index 8d250c7fef6..ec3d7d34eb5 100644
--- a/lucene/core/src/test/org/apache/lucene/codecs/hnsw/TestFlatVectorScorer.java
+++ b/lucene/core/src/test/org/apache/lucene/codecs/hnsw/TestFlatVectorScorer.java
@@ -44,6 +44,7 @@ import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.store.MMapDirectory;
import org.apache.lucene.tests.util.LuceneTestCase;
+import org.apache.lucene.util.IOSupplier;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
@@ -51,10 +52,10 @@ public class TestFlatVectorScorer extends LuceneTestCase {
private static final AtomicInteger count = new AtomicInteger();
private final FlatVectorsScorer flatVectorsScorer;
- private final ThrowingSupplier<Directory> newDirectory;
+ private final IOSupplier<Directory> newDirectory;
public TestFlatVectorScorer(
- FlatVectorsScorer flatVectorsScorer, ThrowingSupplier<Directory> newDirectory) {
+ FlatVectorsScorer flatVectorsScorer, IOSupplier<Directory> newDirectory) {
this.flatVectorsScorer = flatVectorsScorer;
this.newDirectory = newDirectory;
}
@@ -67,7 +68,7 @@ public class TestFlatVectorScorer extends LuceneTestCase {
new Lucene99ScalarQuantizedVectorScorer(new DefaultFlatVectorScorer()),
FlatVectorScorerUtil.getLucene99FlatVectorsScorer());
var dirs =
- List.<ThrowingSupplier<Directory>>of(
+ List.<IOSupplier<Directory>>of(
TestFlatVectorScorer::newDirectory,
() -> new MMapDirectory(createTempDir(count.getAndIncrement() + "-")));
@@ -223,9 +224,4 @@ public class TestFlatVectorScorer extends LuceneTestCase {
public static <T> void assertThat(T actual, Matcher<? super T> matcher) {
MatcherAssert.assertThat("", actual, matcher);
}
-
- @FunctionalInterface
- public interface ThrowingSupplier<T> {
- T get() throws IOException;
- }
}
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestExitableDirectoryReader.java b/lucene/core/src/test/org/apache/lucene/index/TestExitableDirectoryReader.java
index d03c8cf42b5..046179eb82e 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestExitableDirectoryReader.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestExitableDirectoryReader.java
@@ -41,6 +41,7 @@ import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.analysis.MockAnalyzer;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.IOFunction;
import org.apache.lucene.util.SuppressForbidden;
import org.apache.lucene.util.TestVectorUtil;
@@ -334,11 +335,6 @@ public class TestExitableDirectoryReader extends LuceneTestCase {
return () -> true;
}
- @FunctionalInterface
- interface DvFactory {
- DocValuesIterator create(LeafReader leaf) throws IOException;
- }
-
public void testDocValues() throws IOException {
Directory directory = newDirectory();
IndexWriter writer =
@@ -363,8 +359,8 @@ public class TestExitableDirectoryReader extends LuceneTestCase {
DirectoryReader directoryReader;
DirectoryReader exitableDirectoryReader;
- for (DvFactory dvFactory :
- Arrays.<DvFactory>asList(
+ for (IOFunction<LeafReader, DocValuesIterator> dvFactory :
+ Arrays.<IOFunction<LeafReader, DocValuesIterator>>asList(
(r) -> r.getSortedDocValues("sorted"),
(r) -> r.getSortedSetDocValues("sortedset"),
(r) -> r.getSortedNumericDocValues("sortednumeric"),
@@ -381,7 +377,7 @@ public class TestExitableDirectoryReader extends LuceneTestCase {
ExitingReaderException.class,
() -> {
LeafReader leaf = reader.leaves().get(0).reader();
- DocValuesIterator iter = dvFactory.create(leaf);
+ DocValuesIterator iter = dvFactory.apply(leaf);
scan(leaf, iter);
});
reader.close();
@@ -393,7 +389,7 @@ public class TestExitableDirectoryReader extends LuceneTestCase {
{
IndexReader reader = new TestReader(getOnlyLeafReader(exitableDirectoryReader));
final LeafReader leaf = reader.leaves().get(0).reader();
- scan(leaf, dvFactory.create(leaf));
+ scan(leaf, dvFactory.apply(leaf));
assertNull(leaf.getNumericDocValues("absent"));
assertNull(leaf.getBinaryDocValues("absent"));
assertNull(leaf.getSortedDocValues("absent"));
diff --git a/lucene/core/src/test/org/apache/lucene/store/BaseDataOutputTestCase.java b/lucene/core/src/test/org/apache/lucene/store/BaseDataOutputTestCase.java
index 642add994f8..d56a817e209 100644
--- a/lucene/core/src/test/org/apache/lucene/store/BaseDataOutputTestCase.java
+++ b/lucene/core/src/test/org/apache/lucene/store/BaseDataOutputTestCase.java
@@ -38,8 +38,8 @@ public abstract class BaseDataOutputTestCase<T extends DataOutput> extends Lucen
protected abstract byte[] toBytes(T instance);
@FunctionalInterface
- private interface ThrowingBiFunction<T, U, R> {
- R apply(T t, U u) throws Exception;
+ private interface IOBiFunction<T, U, R> {
+ R apply(T t, U u) throws IOException;
}
@Test
@@ -68,8 +68,7 @@ public abstract class BaseDataOutputTestCase<T extends DataOutput> extends Lucen
}
}
- private static final List<ThrowingBiFunction<DataOutput, Random, IOConsumer<DataInput>>>
- GENERATORS;
+ private static final List<IOBiFunction<DataOutput, Random, IOConsumer<DataInput>>> GENERATORS;
static {
GENERATORS = new ArrayList<>();
diff --git a/lucene/join/src/java/org/apache/lucene/search/join/DocValuesTermsCollector.java b/lucene/join/src/java/org/apache/lucene/search/join/DocValuesTermsCollector.java
index 9a2c7263724..56137dadbdd 100644
--- a/lucene/join/src/java/org/apache/lucene/search/join/DocValuesTermsCollector.java
+++ b/lucene/join/src/java/org/apache/lucene/search/join/DocValuesTermsCollector.java
@@ -23,13 +23,12 @@ import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.SortedDocValues;
import org.apache.lucene.index.SortedSetDocValues;
import org.apache.lucene.search.SimpleCollector;
+import org.apache.lucene.util.IOFunction;
abstract class DocValuesTermsCollector<DV> extends SimpleCollector {
@FunctionalInterface
- interface Function<R> {
- R apply(LeafReader t) throws IOException;
- }
+ interface Function<R> extends IOFunction<LeafReader, R> {}
protected DV docValues;
private final Function<DV> docValuesCall;
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingLeafCollector.java b/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingLeafCollector.java
index c4cc2394a78..fbc63f4a3db 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingLeafCollector.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingLeafCollector.java
@@ -17,7 +17,6 @@
package org.apache.lucene.tests.search;
import java.io.IOException;
-import org.apache.lucene.search.CheckedIntConsumer;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.DocIdStream;
import org.apache.lucene.search.FilterDocIdSetIterator;
@@ -25,6 +24,7 @@ import org.apache.lucene.search.FilterLeafCollector;
import org.apache.lucene.search.LeafCollector;
import org.apache.lucene.search.Scorable;
import org.apache.lucene.util.FixedBitSet;
+import org.apache.lucene.util.IOIntConsumer;
/** Wraps another Collector and checks that order is respected. */
class AssertingLeafCollector extends FilterLeafCollector {
@@ -128,7 +128,7 @@ class AssertingLeafCollector extends FilterLeafCollector {
}
@Override
- public void forEach(CheckedIntConsumer<IOException> consumer) throws IOException {
+ public void forEach(IOIntConsumer consumer) throws IOException {
assert lastUpTo != DocIdSetIterator.NO_MORE_DOCS : "exhausted";
stream.forEach(
doc -> {
@@ -143,7 +143,7 @@ class AssertingLeafCollector extends FilterLeafCollector {
}
@Override
- public void forEach(int upTo, CheckedIntConsumer<IOException> consumer) throws IOException {
+ public void forEach(int upTo, IOIntConsumer consumer) throws IOException {
assert lastUpTo < upTo : "upTo=" + upTo + " but previous upTo=" + lastUpTo;
stream.forEach(
doc -> {