Skip to content

Commit

Permalink
refactor: Fix nullable and some XML warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
nikcio committed Aug 16, 2023
1 parent 3b44ba3 commit d6d07a4
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 41 deletions.
19 changes: 15 additions & 4 deletions src/Examine.Lucene/ExamineTaxonomyReplicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Lucene.Net.Replicator;
using Lucene.Net.Store;
using Microsoft.Extensions.Logging;
using static Lucene.Net.Documents.Field;
using static Lucene.Net.Replicator.IndexAndTaxonomyRevision;
using Directory = Lucene.Net.Store.Directory;

Expand Down Expand Up @@ -142,12 +143,22 @@ public void StartIndexReplicationOnSchedule(int milliseconds)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SourceIndex_IndexCommitted(object sender, EventArgs e)
private void SourceIndex_IndexCommitted(object? sender, EventArgs? e)
{
var index = (LuceneIndex)sender;
if (_logger.IsEnabled(LogLevel.Debug))
if(sender == null)
{
_logger.LogDebug("{IndexName} committed", index.Name);
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug("SourceIndex_IndexCommitted was called with the sender parameter being null");
}
}
else
{
var index = (LuceneIndex)sender;
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug("{IndexName} committed", index.Name);
}
}
var rev = new IndexAndTaxonomyRevision(_sourceIndex.IndexWriter.IndexWriter, _sourceIndex.TaxonomyWriter as SnapshotDirectoryTaxonomyWriter);
_replicator.Publish(rev);
Expand Down
5 changes: 3 additions & 2 deletions src/Examine.Lucene/Indexing/DateTimeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ public DateTimeType(string fieldName, ILoggerFactory logger, DateResolution reso
_isFacetable = false;
}

public override void AddValue(Document doc, object value)
/// <inheritdoc/>
public override void AddValue(Document doc, object? value)
{
// Support setting taxonomy path
if (_isFacetable && _taxonomyIndex && value is object[] objArr && objArr != null && objArr.Length == 2)
{
if (!TryConvert(objArr[0], out DateTime parsedVal))
return;
if (!TryConvert(objArr[1], out string[] parsedPathVal))
if (!TryConvert(objArr[1], out string[]? parsedPathVal))
return;

var val = DateToLong(parsedVal);
Expand Down
4 changes: 2 additions & 2 deletions src/Examine.Lucene/Indexing/DoubleType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ public DoubleType(string fieldName, ILoggerFactory logger, bool store = true)
public bool IsTaxonomyFaceted => _taxonomyIndex;

/// <inheritdoc/>
public override void AddValue(Document doc, object value)
public override void AddValue(Document doc, object? value)
{
// Support setting taxonomy path
if (_isFacetable && _taxonomyIndex && value is object[] objArr && objArr != null && objArr.Length == 2)
{
if (!TryConvert(objArr[0], out double parsedVal))
return;
if (!TryConvert(objArr[1], out string[] parsedPathVal))
if (!TryConvert(objArr[1], out string[]? parsedPathVal))
return;

doc.Add(new DoubleField(FieldName, parsedVal, Store ? Field.Store.YES : Field.Store.NO));
Expand Down
7 changes: 4 additions & 3 deletions src/Examine.Lucene/Indexing/FullTextType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ public FullTextType(string fieldName, ILoggerFactory logger, Analyzer? analyzer
public bool IsTaxonomyFaceted => _taxonomyIndex;

/// <inheritdoc/>
public override void AddValue(Document doc, object value)
public override void AddValue(Document doc, object? value)
{
// Support setting taxonomy path
if (_isFacetable && _taxonomyIndex && value is object[] objArr && objArr != null && objArr.Length == 2)
{
if (!TryConvert(objArr[0], out string str))
if (!TryConvert(objArr[0], out string? str))
return;
if (!TryConvert(objArr[1], out string[] parsedPathVal))
if (!TryConvert(objArr[1], out string[]? parsedPathVal))
return;

doc.Add(new TextField(FieldName, str, Field.Store.YES));
Expand All @@ -111,6 +111,7 @@ public override void AddValue(Document doc, object value)
base.AddValue(doc, value);
}

/// <inheritdoc/>
protected override void AddSingleValue(Document doc, object value)
{
if (TryConvert<string>(value, out var str))
Expand Down
5 changes: 3 additions & 2 deletions src/Examine.Lucene/Indexing/Int32Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ public Int32Type(string fieldName, ILoggerFactory logger, bool store = true)
public bool IsTaxonomyFaceted => _taxonomyIndex;

/// <inheritdoc/>
public override void AddValue(Document doc, object value)
public override void AddValue(Document doc, object? value)
{
// Support setting taxonomy path
if (_isFacetable && _taxonomyIndex && value is object[] objArr && objArr != null && objArr.Length == 2)
{
if (!TryConvert(objArr[0], out int parsedVal))
return;
if (!TryConvert(objArr[1], out string[] parsedPathVal))
if (!TryConvert(objArr[1], out string[]? parsedPathVal))
return;

doc.Add(new Int32Field(FieldName, parsedVal, Store ? Field.Store.YES : Field.Store.NO));
Expand All @@ -65,6 +65,7 @@ public override void AddValue(Document doc, object value)
base.AddValue(doc, value);
}

/// <inheritdoc/>
protected override void AddSingleValue(Document doc, object value)
{
if (!TryConvert(value, out int parsedVal))
Expand Down
5 changes: 3 additions & 2 deletions src/Examine.Lucene/Indexing/Int64Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ public Int64Type(string fieldName, ILoggerFactory logger, bool store = true)
public bool IsTaxonomyFaceted => _taxonomyIndex;

/// <inheritdoc/>
public override void AddValue(Document doc, object value)
public override void AddValue(Document doc, object? value)
{
// Support setting taxonomy path
if (_isFacetable && _taxonomyIndex && value is object[] objArr && objArr != null && objArr.Length == 2)
{
if (!TryConvert(objArr[0], out long parsedVal))
return;
if (!TryConvert(objArr[1], out string[] parsedPathVal))
if (!TryConvert(objArr[1], out string[]? parsedPathVal))
return;

doc.Add(new Int64Field(FieldName, parsedVal, Store ? Field.Store.YES : Field.Store.NO));
Expand All @@ -65,6 +65,7 @@ public override void AddValue(Document doc, object value)
base.AddValue(doc, value);
}

/// <inheritdoc/>
protected override void AddSingleValue(Document doc, object value)
{
if (!TryConvert(value, out long parsedVal))
Expand Down
5 changes: 3 additions & 2 deletions src/Examine.Lucene/Indexing/SingleType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ public SingleType(string fieldName, ILoggerFactory logger, bool store = true)
public bool IsTaxonomyFaceted => _taxonomyIndex;

/// <inheritdoc/>
public override void AddValue(Document doc, object value)
public override void AddValue(Document doc, object? value)
{
// Support setting taxonomy path
if (_isFacetable && _taxonomyIndex && value is object[] objArr && objArr != null && objArr.Length == 2)
{
if (!TryConvert(objArr[0], out float parsedVal))
return;
if (!TryConvert(objArr[1], out string[] parsedPathVal))
if (!TryConvert(objArr[1], out string[]? parsedPathVal))
return;

doc.Add(new DoubleField(FieldName, parsedVal, Store ? Field.Store.YES : Field.Store.NO));
Expand All @@ -66,6 +66,7 @@ public override void AddValue(Document doc, object value)
base.AddValue(doc, value);
}

/// <inheritdoc/>
protected override void AddSingleValue(Document doc, object value)
{
if (!TryConvert(value, out float parsedVal))
Expand Down
35 changes: 21 additions & 14 deletions src/Examine.Lucene/Providers/LuceneIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected LuceneIndex(
string name,
IOptionsMonitor<LuceneDirectoryIndexOptions> indexOptions,
Func<LuceneIndex, IIndexCommiter> indexCommiterFactory,
IndexWriter writer = null)
IndexWriter? writer = null)
: base(loggerFactory, name, indexOptions)
{
_options = indexOptions.GetNamedOptions(name);
Expand Down Expand Up @@ -190,7 +190,7 @@ internal LuceneIndex(
/// <summary>
/// Gets a Taxonomy searcher for the index
/// </summary>
public virtual ILuceneTaxonomySearcher TaxonomySearcher => _taxonomySearcher.Value;
public virtual ILuceneTaxonomySearcher? TaxonomySearcher => _taxonomySearcher?.Value;

/// <summary>
/// The async task that runs during an async indexing operation
Expand All @@ -212,11 +212,11 @@ internal LuceneIndex(
// tracks the latest Generation value of what has been indexed.This can be used to force update a searcher to this generation.
private long? _latestGen;

private volatile DirectoryTaxonomyWriter _taxonomyWriter;
private ControlledRealTimeReopenThread<SearcherTaxonomyManager.SearcherAndTaxonomy> _taxonomyNrtReopenThread;
private volatile DirectoryTaxonomyWriter? _taxonomyWriter;
private ControlledRealTimeReopenThread<SearcherTaxonomyManager.SearcherAndTaxonomy>? _taxonomyNrtReopenThread;

private readonly Lazy<LuceneTaxonomySearcher> _taxonomySearcher;
private readonly Lazy<Directory> _taxonomyDirectory;
private readonly Lazy<LuceneTaxonomySearcher>? _taxonomySearcher;
private readonly Lazy<Directory>? _taxonomyDirectory;

#region Properties

Expand Down Expand Up @@ -428,6 +428,12 @@ public void EnsureIndex(bool forceOverwrite)
{
//Now create the taxonomy index
var taxonomyDir = GetLuceneTaxonomyDirectory();

if(taxonomyDir == null)
{
throw new InvalidOperationException($"{Name} is configured to use a taxonomy index but the directory is null");
}

CreateNewTaxonomyIndex(taxonomyDir);
}
}
Expand Down Expand Up @@ -539,7 +545,7 @@ private void CreateNewIndex(Directory? dir)
/// </summary>
private void CreateNewTaxonomyIndex(Directory dir)
{
DirectoryTaxonomyWriter writer = null;
DirectoryTaxonomyWriter? writer = null;
try
{
if (IsLocked(dir))
Expand Down Expand Up @@ -779,7 +785,7 @@ private bool IndexExistsImpl()
return _exists.Value;
}

// <summary>
/// <summary>
/// This will check one time if the taxonomny index exists, we don't want to keep using IndexReader.IndexExists because that will literally go list
/// every file in the index folder and we don't need any more IO ops
/// </summary>
Expand Down Expand Up @@ -1082,7 +1088,7 @@ private bool ProcessQueueItem(IndexOperation item)
/// Returns the Lucene Directory used to store the taxonomy index
/// </summary>
/// <returns></returns>
public Directory GetLuceneTaxonomyDirectory() => _taxonomyWriter != null ? _taxonomyWriter.Directory : _taxonomyDirectory.Value;
public Directory? GetLuceneTaxonomyDirectory() => _taxonomyWriter != null ? _taxonomyWriter.Directory : _taxonomyDirectory?.Value;


/// <summary>
Expand Down Expand Up @@ -1211,9 +1217,9 @@ public TrackingIndexWriter IndexWriter
/// Used to create an index writer - this is called in GetIndexWriter (and therefore, GetIndexWriter should not be overridden)
/// </summary>
/// <returns></returns>
private DirectoryTaxonomyWriter CreateTaxonomyWriterInternal()
private DirectoryTaxonomyWriter? CreateTaxonomyWriterInternal()
{
Directory dir = GetLuceneTaxonomyDirectory();
Directory? dir = GetLuceneTaxonomyDirectory();

// Unfortunatley if the appdomain is taken down this will remain locked, so we can
// ensure that it's unlocked here in that case.
Expand Down Expand Up @@ -1245,7 +1251,7 @@ private DirectoryTaxonomyWriter CreateTaxonomyWriterInternal()
/// </summary>
/// <param name="d"></param>
/// <returns></returns>
protected virtual DirectoryTaxonomyWriter CreateTaxonomyWriter(Directory d)
protected virtual DirectoryTaxonomyWriter CreateTaxonomyWriter(Directory? d)
{
if (d == null)
{
Expand Down Expand Up @@ -1279,7 +1285,7 @@ public DirectoryTaxonomyWriter TaxonomyWriter

}

return _taxonomyWriter;
return _taxonomyWriter; // TODO: should this throw when null
}
}

Expand Down Expand Up @@ -1558,7 +1564,8 @@ protected virtual void Dispose(bool disposing)
_nrtReopenThread.Dispose();
}

if (_taxonomyNrtReopenThread != null)
// The type of _taxonomyNrtReopenThread has overriden the != operator and expects a non null value to compare the references. Therefore we use is not null instead of != null.
if (_taxonomyNrtReopenThread is not null)
{
_taxonomyNrtReopenThread.Interrupt();
_taxonomyNrtReopenThread.Dispose();
Expand Down
3 changes: 3 additions & 0 deletions src/Examine.Lucene/Providers/LuceneTaxonomySearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace Examine.Lucene.Providers
{
/// <summary>
/// A searcher for taxonomy indexes
/// </summary>
public class LuceneTaxonomySearcher : BaseLuceneSearcher, IDisposable, ILuceneTaxonomySearcher
{
private readonly SearcherTaxonomyManager _searcherManager;
Expand Down
2 changes: 1 addition & 1 deletion src/Examine.Lucene/Search/LuceneFacetLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public LuceneFacetLabel(FacetLabel facetLabel)
public int Length => _facetLabel.Length;

/// <inheritdoc/>
public int CompareTo(Examine.Search.IFacetLabel other) => _facetLabel.CompareTo(new FacetLabel(other.Components));
public int CompareTo(Examine.Search.IFacetLabel? other) => _facetLabel.CompareTo(new FacetLabel(other?.Components));

/// <inheritdoc/>
public Examine.Search.IFacetLabel Subpath(int length) => new LuceneFacetLabel(_facetLabel.Subpath(length));
Expand Down
4 changes: 2 additions & 2 deletions src/Examine.Lucene/Search/LuceneSearchExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ private IReadOnlyDictionary<string, IFacetResult> ExtractFacets(FacetsCollector?

var facetFields = _facetFields.OrderBy(field => field.FacetField);

SortedSetDocValuesReaderState sortedSetReaderState = null;
Facets fastTaxonomyFacetCounts = null;
SortedSetDocValuesReaderState? sortedSetReaderState = null;
Facets? fastTaxonomyFacetCounts = null;


foreach (var field in facetFields)
Expand Down
8 changes: 4 additions & 4 deletions src/Examine.Lucene/Search/LuceneSearchQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ internal IFacetOperations FacetInternal(string field, Action<IFacetQueryField>?

var valueType = _searchContext.GetFieldValueType(field) as IIndexFacetValueType;

var facet = new FacetFullTextField(field, values, GetFacetField(field), isTaxonomyIndexed: valueType.IsTaxonomyFaceted);
var facet = new FacetFullTextField(field, values, GetFacetField(field), isTaxonomyIndexed: valueType?.IsTaxonomyFaceted ?? false);

if(facetConfiguration != null)
{
Expand All @@ -377,7 +377,7 @@ internal IFacetOperations FacetInternal(string field, params DoubleRange[] doubl
}

var valueType = _searchContext.GetFieldValueType(field) as IIndexFacetValueType;
var facet = new FacetDoubleField(field, doubleRanges, GetFacetField(field), isTaxonomyIndexed: valueType.IsTaxonomyFaceted);
var facet = new FacetDoubleField(field, doubleRanges, GetFacetField(field), isTaxonomyIndexed: valueType?.IsTaxonomyFaceted ?? false);

_facetFields.Add(facet);

Expand All @@ -392,7 +392,7 @@ internal IFacetOperations FacetInternal(string field, params FloatRange[] floatR
}

var valueType = _searchContext.GetFieldValueType(field) as IIndexFacetValueType;
var facet = new FacetFloatField(field, floatRanges, GetFacetField(field), isTaxonomyIndexed: valueType.IsTaxonomyFaceted);
var facet = new FacetFloatField(field, floatRanges, GetFacetField(field), isTaxonomyIndexed: valueType?.IsTaxonomyFaceted ?? false);

_facetFields.Add(facet);

Expand All @@ -407,7 +407,7 @@ internal IFacetOperations FacetInternal(string field, params Int64Range[] longRa
}

var valueType = _searchContext.GetFieldValueType(field) as IIndexFacetValueType;
var facet = new FacetLongField(field, longRanges, GetFacetField(field), isTaxonomyIndexed: valueType.IsTaxonomyFaceted);
var facet = new FacetLongField(field, longRanges, GetFacetField(field), isTaxonomyIndexed: valueType?.IsTaxonomyFaceted ?? false);

_facetFields.Add(facet);

Expand Down
2 changes: 1 addition & 1 deletion src/Examine.Lucene/Search/LuceneSearchResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class LuceneSearchResults : ILuceneSearchResults, IFacetResults
/// <summary>
/// Gets an empty search result
/// </summary>
public static LuceneSearchResults Empty { get; } = new LuceneSearchResults(Array.Empty<ISearchResult>(), 0, new Dictionary<string, IFacetResult>(), float.NaN, default);
public static LuceneSearchResults Empty { get; } = new LuceneSearchResults(Array.Empty<ISearchResult>(), 0, new Dictionary<string, IFacetResult>(), float.NaN, null);

private readonly IReadOnlyCollection<ISearchResult> _results;

Expand Down
2 changes: 1 addition & 1 deletion src/Examine.Lucene/Search/TaxonomySearchContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class TaxonomySearchContext : ITaxonomySearchContext
{
private readonly SearcherTaxonomyManager _searcherManager;
private readonly FieldValueTypeCollection _fieldValueTypeCollection;
private string[] _searchableFields;
private string[]? _searchableFields;

/// <summary>
/// Constructor
Expand Down
2 changes: 1 addition & 1 deletion src/Examine.Test/ExamineBaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public TestIndex GetTestIndex(IndexWriter writer)
writer);
}

public TestIndex GetTaxonomyTestIndex(Directory d, Directory taxonomyDirectory, Analyzer analyzer, FieldDefinitionCollection fieldDefinitions = null, IndexDeletionPolicy indexDeletionPolicy = null, IReadOnlyDictionary<string, IFieldValueTypeFactory> indexValueTypesFactory = null, FacetsConfig? facetsConfig = null)
public TestIndex GetTaxonomyTestIndex(Directory d, Directory taxonomyDirectory, Analyzer analyzer, FieldDefinitionCollection fieldDefinitions = null, IndexDeletionPolicy indexDeletionPolicy = null, IReadOnlyDictionary<string, IFieldValueTypeFactory> indexValueTypesFactory = null, FacetsConfig facetsConfig = null)
{
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Debug));
return new TestIndex(
Expand Down

0 comments on commit d6d07a4

Please sign in to comment.