Skip to content

Commit

Permalink
BREAKING: Removed (more) unnecessary nullable value types (see #574 and
Browse files Browse the repository at this point in the history
#581) (#583)

* BREAKING: Lucene.Net.Queries.Function.ValueSources.EnumFieldSource: Removed nullable value types from public API

* Lucene.Net.Queries: Removed unnecessary nullable value types

* SWEEP: Removed several unnecessary nullable value type declarations in the test framework and in tests

* BREAKING: Lucene.Net.QueryParsers.Flexible: Removed unnecessary nullable value types from ConfigurationKeys and configuration setters/getters in StandardQueryParser. Added AbstractQueryConfig.TryGetValue() method to allow retrieving value types so they can be defaulted properly.
  • Loading branch information
NightOwl888 authored Dec 20, 2021
1 parent 55ebd1e commit 93cd1e9
Show file tree
Hide file tree
Showing 52 changed files with 267 additions and 268 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public override bool Equals(object o)

public override int GetHashCode()
{
int h = m_parser == null ? typeof(double?).GetHashCode() : m_parser.GetType().GetHashCode();
int h = m_parser == null ? typeof(double).GetHashCode() : m_parser.GetType().GetHashCode();
h += base.GetHashCode();
return h;
}
Expand Down
34 changes: 14 additions & 20 deletions src/Lucene.Net.Queries/Function/ValueSources/EnumFieldSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ namespace Lucene.Net.Queries.Function.ValueSources
/// <summary>
/// Obtains <see cref="int"/> field values from <see cref="IFieldCache.GetInt32s(AtomicReader, string, FieldCache.IInt32Parser, bool)"/> and makes
/// those values available as other numeric types, casting as needed.
/// StrVal of the value is not the <see cref="int"/> value, but its <see cref="string"/> (displayed) value
/// StrVal of the value is not the <see cref="int"/> value, but its <see cref="string"/> (displayed) value.
/// </summary>
public class EnumFieldSource : FieldCacheSource
{
private const int DEFAULT_VALUE = -1;

private readonly FieldCache.IInt32Parser parser;
private readonly IDictionary<int?, string> enumIntToStringMap;
private readonly IDictionary<string, int?> enumStringToIntMap;
private readonly IDictionary<int, string> enumIntToStringMap;
private readonly IDictionary<string, int> enumStringToIntMap;

public EnumFieldSource(string field, FieldCache.IInt32Parser parser, IDictionary<int?, string> enumIntToStringMap, IDictionary<string, int?> enumStringToIntMap)
public EnumFieldSource(string field, FieldCache.IInt32Parser parser, IDictionary<int, string> enumIntToStringMap, IDictionary<string, int> enumStringToIntMap)
: base(field)
{
this.parser = parser;
Expand All @@ -54,15 +54,11 @@ public EnumFieldSource(string field, FieldCache.IInt32Parser parser, IDictionary
/// <summary>
/// NOTE: This was intValueToStringValue() in Lucene
/// </summary>
private string Int32ValueToStringValue(int? intVal)
private string Int32ValueToStringValue(int intVal)
{
if (intVal == null)
{
return null;
}
// LUCENENET: null value not applicable for value types (it defaults to 0 anyway)

string enumString = enumIntToStringMap[intVal];
if (enumString != null)
if (enumIntToStringMap.TryGetValue(intVal, out string enumString))
{
return enumString;
}
Expand All @@ -80,8 +76,7 @@ private string Int32ValueToStringValue(int? intVal)
return null;
}

int? enumInt = enumStringToIntMap[stringVal];
if (enumInt != null) //enum int found for str
if (enumStringToIntMap.TryGetValue(stringVal, out int enumInt)) //enum int found for str
{
return enumInt;
}
Expand All @@ -91,8 +86,7 @@ private string Int32ValueToStringValue(int? intVal)
{
intValue = DEFAULT_VALUE;
}
string enumString = enumIntToStringMap[intValue];
if (enumString != null) //has matching str
if (enumIntToStringMap.ContainsKey(intValue)) //has matching str
{
return intValue;
}
Expand Down Expand Up @@ -163,7 +157,7 @@ public override double DoubleVal(int doc)

public override string StrVal(int doc)
{
int? intValue = arr.Get(doc);
int intValue = arr.Get(doc);
return outerInstance.Int32ValueToStringValue(intValue);
}

Expand Down Expand Up @@ -255,11 +249,11 @@ public override bool Equals(object o)

// LUCENENET specific: must use DictionaryEqualityComparer.Equals() to ensure values
// contained within the dictionaries are compared for equality
if (!JCG.DictionaryEqualityComparer<int?, string>.Default.Equals(enumIntToStringMap, that.enumIntToStringMap))
if (!JCG.DictionaryEqualityComparer<int, string>.Default.Equals(enumIntToStringMap, that.enumIntToStringMap))
{
return false;
}
if (!JCG.DictionaryEqualityComparer<string, int?>.Default.Equals(enumStringToIntMap, that.enumStringToIntMap))
if (!JCG.DictionaryEqualityComparer<string, int>.Default.Equals(enumStringToIntMap, that.enumStringToIntMap))
{
return false;
}
Expand All @@ -277,8 +271,8 @@ public override int GetHashCode()
result = 31 * result + parser.GetHashCode();
// LUCENENET specific: must use DictionaryEqualityComparer.GetHashCode() to ensure values
// contained within the dictionaries are compared for equality
result = 31 * result + JCG.DictionaryEqualityComparer<int?, string>.Default.GetHashCode(enumIntToStringMap);
result = 31 * result + JCG.DictionaryEqualityComparer<string, int?>.Default.GetHashCode(enumStringToIntMap);
result = 31 * result + JCG.DictionaryEqualityComparer<int, string>.Default.GetHashCode(enumIntToStringMap);
result = 31 * result + JCG.DictionaryEqualityComparer<string, int>.Default.GetHashCode(enumStringToIntMap);
return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public override bool Equals(object o)

public override int GetHashCode()
{
int h = m_parser == null ? typeof(float?).GetHashCode() : m_parser.GetType().GetHashCode();
int h = m_parser == null ? typeof(float).GetHashCode() : m_parser.GetType().GetHashCode();
h += base.GetHashCode();
return h;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public override bool Equals(object o)

public override int GetHashCode()
{
int h = parser == null ? typeof(int?).GetHashCode() : parser.GetType().GetHashCode();
int h = parser == null ? typeof(int).GetHashCode() : parser.GetType().GetHashCode();
h += base.GetHashCode();
return h;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public override bool Equals(object o)

public override int GetHashCode()
{
var h = parser == null ? typeof(short?).GetHashCode() : parser.GetType().GetHashCode();
var h = parser == null ? typeof(short).GetHashCode() : parser.GetType().GetHashCode();
h += base.GetHashCode();
return h;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@ internal AbstractQueryConfig()
// although this class is public, it can only be constructed from package
}

/// <summary>
/// Gets the value associated with the specified key.
/// </summary>
/// <typeparam name="T">the value's type</typeparam>
/// <param name="key">the key, cannot be <c>null</c></param>
/// <param name="value">When this method returns, contains the value associated with the specified key,
/// if the key is found; otherwise, the default value for the type of the <paramref name="value"/> parameter.
/// This parameter is passed uninitialized.</param>
/// <returns><c>true</c> if the configuration contains an element with the specified <paramref name="key"/>; otherwise, <c>false</c>.</returns>
// LUCENENET specific - using this method allows us to store non-nullable value types
public virtual bool TryGetValue<T>(ConfigurationKey<T> key, out T value)
{
if (key is null)
throw new ArgumentNullException(nameof(key), "key cannot be null!");
if (this.configMap.TryGetValue(key, out object resultObj))
{
if (typeof(T).IsValueType)
value = ((T[])resultObj)[0]; // LUCENENET: Retrieve a 1 dimensionsal array for value types to avoid unboxing
else
value = (T)resultObj;
return true;
}
value = default;
return false;
}

/// <summary>
/// Returns the value held by the given key.
/// </summary>
Expand All @@ -50,8 +76,9 @@ public virtual T Get<T>(ConfigurationKey<T> key)
{
throw new ArgumentNullException(nameof(key), "key cannot be null!"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentNullException (.NET convention)
}
this.configMap.TryGetValue(key, out object result);
return result == null ? default : (T)result;
return !this.configMap.TryGetValue(key, out object result) || result is null ? default :
// LUCENENET: Retrieve a 1 dimensionsal array for value types to avoid unboxing
(typeof(T).IsValueType ? ((T[])result)[0] : (T)result);
}

/// <summary>
Expand Down Expand Up @@ -87,6 +114,10 @@ public virtual void Set<T>(ConfigurationKey<T> key, T value)
{
Unset(key);
}
else if (typeof(T).IsValueType)
{
this.configMap[key] = new T[] { value }; // LUCENENET: Store a 1 dimensionsal array for value types to avoid boxing
}
else
{
this.configMap[key] = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,9 @@ public FieldBoostMapFCListener(QueryConfigHandler config)

public virtual void BuildFieldConfig(FieldConfig fieldConfig)
{
IDictionary<string, float?> fieldBoostMap = this.config.Get(ConfigurationKeys.FIELD_BOOST_MAP);

if (fieldBoostMap != null)
{
if (fieldBoostMap.TryGetValue(fieldConfig.Field, out float? boost) && boost != null)
{
if (this.config.TryGetValue(ConfigurationKeys.FIELD_BOOST_MAP, out IDictionary<string, float> fieldBoostMap)
&& fieldBoostMap.TryGetValue(fieldConfig.Field, out float boost))
fieldConfig.Set(ConfigurationKeys.BOOST, boost);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,12 @@ public FieldDateResolutionFCListener(QueryConfigHandler config)

public virtual void BuildFieldConfig(FieldConfig fieldConfig)
{
DateResolution? dateRes = null;
IDictionary<string, DateResolution?> dateResMap = this.config.Get(ConfigurationKeys.FIELD_DATE_RESOLUTION_MAP);

if (dateResMap != null)
{
dateResMap.TryGetValue(fieldConfig.Field, out dateRes);
}

if (dateRes == null)
{
dateRes = this.config.Get(ConfigurationKeys.DATE_RESOLUTION);
}

if (dateRes != null)
// LUCENENET: Simplified logic using TryGetValue
if ((this.config.TryGetValue(ConfigurationKeys.FIELD_DATE_RESOLUTION_MAP, out IDictionary<string, DateResolution> dateResMap)
&& dateResMap.TryGetValue(fieldConfig.Field, out DateResolution dateRes))
|| this.config.TryGetValue(ConfigurationKeys.DATE_RESOLUTION, out dateRes))
{
fieldConfig.Set(ConfigurationKeys.DATE_RESOLUTION, dateRes.Value);
fieldConfig.Set(ConfigurationKeys.DATE_RESOLUTION, dateRes);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ public StandardQueryConfigHandler()
Set(ConfigurationKeys.PHRASE_SLOP, 0); //default value 2.4
Set(ConfigurationKeys.LOWERCASE_EXPANDED_TERMS, true); //default value 2.4
Set(ConfigurationKeys.ENABLE_POSITION_INCREMENTS, false); //default value 2.4
Set(ConfigurationKeys.FIELD_BOOST_MAP, new JCG.LinkedDictionary<string, float?>());
Set(ConfigurationKeys.FIELD_BOOST_MAP, new JCG.LinkedDictionary<string, float>());
Set(ConfigurationKeys.FUZZY_CONFIG, new FuzzyConfig());
Set(ConfigurationKeys.LOCALE, null);
Set(ConfigurationKeys.MULTI_TERM_REWRITE_METHOD, MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT);
Set(ConfigurationKeys.FIELD_DATE_RESOLUTION_MAP, new JCG.Dictionary<string, DateResolution?>());
Set(ConfigurationKeys.FIELD_DATE_RESOLUTION_MAP, new JCG.Dictionary<string, DateResolution>());
}

/// <summary>
Expand All @@ -76,19 +76,19 @@ public sealed class ConfigurationKeys
/// Key used to set whether position increments is enabled
/// </summary>
/// <seealso cref="StandardQueryParser.EnablePositionIncrements"/>
public readonly static ConfigurationKey<bool?> ENABLE_POSITION_INCREMENTS = ConfigurationKey.NewInstance<bool?>();
public readonly static ConfigurationKey<bool> ENABLE_POSITION_INCREMENTS = ConfigurationKey.NewInstance<bool>();

/// <summary>
/// Key used to set whether expanded terms should be lower-cased
/// </summary>
/// <seealso cref="StandardQueryParser.LowercaseExpandedTerms"/>
public readonly static ConfigurationKey<bool?> LOWERCASE_EXPANDED_TERMS = ConfigurationKey.NewInstance<bool?>();
public readonly static ConfigurationKey<bool> LOWERCASE_EXPANDED_TERMS = ConfigurationKey.NewInstance<bool>();

/// <summary>
/// Key used to set whether leading wildcards are supported
/// </summary>
/// <seealso cref="StandardQueryParser.AllowLeadingWildcard"/>
public readonly static ConfigurationKey<bool?> ALLOW_LEADING_WILDCARD = ConfigurationKey.NewInstance<bool?>();
public readonly static ConfigurationKey<bool> ALLOW_LEADING_WILDCARD = ConfigurationKey.NewInstance<bool>();

/// <summary>
/// Key used to set the <see cref="Analyzer"/> used for terms found in the query
Expand All @@ -106,7 +106,7 @@ public sealed class ConfigurationKeys
/// Key used to set the default phrase slop
/// </summary>
/// <seealso cref="StandardQueryParser.PhraseSlop"/>
public readonly static ConfigurationKey<int?> PHRASE_SLOP = ConfigurationKey.NewInstance<int?>();
public readonly static ConfigurationKey<int> PHRASE_SLOP = ConfigurationKey.NewInstance<int>();

/// <summary>
/// Key used to set the <see cref="CultureInfo">locale</see> used when parsing the query
Expand Down Expand Up @@ -134,14 +134,14 @@ public sealed class ConfigurationKeys
/// Key used to set a field to boost map that is used to set the boost for each field
/// </summary>
/// <seealso cref="StandardQueryParser.FieldsBoost"/>
public readonly static ConfigurationKey<IDictionary<string, float?>> FIELD_BOOST_MAP = ConfigurationKey.NewInstance<IDictionary<string, float?>>();
public readonly static ConfigurationKey<IDictionary<string, float>> FIELD_BOOST_MAP = ConfigurationKey.NewInstance<IDictionary<string, float>>();

/// <summary>
/// Key used to set a field to <see cref="DateResolution"/> map that is used
/// to normalize each date field value.
/// </summary>
/// <seealso cref="StandardQueryParser.DateResolutionMap"/>
public readonly static ConfigurationKey<IDictionary<string, DateResolution?>> FIELD_DATE_RESOLUTION_MAP = ConfigurationKey.NewInstance<IDictionary<string, DateResolution?>>();
public readonly static ConfigurationKey<IDictionary<string, DateResolution>> FIELD_DATE_RESOLUTION_MAP = ConfigurationKey.NewInstance<IDictionary<string, DateResolution>>();

/// <summary>
/// Key used to set the <see cref="FuzzyConfig"/> used to create fuzzy queries.
Expand All @@ -161,7 +161,7 @@ public sealed class ConfigurationKeys
/// Key used to set the boost value in <see cref="FieldConfig"/> objects.
/// </summary>
/// <seealso cref="StandardQueryParser.FieldsBoost"/>
public readonly static ConfigurationKey<float?> BOOST = ConfigurationKey.NewInstance<float?>();
public readonly static ConfigurationKey<float> BOOST = ConfigurationKey.NewInstance<float>();

/// <summary>
/// Key used to set a field to its <see cref="NumericConfig"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,22 @@ public AnalyzerQueryNodeProcessor()

public override IQueryNode Process(IQueryNode queryTree)
{
Analyzer analyzer = GetQueryConfigHandler().Get(ConfigurationKeys.ANALYZER);
var queryConfigHandler = GetQueryConfigHandler();
Analyzer analyzer = queryConfigHandler.Get(ConfigurationKeys.ANALYZER);

if (analyzer != null)
{
this.analyzer = analyzer;
this.positionIncrementsEnabled = false;
bool? positionIncrementsEnabled = GetQueryConfigHandler().Get(ConfigurationKeys.ENABLE_POSITION_INCREMENTS);

// LUCENENET specific - rather than using null, we are relying on the behavior that the default
// value for an enum is 0 (OR in this case).
//var defaultOperator = GetQueryConfigHandler().Get(ConfigurationKeys.DEFAULT_OPERATOR);
//this.defaultOperator = defaultOperator != null ? defaultOperator.Value : Operator.OR;
this.defaultOperator = GetQueryConfigHandler().Get(ConfigurationKeys.DEFAULT_OPERATOR);

if (positionIncrementsEnabled != null)
// LUCENENET: Use TryGetValue() to determine if the value exists
if (GetQueryConfigHandler().TryGetValue(ConfigurationKeys.ENABLE_POSITION_INCREMENTS, out bool positionIncrementsEnabled))
{
this.positionIncrementsEnabled = positionIncrementsEnabled.Value;
this.positionIncrementsEnabled = positionIncrementsEnabled;
}

if (this.analyzer != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ protected override IQueryNode PostProcessNode(IQueryNode node)

if (fieldConfig != null)
{
float? boost = fieldConfig.Get(ConfigurationKeys.BOOST);

if (boost != null)
if (fieldConfig.TryGetValue(ConfigurationKeys.BOOST, out float boost))
{
return new BoostQueryNode(node, boost.Value);
return new BoostQueryNode(node, boost);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ public override IQueryNode Process(IQueryNode queryTree)

if (queryConfig != null)
{
int? defaultPhraseSlop = queryConfig.Get(ConfigurationKeys.PHRASE_SLOP);

if (defaultPhraseSlop != null)
if (queryConfig.TryGetValue(ConfigurationKeys.PHRASE_SLOP, out int defaultPhraseSlop))
{
this.defaultPhraseSlop = defaultPhraseSlop.Value;
this.defaultPhraseSlop = defaultPhraseSlop;

return base.Process(queryTree);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ public LowercaseExpandedTermsQueryNodeProcessor()

public override IQueryNode Process(IQueryNode queryTree)
{
bool? lowercaseExpandedTerms = GetQueryConfigHandler().Get(ConfigurationKeys.LOWERCASE_EXPANDED_TERMS);

if (lowercaseExpandedTerms != null && lowercaseExpandedTerms.Value)
if (GetQueryConfigHandler().TryGetValue(ConfigurationKeys.LOWERCASE_EXPANDED_TERMS, out bool lowercaseExpandedTerms)
&& lowercaseExpandedTerms)
{
return base.Process(queryTree);
}
Expand Down
Loading

0 comments on commit 93cd1e9

Please sign in to comment.