You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2022/11/12 11:12:17 UTC

[lucenenet] branch master updated: fix: Aligned disposable patterns (#746)

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


The following commit(s) were added to refs/heads/master by this push:
     new cf259c918 fix: Aligned disposable patterns (#746)
cf259c918 is described below

commit cf259c918a419abf3025664a8af31c60680220e3
Author: Nikolaj Brask-Nielsen <ni...@gmail.com>
AuthorDate: Sat Nov 12 12:12:11 2022 +0100

    fix: Aligned disposable patterns (#746)
    
    * fix: Aligned disposable patterns
    
    * Lucene.Net.Analysis.Util.CharArrayMap: Reverted changes to avoid conflict with other work that has been done to CharArrayMap
    
    * SWEEP: Added comments to indicate places where we have diverged from Lucene to implement the disposable pattern properly.
    
    * Lucene.Net.Codecs.Memory.MemoryDocValuesConsumer: Changed code order of Dispose() and Dispose(bool) to align with other classes
    
    Co-authored-by: Shad Storhaug <sh...@shadstorhaug.com>
---
 .../PrefixAndSuffixAwareTokenFilter.cs              |  3 ++-
 .../Miscellaneous/PrefixAwareTokenFilter.cs         |  3 ++-
 .../Analysis/Util/BufferedCharFilter.cs             |  1 +
 .../ByTask/Tasks/NearRealtimeReaderTask.cs          |  1 +
 .../ByTask/Tasks/TaskSequence.cs                    |  1 +
 .../Memory/MemoryDocValuesConsumer.cs               | 13 +++++++++++--
 .../Taxonomy/WriterCache/CollisionMap.cs            |  2 +-
 .../Prefix/AbstractVisitingPrefixTreeFilter.cs      |  2 +-
 src/Lucene.Net/Analysis/TokenFilter.cs              |  1 +
 src/Lucene.Net/Analysis/Tokenizer.cs                |  1 +
 .../Codecs/PerField/PerFieldDocValuesFormat.cs      | 13 +++++++++++--
 .../Codecs/PerField/PerFieldPostingsFormat.cs       | 11 ++++++++++-
 src/Lucene.Net/Index/IndexWriter.cs                 | 11 ++++++++++-
 src/Lucene.Net/Index/PrefixCodedTerms.cs            |  2 +-
 src/Lucene.Net/Store/FSDirectory.cs                 |  1 +
 src/Lucene.Net/Support/IO/SafeTextWriterWrapper.cs  |  1 +
 src/Lucene.Net/Support/Index/TaskMergeScheduler.cs  | 21 +++++++++++++++------
 src/Lucene.Net/Util/PrintStreamInfoStream.cs        |  3 ++-
 .../CommandLine/CommandLineApplication.cs           |  2 +-
 .../SourceCode/SourceCodeSectionReader.cs           |  1 +
 20 files changed, 75 insertions(+), 19 deletions(-)

diff --git a/src/Lucene.Net.Analysis.Common/Analysis/Miscellaneous/PrefixAndSuffixAwareTokenFilter.cs b/src/Lucene.Net.Analysis.Common/Analysis/Miscellaneous/PrefixAndSuffixAwareTokenFilter.cs
index cc85abe77..67f22a959 100644
--- a/src/Lucene.Net.Analysis.Common/Analysis/Miscellaneous/PrefixAndSuffixAwareTokenFilter.cs
+++ b/src/Lucene.Net.Analysis.Common/Analysis/Miscellaneous/PrefixAndSuffixAwareTokenFilter.cs
@@ -1,4 +1,4 @@
-// Lucene version compatibility level 4.8.1
+// Lucene version compatibility level 4.8.1
 namespace Lucene.Net.Analysis.Miscellaneous
 {
     /*
@@ -96,6 +96,7 @@ namespace Lucene.Net.Analysis.Miscellaneous
             {
                 suffix.Dispose();
             }
+            base.Dispose(disposing); // LUCENENET specific - disposable pattern requires calling the base class implementation
         }
 
         public override void End()
diff --git a/src/Lucene.Net.Analysis.Common/Analysis/Miscellaneous/PrefixAwareTokenFilter.cs b/src/Lucene.Net.Analysis.Common/Analysis/Miscellaneous/PrefixAwareTokenFilter.cs
index 58ca6ff64..118f67b03 100644
--- a/src/Lucene.Net.Analysis.Common/Analysis/Miscellaneous/PrefixAwareTokenFilter.cs
+++ b/src/Lucene.Net.Analysis.Common/Analysis/Miscellaneous/PrefixAwareTokenFilter.cs
@@ -1,4 +1,4 @@
-// Lucene version compatibility level 4.8.1
+// Lucene version compatibility level 4.8.1
 using Lucene.Net.Analysis.TokenAttributes;
 using Lucene.Net.Util;
 
@@ -182,6 +182,7 @@ namespace Lucene.Net.Analysis.Miscellaneous
                 prefix.Dispose();
                 suffix.Dispose();
             }
+            base.Dispose(disposing); // LUCENENET specific - disposable pattern requires calling the base class implementation
         }
 
         public override void Reset()
diff --git a/src/Lucene.Net.Analysis.Common/Analysis/Util/BufferedCharFilter.cs b/src/Lucene.Net.Analysis.Common/Analysis/Util/BufferedCharFilter.cs
index cc6ecb33e..8bf2ee334 100644
--- a/src/Lucene.Net.Analysis.Common/Analysis/Util/BufferedCharFilter.cs
+++ b/src/Lucene.Net.Analysis.Common/Analysis/Util/BufferedCharFilter.cs
@@ -138,6 +138,7 @@ namespace Lucene.Net.Analysis.Util
                 this.isDisposing = false;
 #endif
             }
+            base.Dispose(disposing); // LUCENENET specific - disposable pattern requires calling the base class implementation
         }
 
         /// <summary>
diff --git a/src/Lucene.Net.Benchmark/ByTask/Tasks/NearRealtimeReaderTask.cs b/src/Lucene.Net.Benchmark/ByTask/Tasks/NearRealtimeReaderTask.cs
index df2824ec5..f4b5178b0 100644
--- a/src/Lucene.Net.Benchmark/ByTask/Tasks/NearRealtimeReaderTask.cs
+++ b/src/Lucene.Net.Benchmark/ByTask/Tasks/NearRealtimeReaderTask.cs
@@ -123,6 +123,7 @@ namespace Lucene.Net.Benchmarks.ByTask.Tasks
                 }
                 Console.WriteLine();
             }
+            base.Dispose(disposing); // LUCENENET specific - disposable pattern requires calling the base class implementation
         }
 
         public override bool SupportsParams => true;
diff --git a/src/Lucene.Net.Benchmark/ByTask/Tasks/TaskSequence.cs b/src/Lucene.Net.Benchmark/ByTask/Tasks/TaskSequence.cs
index 608a03512..698234c65 100644
--- a/src/Lucene.Net.Benchmark/ByTask/Tasks/TaskSequence.cs
+++ b/src/Lucene.Net.Benchmark/ByTask/Tasks/TaskSequence.cs
@@ -76,6 +76,7 @@ namespace Lucene.Net.Benchmarks.ByTask.Tasks
                 }
                 RunData.DocMaker.Dispose();
             }
+            base.Dispose(disposing); // LUCENENET specific - disposable pattern requires calling the base class implementation
         }
 
         private void InitTasksArray()
diff --git a/src/Lucene.Net.Codecs/Memory/MemoryDocValuesConsumer.cs b/src/Lucene.Net.Codecs/Memory/MemoryDocValuesConsumer.cs
index df4a64622..e51dc16d3 100644
--- a/src/Lucene.Net.Codecs/Memory/MemoryDocValuesConsumer.cs
+++ b/src/Lucene.Net.Codecs/Memory/MemoryDocValuesConsumer.cs
@@ -499,8 +499,17 @@ namespace Lucene.Net.Codecs.Memory
 
             public void Dispose()
             {
-                this.counts.Dispose();
-                this.ords.Dispose();
+                Dispose(true);
+                GC.SuppressFinalize(this);
+            }
+
+            protected virtual void Dispose(bool disposing)
+            {
+                if (disposing)
+                {
+                    this.counts.Dispose();
+                    this.ords.Dispose();
+                }
             }
 
             // LUCENENET: Remove() not supported in .NET
diff --git a/src/Lucene.Net.Facet/Taxonomy/WriterCache/CollisionMap.cs b/src/Lucene.Net.Facet/Taxonomy/WriterCache/CollisionMap.cs
index 630abf80e..c466b89a7 100644
--- a/src/Lucene.Net.Facet/Taxonomy/WriterCache/CollisionMap.cs
+++ b/src/Lucene.Net.Facet/Taxonomy/WriterCache/CollisionMap.cs
@@ -213,7 +213,7 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
             return memoryUsage;
         }
 
-        private class EntryEnumerator : IEnumerator<Entry>
+        private sealed class EntryEnumerator : IEnumerator<Entry> // LUCENENET: Marked sealed
         {
             internal Entry next; // next entry to return
             internal int index; // current slot
diff --git a/src/Lucene.Net.Spatial/Prefix/AbstractVisitingPrefixTreeFilter.cs b/src/Lucene.Net.Spatial/Prefix/AbstractVisitingPrefixTreeFilter.cs
index 4f2a53adb..534273dff 100644
--- a/src/Lucene.Net.Spatial/Prefix/AbstractVisitingPrefixTreeFilter.cs
+++ b/src/Lucene.Net.Spatial/Prefix/AbstractVisitingPrefixTreeFilter.cs
@@ -367,7 +367,7 @@ namespace Lucene.Net.Spatial.Prefix
         /// <summary>
         /// Used for <see cref="VNode.children"/>.
         /// </summary>
-        private class VNodeCellEnumerator : IEnumerator<VNode>
+        private sealed class VNodeCellEnumerator : IEnumerator<VNode>// LUCENENET: Marked sealed
         {
             internal readonly IEnumerator<Cell> cellIter;
             private readonly VNode vNode;
diff --git a/src/Lucene.Net/Analysis/TokenFilter.cs b/src/Lucene.Net/Analysis/TokenFilter.cs
index 400a6ddf8..fc5630a94 100644
--- a/src/Lucene.Net/Analysis/TokenFilter.cs
+++ b/src/Lucene.Net/Analysis/TokenFilter.cs
@@ -82,6 +82,7 @@ namespace Lucene.Net.Analysis
             {
                 m_input.Dispose();
             }
+            base.Dispose(disposing); // LUCENENET specific - disposable pattern requires calling the base class implementation
         }
 
         /// <summary>
diff --git a/src/Lucene.Net/Analysis/Tokenizer.cs b/src/Lucene.Net/Analysis/Tokenizer.cs
index fc49d4871..4fa95e017 100644
--- a/src/Lucene.Net/Analysis/Tokenizer.cs
+++ b/src/Lucene.Net/Analysis/Tokenizer.cs
@@ -79,6 +79,7 @@ namespace Lucene.Net.Analysis
                 inputPending = ILLEGAL_STATE_READER;
                 m_input = ILLEGAL_STATE_READER;
             }
+            base.Dispose(disposing); // LUCENENET specific - disposable pattern requires calling the base class implementation
         }
 
         /// <summary>
diff --git a/src/Lucene.Net/Codecs/PerField/PerFieldDocValuesFormat.cs b/src/Lucene.Net/Codecs/PerField/PerFieldDocValuesFormat.cs
index 40faa73c7..82799e38b 100644
--- a/src/Lucene.Net/Codecs/PerField/PerFieldDocValuesFormat.cs
+++ b/src/Lucene.Net/Codecs/PerField/PerFieldDocValuesFormat.cs
@@ -1,4 +1,4 @@
-using J2N.Runtime.CompilerServices;
+using J2N.Runtime.CompilerServices;
 using Lucene.Net.Diagnostics;
 using System;
 using System.Collections.Generic;
@@ -94,7 +94,16 @@ namespace Lucene.Net.Codecs.PerField
 
             public void Dispose()
             {
-                Consumer.Dispose();
+                Dispose(true);
+                GC.SuppressFinalize(this);
+            }
+
+            protected virtual void Dispose(bool disposing)
+            {
+                if (disposing)
+                {
+                    Consumer.Dispose();
+                }
             }
         }
 
diff --git a/src/Lucene.Net/Codecs/PerField/PerFieldPostingsFormat.cs b/src/Lucene.Net/Codecs/PerField/PerFieldPostingsFormat.cs
index 37ea7c32a..2738edcb6 100644
--- a/src/Lucene.Net/Codecs/PerField/PerFieldPostingsFormat.cs
+++ b/src/Lucene.Net/Codecs/PerField/PerFieldPostingsFormat.cs
@@ -89,7 +89,16 @@ namespace Lucene.Net.Codecs.PerField
             [MethodImpl(MethodImplOptions.AggressiveInlining)]
             public void Dispose()
             {
-                Consumer.Dispose();
+                Dispose(true);
+                GC.SuppressFinalize(this);
+            }
+
+            protected virtual void Dispose(bool disposing)
+            {
+                if (disposing)
+                {
+                    Consumer.Dispose();
+                }
             }
         }
 
diff --git a/src/Lucene.Net/Index/IndexWriter.cs b/src/Lucene.Net/Index/IndexWriter.cs
index 941095fb7..12a5540fd 100644
--- a/src/Lucene.Net/Index/IndexWriter.cs
+++ b/src/Lucene.Net/Index/IndexWriter.cs
@@ -593,7 +593,16 @@ namespace Lucene.Net.Index
 
             public void Dispose()
             {
-                DropAll(false);
+                Dispose(true);
+                GC.SuppressFinalize(this);
+            }
+
+            protected virtual void Dispose(bool disposing)
+            {
+                if (disposing)
+                {
+                    DropAll(false);
+                }
             }
 
             /// <summary>
diff --git a/src/Lucene.Net/Index/PrefixCodedTerms.cs b/src/Lucene.Net/Index/PrefixCodedTerms.cs
index 4c1090a1f..c4b4ec6e8 100644
--- a/src/Lucene.Net/Index/PrefixCodedTerms.cs
+++ b/src/Lucene.Net/Index/PrefixCodedTerms.cs
@@ -87,7 +87,7 @@ namespace Lucene.Net.Index
             public void Dispose()
             {
                 Dispose(true);
-                GC.SuppressFinalize(true);
+                GC.SuppressFinalize(this);
             }
 
             protected virtual void Dispose(bool disposing)
diff --git a/src/Lucene.Net/Store/FSDirectory.cs b/src/Lucene.Net/Store/FSDirectory.cs
index 6471023c9..fee4ffe41 100644
--- a/src/Lucene.Net/Store/FSDirectory.cs
+++ b/src/Lucene.Net/Store/FSDirectory.cs
@@ -562,6 +562,7 @@ namespace Lucene.Net.Store
                         }
                     }
                 }
+                //base.Dispose(disposing); // LUCENENET: No need to call base class, we are not using the functionality of BufferedIndexOutput
             }
 
             /// <summary>
diff --git a/src/Lucene.Net/Support/IO/SafeTextWriterWrapper.cs b/src/Lucene.Net/Support/IO/SafeTextWriterWrapper.cs
index 3cc52bbb9..29fae0a5d 100644
--- a/src/Lucene.Net/Support/IO/SafeTextWriterWrapper.cs
+++ b/src/Lucene.Net/Support/IO/SafeTextWriterWrapper.cs
@@ -358,6 +358,7 @@ namespace Lucene.Net.Support.IO
             {
                 textWriter.Dispose();
             }
+            base.Dispose(disposing); // LUCENENET specific - disposable pattern requires calling the base class implementation
         }
     }
 }
diff --git a/src/Lucene.Net/Support/Index/TaskMergeScheduler.cs b/src/Lucene.Net/Support/Index/TaskMergeScheduler.cs
index ec93e9a17..984593708 100644
--- a/src/Lucene.Net/Support/Index/TaskMergeScheduler.cs
+++ b/src/Lucene.Net/Support/Index/TaskMergeScheduler.cs
@@ -621,14 +621,23 @@ namespace Lucene.Net.Index
 
             public void Dispose()
             {
-                if (_isDisposed)
+                Dispose(true);
+                GC.SuppressFinalize(this);
+            }
+
+            protected virtual void Dispose(bool disposing)
+            {
+                if (disposing)
                 {
-                    return;
-                }
+                    if (_isDisposed)
+                    {
+                        return;
+                    }
 
-                _isDisposed = true;
-                _lock.Dispose();
-                _cancellationTokenSource.Dispose();
+                    _isDisposed = true;
+                    _lock.Dispose();
+                    _cancellationTokenSource.Dispose();
+                }
             }
 
             public override string ToString()
diff --git a/src/Lucene.Net/Util/PrintStreamInfoStream.cs b/src/Lucene.Net/Util/PrintStreamInfoStream.cs
index fb1a1b4b9..451586e7f 100644
--- a/src/Lucene.Net/Util/PrintStreamInfoStream.cs
+++ b/src/Lucene.Net/Util/PrintStreamInfoStream.cs
@@ -1,4 +1,4 @@
-using J2N.Threading.Atomic;
+using J2N.Threading.Atomic;
 using Lucene.Net.Support.IO;
 using System;
 using System.IO;
@@ -90,6 +90,7 @@ namespace Lucene.Net.Util
             {
                 m_stream.Dispose();
             }
+            base.Dispose(disposing); // LUCENENET specific - disposable pattern requires calling the base class implementation
         }
 
         public virtual bool IsSystemStream => isSystemStream;
diff --git a/src/dotnet/tools/lucene-cli/CommandLine/CommandLineApplication.cs b/src/dotnet/tools/lucene-cli/CommandLine/CommandLineApplication.cs
index f5971e5cc..e3d8240f0 100644
--- a/src/dotnet/tools/lucene-cli/CommandLine/CommandLineApplication.cs
+++ b/src/dotnet/tools/lucene-cli/CommandLine/CommandLineApplication.cs
@@ -534,7 +534,7 @@ namespace Lucene.Net.Cli.CommandLine
             }
         }
 
-        private class CommandArgumentEnumerator : IEnumerator<CommandArgument>
+        private sealed class CommandArgumentEnumerator : IEnumerator<CommandArgument>
         {
             private readonly IEnumerator<CommandArgument> _enumerator;
 
diff --git a/src/dotnet/tools/lucene-cli/SourceCode/SourceCodeSectionReader.cs b/src/dotnet/tools/lucene-cli/SourceCode/SourceCodeSectionReader.cs
index 0ec50949e..c33eebafb 100644
--- a/src/dotnet/tools/lucene-cli/SourceCode/SourceCodeSectionReader.cs
+++ b/src/dotnet/tools/lucene-cli/SourceCode/SourceCodeSectionReader.cs
@@ -150,6 +150,7 @@ namespace Lucene.Net.Cli.SourceCode
             {
                 this.reader?.Dispose();
             }
+            base.Dispose(disposing);
         }
     }
 }