Пример #1
0
        /// <summary>
        /// Records that you are now using this IndexSearcher.
        ///  Always call this when you've obtained a possibly new
        ///  <seealso cref="IndexSearcher"/>, for example from {@link
        ///  SearcherManager}.  It's fine if you already passed the
        ///  same searcher to this method before.
        ///
        ///  <p>this returns the long token that you can later pass
        ///  to <seealso cref="#acquire"/> to retrieve the same IndexSearcher.
        ///  You should record this long token in the search results
        ///  sent to your user, such that if the user performs a
        ///  follow-on action (clicks next page, drills down, etc.)
        ///  the token is returned.
        /// </summary>
        public virtual long Record(IndexSearcher searcher)
        {
            EnsureOpen();
            // TODO: we don't have to use IR.getVersion to track;
            // could be risky (if it's buggy); we could get better
            // bug isolation if we assign our own private ID:
            long            version = ((DirectoryReader)searcher.IndexReader).Version;
            SearcherTracker tracker = Searchers[version];

            if (tracker == null)
            {
                //System.out.println("RECORD version=" + version + " ms=" + System.currentTimeMillis());
                tracker = new SearcherTracker(searcher);
                if (Searchers.AddIfAbsent(version, tracker) != null)
                {
                    // Another thread beat us -- must decRef to undo
                    // incRef done by SearcherTracker ctor:
                    tracker.Dispose();
                }
            }
            else if (tracker.Searcher != searcher)
            {
                throw new System.ArgumentException("the provided searcher has the same underlying reader version yet the searcher instance differs from before (new=" + searcher + " vs old=" + tracker.Searcher);
            }

            return(version);
        }
Пример #2
0
        /// <summary>
        /// Retrieve a previously recorded <seealso cref="IndexSearcher"/>, if it
        ///  has not yet been closed
        ///
        ///  <p><b>NOTE</b>: this may return null when the
        ///  requested searcher has already timed out.  When this
        ///  happens you should notify your user that their session
        ///  timed out and that they'll have to restart their
        ///  search.</p>
        ///
        ///  <p>If this returns a non-null result, you must match
        ///  later call <seealso cref="#release"/> on this searcher, best
        ///  from a finally clause.</p>
        /// </summary>
        public virtual IndexSearcher Acquire(long version)
        {
            EnsureOpen();
            SearcherTracker tracker = _searchers[version];

            if (tracker != null && tracker.Searcher.IndexReader.TryIncRef())
            {
                return(tracker.Searcher);
            }

            return(null);
        }