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 Jul 28, 2023
1 parent 0bc86e5 commit f0e72bb
Show file tree
Hide file tree
Showing 17 changed files with 81 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/Examine.Host/ServicesCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ public static IServiceCollection AddExamineLuceneMultiSearcher(
this IServiceCollection serviceCollection,
string name,
string[] indexNames,
Analyzer analyzer = null,
FacetsConfig facetsConfig = null)
Analyzer? analyzer = null,
FacetsConfig? facetsConfig = null)
=> serviceCollection.AddExamineSearcher<MultiIndexSearcher>(name, s =>
{
IEnumerable<IIndex> matchedIndexes = s.GetServices<IIndex>()
Expand Down
9 changes: 7 additions & 2 deletions src/Examine.Lucene/Directories/DirectoryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ namespace Examine.Lucene.Directories
public class GenericDirectoryFactory : DirectoryFactoryBase
{
private readonly Func<string, Directory> _factory;
private readonly Func<string, Directory> _taxonomyDirectoryFactory;
private readonly Func<string, Directory>? _taxonomyDirectoryFactory;

/// <inheritdoc/>
public GenericDirectoryFactory(Func<string, Directory> factory, Func<string, Directory> taxonomyDirectoryFactory = null)
public GenericDirectoryFactory(Func<string, Directory> factory, Func<string, Directory>? taxonomyDirectoryFactory = null)
{
_factory = factory;
_taxonomyDirectoryFactory = taxonomyDirectoryFactory;
Expand All @@ -31,6 +31,11 @@ protected override Directory CreateDirectory(LuceneIndex luceneIndex, bool force
/// <inheritdoc/>
protected override Directory CreateTaxonomyDirectory(LuceneIndex luceneIndex, bool forceUnlock)
{
if(_taxonomyDirectoryFactory == null)
{
throw new InvalidOperationException("No taxonomy directory factory was specified");
}

Directory dir = _taxonomyDirectoryFactory(luceneIndex.Name + "taxonomy");
if (forceUnlock)
{
Expand Down
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 @@ -49,14 +49,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 @@ -42,14 +42,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 @@ -81,14 +81,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 @@ -108,6 +108,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 @@ -42,14 +42,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 @@ -61,6 +61,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 @@ -42,14 +42,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 @@ -61,6 +61,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 @@ -43,14 +43,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 @@ -62,6 +62,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
10 changes: 5 additions & 5 deletions src/Examine.Lucene/Search/LuceneSearchExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class LuceneSearchExecutor
private int? _maxDoc;

internal LuceneSearchExecutor(QueryOptions? options, Query query, IEnumerable<SortField> sortField, ISearchContext searchContext,
ISet<string> fieldsToLoad, IEnumerable<IFacetField> facetFields, FacetsConfig facetsConfig)
ISet<string>? fieldsToLoad, IEnumerable<IFacetField> facetFields, FacetsConfig facetsConfig)
{
_options = options ?? QueryOptions.Default;
_luceneQueryOptions = _options as LuceneQueryOptions;
Expand Down Expand Up @@ -131,7 +131,7 @@ public ISearchResults Execute()
{
topDocsCollector = TopScoreDocCollector.Create(numHits, scoreDocAfter, true);
}
FacetsCollector facetsCollector = null;
FacetsCollector? facetsCollector = null;
if (_facetFields.Any() && _luceneQueryOptions != null && _luceneQueryOptions.FacetRandomSampling != null)
{
var facetsCollectors = new RandomSamplingFacetsCollector(_luceneQueryOptions.FacetRandomSampling.SampleSize, _luceneQueryOptions.FacetRandomSampling.Seed);
Expand Down Expand Up @@ -216,7 +216,7 @@ private static FieldDoc GetScoreDocAfter(SearchAfterOptions searchAfterOptions)
return scoreDocAfter;
}

private static SearchAfterOptions GetSearchAfterOptions(TopDocs topDocs)
private static SearchAfterOptions? GetSearchAfterOptions(TopDocs topDocs)
{
if (topDocs.TotalHits > 0)
{
Expand All @@ -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
Loading

0 comments on commit f0e72bb

Please sign in to comment.