Skip to content

Commit 152c397

Browse files
committed
More nullable fixes and code cleanup, use TaskCanceledException instead of OperationCanceledException
1 parent c660b52 commit 152c397

6 files changed

Lines changed: 92 additions & 66 deletions

File tree

Directory.Build.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
Condition=" $(TargetFramework.StartsWith('net8.')) Or $(TargetFramework.StartsWith('net9.')) ">
5252

5353
<DefineConstants>$(DefineConstants);FEATURE_ASPNETCORE_TESTHOST</DefineConstants>
54+
<DefineConstants>$(DefineConstants);FEATURE_CANCELLATIONTOKENSOURCE_CANCELASYNC</DefineConstants>
5455

5556
</PropertyGroup>
5657

src/Lucene.Net.Replicator/IAsyncReplicator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public interface IAsyncReplicator
1818
/// <param name="currentVersion">Current version of the index.</param>
1919
/// <param name="cancellationToken">Cancellation token.</param>
2020
/// <returns>A <see cref="SessionToken"/> if an update exists; otherwise, <c>null</c>.</returns>
21-
Task<SessionToken?> CheckForUpdateAsync(string currentVersion, CancellationToken cancellationToken = default);
21+
Task<SessionToken?> CheckForUpdateAsync(string? currentVersion, CancellationToken cancellationToken = default);
2222

2323
/// <summary>
2424
/// Returns a stream for the requested file and source.

src/Lucene.Net.Replicator/IndexAndTaxonomyReplicationHandler.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using System.IO;
77
using Directory = Lucene.Net.Store.Directory;
88

9+
#nullable enable
10+
911
namespace Lucene.Net.Replicator
1012
{
1113
/*
@@ -50,17 +52,17 @@ public class IndexAndTaxonomyReplicationHandler : IReplicationHandler
5052

5153
private readonly Directory indexDirectory;
5254
private readonly Directory taxonomyDirectory;
53-
private readonly Action callback;
55+
private readonly Action? callback;
5456

55-
private volatile IDictionary<string, IList<RevisionFile>> currentRevisionFiles;
56-
private volatile string currentVersion;
57+
private volatile IDictionary<string, IList<RevisionFile>>? currentRevisionFiles;
58+
private volatile string? currentVersion;
5759
private volatile InfoStream infoStream = InfoStream.Default;
5860

5961
/// <summary>
6062
/// Constructor with the given index directory and callback to notify when the indexes were updated.
6163
/// </summary>
6264
/// <exception cref="IOException"></exception>
63-
public IndexAndTaxonomyReplicationHandler(Directory indexDirectory, Directory taxonomyDirectory, Action callback)
65+
public IndexAndTaxonomyReplicationHandler(Directory indexDirectory, Directory taxonomyDirectory, Action? callback)
6466
{
6567
this.indexDirectory = indexDirectory;
6668
this.taxonomyDirectory = taxonomyDirectory;
@@ -73,24 +75,24 @@ public IndexAndTaxonomyReplicationHandler(Directory indexDirectory, Directory ta
7375
bool taxonomyExists = DirectoryReader.IndexExists(taxonomyDirectory);
7476

7577
if (indexExists != taxonomyExists)
76-
throw IllegalStateException.Create(string.Format("search and taxonomy indexes must either both exist or not: index={0} taxo={1}", indexExists, taxonomyExists));
78+
throw IllegalStateException.Create($"search and taxonomy indexes must either both exist or not: index={indexExists} taxo={taxonomyExists}");
7779

7880
if (indexExists)
7981
{
80-
IndexCommit indexCommit = IndexReplicationHandler.GetLastCommit(indexDirectory);
81-
IndexCommit taxonomyCommit = IndexReplicationHandler.GetLastCommit(taxonomyDirectory);
82+
IndexCommit? indexCommit = IndexReplicationHandler.GetLastCommit(indexDirectory);
83+
IndexCommit? taxonomyCommit = IndexReplicationHandler.GetLastCommit(taxonomyDirectory);
8284

8385
currentRevisionFiles = IndexAndTaxonomyRevision.RevisionFiles(indexCommit, taxonomyCommit);
8486
currentVersion = IndexAndTaxonomyRevision.RevisionVersion(indexCommit, taxonomyCommit);
8587

8688
WriteToInfoStream(
87-
string.Format("constructor(): currentVersion={0} currentRevisionFiles={1}", currentVersion, currentRevisionFiles),
88-
string.Format("constructor(): indexCommit={0} taxoCommit={1}", indexCommit, taxonomyCommit));
89+
$"constructor(): currentVersion={currentVersion} currentRevisionFiles={currentRevisionFiles}",
90+
$"constructor(): indexCommit={indexCommit} taxoCommit={taxonomyCommit}");
8991
}
9092
}
9193

92-
public virtual string CurrentVersion => currentVersion;
93-
public virtual IDictionary<string, IList<RevisionFile>> CurrentRevisionFiles => currentRevisionFiles;
94+
public virtual string? CurrentVersion => currentVersion;
95+
public virtual IDictionary<string, IList<RevisionFile>>? CurrentRevisionFiles => currentRevisionFiles;
9496

9597
public virtual void RevisionReady(string version,
9698
IDictionary<string, IList<RevisionFile>> revisionFiles,
@@ -101,8 +103,8 @@ public virtual void RevisionReady(string version,
101103
Directory indexClientDirectory = sourceDirectory[IndexAndTaxonomyRevision.INDEX_SOURCE];
102104
IList<string> taxonomyFiles = copiedFiles[IndexAndTaxonomyRevision.TAXONOMY_SOURCE];
103105
IList<string> indexFiles = copiedFiles[IndexAndTaxonomyRevision.INDEX_SOURCE];
104-
string taxonomySegmentsFile = IndexReplicationHandler.GetSegmentsFile(taxonomyFiles, true);
105-
string indexSegmentsFile = IndexReplicationHandler.GetSegmentsFile(indexFiles, false);
106+
string? taxonomySegmentsFile = IndexReplicationHandler.GetSegmentsFile(taxonomyFiles, true);
107+
string indexSegmentsFile = IndexReplicationHandler.GetSegmentsFile(indexFiles, false)!; // [!]: verified by GetSegmentsFile
106108

107109
bool success = false;
108110
try
@@ -132,7 +134,11 @@ public virtual void RevisionReady(string version,
132134
{
133135
if (!success)
134136
{
135-
taxonomyFiles.Add(taxonomySegmentsFile); // add it back so it gets deleted too
137+
if (taxonomySegmentsFile != null) // LUCENENET specific - null check
138+
{
139+
taxonomyFiles.Add(taxonomySegmentsFile); // add it back so it gets deleted too
140+
}
141+
136142
IndexReplicationHandler.CleanupFilesOnFailure(taxonomyDirectory, taxonomyFiles);
137143
indexFiles.Add(indexSegmentsFile); // add it back so it gets deleted too
138144
IndexReplicationHandler.CleanupFilesOnFailure(indexDirectory, indexFiles);
@@ -188,6 +194,7 @@ private void WriteToInfoStream(params string[] messages)
188194
public virtual InfoStream InfoStream
189195
{
190196
get => infoStream;
197+
// ReSharper disable once NullCoalescingConditionIsAlwaysNotNullAccordingToAPIContract
191198
set => infoStream = value ?? InfoStream.NO_OUTPUT;
192199
}
193200
}

src/Lucene.Net.Replicator/IndexReplicationHandler.cs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using JCG = J2N.Collections.Generic;
1010
using Directory = Lucene.Net.Store.Directory;
1111

12+
#nullable enable
13+
1214
namespace Lucene.Net.Replicator
1315
{
1416
/*
@@ -57,10 +59,10 @@ public class IndexReplicationHandler : IReplicationHandler
5759
public const string INFO_STREAM_COMPONENT = "IndexReplicationHandler";
5860

5961
private readonly Directory indexDirectory;
60-
private readonly Action callback;
62+
private readonly Action? callback;
6163

62-
private volatile IDictionary<string, IList<RevisionFile>> currentRevisionFiles;
63-
private volatile string currentVersion;
64+
private volatile IDictionary<string, IList<RevisionFile>>? currentRevisionFiles;
65+
private volatile string? currentVersion;
6466
private volatile InfoStream infoStream;
6567

6668
//Note: LUCENENET Specific Utility Method
@@ -78,7 +80,7 @@ private void WriteToInfoStream(params string[] messages)
7880
/// <c>null</c> if there are no commits.
7981
/// </summary>
8082
/// <exception cref="IOException"></exception>
81-
public static IndexCommit GetLastCommit(Directory directory)
83+
public static IndexCommit? GetLastCommit(Directory directory)
8284
{
8385
try
8486
{
@@ -106,7 +108,7 @@ public static IndexCommit GetLastCommit(Directory directory)
106108
/// The reason why the code fails instead of putting segments_N file last is
107109
/// that this indicates an error in the <see cref="IRevision"/> implementation.
108110
/// </summary>
109-
public static string GetSegmentsFile(IList<string> files, bool allowEmpty)
111+
public static string? GetSegmentsFile(IList<string> files, bool allowEmpty)
110112
{
111113
if (files.Count == 0)
112114
{
@@ -121,7 +123,7 @@ public static string GetSegmentsFile(IList<string> files, bool allowEmpty)
121123
if (!segmentsFile.StartsWith(IndexFileNames.SEGMENTS, StringComparison.Ordinal) || segmentsFile.Equals(IndexFileNames.SEGMENTS_GEN, StringComparison.Ordinal))
122124
{
123125
throw IllegalStateException.Create(
124-
string.Format("last file to copy+sync must be segments_N but got {0}; check your Revision implementation!", segmentsFile));
126+
$"last file to copy+sync must be segments_N but got {segmentsFile}; check your Revision implementation!");
125127
}
126128
return segmentsFile;
127129
}
@@ -157,11 +159,11 @@ public static void CleanupFilesOnFailure(Directory directory, IList<string> file
157159
/// directory. It suppresses any exceptions that occur, as this can be retried
158160
/// the next time.
159161
/// </remarks>
160-
public static void CleanupOldIndexFiles(Directory directory, string segmentsFile)
162+
public static void CleanupOldIndexFiles(Directory directory, string? segmentsFile)
161163
{
162164
try
163165
{
164-
IndexCommit commit = GetLastCommit(directory);
166+
IndexCommit? commit = GetLastCommit(directory);
165167
// commit is null means weird IO errors occurred, ignore them
166168
// if there were any IO errors reading the expected commit point (i.e.
167169
// segments files mismatch), then ignore that commit either.
@@ -218,7 +220,7 @@ public static void CopyFiles(Directory source, Directory target, IList<string> f
218220
/// the generation from the given <paramref name="segmentsFile"/>. If it is <c>null</c>,
219221
/// this method deletes segments.gen from the directory.
220222
/// </summary>
221-
public static void WriteSegmentsGen(string segmentsFile, Directory directory)
223+
public static void WriteSegmentsGen(string? segmentsFile, Directory directory)
222224
{
223225
if (segmentsFile != null)
224226
{
@@ -240,9 +242,9 @@ public static void WriteSegmentsGen(string segmentsFile, Directory directory)
240242
/// Constructor with the given index directory and callback to notify when the
241243
/// indexes were updated.
242244
/// </summary>
243-
public IndexReplicationHandler(Directory indexDirectory, Action callback)
245+
public IndexReplicationHandler(Directory indexDirectory, Action? callback)
244246
{
245-
this.InfoStream = InfoStream.Default;
247+
infoStream = InfoStream.Default;
246248
this.callback = callback;
247249
this.indexDirectory = indexDirectory;
248250

@@ -258,25 +260,26 @@ public IndexReplicationHandler(Directory indexDirectory, Action callback)
258260
currentRevisionFiles = IndexRevision.RevisionFiles(commit);
259261

260262
WriteToInfoStream(
261-
string.Format("constructor(): currentVersion={0} currentRevisionFiles={1}", currentVersion, currentRevisionFiles),
262-
string.Format("constructor(): commit={0}", commit));
263+
$"constructor(): currentVersion={currentVersion} currentRevisionFiles={currentRevisionFiles}",
264+
$"constructor(): commit={commit}");
263265
}
264266
}
265267

266-
public virtual string CurrentVersion => currentVersion;
268+
public virtual string? CurrentVersion => currentVersion;
267269

268-
public virtual IDictionary<string, IList<RevisionFile>> CurrentRevisionFiles => currentRevisionFiles;
270+
public virtual IDictionary<string, IList<RevisionFile>>? CurrentRevisionFiles => currentRevisionFiles;
269271

270272
public virtual void RevisionReady(string version,
271273
IDictionary<string, IList<RevisionFile>> revisionFiles,
272274
IDictionary<string, IList<string>> copiedFiles,
273275
IDictionary<string, Directory> sourceDirectory)
274276
{
275-
if (revisionFiles.Count > 1) throw new ArgumentException(string.Format("this handler handles only a single source; got {0}", revisionFiles.Keys));
277+
if (revisionFiles.Count > 1)
278+
throw new ArgumentException($"this handler handles only a single source; got {revisionFiles.Keys}");
276279

277280
Directory clientDirectory = sourceDirectory.Values.First();
278281
IList<string> files = copiedFiles.Values.First();
279-
string segmentsFile = GetSegmentsFile(files, false);
282+
string segmentsFile = GetSegmentsFile(files, false)!; // [!]: verified by GetSegmentsFile
280283

281284
bool success = false;
282285
try
@@ -306,7 +309,7 @@ public virtual void RevisionReady(string version,
306309
currentRevisionFiles = revisionFiles;
307310
currentVersion = version;
308311

309-
WriteToInfoStream(string.Format("revisionReady(): currentVersion={0} currentRevisionFiles={1}", currentVersion, currentRevisionFiles));
312+
WriteToInfoStream($"revisionReady(): currentVersion={currentVersion} currentRevisionFiles={currentRevisionFiles}");
310313

311314
// update the segments.gen file
312315
WriteSegmentsGen(segmentsFile, indexDirectory);
@@ -339,6 +342,7 @@ public virtual void RevisionReady(string version,
339342
public virtual InfoStream InfoStream
340343
{
341344
get => infoStream;
345+
// ReSharper disable once NullCoalescingConditionIsAlwaysNotNullAccordingToAPIContract
342346
set => infoStream = value ?? InfoStream.NO_OUTPUT;
343347
}
344348
}

0 commit comments

Comments
 (0)