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 24d802cbdcec2bb2148b52680dfe9b872c487b76 Author: Shad Storhaug <[email protected]> AuthorDate: Sun Mar 7 22:54:37 2021 +0700 Lucene.Net.Search.FieldCacheImpl: Restored readerCache locks to the state of Lucene 4.8.0 (closes #272) --- src/Lucene.Net/Search/FieldCacheImpl.cs | 74 ++++++++++++++++----------------- 1 file changed, 36 insertions(+), 38 deletions(-) diff --git a/src/Lucene.Net/Search/FieldCacheImpl.cs b/src/Lucene.Net/Search/FieldCacheImpl.cs index 64f2d97..8f8666d 100644 --- a/src/Lucene.Net/Search/FieldCacheImpl.cs +++ b/src/Lucene.Net/Search/FieldCacheImpl.cs @@ -1,4 +1,4 @@ -using J2N.Collections.Generic.Extensions; +using J2N.Collections.Generic.Extensions; using Lucene.Net.Diagnostics; using Lucene.Net.Index; using Lucene.Net.Support; @@ -144,9 +144,7 @@ namespace Lucene.Net.Search private void AddCacheEntries<TKey, TValue>(IList<FieldCache.CacheEntry> result, Type cacheType, Cache<TKey, TValue> cache) where TKey : CacheKey { -#if !FEATURE_CONDITIONALWEAKTABLE_ENUMERATOR lock (cache.readerCache) -#endif { foreach (var readerCacheEntry in cache.readerCache) { @@ -248,9 +246,7 @@ namespace Lucene.Net.Search /// Remove this reader from the cache, if present. </summary> public virtual void PurgeByCacheKey(object coreCacheKey) { -#if !FEATURE_CONDITIONALWEAKTABLE_ENUMERATOR lock (readerCache) -#endif readerCache.Remove(coreCacheKey); } @@ -262,19 +258,20 @@ namespace Lucene.Net.Search { ConcurrentDictionary<TKey, object> innerCache; object readerKey = reader.CoreCacheKey; -#if FEATURE_CONDITIONALWEAKTABLE_ENUMERATOR - innerCache = readerCache.GetValue(readerKey, (readerKey) => + lock (readerCache) { - // First time this reader is using FieldCache - wrapper.InitReader(reader); - return new ConcurrentDictionary<TKey, object> +#if FEATURE_CONDITIONALWEAKTABLE_ENUMERATOR + + innerCache = readerCache.GetValue(readerKey, (readerKey) => { - [key] = value - }; - }); + // First time this reader is using FieldCache + wrapper.InitReader(reader); + return new ConcurrentDictionary<TKey, object> + { + [key] = value + }; + }); #else - lock (readerCache) - { if (!readerCache.TryGetValue(readerKey, out innerCache) || innerCache is null) { // First time this reader is using FieldCache @@ -285,8 +282,9 @@ namespace Lucene.Net.Search readerCache.Add(readerKey, innerCache); wrapper.InitReader(reader); } - } #endif + } + // If another thread beat us to it, leave the current value innerCache.TryAdd(key, value); } @@ -295,20 +293,19 @@ namespace Lucene.Net.Search { ConcurrentDictionary<TKey, object> innerCache; object readerKey = reader.CoreCacheKey; -#if FEATURE_CONDITIONALWEAKTABLE_ENUMERATOR - innerCache = readerCache.GetValue(readerKey, (readerKey) => + lock (readerCache) { - // First time this reader is using FieldCache - wrapper.InitReader(reader); - return new ConcurrentDictionary<TKey, object> +#if FEATURE_CONDITIONALWEAKTABLE_ENUMERATOR + innerCache = readerCache.GetValue(readerKey, (readerKey) => { - [key] = new FieldCache.CreationPlaceholder<TValue>() - }; - }); - + // First time this reader is using FieldCache + wrapper.InitReader(reader); + return new ConcurrentDictionary<TKey, object> + { + [key] = new FieldCache.CreationPlaceholder<TValue>() + }; + }); #else - lock (readerCache) - { if (!readerCache.TryGetValue(readerKey, out innerCache) || innerCache is null) { // First time this reader is using FieldCache @@ -319,8 +316,8 @@ namespace Lucene.Net.Search readerCache[readerKey] = innerCache; wrapper.InitReader(reader); } - } #endif + } object value = innerCache.GetOrAdd(key, (cacheKey) => new FieldCache.CreationPlaceholder<TValue>()); if (value is FieldCache.CreationPlaceholder<TValue> progress) { @@ -329,18 +326,19 @@ namespace Lucene.Net.Search if (progress.Value is null) { progress.Value = CreateValue(reader, key, setDocsWithField); - if (innerCache.TryUpdate(key, progress.Value, value)) + lock (readerCache) + { + innerCache.TryUpdate(key, progress.Value, value); + } + // Only check if key.custom (the parser) is + // non-null; else, we check twice for a single + // call to FieldCache.getXXX + if (!(key.Custom is null) && !(wrapper is null)) { - // Only check if key.custom (the parser) is - // non-null; else, we check twice for a single - // call to FieldCache.getXXX - if (!(key.Custom is null) && !(wrapper is null)) + TextWriter infoStream = wrapper.InfoStream; + if (infoStream != null) { - TextWriter infoStream = wrapper.InfoStream; - if (infoStream != null) - { - PrintNewInsanity(infoStream, progress.Value); - } + PrintNewInsanity(infoStream, progress.Value); } } }
