You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by GitBox <gi...@apache.org> on 2022/09/28 15:28:27 UTC

[GitHub] [lucene] mikemccand commented on a diff in pull request #11780: GH#11601: Add ability to compute reader states after refresh

mikemccand commented on code in PR #11780:
URL: https://github.com/apache/lucene/pull/11780#discussion_r982553986


##########
lucene/core/src/java/org/apache/lucene/search/ReferenceManager.java:
##########
@@ -219,6 +219,36 @@ public final boolean maybeRefresh() throws IOException {
     return doTryRefresh;
   }
 
+  /** Compute some state of the reference using a parameter. */
+  @FunctionalInterface
+  public interface StateCalculator<R, G, P> {
+    R calculate(G current, P param);
+  }
+
+  /**
+   * If you need to compute something after the refresh, you can use this method instead of {@link
+   * #maybeRefresh()}.
+   *
+   * @throws IOException if refreshing the resource causes an {@link IOException}
+   */
+  public final <R, P> R maybeRefreshAndReturnState(
+      StateCalculator<R, G, P> refreshedStateCalculator, P param) throws IOException {
+    ensureOpen();
+
+    // Ensure only 1 thread does refresh at once; other threads just return immediately:
+    final boolean doTryRefresh = refreshLock.tryLock();
+    if (doTryRefresh) {
+      try {
+        doMaybeRefresh();
+        return refreshedStateCalculator.calculate(current, param);

Review Comment:
   Hmm why is it necessary to compute this state under lock?  Can't the caller just compute the state themselves when they get the refreshed reader?



-- 
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: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org