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 2019/12/17 17:00:59 UTC
[lucenenet] 03/04: SWEEP: Fixed code that accesses indexer for
HashMap so no KeyNotFoundExceptions are thrown
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 d35af3d2300283a4a0af5cd58f1b6836c41db001
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Fri Nov 1 19:28:51 2019 +0700
SWEEP: Fixed code that accesses indexer for HashMap so no KeyNotFoundExceptions are thrown
---
.../Taxonomy/CachedOrdinalsReader.cs | 3 +-
.../Highlight/QueryScorer.cs | 5 +-
.../GroupFacetCollectorTest.cs | 3 +-
src/Lucene.Net.Tests.Grouping/TestGrouping.cs | 3 +-
.../VectorHighlight/FieldQueryTest.cs | 63 ++++++++++++-----
src/Lucene.Net.Tests/Support/TestHashMap.cs | 66 ++++++++++--------
src/Lucene.Net.Tests/Support/TestLinkedHashMap.cs | 80 ----------------------
.../Support/TestWeakDictionaryBehavior.cs | 12 ++--
src/Lucene.Net/Search/FieldCacheImpl.cs | 6 +-
src/Lucene.Net/Search/SloppyPhraseScorer.cs | 5 +-
.../Util/Automaton/DaciukMihovAutomatonBuilder.cs | 3 +-
src/Lucene.Net/Util/RamUsageEstimator.cs | 24 +++++--
12 files changed, 118 insertions(+), 155 deletions(-)
diff --git a/src/Lucene.Net.Facet/Taxonomy/CachedOrdinalsReader.cs b/src/Lucene.Net.Facet/Taxonomy/CachedOrdinalsReader.cs
index 9e2cbd0..ee03c7d 100644
--- a/src/Lucene.Net.Facet/Taxonomy/CachedOrdinalsReader.cs
+++ b/src/Lucene.Net.Facet/Taxonomy/CachedOrdinalsReader.cs
@@ -77,8 +77,7 @@ namespace Lucene.Net.Facet.Taxonomy
lock (this)
{
object cacheKey = context.Reader.CoreCacheKey;
- CachedOrds ords = ordsCache[cacheKey];
- if (ords == null)
+ if (!ordsCache.TryGetValue(cacheKey, out CachedOrds ords) || ords == null)
{
ords = new CachedOrds(source.GetReader(context), context.Reader.MaxDoc);
ordsCache[cacheKey] = ords;
diff --git a/src/Lucene.Net.Highlighter/Highlight/QueryScorer.cs b/src/Lucene.Net.Highlighter/Highlight/QueryScorer.cs
index d44a446..08228ab 100644
--- a/src/Lucene.Net.Highlighter/Highlight/QueryScorer.cs
+++ b/src/Lucene.Net.Highlighter/Highlight/QueryScorer.cs
@@ -114,9 +114,8 @@ namespace Lucene.Net.Search.Highlight
foreach (WeightedSpanTerm t in weightedTerms)
{
- WeightedSpanTerm existingTerm = fieldWeightedSpanTerms[t.Term];
-
- if ((existingTerm == null) ||
+ if (!fieldWeightedSpanTerms.TryGetValue(t.Term, out WeightedSpanTerm existingTerm) ||
+ (existingTerm == null) ||
(existingTerm.Weight < t.Weight))
{
// if a term is defined more than once, always use the highest
diff --git a/src/Lucene.Net.Tests.Grouping/GroupFacetCollectorTest.cs b/src/Lucene.Net.Tests.Grouping/GroupFacetCollectorTest.cs
index dd2605c..55befcd 100644
--- a/src/Lucene.Net.Tests.Grouping/GroupFacetCollectorTest.cs
+++ b/src/Lucene.Net.Tests.Grouping/GroupFacetCollectorTest.cs
@@ -809,8 +809,7 @@ namespace Lucene.Net.Search.Grouping
// Only include null count when no facet prefix is specified
if (facetPrefix == null)
{
- ISet<string> groups = facetGroups[null];
- if (groups != null)
+ if (facetGroups.TryGetValue(null, out ISet<string> groups) && groups != null)
{
totalMissCount = groups.size();
}
diff --git a/src/Lucene.Net.Tests.Grouping/TestGrouping.cs b/src/Lucene.Net.Tests.Grouping/TestGrouping.cs
index 16881aa..ca4d5e8 100644
--- a/src/Lucene.Net.Tests.Grouping/TestGrouping.cs
+++ b/src/Lucene.Net.Tests.Grouping/TestGrouping.cs
@@ -588,8 +588,7 @@ namespace Lucene.Net.Search.Grouping
}
}
- List<GroupDoc> l = groups[d.group];
- if (l == null)
+ if (!groups.TryGetValue(d.group, out List<GroupDoc> l) || l == null)
{
//Console.WriteLine(" add sortedGroup=" + groupToString(d.group));
sortedGroups.Add(d.group);
diff --git a/src/Lucene.Net.Tests.Highlighter/VectorHighlight/FieldQueryTest.cs b/src/Lucene.Net.Tests.Highlighter/VectorHighlight/FieldQueryTest.cs
index da0884c..bb50289 100644
--- a/src/Lucene.Net.Tests.Highlighter/VectorHighlight/FieldQueryTest.cs
+++ b/src/Lucene.Net.Tests.Highlighter/VectorHighlight/FieldQueryTest.cs
@@ -313,13 +313,16 @@ namespace Lucene.Net.Search.VectorHighlight
[Test]
public void TestQueryPhraseMap1Term()
{
+ // LUCENENET specific - altered some of the tests because
+ // dictionaries throw KeyNotFoundException rather than returning null.
+
Query query = tq("a");
// phraseHighlight = true, fieldMatch = true
FieldQuery fq = new FieldQuery(query, true, true);
IDictionary<String, QueryPhraseMap> map = fq.rootMaps;
assertEquals(1, map.size());
- assertNull(map[null]);
+ assertFalse(map.TryGetValue(null, out _)); // assertNull(map[null]);
assertNotNull(map[F]);
QueryPhraseMap qpm = map[F];
assertEquals(1, qpm.subMap.size());
@@ -331,7 +334,7 @@ namespace Lucene.Net.Search.VectorHighlight
fq = new FieldQuery(query, true, false);
map = fq.rootMaps;
assertEquals(1, map.size());
- assertNull(map[F]);
+ assertFalse(map.TryGetValue(F, out _)); // assertNull(map[F]);
assertNotNull(map[null]);
qpm = map[null];
assertEquals(1, qpm.subMap.size());
@@ -343,7 +346,7 @@ namespace Lucene.Net.Search.VectorHighlight
fq = new FieldQuery(query, false, true);
map = fq.rootMaps;
assertEquals(1, map.size());
- assertNull(map[null]);
+ assertFalse(map.TryGetValue(null, out _)); // assertNull(map[null]);
assertNotNull(map[F]);
qpm = map[F];
assertEquals(1, qpm.subMap.size());
@@ -355,7 +358,7 @@ namespace Lucene.Net.Search.VectorHighlight
fq = new FieldQuery(query, false, false);
map = fq.rootMaps;
assertEquals(1, map.size());
- assertNull(map[F]);
+ assertFalse(map.TryGetValue(F, out _)); // assertNull(map[F]);
assertNotNull(map[null]);
qpm = map[null];
assertEquals(1, qpm.subMap.size());
@@ -374,13 +377,16 @@ namespace Lucene.Net.Search.VectorHighlight
[Test]
public void TestQueryPhraseMap1Phrase()
{
+ // LUCENENET specific - altered some of the tests because
+ // dictionaries throw KeyNotFoundException rather than returning null.
+
Query query = pqF("a", "b");
// phraseHighlight = true, fieldMatch = true
FieldQuery fq = new FieldQuery(query, true, true);
IDictionary<String, QueryPhraseMap> map = fq.rootMaps;
assertEquals(1, map.size());
- assertNull(map[null]);
+ assertFalse(map.TryGetValue(null, out _)); //assertNull(map[null]);
assertNotNull(map[F]);
QueryPhraseMap qpm = map[F];
assertEquals(1, qpm.subMap.size());
@@ -397,7 +403,7 @@ namespace Lucene.Net.Search.VectorHighlight
fq = new FieldQuery(query, true, false);
map = fq.rootMaps;
assertEquals(1, map.size());
- assertNull(map[F]);
+ assertFalse(map.TryGetValue(F, out _)); //assertNull(map[F]);
assertNotNull(map[null]);
qpm = map[null];
assertEquals(1, qpm.subMap.size());
@@ -414,7 +420,7 @@ namespace Lucene.Net.Search.VectorHighlight
fq = new FieldQuery(query, false, true);
map = fq.rootMaps;
assertEquals(1, map.size());
- assertNull(map[null]);
+ assertFalse(map.TryGetValue(null, out _)); // assertNull(map[null]);
assertNotNull(map[F]);
qpm = map[F];
assertEquals(2, qpm.subMap.size());
@@ -437,7 +443,7 @@ namespace Lucene.Net.Search.VectorHighlight
fq = new FieldQuery(query, false, false);
map = fq.rootMaps;
assertEquals(1, map.size());
- assertNull(map[F]);
+ assertFalse(map.TryGetValue(F, out _)); // assertNull(map[F]);
assertNotNull(map[null]);
qpm = map[null];
assertEquals(2, qpm.subMap.size());
@@ -473,13 +479,16 @@ namespace Lucene.Net.Search.VectorHighlight
[Test]
public void TestQueryPhraseMap1PhraseAnother()
{
+ // LUCENENET specific - altered some of the tests because
+ // dictionaries throw KeyNotFoundException rather than returning null.
+
Query query = pqF("search", "engines");
// phraseHighlight = true, fieldMatch = true
FieldQuery fq = new FieldQuery(query, true, true);
IDictionary<String, QueryPhraseMap> map = fq.rootMaps;
assertEquals(1, map.size());
- assertNull(map[null]);
+ assertFalse(map.TryGetValue(null, out _)); // assertNull(map[null]);
assertNotNull(map[F]);
QueryPhraseMap qpm = map[F];
assertEquals(1, qpm.subMap.size());
@@ -496,6 +505,9 @@ namespace Lucene.Net.Search.VectorHighlight
[Test]
public void TestQueryPhraseMap2Phrases()
{
+ // LUCENENET specific - altered some of the tests because
+ // dictionaries throw KeyNotFoundException rather than returning null.
+
BooleanQuery query = new BooleanQuery();
query.Add(pqF("a", "b"), Occur.SHOULD);
query.Add(pqF(2, "c", "d"), Occur.SHOULD);
@@ -504,7 +516,7 @@ namespace Lucene.Net.Search.VectorHighlight
FieldQuery fq = new FieldQuery(query, true, true);
IDictionary<String, QueryPhraseMap> map = fq.rootMaps;
assertEquals(1, map.size());
- assertNull(map[null]);
+ assertFalse(map.TryGetValue(null, out _)); // assertNull(map[null]);
assertNotNull(map[F]);
QueryPhraseMap qpm = map[F];
assertEquals(2, qpm.subMap.size());
@@ -533,6 +545,9 @@ namespace Lucene.Net.Search.VectorHighlight
[Test]
public void TestQueryPhraseMap2PhrasesFields()
{
+ // LUCENENET specific - altered some of the tests because
+ // dictionaries throw KeyNotFoundException rather than returning null.
+
BooleanQuery query = new BooleanQuery();
query.Add(pq(F1, "a", "b"), Occur.SHOULD);
query.Add(pq(2F, F2, "c", "d"), Occur.SHOULD);
@@ -541,7 +556,7 @@ namespace Lucene.Net.Search.VectorHighlight
FieldQuery fq = new FieldQuery(query, true, true);
IDictionary<String, QueryPhraseMap> map = fq.rootMaps;
assertEquals(2, map.size());
- assertNull(map[null]);
+ assertFalse(map.TryGetValue(null, out _)); // assertNull(map[null]);
// "a b"
assertNotNull(map[F1]);
@@ -573,8 +588,8 @@ namespace Lucene.Net.Search.VectorHighlight
fq = new FieldQuery(query, true, false);
map = fq.rootMaps;
assertEquals(1, map.size());
- assertNull(map[F1]);
- assertNull(map[F2]);
+ assertFalse(map.TryGetValue(F1, out _)); // assertNull(map[F1]);
+ assertFalse(map.TryGetValue(F2, out _)); // assertNull(map[F2]);
assertNotNull(map[null]);
qpm = map[null];
assertEquals(2, qpm.subMap.size());
@@ -611,6 +626,9 @@ namespace Lucene.Net.Search.VectorHighlight
[Test]
public void TestQueryPhraseMapOverlapPhrases()
{
+ // LUCENENET specific - altered some of the tests because
+ // dictionaries throw KeyNotFoundException rather than returning null.
+
BooleanQuery query = new BooleanQuery();
query.Add(pqF("a", "b", "c"), Occur.SHOULD);
query.Add(pqF(2, "b", "c", "d"), Occur.SHOULD);
@@ -620,7 +638,7 @@ namespace Lucene.Net.Search.VectorHighlight
FieldQuery fq = new FieldQuery(query, true, true);
IDictionary<String, QueryPhraseMap> map = fq.rootMaps;
assertEquals(1, map.size());
- assertNull(map[null]);
+ assertFalse(map.TryGetValue(null, out _)); // assertNull(map[null]);
assertNotNull(map[F]);
QueryPhraseMap qpm = map[F];
assertEquals(2, qpm.subMap.size());
@@ -671,6 +689,9 @@ namespace Lucene.Net.Search.VectorHighlight
[Test]
public void TestQueryPhraseMapOverlapPhrases2()
{
+ // LUCENENET specific - altered some of the tests because
+ // dictionaries throw KeyNotFoundException rather than returning null.
+
BooleanQuery query = new BooleanQuery();
query.Add(pqF("a", "b"), Occur.SHOULD);
query.Add(pqF(2, "a", "b", "c"), Occur.SHOULD);
@@ -679,7 +700,7 @@ namespace Lucene.Net.Search.VectorHighlight
FieldQuery fq = new FieldQuery(query, true, true);
IDictionary<String, QueryPhraseMap> map = fq.rootMaps;
assertEquals(1, map.size());
- assertNull(map[null]);
+ assertFalse(map.TryGetValue(null, out _)); // assertNull(map[null]);
assertNotNull(map[F]);
QueryPhraseMap qpm = map[F];
assertEquals(1, qpm.subMap.size());
@@ -713,6 +734,9 @@ namespace Lucene.Net.Search.VectorHighlight
[Test]
public void TestQueryPhraseMapOverlapPhrases3()
{
+ // LUCENENET specific - altered some of the tests because
+ // dictionaries throw KeyNotFoundException rather than returning null.
+
BooleanQuery query = new BooleanQuery();
query.Add(pqF("a", "a", "a", "a"), Occur.SHOULD);
query.Add(pqF(2, "a", "a", "a"), Occur.SHOULD);
@@ -721,7 +745,7 @@ namespace Lucene.Net.Search.VectorHighlight
FieldQuery fq = new FieldQuery(query, true, true);
IDictionary<String, QueryPhraseMap> map = fq.rootMaps;
assertEquals(1, map.size());
- assertNull(map[null]);
+ assertFalse(map.TryGetValue(null, out _)); // assertNull(map[null]);
assertNotNull(map[F]);
QueryPhraseMap qpm = map[F];
assertEquals(1, qpm.subMap.size());
@@ -761,6 +785,9 @@ namespace Lucene.Net.Search.VectorHighlight
[Test]
public void TestQueryPhraseMapOverlap2gram()
{
+ // LUCENENET specific - altered some of the tests because
+ // dictionaries throw KeyNotFoundException rather than returning null.
+
BooleanQuery query = new BooleanQuery();
query.Add(toPhraseQuery(analyze("abc", F, analyzerB), F), Occur.MUST);
query.Add(toPhraseQuery(analyze("bcd", F, analyzerB), F), Occur.MUST);
@@ -769,7 +796,7 @@ namespace Lucene.Net.Search.VectorHighlight
FieldQuery fq = new FieldQuery(query, true, true);
IDictionary<String, QueryPhraseMap> map = fq.rootMaps;
assertEquals(1, map.size());
- assertNull(map[null]);
+ assertFalse(map.TryGetValue(null, out _)); // assertNull(map[null]);
assertNotNull(map[F]);
QueryPhraseMap qpm = map[F];
assertEquals(2, qpm.subMap.size());
@@ -805,7 +832,7 @@ namespace Lucene.Net.Search.VectorHighlight
fq = new FieldQuery(query, false, true);
map = fq.rootMaps;
assertEquals(1, map.size());
- assertNull(map[null]);
+ assertFalse(map.TryGetValue(null, out _)); // assertNull(map[null]);
assertNotNull(map[F]);
qpm = map[F];
assertEquals(3, qpm.subMap.size());
diff --git a/src/Lucene.Net.Tests/Support/TestHashMap.cs b/src/Lucene.Net.Tests/Support/TestHashMap.cs
index 7707e4c..a70121b 100644
--- a/src/Lucene.Net.Tests/Support/TestHashMap.cs
+++ b/src/Lucene.Net.Tests/Support/TestHashMap.cs
@@ -116,7 +116,7 @@ namespace Lucene.Net.Support
{
var dict = GetDefaultHashMap1();
Assert.IsFalse(dict.ContainsKey(null));
- Assert.IsNull(dict[null]);
+ Assert.IsFalse(dict.TryGetValue(null, out _));
dict.Add(null, "value");
Assert.IsTrue(dict.ContainsKey(null));
@@ -151,35 +151,41 @@ namespace Lucene.Net.Support
Assert.AreEqual(2, dict.Count);
}
- [Test, LuceneNetSpecific]
- public virtual void TestGetWithNonExistantKey_EmptyCollection()
- {
- var dict = GetNewHashMap<string, string>();
- Assert.IsNull(dict["nothing"]);
- Assert.IsNull(dict[null]);
- }
-
- [Test, LuceneNetSpecific]
- public virtual void TestGetWithNonExistantKey()
- {
- var dict = GetDefaultHashMap1();
- Assert.IsNull(dict["nothing"]);
- Assert.IsNull(dict[null]);
- }
-
- [Test, LuceneNetSpecific]
- public virtual void TestAddsUpdate_NotThrowException()
- {
- var dict = GetNewHashMap<string, string>();
-
- dict.Add("key1", "value1");
- Assert.AreEqual("value1", dict["key1"]);
- Assert.AreEqual(1, dict.Count);
-
- dict.Add("key1", "value2");
- Assert.AreEqual("value2", dict["key1"], "Value was not updated by Add!");
- Assert.AreEqual(1, dict.Count);
- }
+ // LUCENENET NOTE: The original "java like" functionality was replaced with a standard
+ // dictionary that accepts null keys. The following tests will not pass because we should be
+ // using TryGetValue and ContainsKey to test whether a value exists. The problem with this
+ // approach is that it does not support value types and also does not distinguish between
+ // a null value in a KeyValuePair or a missing KeyValuePair.
+
+ //[Test, LuceneNetSpecific]
+ //public virtual void TestGetWithNonExistantKey_EmptyCollection()
+ //{
+ // var dict = GetNewHashMap<string, string>();
+ // Assert.IsNull(dict["nothing"]);
+ // Assert.IsNull(dict[null]);
+ //}
+
+ //[Test, LuceneNetSpecific]
+ //public virtual void TestGetWithNonExistantKey()
+ //{
+ // var dict = GetDefaultHashMap1();
+ // Assert.IsNull(dict["nothing"]);
+ // Assert.IsNull(dict[null]);
+ //}
+
+ //[Test, LuceneNetSpecific]
+ //public virtual void TestAddsUpdate_NotThrowException()
+ //{
+ // var dict = GetNewHashMap<string, string>();
+
+ // dict.Add("key1", "value1");
+ // Assert.AreEqual("value1", dict["key1"]);
+ // Assert.AreEqual(1, dict.Count);
+
+ // dict.Add("key1", "value2");
+ // Assert.AreEqual("value2", dict["key1"], "Value was not updated by Add!");
+ // Assert.AreEqual(1, dict.Count);
+ //}
[Test, LuceneNetSpecific]
public virtual void TestIndexersUpdate_NotThrowException()
diff --git a/src/Lucene.Net.Tests/Support/TestLinkedHashMap.cs b/src/Lucene.Net.Tests/Support/TestLinkedHashMap.cs
index f103307..73875e2 100644
--- a/src/Lucene.Net.Tests/Support/TestLinkedHashMap.cs
+++ b/src/Lucene.Net.Tests/Support/TestLinkedHashMap.cs
@@ -355,85 +355,5 @@ namespace Lucene.Net.Support
dict.Remove(nullTarget);
Assert.IsFalse(dict.Contains(nullTarget));
}
-
-
- #region TestHashMap
- // LUCENENET NOTE: Tests in a base class are not pulled into the correct
- // context in Visual Studio. This fixes that with the minimum amount of code necessary
- // to run them in the correct context without duplicating all of the tests.
-
- [Test, LuceneNetSpecific]
- public override void TestKeyEnumeration()
- {
- base.TestKeyEnumeration();
- }
-
- [Test, LuceneNetSpecific]
- public override void TestValueEnumeration()
- {
- base.TestValueEnumeration();
- }
-
- [Test, LuceneNetSpecific]
- public override void TestKeyValuePairEnumeration()
- {
- base.TestKeyValuePairEnumeration();
- }
-
- [Test, LuceneNetSpecific]
- public override void TestContainsNullKey()
- {
- base.TestContainsNullKey();
- }
-
- [Test, LuceneNetSpecific]
- public override void TestContainsKey()
- {
- base.TestContainsKey();
- }
-
- [Test, LuceneNetSpecific]
- public override void TestAdd_NoNullKeys_NullValues()
- {
- base.TestAdd_NoNullKeys_NullValues();
- }
-
- [Test, LuceneNetSpecific]
- public override void TestAdd_WithNullKeys_NoNullValues()
- {
- base.TestAdd_WithNullKeys_NoNullValues();
- }
-
- [Test, LuceneNetSpecific]
- public override void TestGetWithNonExistantKey_EmptyCollection()
- {
- base.TestGetWithNonExistantKey_EmptyCollection();
- }
-
- [Test, LuceneNetSpecific]
- public override void TestGetWithNonExistantKey()
- {
- base.TestGetWithNonExistantKey();
- }
-
- [Test, LuceneNetSpecific]
- public override void TestAddsUpdate_NotThrowException()
- {
- base.TestAddsUpdate_NotThrowException();
- }
-
- [Test, LuceneNetSpecific]
- public override void TestIndexersUpdate_NotThrowException()
- {
- base.TestIndexersUpdate_NotThrowException();
- }
-
- [Test, LuceneNetSpecific]
- public override void TestWithValueType()
- {
- base.TestWithValueType();
- }
-
- #endregion
}
}
diff --git a/src/Lucene.Net.Tests/Support/TestWeakDictionaryBehavior.cs b/src/Lucene.Net.Tests/Support/TestWeakDictionaryBehavior.cs
index ce6ac5a..d4c31c2 100644
--- a/src/Lucene.Net.Tests/Support/TestWeakDictionaryBehavior.cs
+++ b/src/Lucene.Net.Tests/Support/TestWeakDictionaryBehavior.cs
@@ -127,7 +127,8 @@ namespace Lucene.Net.Support
Assert.AreEqual(0, dictionary.Count);
Assert.IsFalse(dictionary.ContainsKey(key));
- Assert.IsNull(dictionary[key]);
+ Assert.IsFalse(dictionary.TryGetValue(key, out _));
+ Assert.Throws<KeyNotFoundException>(() => { var x = dictionary[key]; });
}
[Test, LuceneNetSpecific]
@@ -140,7 +141,8 @@ namespace Lucene.Net.Support
Assert.AreEqual(0, dictionary.Count);
Assert.IsFalse(dictionary.ContainsKey(key));
- Assert.IsNull(dictionary[key]);
+ Assert.IsFalse(dictionary.TryGetValue(key, out _));
+ Assert.Throws<KeyNotFoundException>(() => { var x = dictionary[key]; });
}
[Test, LuceneNetSpecific]
@@ -154,7 +156,8 @@ namespace Lucene.Net.Support
Assert.AreEqual(0, dictionary.Count);
Assert.IsFalse(dictionary.ContainsKey(key));
- Assert.IsNull(dictionary[key]);
+ Assert.IsFalse(dictionary.TryGetValue(key, out _));
+ Assert.Throws<KeyNotFoundException>(() => { var x = dictionary[key]; });
}
[Test, LuceneNetSpecific]
@@ -211,7 +214,8 @@ namespace Lucene.Net.Support
Assert.AreEqual("value2", dictionary[key2]);
dictionary.Remove(key1);
- Assert.AreEqual(null, dictionary[key1]);
+ Assert.IsFalse(dictionary.TryGetValue(key1, out _));
+ Assert.Throws<KeyNotFoundException>(() => { var x = dictionary[key1]; });
}
[Test, LuceneNetSpecific]
diff --git a/src/Lucene.Net/Search/FieldCacheImpl.cs b/src/Lucene.Net/Search/FieldCacheImpl.cs
index 3d9ebac..2345c84 100644
--- a/src/Lucene.Net/Search/FieldCacheImpl.cs
+++ b/src/Lucene.Net/Search/FieldCacheImpl.cs
@@ -229,8 +229,7 @@ namespace Lucene.Net.Search
object readerKey = reader.CoreCacheKey;
lock (readerCache)
{
- IDictionary<CacheKey, object> innerCache = readerCache[readerKey];
- if (innerCache == null)
+ if (!readerCache.TryGetValue(readerKey, out IDictionary<CacheKey, object> innerCache) || innerCache == null)
{
// First time this reader is using FieldCache
innerCache = new Dictionary<CacheKey, object>();
@@ -241,8 +240,7 @@ namespace Lucene.Net.Search
// don't overwrite value variable with the null
// that will result when this if block succeeds; otherwise
// we won't have a value to put in the cache.
- object temp;
- if (!innerCache.TryGetValue(key, out temp))
+ if (!innerCache.TryGetValue(key, out object temp))
{
innerCache[key] = value;
}
diff --git a/src/Lucene.Net/Search/SloppyPhraseScorer.cs b/src/Lucene.Net/Search/SloppyPhraseScorer.cs
index 564064d..093f86d 100644
--- a/src/Lucene.Net/Search/SloppyPhraseScorer.cs
+++ b/src/Lucene.Net/Search/SloppyPhraseScorer.cs
@@ -593,9 +593,10 @@ namespace Lucene.Net.Search
{
FixedBitSet b = new FixedBitSet(tord.Count);
var ord = new int?();
- foreach (var t in pp.terms.Where(t => (ord = tord[t]) != null))
+ foreach (var t in pp.terms)
{
- b.Set((int)ord);
+ if (tord.TryGetValue(t, out ord) && ord != null)
+ b.Set((int)ord);
}
bb.Add(b);
}
diff --git a/src/Lucene.Net/Util/Automaton/DaciukMihovAutomatonBuilder.cs b/src/Lucene.Net/Util/Automaton/DaciukMihovAutomatonBuilder.cs
index 681c78b..96c4294 100644
--- a/src/Lucene.Net/Util/Automaton/DaciukMihovAutomatonBuilder.cs
+++ b/src/Lucene.Net/Util/Automaton/DaciukMihovAutomatonBuilder.cs
@@ -274,8 +274,7 @@ namespace Lucene.Net.Util.Automaton
/// </summary>
private static Util.Automaton.State Convert(State s, IdentityHashMap<State, Lucene.Net.Util.Automaton.State> visited)
{
- Util.Automaton.State converted = visited[s];
- if (converted != null)
+ if (visited.TryGetValue(s, out Util.Automaton.State converted) && converted != null)
{
return converted;
}
diff --git a/src/Lucene.Net/Util/RamUsageEstimator.cs b/src/Lucene.Net/Util/RamUsageEstimator.cs
index b337910..7edbd44 100644
--- a/src/Lucene.Net/Util/RamUsageEstimator.cs
+++ b/src/Lucene.Net/Util/RamUsageEstimator.cs
@@ -42,15 +42,15 @@ namespace Lucene.Net.Util
/// <summary>
/// One kilobyte bytes. </summary>
- public static readonly long ONE_KB = 1024;
+ public const long ONE_KB = 1024;
/// <summary>
/// One megabyte bytes. </summary>
- public static readonly long ONE_MB = ONE_KB * ONE_KB;
+ public const long ONE_MB = ONE_KB * ONE_KB;
/// <summary>
/// One gigabyte bytes. </summary>
- public static readonly long ONE_GB = ONE_KB * ONE_MB;
+ public const long ONE_GB = ONE_KB * ONE_MB;
/// <summary>
/// No instantiation. </summary>
@@ -552,8 +552,7 @@ namespace Lucene.Net.Util
*/
try
{
- ClassCache cachedInfo = classCache[obClazz];
- if (cachedInfo == null)
+ if (!classCache.TryGetValue(obClazz, out ClassCache cachedInfo) || cachedInfo == null)
{
classCache[obClazz] = cachedInfo = CreateCacheEntry(obClazz);
}
@@ -631,7 +630,11 @@ namespace Lucene.Net.Util
private static long AdjustForField(long sizeSoFar, FieldInfo f)
{
Type type = f.FieldType;
- int fsize = type.GetTypeInfo().IsPrimitive ? primitiveSizes[type] : NUM_BYTES_OBJECT_REF;
+ int fsize = 0;
+
+ if (!typeof(IntPtr).Equals(type) && !typeof(UIntPtr).Equals(type))
+ fsize = type.GetTypeInfo().IsPrimitive ? primitiveSizes[type] : NUM_BYTES_OBJECT_REF;
+
// LUCENENET NOTE: I dont think this will ever not be null
//if (ObjectFieldOffsetMethod != null)
//{
@@ -694,6 +697,15 @@ namespace Lucene.Net.Util
}
/// <summary>
+ /// Return a human-readable size of a given object. </summary>
+ /// <seealso cref="SizeOf(object)"/>
+ /// <seealso cref="HumanReadableUnits(long)"/>
+ public static string HumanSizeOf(object @object, IFormatProvider df)
+ {
+ return HumanReadableUnits(SizeOf(@object), df);
+ }
+
+ /// <summary>
/// An identity hash set implemented using open addressing. No null keys are allowed.
/// <para/>
/// TODO: If this is useful outside this class, make it public - needs some work