salvatorecampagna commented on code in PR #15378:
URL: https://github.com/apache/lucene/pull/15378#discussion_r2483807208
##########
lucene/core/src/java/org/apache/lucene/index/MergePolicy.java:
##########
@@ -939,4 +940,93 @@ static final class MergeReader {
this.hardLiveDocs = hardLiveDocs;
}
}
+
+ /**
+ * Observer for merge operations returned by {@link
IndexWriter#forceMergeDeletes(boolean)}.
+ * Provides methods to query merge status and wait for completion.
+ *
+ * <p>When no merges are needed, {@link #numMerges()} returns 0. In this
case, {@link #await()}
+ * returns {@code true} immediately since there is nothing to wait for.
+ *
+ * @lucene.experimental
+ */
+ public static final class MergeObserver {
+ private final MergePolicy.MergeSpecification spec;
+
+ MergeObserver(MergePolicy.MergeSpecification spec) {
+ this.spec = spec;
+ }
+
+ /**
+ * Returns the number of merges in this specification.
+ *
+ * @return number of merges, or 0 if no merges were scheduled
+ */
+ public int numMerges() {
+ return spec == null ? 0 : spec.merges.size();
+ }
+
+ /**
+ * Returns the number of completed merges in this specification. Useful
for tracking merge
+ * progress: {@code numCompletedMerges() / numMerges()}.
+ *
+ * @return number of completed merges
+ */
+ public int numCompletedMerges() {
+ if (spec == null) {
+ return 0;
+ }
+ int completed = 0;
+ for (OneMerge merge : spec.merges) {
+ if (merge.mergeCompleted.isDone()) {
+ completed++;
+ }
+ }
+ return completed;
+ }
+
+ /**
+ * Waits for all merges in this specification to complete. Returns
immediately if no merges were
+ * scheduled.
+ *
+ * @return {@code true} if all merges completed successfully or no merges
were needed, {@code
+ * false} on error
+ */
+ public boolean await() {
+ return spec == null || spec.await();
+ }
+
+ /**
+ * Waits for all merges in this specification to complete, with timeout.
Returns immediately if
+ * no merges were scheduled.
+ *
+ * @param timeout maximum time to wait
+ * @param unit time unit for timeout
+ * @return {@code true} if all merges completed within timeout or no
merges were needed, {@code
+ * false} on timeout or error
+ */
+ public boolean await(long timeout, TimeUnit unit) {
+ return spec == null || spec.await(timeout, unit);
+ }
+
+ /**
+ * Returns a {@link CompletableFuture} that completes when all merges
finish. Returns an
+ * already-completed future if no merges were scheduled.
+ *
+ * @return future that completes when merges finish
+ */
+ public CompletableFuture<Void> awaitAsync() {
+ return spec == null
+ ? CompletableFuture.completedFuture(null)
+ : spec.getMergeCompletedFutures();
+ }
+
+ @Override
+ public String toString() {
+ if (spec == null) {
+ return "MergeObserver: no merges";
+ }
+ return String.format(Locale.ROOT, "MergeObserver: %d merges",
numMerges());
Review Comment:
My concern is that spec.toString() was internal before, so we controlled
where it's used. Now that MergeObserver is public API, users might call
toString() frequently (logging, monitoring) without realizing it could be
expensive with many merges.
##########
lucene/core/src/java/org/apache/lucene/index/MergePolicy.java:
##########
@@ -939,4 +940,93 @@ static final class MergeReader {
this.hardLiveDocs = hardLiveDocs;
}
}
+
+ /**
+ * Observer for merge operations returned by {@link
IndexWriter#forceMergeDeletes(boolean)}.
+ * Provides methods to query merge status and wait for completion.
+ *
+ * <p>When no merges are needed, {@link #numMerges()} returns 0. In this
case, {@link #await()}
+ * returns {@code true} immediately since there is nothing to wait for.
+ *
+ * @lucene.experimental
+ */
+ public static final class MergeObserver {
+ private final MergePolicy.MergeSpecification spec;
+
+ MergeObserver(MergePolicy.MergeSpecification spec) {
+ this.spec = spec;
+ }
+
+ /**
+ * Returns the number of merges in this specification.
+ *
+ * @return number of merges, or 0 if no merges were scheduled
+ */
+ public int numMerges() {
+ return spec == null ? 0 : spec.merges.size();
+ }
+
+ /**
+ * Returns the number of completed merges in this specification. Useful
for tracking merge
+ * progress: {@code numCompletedMerges() / numMerges()}.
+ *
+ * @return number of completed merges
+ */
+ public int numCompletedMerges() {
+ if (spec == null) {
+ return 0;
+ }
+ int completed = 0;
+ for (OneMerge merge : spec.merges) {
+ if (merge.mergeCompleted.isDone()) {
+ completed++;
+ }
+ }
+ return completed;
+ }
+
+ /**
+ * Waits for all merges in this specification to complete. Returns
immediately if no merges were
+ * scheduled.
+ *
+ * @return {@code true} if all merges completed successfully or no merges
were needed, {@code
+ * false} on error
+ */
+ public boolean await() {
+ return spec == null || spec.await();
+ }
+
+ /**
+ * Waits for all merges in this specification to complete, with timeout.
Returns immediately if
+ * no merges were scheduled.
+ *
+ * @param timeout maximum time to wait
+ * @param unit time unit for timeout
+ * @return {@code true} if all merges completed within timeout or no
merges were needed, {@code
+ * false} on timeout or error
+ */
+ public boolean await(long timeout, TimeUnit unit) {
+ return spec == null || spec.await(timeout, unit);
+ }
+
+ /**
+ * Returns a {@link CompletableFuture} that completes when all merges
finish. Returns an
+ * already-completed future if no merges were scheduled.
+ *
+ * @return future that completes when merges finish
+ */
+ public CompletableFuture<Void> awaitAsync() {
+ return spec == null
+ ? CompletableFuture.completedFuture(null)
+ : spec.getMergeCompletedFutures();
+ }
+
+ @Override
+ public String toString() {
+ if (spec == null) {
+ return "MergeObserver: no merges";
+ }
+ return String.format(Locale.ROOT, "MergeObserver: %d merges",
numMerges());
Review Comment:
My concern is that `spec.toString()` was internal before, so we controlled
where it's used. Now that MergeObserver is public API, users might call
toString() frequently (logging, monitoring) without realizing it could be
expensive with many merges.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]