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:55 UTC

[lucenenet] 06/09: Lucene.Net.Support: Renamed ConcurrentHashMapWrapper > ConcurrentDictionaryWrapper, marked internal, and added DictionaryExtensions.AsConcurrent() extension method to make usage simpler

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 bb873f48ea6217ff573c9d632bd176f760a41286
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Mon Jan 27 05:44:13 2020 +0700

    Lucene.Net.Support: Renamed ConcurrentHashMapWrapper > ConcurrentDictionaryWrapper, marked internal, and added DictionaryExtensions.AsConcurrent() extension method to make usage simpler
---
 .../Index/ThreadedIndexingAndSearchingTestCase.cs  |  3 +-
 .../Search/AssertingScorer.cs                      |  2 +-
 src/Lucene.Net/Search/CachingWrapperFilter.cs      |  3 +-
 ...apWrapper.cs => ConcurrentDictionaryWrapper.cs} | 36 +++++++--------
 src/Lucene.Net/Support/DictionaryExtensions.cs     | 51 +++++++++++++++-------
 5 files changed, 57 insertions(+), 38 deletions(-)

diff --git a/src/Lucene.Net.TestFramework/Index/ThreadedIndexingAndSearchingTestCase.cs b/src/Lucene.Net.TestFramework/Index/ThreadedIndexingAndSearchingTestCase.cs
index 2dddef4..bade91d 100644
--- a/src/Lucene.Net.TestFramework/Index/ThreadedIndexingAndSearchingTestCase.cs
+++ b/src/Lucene.Net.TestFramework/Index/ThreadedIndexingAndSearchingTestCase.cs
@@ -549,8 +549,7 @@ namespace Lucene.Net.Index
 #if FEATURE_CONDITIONALWEAKTABLE_ADDORUPDATE
         private readonly ConditionalWeakTable<SegmentCoreReaders, BooleanRef> warmed = new ConditionalWeakTable<SegmentCoreReaders, BooleanRef>();
 #else
-        private readonly IDictionary<SegmentCoreReaders, BooleanRef> warmed = new ConcurrentHashMapWrapper<SegmentCoreReaders, BooleanRef>(new WeakDictionary<SegmentCoreReaders, BooleanRef>());
-                                                                                                                                    // Collections.synchronizedMap(new WeakHashMap<SegmentCoreReaders, BooleanRef>());
+        private readonly IDictionary<SegmentCoreReaders, BooleanRef> warmed = new WeakDictionary<SegmentCoreReaders, BooleanRef>().AsConcurrent();
 #endif
 
         public virtual void RunTest(string testName)
diff --git a/src/Lucene.Net.TestFramework/Search/AssertingScorer.cs b/src/Lucene.Net.TestFramework/Search/AssertingScorer.cs
index 023378e..df19e58 100644
--- a/src/Lucene.Net.TestFramework/Search/AssertingScorer.cs
+++ b/src/Lucene.Net.TestFramework/Search/AssertingScorer.cs
@@ -36,7 +36,7 @@ namespace Lucene.Net.Search
             new ConditionalWeakTable<Scorer, WeakReference<AssertingScorer>>();
 #else
         private static readonly IDictionary<Scorer, WeakReference<AssertingScorer>> ASSERTING_INSTANCES = 
-            new ConcurrentHashMapWrapper<Scorer, WeakReference<AssertingScorer>>(new WeakDictionary<Scorer, WeakReference<AssertingScorer>>());
+            new WeakDictionary<Scorer, WeakReference<AssertingScorer>>().AsConcurrent();
 #endif
 
         public static Scorer Wrap(Random random, Scorer other)
diff --git a/src/Lucene.Net/Search/CachingWrapperFilter.cs b/src/Lucene.Net/Search/CachingWrapperFilter.cs
index af4175e..b933d55 100644
--- a/src/Lucene.Net/Search/CachingWrapperFilter.cs
+++ b/src/Lucene.Net/Search/CachingWrapperFilter.cs
@@ -41,8 +41,7 @@ namespace Lucene.Net.Search
 #if FEATURE_CONDITIONALWEAKTABLE_ADDORUPDATE
         private readonly ConditionalWeakTable<object, DocIdSet> _cache = new ConditionalWeakTable<object, DocIdSet>();
 #else
-        //private readonly IDictionary<object, DocIdSet> Cache = Collections.synchronizedMap(new WeakHashMap<object, DocIdSet>());
-        private readonly IDictionary<object, DocIdSet> _cache = new ConcurrentHashMapWrapper<object, DocIdSet>(new WeakDictionary<object, DocIdSet>());
+        private readonly IDictionary<object, DocIdSet> _cache = new WeakDictionary<object, DocIdSet>().AsConcurrent();
 #endif
 
         /// <summary>
diff --git a/src/Lucene.Net/Support/ConcurrentHashMapWrapper.cs b/src/Lucene.Net/Support/ConcurrentDictionaryWrapper.cs
similarity index 83%
rename from src/Lucene.Net/Support/ConcurrentHashMapWrapper.cs
rename to src/Lucene.Net/Support/ConcurrentDictionaryWrapper.cs
index b257082..94e3a34 100644
--- a/src/Lucene.Net/Support/ConcurrentHashMapWrapper.cs
+++ b/src/Lucene.Net/Support/ConcurrentDictionaryWrapper.cs
@@ -7,28 +7,28 @@ using System.Threading;
 namespace Lucene.Net.Support
 {
     /*
-	 * Licensed to the Apache Software Foundation (ASF) under one or more
-	 * contributor license agreements.  See the NOTICE file distributed with
-	 * this work for additional information regarding copyright ownership.
-	 * The ASF licenses this file to You under the Apache License, Version 2.0
-	 * (the "License"); you may not use this file except in compliance with
-	 * the License.  You may obtain a copy of the License at
-	 *
-	 *     http://www.apache.org/licenses/LICENSE-2.0
-	 *
-	 * Unless required by applicable law or agreed to in writing, software
-	 * distributed under the License is distributed on an "AS IS" BASIS,
-	 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-	 * See the License for the specific language governing permissions and
-	 * limitations under the License.
-	 */
-
-    public class ConcurrentHashMapWrapper<TKey, TValue> : IDictionary<TKey, TValue>
+     * Licensed to the Apache Software Foundation (ASF) under one or more
+     * contributor license agreements.  See the NOTICE file distributed with
+     * this work for additional information regarding copyright ownership.
+     * The ASF licenses this file to You under the Apache License, Version 2.0
+     * (the "License"); you may not use this file except in compliance with
+     * the License.  You may obtain a copy of the License at
+     *
+     *     http://www.apache.org/licenses/LICENSE-2.0
+     *
+     * Unless required by applicable law or agreed to in writing, software
+     * distributed under the License is distributed on an "AS IS" BASIS,
+     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     * See the License for the specific language governing permissions and
+     * limitations under the License.
+     */
+
+    internal class ConcurrentDictionaryWrapper<TKey, TValue> : IDictionary<TKey, TValue>
     {
         private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
         private readonly IDictionary<TKey, TValue> _dict;
 
-        public ConcurrentHashMapWrapper(IDictionary<TKey, TValue> wrapped)
+        public ConcurrentDictionaryWrapper(IDictionary<TKey, TValue> wrapped)
         {
             this._dict = wrapped;
         }
diff --git a/src/Lucene.Net/Support/DictionaryExtensions.cs b/src/Lucene.Net/Support/DictionaryExtensions.cs
index d9365c2..e3e60ac 100644
--- a/src/Lucene.Net/Support/DictionaryExtensions.cs
+++ b/src/Lucene.Net/Support/DictionaryExtensions.cs
@@ -5,21 +5,21 @@ using System.IO;
 namespace Lucene.Net.Support
 {
     /*
-	 * Licensed to the Apache Software Foundation (ASF) under one or more
-	 * contributor license agreements.  See the NOTICE file distributed with
-	 * this work for additional information regarding copyright ownership.
-	 * The ASF licenses this file to You under the Apache License, Version 2.0
-	 * (the "License"); you may not use this file except in compliance with
-	 * the License.  You may obtain a copy of the License at
-	 *
-	 *     http://www.apache.org/licenses/LICENSE-2.0
-	 *
-	 * Unless required by applicable law or agreed to in writing, software
-	 * distributed under the License is distributed on an "AS IS" BASIS,
-	 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-	 * See the License for the specific language governing permissions and
-	 * limitations under the License.
-	 */
+     * Licensed to the Apache Software Foundation (ASF) under one or more
+     * contributor license agreements.  See the NOTICE file distributed with
+     * this work for additional information regarding copyright ownership.
+     * The ASF licenses this file to You under the Apache License, Version 2.0
+     * (the "License"); you may not use this file except in compliance with
+     * the License.  You may obtain a copy of the License at
+     *
+     *     http://www.apache.org/licenses/LICENSE-2.0
+     *
+     * Unless required by applicable law or agreed to in writing, software
+     * distributed under the License is distributed on an "AS IS" BASIS,
+     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     * See the License for the specific language governing permissions and
+     * limitations under the License.
+     */
 
     public static class DictionaryExtensions
     {
@@ -42,6 +42,27 @@ namespace Lucene.Net.Support
         }
 
         /// <summary>
+        /// Returns a concurrent wrapper for the current <see cref="IDictionary{TKey, TValue}"/>.
+        /// </summary>
+        /// <typeparam name="TKey">The type of keys in the dictionary.</typeparam>
+        /// <typeparam name="TValue">The type of values in the dictionary.</typeparam>
+        /// <param name="dictionary">The collection to make concurrent (thread-safe).</param>
+        /// <returns>An object that acts as a read-only wrapper around the current <see cref="ISet{T}"/>.</returns>
+        /// <exception cref="ArgumentNullException"><paramref name="dictionary"/> is <c>null</c>.</exception>
+        /// <remarks>
+        /// To synchronize any modifications to the <see cref="ISet{T}"/> object, expose it only through this wrapper.
+        /// <para/>
+        /// The set returned uses simple locking and may not be the most performant solution, but it provides a quick
+        /// way to make any set thread-safe.
+        /// <para/>
+        /// This method is an O(1) operation.
+        /// </remarks>
+        internal static IDictionary<TKey, TValue> AsConcurrent<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
+        {
+            return new ConcurrentDictionaryWrapper<TKey, TValue>(dictionary);
+        }
+
+        /// <summary>
         /// Loads properties from the specified <see cref="Stream"/>. The encoding is
         /// ISO8859-1. 
         /// </summary>