mikemccand commented on code in PR #11780:
URL: https://github.com/apache/lucene/pull/11780#discussion_r983451389
##########
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:
Yeah I don't think that's important to guard against?
An application typically has a separate refresh thread, and that (one)
thread will call refresh, compute top-level data structures like `OrdinalMap`,
SSDV states, etc., and then wait some time and refresh again. So the
application can easily ensure it's not double-calling refresh, and if it might,
it should use its own locking.
--
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]