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 6e341886d2a2e40315525f0997ee4b456bc8f4bd Author: Jason Nelson <[email protected]> AuthorDate: Tue Nov 8 10:50:30 2022 -0800 Use null propagation --- src/Lucene.Net/Index/CheckIndex.cs | 202 +++++++++++---------------- src/Lucene.Net/Store/BaseDirectory.cs | 5 +- src/Lucene.Net/Store/ByteBufferIndexInput.cs | 5 +- src/Lucene.Net/Store/RAMFile.cs | 5 +- src/Lucene.Net/Util/IOUtils.cs | 30 +--- 5 files changed, 87 insertions(+), 160 deletions(-) diff --git a/src/Lucene.Net/Index/CheckIndex.cs b/src/Lucene.Net/Index/CheckIndex.cs index cf84648d8..4c358d29b 100644 --- a/src/Lucene.Net/Index/CheckIndex.cs +++ b/src/Lucene.Net/Index/CheckIndex.cs @@ -473,10 +473,7 @@ namespace Lucene.Net.Index private static void Msg(TextWriter @out, string msg) { - if (@out != null) - { - @out.WriteLine(msg); - } + @out?.WriteLine(msg); } /// <summary> @@ -522,14 +519,13 @@ namespace Lucene.Net.Index { Msg(infoStream, "ERROR: could not read any segments file in directory"); result.MissingSegments = true; - if (infoStream != null) - { - // LUCENENET NOTE: Some tests rely on the error type being in - // the message. We can't get the error type with StackTrace, we - // need ToString() for that. - infoStream.WriteLine(t.ToString()); - //infoStream.WriteLine(t.StackTrace); - } + + // LUCENENET NOTE: Some tests rely on the error type being in + // the message. We can't get the error type with StackTrace, we + // need ToString() for that. + infoStream?.WriteLine(t.ToString()); + //infoStream.WriteLine(t.StackTrace); + return result; } @@ -571,14 +567,13 @@ namespace Lucene.Net.Index catch (Exception t) when (t.IsThrowable()) { Msg(infoStream, "ERROR: could not open segments file in directory"); - if (infoStream != null) - { - // LUCENENET NOTE: Some tests rely on the error type being in - // the message. We can't get the error type with StackTrace, we - // need ToString() for that. - infoStream.WriteLine(t.ToString()); - //infoStream.WriteLine(t.StackTrace); - } + + // LUCENENET NOTE: Some tests rely on the error type being in + // the message. We can't get the error type with StackTrace, we + // need ToString() for that. + infoStream?.WriteLine(t.ToString()); + //infoStream.WriteLine(t.StackTrace); + result.CantOpenSegments = true; return result; } @@ -590,23 +585,19 @@ namespace Lucene.Net.Index catch (Exception t) when (t.IsThrowable()) { Msg(infoStream, "ERROR: could not read segment file version in directory"); - if (infoStream != null) - { - // LUCENENET NOTE: Some tests rely on the error type being in - // the message. We can't get the error type with StackTrace, we - // need ToString() for that. - infoStream.WriteLine(t.ToString()); - //infoStream.WriteLine(t.StackTrace); - } + + // LUCENENET NOTE: Some tests rely on the error type being in + // the message. We can't get the error type with StackTrace, we + // need ToString() for that. + infoStream?.WriteLine(t.ToString()); + //infoStream.WriteLine(t.StackTrace); + result.MissingSegmentVersion = true; return result; } finally { - if (input != null) - { - input.Dispose(); - } + input?.Dispose(); } string sFormat = ""; @@ -745,26 +736,21 @@ namespace Lucene.Net.Index segInfoStat.HasDeletions = true; segInfoStat.DeletionsGen = info.DelGen; } - if (infoStream != null) - { - infoStream.Write(" test: open reader........."); - } + + infoStream?.Write(" test: open reader........."); + reader = new SegmentReader(info, DirectoryReader.DEFAULT_TERMS_INDEX_DIVISOR, IOContext.DEFAULT); Msg(infoStream, "OK"); segInfoStat.OpenReaderPassed = true; - if (infoStream != null) - { - infoStream.Write(" test: check integrity....."); - } + infoStream?.Write(" test: check integrity....."); + reader.CheckIntegrity(); Msg(infoStream, "OK"); - if (infoStream != null) - { - infoStream.Write(" test: check live docs....."); - } + infoStream?.Write(" test: check live docs....."); + int numDocs = reader.NumDocs; toLoseDocCount = numDocs; if (reader.HasDeletions) @@ -831,10 +817,8 @@ namespace Lucene.Net.Index } // Test getFieldInfos() - if (infoStream != null) - { - infoStream.Write(" test: fields.............."); - } + infoStream?.Write(" test: fields.............."); + FieldInfos fieldInfos = reader.FieldInfos; Msg(infoStream, "OK [" + fieldInfos.Count + " fields]"); segInfoStat.NumFields = fieldInfos.Count; @@ -884,13 +868,12 @@ namespace Lucene.Net.Index string comment; comment = "fixIndex() would remove reference to this segment"; Msg(infoStream, " WARNING: " + comment + "; full exception:"); - if (infoStream != null) - { - // LUCENENET NOTE: Some tests rely on the error type being in - // the message. We can't get the error type with StackTrace, we - // need ToString() for that. - infoStream.WriteLine(t.ToString()); - } + + // LUCENENET NOTE: Some tests rely on the error type being in + // the message. We can't get the error type with StackTrace, we + // need ToString() for that. + infoStream?.WriteLine(t.ToString()); + Msg(infoStream, ""); result.TotLoseDocCount += toLoseDocCount; result.NumBadSegments++; @@ -898,10 +881,7 @@ namespace Lucene.Net.Index } finally { - if (reader != null) - { - reader.Dispose(); - } + reader?.Dispose(); } // Keeper @@ -944,10 +924,8 @@ namespace Lucene.Net.Index try { // Test Field Norms - if (infoStream != null) - { - infoStream.Write(" test: field norms........."); - } + infoStream?.Write(" test: field norms........."); + foreach (FieldInfo info in reader.FieldInfos) { if (info.HasNorms) @@ -976,14 +954,12 @@ namespace Lucene.Net.Index { Msg(infoStream, "ERROR [" + e.Message + "]"); status.Error = e; - if (infoStream != null) - { - // LUCENENET NOTE: Some tests rely on the error type being in - // the message. We can't get the error type with StackTrace, we - // need ToString() for that. - infoStream.WriteLine(e.ToString()); - //infoStream.WriteLine(e.StackTrace); - } + + // LUCENENET NOTE: Some tests rely on the error type being in + // the message. We can't get the error type with StackTrace, we + // need ToString() for that. + infoStream?.WriteLine(e.ToString()); + //infoStream.WriteLine(e.StackTrace); } return status; @@ -1668,20 +1644,14 @@ namespace Lucene.Net.Index try { - if (infoStream != null) - { - infoStream.Write(" test: terms, freq, prox..."); - } - + infoStream?.Write(" test: terms, freq, prox..."); + Fields fields = reader.Fields; FieldInfos fieldInfos = reader.FieldInfos; status = CheckFields(fields, liveDocs, maxDoc, fieldInfos, true, false, infoStream, verbose); if (liveDocs != null) { - if (infoStream != null) - { - infoStream.Write(" test (ignoring deletes): terms, freq, prox..."); - } + infoStream?.Write(" test (ignoring deletes): terms, freq, prox..."); CheckFields(fields, null, maxDoc, fieldInfos, true, false, infoStream, verbose); } } @@ -1690,14 +1660,12 @@ namespace Lucene.Net.Index Msg(infoStream, "ERROR: " + e); status = new Status.TermIndexStatus(); status.Error = e; - if (infoStream != null) - { - // LUCENENET NOTE: Some tests rely on the error type being in - // the message. We can't get the error type with StackTrace, we - // need ToString() for that. - infoStream.WriteLine(e.ToString()); - //infoStream.WriteLine(e.StackTrace); - } + + // LUCENENET NOTE: Some tests rely on the error type being in + // the message. We can't get the error type with StackTrace, we + // need ToString() for that. + infoStream?.WriteLine(e.ToString()); + //infoStream.WriteLine(e.StackTrace); } return status; @@ -1714,10 +1682,7 @@ namespace Lucene.Net.Index try { - if (infoStream != null) - { - infoStream.Write(" test: stored fields......."); - } + infoStream?.Write(" test: stored fields......."); // Scan stored fields for all documents IBits liveDocs = reader.LiveDocs; @@ -1745,14 +1710,12 @@ namespace Lucene.Net.Index { Msg(infoStream, "ERROR [" + e.Message + "]"); status.Error = e; - if (infoStream != null) - { - // LUCENENET NOTE: Some tests rely on the error type being in - // the message. We can't get the error type with StackTrace, we - // need ToString() for that. - infoStream.WriteLine(e.ToString()); - //infoStream.WriteLine(e.StackTrace); - } + + // LUCENENET NOTE: Some tests rely on the error type being in + // the message. We can't get the error type with StackTrace, we + // need ToString() for that. + infoStream?.WriteLine(e.ToString()); + //infoStream.WriteLine(e.StackTrace); } return status; @@ -1768,10 +1731,8 @@ namespace Lucene.Net.Index Status.DocValuesStatus status = new Status.DocValuesStatus(); try { - if (infoStream != null) - { - infoStream.Write(" test: docvalues..........."); - } + infoStream?.Write(" test: docvalues..........."); + foreach (FieldInfo fieldInfo in reader.FieldInfos) { if (fieldInfo.HasDocValues) @@ -1794,14 +1755,12 @@ namespace Lucene.Net.Index { Msg(infoStream, "ERROR [" + e.Message + "]"); status.Error = e; - if (infoStream != null) - { - // LUCENENET NOTE: Some tests rely on the error type being in - // the message. We can't get the error type with StackTrace, we - // need ToString() for that. - infoStream.WriteLine(e.ToString()); - //infoStream.WriteLine(e.StackTrace); - } + + // LUCENENET NOTE: Some tests rely on the error type being in + // the message. We can't get the error type with StackTrace, we + // need ToString() for that. + infoStream?.WriteLine(e.ToString()); + //infoStream.WriteLine(e.StackTrace); } return status; } @@ -2069,10 +2028,7 @@ namespace Lucene.Net.Index try { - if (infoStream != null) - { - infoStream.Write(" test: term vectors........"); - } + infoStream?.Write(" test: term vectors........"); DocsEnum docs = null; DocsAndPositionsEnum postings = null; @@ -2322,14 +2278,12 @@ namespace Lucene.Net.Index { Msg(infoStream, "ERROR [" + e.Message + "]"); status.Error = e; - if (infoStream != null) - { - // LUCENENET NOTE: Some tests rely on the error type being in - // the message. We can't get the error type with StackTrace, we - // need ToString() for that. - infoStream.WriteLine(e.ToString()); - //infoStream.WriteLine(e.StackTrace); - } + + // LUCENENET NOTE: Some tests rely on the error type being in + // the message. We can't get the error type with StackTrace, we + // need ToString() for that. + infoStream?.WriteLine(e.ToString()); + //infoStream.WriteLine(e.StackTrace); } return status; diff --git a/src/Lucene.Net/Store/BaseDirectory.cs b/src/Lucene.Net/Store/BaseDirectory.cs index 94974784d..fe9745850 100644 --- a/src/Lucene.Net/Store/BaseDirectory.cs +++ b/src/Lucene.Net/Store/BaseDirectory.cs @@ -58,10 +58,7 @@ namespace Lucene.Net.Store public override void ClearLock(string name) { - if (m_lockFactory != null) - { - m_lockFactory.ClearLock(name); - } + m_lockFactory?.ClearLock(name); } public override void SetLockFactory(LockFactory lockFactory) diff --git a/src/Lucene.Net/Store/ByteBufferIndexInput.cs b/src/Lucene.Net/Store/ByteBufferIndexInput.cs index b2b8a0ff1..1346b833a 100644 --- a/src/Lucene.Net/Store/ByteBufferIndexInput.cs +++ b/src/Lucene.Net/Store/ByteBufferIndexInput.cs @@ -389,10 +389,7 @@ namespace Lucene.Net.Store // make local copy, then un-set early ByteBuffer[] bufs = buffers; UnsetBuffers(); - if (clones != null) - { - clones.Remove(this); - } + clones?.Remove(this); if (isClone) { diff --git a/src/Lucene.Net/Store/RAMFile.cs b/src/Lucene.Net/Store/RAMFile.cs index 2937ca9dc..11b512df6 100644 --- a/src/Lucene.Net/Store/RAMFile.cs +++ b/src/Lucene.Net/Store/RAMFile.cs @@ -90,10 +90,7 @@ namespace Lucene.Net.Store UninterruptableMonitor.Exit(this); } - if (directory != null) - { - directory.m_sizeInBytes.AddAndGet(size); - } + directory?.m_sizeInBytes.AddAndGet(size); return buffer; } diff --git a/src/Lucene.Net/Util/IOUtils.cs b/src/Lucene.Net/Util/IOUtils.cs index 71bcd740b..a90110860 100644 --- a/src/Lucene.Net/Util/IOUtils.cs +++ b/src/Lucene.Net/Util/IOUtils.cs @@ -175,10 +175,7 @@ namespace Lucene.Net.Util { try { - if (@object != null) - { - @object.Dispose(); - } + @object?.Dispose(); } catch (Exception t) when (t.IsThrowable()) { @@ -212,10 +209,7 @@ namespace Lucene.Net.Util { try { - if (@object != null) - { - @object.Dispose(); - } + @object?.Dispose(); } catch (Exception t) when (t.IsThrowable()) { @@ -255,10 +249,7 @@ namespace Lucene.Net.Util { try { - if (@object != null) - { - @object.Dispose(); - } + @object?.Dispose(); } catch (Exception t) when (t.IsThrowable()) { @@ -285,10 +276,7 @@ namespace Lucene.Net.Util { try { - if (@object != null) - { - @object.Dispose(); - } + @object?.Dispose(); } catch (Exception t) when (t.IsThrowable()) { @@ -316,10 +304,7 @@ namespace Lucene.Net.Util { try { - if (o != null) - { - o.Dispose(); - } + o?.Dispose(); } catch (Exception t) when (t.IsThrowable()) { @@ -338,10 +323,7 @@ namespace Lucene.Net.Util { try { - if (@object != null) - { - @object.Dispose(); - } + @object?.Dispose(); } catch (Exception t) when (t.IsThrowable()) {
