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 9f85b4fc2cc0d9bf27432e1f3f56c4ffc21bac0e
Author: Shad Storhaug <[email protected]>
AuthorDate: Sat Jul 13 09:33:38 2019 +0700

    Lucene.Net.Analysis.ICU.Collation: Added Dispose() to each class that 
clones its own ICU Collator, as it contains unmanaged disposable resources. 
Except for ICUCollationKeyFilter because IncrementToken() is called after 
Dispose(). See LUCENENET-611.
---
 .../Collation/ICUCollationDocValuesField.cs             |  9 ++++++++-
 .../Collation/ICUCollationKeyFilter.cs                  |  3 +++
 .../Collation/ICUCollationKeyFilterFactory.cs           | 17 ++++++++++++++++-
 .../TokenAttributes/ICUCollatedTermAttributeImpl.cs     | 13 +++++++++----
 4 files changed, 36 insertions(+), 6 deletions(-)

diff --git 
a/src/Lucene.Net.Analysis.ICU/Collation/ICUCollationDocValuesField.cs 
b/src/Lucene.Net.Analysis.ICU/Collation/ICUCollationDocValuesField.cs
index 6b7e11c..70f8850 100644
--- a/src/Lucene.Net.Analysis.ICU/Collation/ICUCollationDocValuesField.cs
+++ b/src/Lucene.Net.Analysis.ICU/Collation/ICUCollationDocValuesField.cs
@@ -2,6 +2,7 @@
 using Lucene.Net.Documents;
 using Lucene.Net.Support;
 using Lucene.Net.Util;
+using System;
 #if NETSTANDARD
 using SortKey = Icu.SortKey;
 #else
@@ -39,7 +40,7 @@ namespace Lucene.Net.Collation
     /// and use less memory than FieldCache.
     /// </remarks>
     [ExceptionToClassNameConvention]
-    public sealed class ICUCollationDocValuesField : Field
+    public sealed class ICUCollationDocValuesField : Field, IDisposable
     {
         private readonly string name;
         private readonly Collator collator;
@@ -80,5 +81,11 @@ namespace Lucene.Net.Collation
             bytes.Offset = 0;
             bytes.Length = key.KeyData.Length;
         }
+
+        // LUCENENET specific - need to dispose collator
+        public void Dispose()
+        {
+            this.collator.Dispose();
+        }
     }
 }
diff --git a/src/Lucene.Net.Analysis.ICU/Collation/ICUCollationKeyFilter.cs 
b/src/Lucene.Net.Analysis.ICU/Collation/ICUCollationKeyFilter.cs
index afc92f7..1fd0b94 100644
--- a/src/Lucene.Net.Analysis.ICU/Collation/ICUCollationKeyFilter.cs
+++ b/src/Lucene.Net.Analysis.ICU/Collation/ICUCollationKeyFilter.cs
@@ -103,5 +103,8 @@ namespace Lucene.Net.Collation
                 return false;
             }
         }
+
+        // LUCENENET-611 - Dispose is being called and then IncrementToken() 
is called afterward, so we cannot dispose our
+        // disposable collator instance here. Need to fix the bug, then 
override Dispose(bool).
     }
 }
diff --git 
a/src/Lucene.Net.Analysis.ICU/Collation/ICUCollationKeyFilterFactory.cs 
b/src/Lucene.Net.Analysis.ICU/Collation/ICUCollationKeyFilterFactory.cs
index 1609e14..040717e 100644
--- a/src/Lucene.Net.Analysis.ICU/Collation/ICUCollationKeyFilterFactory.cs
+++ b/src/Lucene.Net.Analysis.ICU/Collation/ICUCollationKeyFilterFactory.cs
@@ -64,7 +64,7 @@ namespace Lucene.Net.Collation
     /// LUCENENET NOTE: variableTop is not supported by icu.net
     [Obsolete("Use ICUCollationKeyAnalyzer instead.")]
     [ExceptionToClassNameConvention]
-    public class ICUCollationKeyFilterFactory : TokenFilterFactory, 
IMultiTermAwareComponent, IResourceLoaderAware
+    public class ICUCollationKeyFilterFactory : TokenFilterFactory, 
IMultiTermAwareComponent, IResourceLoaderAware, IDisposable
     {
         private Collator collator;
         private readonly string custom;
@@ -258,5 +258,20 @@ namespace Lucene.Net.Collation
             }
             return sb.ToString();
         }
+
+        // LUCENENET specific - must dispose collator
+        public void Dispose()
+        {
+            Dispose(true);
+            GC.SuppressFinalize(this);
+        }
+
+        protected virtual void Dispose(bool disposing)
+        {
+            if (disposing)
+            {
+                collator?.Dispose();
+            }
+        }
     }
 }
diff --git 
a/src/Lucene.Net.Analysis.ICU/Collation/TokenAttributes/ICUCollatedTermAttributeImpl.cs
 
b/src/Lucene.Net.Analysis.ICU/Collation/TokenAttributes/ICUCollatedTermAttributeImpl.cs
index 1dc44f8..6129d8c 100644
--- 
a/src/Lucene.Net.Analysis.ICU/Collation/TokenAttributes/ICUCollatedTermAttributeImpl.cs
+++ 
b/src/Lucene.Net.Analysis.ICU/Collation/TokenAttributes/ICUCollatedTermAttributeImpl.cs
@@ -2,6 +2,7 @@
 using Lucene.Net.Analysis.TokenAttributes;
 using Lucene.Net.Support;
 using Lucene.Net.Util;
+using System;
 #if NETSTANDARD
 using SortKey = Icu.SortKey;
 #else
@@ -32,11 +33,9 @@ namespace Lucene.Net.Collation.TokenAttributes
     /// text as a binary Unicode collation key instead of as UTF-8 bytes.
     /// </summary>
     [ExceptionToClassNameConvention]
-    public class ICUCollatedTermAttribute : CharTermAttribute
+    public sealed class ICUCollatedTermAttribute : CharTermAttribute, 
IDisposable
     {
         private readonly Collator collator;
-        //private readonly RawCollationKey key = new RawCollationKey();
-        private SortKey key;
 
         /// <summary>
         /// Create a new ICUCollatedTermAttribute
@@ -51,10 +50,16 @@ namespace Lucene.Net.Collation.TokenAttributes
         public override void FillBytesRef()
         {
             BytesRef bytes = this.BytesRef;
-            key = collator.GetSortKey(ToString());
+            SortKey key = collator.GetSortKey(ToString());
             bytes.Bytes = key.KeyData;
             bytes.Offset = 0;
             bytes.Length = key.KeyData.Length;
         }
+
+        // LUCENENET specific - must dispose collator
+        public void Dispose()
+        {
+            collator.Dispose();
+        }
     }
 }

Reply via email to