Corrected bug in Lifetime reference count

Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/6cf6a6e5
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/6cf6a6e5
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/6cf6a6e5

Branch: refs/heads/master
Commit: 6cf6a6e51453c74c6b4f26a8bd2d2be881cb4888
Parents: 1cfbd8b
Author: Vincent Van Den Berghe <v...@bvdep.com>
Authored: Sun Jul 30 18:40:47 2017 +0200
Committer: Shad Storhaug <s...@shadstorhaug.com>
Committed: Fri Aug 4 19:33:25 2017 +0700

----------------------------------------------------------------------
 src/Lucene.Net/Search/SearcherLifetimeManager.cs | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/6cf6a6e5/src/Lucene.Net/Search/SearcherLifetimeManager.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net/Search/SearcherLifetimeManager.cs 
b/src/Lucene.Net/Search/SearcherLifetimeManager.cs
index 8b7b3d4..7c51a58 100644
--- a/src/Lucene.Net/Search/SearcherLifetimeManager.cs
+++ b/src/Lucene.Net/Search/SearcherLifetimeManager.cs
@@ -145,7 +145,7 @@ namespace Lucene.Net.Search
         // TODO: we could get by w/ just a "set"; need to have
         // Tracker hash by its version and have compareTo(Long)
         // compare to its version
-        private readonly ConcurrentDictionary<long, SearcherTracker> 
_searchers = new ConcurrentDictionary<long, SearcherTracker>();
+        private readonly ConcurrentDictionary<long, Lazy<SearcherTracker>> 
_searchers = new ConcurrentDictionary<long, Lazy<SearcherTracker>>();
 
         private void EnsureOpen()
         {
@@ -177,7 +177,7 @@ namespace Lucene.Net.Search
             // bug isolation if we assign our own private ID:
             var version = ((DirectoryReader)searcher.IndexReader).Version;
             var factoryMethodCalled = false;
-            var tracker = _searchers.GetOrAdd(version, l => { 
factoryMethodCalled = true; return new SearcherTracker(searcher); });
+                               var tracker = _searchers.GetOrAdd(version, l => 
new Lazy<SearcherTracker>(() => { factoryMethodCalled = true; return new 
SearcherTracker(searcher); })).Value;
             if (!factoryMethodCalled && tracker.Searcher != searcher)
             {
                 throw new ArgumentException("the provided searcher has the 
same underlying reader version yet the searcher instance differs from before 
(new=" + searcher + " vs old=" + tracker.Searcher);
@@ -203,10 +203,10 @@ namespace Lucene.Net.Search
         public virtual IndexSearcher Acquire(long version)
         {
             EnsureOpen();
-            SearcherTracker tracker;
-            if (_searchers.TryGetValue(version, out tracker) && tracker != 
null && tracker.Searcher.IndexReader.TryIncRef())
+            Lazy<SearcherTracker> tracker;
+            if (_searchers.TryGetValue(version, out tracker) && 
tracker.IsValueCreated && tracker.Value.Searcher.IndexReader.TryIncRef())
             {
-                return tracker.Searcher;
+                return tracker.Value.Searcher;
             }
 
             return null;
@@ -279,7 +279,7 @@ namespace Lucene.Net.Search
                 // (not thread-safe since the values can change while
                 // ArrayList is init'ing itself); must instead iterate
                 // ourselves:
-                var trackers = _searchers.Values.ToList();
+                var trackers = _searchers.Values.Select(item => 
item.Value).ToList();
                 trackers.Sort();
                 var lastRecordTimeSec = 0.0;
                 double now = Time.NanoTime() / NANOS_PER_SEC;
@@ -301,7 +301,7 @@ namespace Lucene.Net.Search
                     if (pruner.DoPrune(ageSec, tracker.Searcher))
                     {
                         //System.out.println("PRUNE version=" + 
tracker.version + " age=" + ageSec + " ms=" + System.currentTimeMillis());
-                        SearcherTracker _;
+                        Lazy<SearcherTracker> _;
                         _searchers.TryRemove(tracker.Version, out _);
                         tracker.Dispose();
                     }
@@ -326,13 +326,13 @@ namespace Lucene.Net.Search
             lock (this)
             {
                 _closed = true;
-                IList<SearcherTracker> toClose = new 
List<SearcherTracker>(_searchers.Values);
+                IList<SearcherTracker> toClose = new 
List<SearcherTracker>(_searchers.Values.Select(item => item.Value));
 
                 // Remove up front in case exc below, so we don't
                 // over-decRef on double-close:
                 foreach (var tracker in toClose)
                 {
-                    SearcherTracker _;
+                    Lazy<SearcherTracker> _;
                     _searchers.TryRemove(tracker.Version, out _);
                 }
 

Reply via email to