You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2020/01/30 08:15:50 UTC

[lucenenet] 01/09: Lucene.Net.Spatial.Util.ShapeFieldCacheProvider: Simplified cache value creation by using ConditionalWeakTable.GetValue(), and removed dependency on WeakDictionary (See LUCENENET-610, LUCENENET-640).

This is an automated email from the ASF dual-hosted git repository.

nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git

commit 01ddd63fb9350c14ddf3e94a198618bfc6754595
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Sun Jan 26 19:41:42 2020 +0700

    Lucene.Net.Spatial.Util.ShapeFieldCacheProvider: Simplified cache value creation by using ConditionalWeakTable<TKey, TValue>.GetValue(), and removed dependency on WeakDictionary (See LUCENENET-610, LUCENENET-640).
---
 .../Util/ShapeFieldCacheProvider.cs                | 24 +++++-----------------
 1 file changed, 5 insertions(+), 19 deletions(-)

diff --git a/src/Lucene.Net.Spatial/Util/ShapeFieldCacheProvider.cs b/src/Lucene.Net.Spatial/Util/ShapeFieldCacheProvider.cs
index 55c076f..e36490f 100644
--- a/src/Lucene.Net.Spatial/Util/ShapeFieldCacheProvider.cs
+++ b/src/Lucene.Net.Spatial/Util/ShapeFieldCacheProvider.cs
@@ -1,6 +1,5 @@
 using Lucene.Net.Index;
 using Lucene.Net.Search;
-using Lucene.Net.Support;
 using Lucene.Net.Util;
 using Spatial4n.Core.Shapes;
 using System.Runtime.CompilerServices;
@@ -40,13 +39,8 @@ namespace Lucene.Net.Spatial.Util
     {
         //private Logger log = Logger.GetLogger(GetType().FullName);
 
-#if FEATURE_CONDITIONALWEAKTABLE_ADDORUPDATE
         private readonly ConditionalWeakTable<IndexReader, ShapeFieldCache<T>> sidx =
             new ConditionalWeakTable<IndexReader, ShapeFieldCache<T>>();
-#else
-        private readonly WeakDictionary<IndexReader, ShapeFieldCache<T>> sidx =
-            new WeakDictionary<IndexReader, ShapeFieldCache<T>>();
-#endif
 
         protected internal readonly int m_defaultSize;
         protected internal readonly string m_shapeField;
@@ -60,23 +54,17 @@ namespace Lucene.Net.Spatial.Util
 
         protected internal abstract T ReadShape(BytesRef term);
 
-        private readonly object locker = new object();
-
         public virtual ShapeFieldCache<T> GetCache(AtomicReader reader)
         {
-            lock (locker)
+            // LUCENENET: ConditionalWeakTable allows us to simplify and remove locks
+            return sidx.GetValue(reader, (key) =>
             {
-                ShapeFieldCache<T> idx;
-                if (sidx.TryGetValue(reader, out idx) && idx != null)
-                {
-                    return idx;
-                }
                 /*long startTime = Runtime.CurrentTimeMillis();
                 log.Fine("Building Cache [" + reader.MaxDoc() + "]");*/
-                idx = new ShapeFieldCache<T>(reader.MaxDoc, m_defaultSize);
+                ShapeFieldCache<T> idx = new ShapeFieldCache<T>(key.MaxDoc, m_defaultSize);
                 int count = 0;
                 DocsEnum docs = null;
-                Terms terms = reader.GetTerms(m_shapeField);
+                Terms terms = ((AtomicReader)key).GetTerms(m_shapeField);
                 TermsEnum te = null;
                 if (terms != null)
                 {
@@ -99,12 +87,10 @@ namespace Lucene.Net.Spatial.Util
                         term = te.Next();
                     }
                 }
-                sidx.AddOrUpdate(reader, idx);
-
                 /*long elapsed = Runtime.CurrentTimeMillis() - startTime;
                 log.Fine("Cached: [" + count + " in " + elapsed + "ms] " + idx);*/
                 return idx;
-            }
+            });
         }
     }
 }