Uploaded image for project: 'Solr'
  1. Solr
  2. SOLR-12374

Add SolrCore.withSearcher(lambda accepting SolrIndexSearcher)

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • None
    • 7.4
    • None
    • None

    Description

      I propose adding the following to SolrCore:

        /**
         * Executes the lambda with the {@link SolrIndexSearcher}.  This is more convenience than using
         * {@link #getSearcher()} since there is no ref-counting business to worry about.
         * Example:
         * <pre class="prettyprint">
         *   IndexReader reader = h.getCore().withSearcher(SolrIndexSearcher::getIndexReader);
         * </pre>
         */
        @SuppressWarnings("unchecked")
        public <R> R withSearcher(Function<SolrIndexSearcher,R> lambda) {
          final RefCounted<SolrIndexSearcher> refCounted = getSearcher();
          try {
            return lambda.apply(refCounted.get());
          } finally {
            refCounted.decref();
          }
        }
      

      This is a nice tight convenience method, avoiding the clumsy RefCounted API which is easy to accidentally incorrectly use – see https://issues.apache.org/jira/browse/SOLR-11616?focusedCommentId=16477719&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-16477719

      I guess my only (small) concern is if hypothetically you might make the lambda short because it's easy to do that (see the one-liner example above) but the object you return that you're interested in  (say IndexReader) could potentially become invalid if the SolrIndexSearcher closes.  But I think/hope that's impossible normally based on when this getSearcher() used?  I could at least add a warning to the docs.

      Attachments

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            dsmiley David Smiley
            dsmiley David Smiley
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment