lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nightowl...@apache.org
Subject [09/20] lucenenet git commit: Lucene.Net.Core.Codecs.FieldsProducer: implemented .NET dispose pattern
Date Sun, 02 Apr 2017 07:06:09 GMT
Lucene.Net.Core.Codecs.FieldsProducer: implemented .NET dispose pattern


Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/8105155d
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/8105155d
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/8105155d

Branch: refs/heads/api-work
Commit: 8105155d0d40ffbc71c5b5d17b2329c5b19a4c65
Parents: c8c94d1
Author: Shad Storhaug <shad@shadstorhaug.com>
Authored: Sun Apr 2 10:16:04 2017 +0700
Committer: Shad Storhaug <shad@shadstorhaug.com>
Committed: Sun Apr 2 11:58:33 2017 +0700

----------------------------------------------------------------------
 .../BlockTerms/BlockTermsReader.cs              | 37 +++++++++++---------
 .../Bloom/BloomFilteringPostingsFormat.cs       |  7 ++--
 .../Memory/DirectPostingsFormat.cs              |  2 +-
 .../Memory/FSTOrdTermsReader.cs                 | 17 +++++----
 src/Lucene.Net.Codecs/Memory/FSTTermsReader.cs  | 17 +++++----
 .../Memory/MemoryPostingsFormat.cs              | 11 +++---
 .../SimpleText/SimpleTextFieldsReader.cs        |  7 ++--
 .../Codecs/BlockTreeTermsReader.cs              | 21 ++++++-----
 src/Lucene.Net.Core/Codecs/FieldsProducer.cs    |  9 ++++-
 .../Codecs/Lucene3x/Lucene3xFields.cs           |  7 ++--
 .../Codecs/PerField/PerFieldPostingsFormat.cs   |  7 ++--
 .../Codecs/Asserting/AssertingPostingsFormat.cs |  9 ++---
 .../Codecs/RAMOnly/RAMOnlyPostingsFormat.cs     |  2 +-
 13 files changed, 92 insertions(+), 61 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8105155d/src/Lucene.Net.Codecs/BlockTerms/BlockTermsReader.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Codecs/BlockTerms/BlockTermsReader.cs b/src/Lucene.Net.Codecs/BlockTerms/BlockTermsReader.cs
index 8dd48d7..631349c 100644
--- a/src/Lucene.Net.Codecs/BlockTerms/BlockTermsReader.cs
+++ b/src/Lucene.Net.Codecs/BlockTerms/BlockTermsReader.cs
@@ -190,36 +190,39 @@ namespace Lucene.Net.Codecs.BlockTerms
             input.Seek(dirOffset);
         }
 
-        public override void Dispose()
+        protected override void Dispose(bool disposing)
         {
-            try
+            if (disposing)
             {
                 try
                 {
-                    if (indexReader != null)
+                    try
+                    {
+                        if (indexReader != null)
+                        {
+                            indexReader.Dispose();
+                        }
+                    }
+                    finally
                     {
-                        indexReader.Dispose();
+                        // null so if an app hangs on to us (ie, we are not
+                        // GCable, despite being closed) we still free most
+                        // ram
+                        indexReader = null;
+                        if (input != null)
+                        {
+                            input.Dispose();
+                        }
                     }
                 }
                 finally
                 {
-                    // null so if an app hangs on to us (ie, we are not
-                    // GCable, despite being closed) we still free most
-                    // ram
-                    indexReader = null;
-                    if (input != null)
+                    if (postingsReader != null)
                     {
-                        input.Dispose();
+                        postingsReader.Dispose();
                     }
                 }
             }
-            finally
-            {
-                if (postingsReader != null)
-                {
-                    postingsReader.Dispose();
-                }
-            }
         }
 
         public override IEnumerator<string> GetEnumerator()

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8105155d/src/Lucene.Net.Codecs/Bloom/BloomFilteringPostingsFormat.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Codecs/Bloom/BloomFilteringPostingsFormat.cs b/src/Lucene.Net.Codecs/Bloom/BloomFilteringPostingsFormat.cs
index 204eb16..2f9b2c4 100644
--- a/src/Lucene.Net.Codecs/Bloom/BloomFilteringPostingsFormat.cs
+++ b/src/Lucene.Net.Codecs/Bloom/BloomFilteringPostingsFormat.cs
@@ -185,9 +185,12 @@ namespace Lucene.Net.Codecs.Bloom
                 return _delegateFieldsProducer.GetEnumerator();
             }
 
-            public override void Dispose()
+            protected override void Dispose(bool disposing)
             {
-                _delegateFieldsProducer.Dispose();
+                if (disposing)
+                {
+                    _delegateFieldsProducer.Dispose();
+                }
             }
 
             public override Terms GetTerms(string field)

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8105155d/src/Lucene.Net.Codecs/Memory/DirectPostingsFormat.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Codecs/Memory/DirectPostingsFormat.cs b/src/Lucene.Net.Codecs/Memory/DirectPostingsFormat.cs
index 0cb8e26..95211d2 100644
--- a/src/Lucene.Net.Codecs/Memory/DirectPostingsFormat.cs
+++ b/src/Lucene.Net.Codecs/Memory/DirectPostingsFormat.cs
@@ -171,7 +171,7 @@ namespace Lucene.Net.Codecs.Memory
                 }
             }
 
-            public override void Dispose()
+            protected override void Dispose(bool disposing)
             {
             }
 

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8105155d/src/Lucene.Net.Codecs/Memory/FSTOrdTermsReader.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Codecs/Memory/FSTOrdTermsReader.cs b/src/Lucene.Net.Codecs/Memory/FSTOrdTermsReader.cs
index 85754de..ccf7a59 100644
--- a/src/Lucene.Net.Codecs/Memory/FSTOrdTermsReader.cs
+++ b/src/Lucene.Net.Codecs/Memory/FSTOrdTermsReader.cs
@@ -180,15 +180,18 @@ namespace Lucene.Net.Codecs.Memory
             }
         }
 
-        public override void Dispose()
+        protected override void Dispose(bool disposing)
         {
-            try
-            {
-                IOUtils.Close(postingsReader);
-            }
-            finally
+            if (disposing)
             {
-                fields.Clear();
+                try
+                {
+                    IOUtils.Close(postingsReader);
+                }
+                finally
+                {
+                    fields.Clear();
+                }
             }
         }
 

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8105155d/src/Lucene.Net.Codecs/Memory/FSTTermsReader.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Codecs/Memory/FSTTermsReader.cs b/src/Lucene.Net.Codecs/Memory/FSTTermsReader.cs
index 7d732da..5141776 100644
--- a/src/Lucene.Net.Codecs/Memory/FSTTermsReader.cs
+++ b/src/Lucene.Net.Codecs/Memory/FSTTermsReader.cs
@@ -176,15 +176,18 @@ namespace Lucene.Net.Codecs.Memory
             get { return fields.Count; }
         }
 
-        public override void Dispose()
+        protected override void Dispose(bool disposing)
         {
-            try
-            {
-                IOUtils.Close(postingsReader);
-            }
-            finally
+            if (disposing)
             {
-                fields.Clear();
+                try
+                {
+                    IOUtils.Close(postingsReader);
+                }
+                finally
+                {
+                    fields.Clear();
+                }
             }
         }
 

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8105155d/src/Lucene.Net.Codecs/Memory/MemoryPostingsFormat.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Codecs/Memory/MemoryPostingsFormat.cs b/src/Lucene.Net.Codecs/Memory/MemoryPostingsFormat.cs
index 55a7e6f..d617030 100644
--- a/src/Lucene.Net.Codecs/Memory/MemoryPostingsFormat.cs
+++ b/src/Lucene.Net.Codecs/Memory/MemoryPostingsFormat.cs
@@ -1089,12 +1089,15 @@ namespace Lucene.Net.Codecs.Memory
                 }
             }
 
-            public override void Dispose()
+            protected override void Dispose(bool disposing)
             {
-                // Drop ref to FST:
-                foreach (var field in _fields)
+                if (disposing)
                 {
-                    field.Value.fst = null;
+                    // Drop ref to FST:
+                    foreach (var field in _fields)
+                    {
+                        field.Value.fst = null;
+                    }
                 }
             }
 

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8105155d/src/Lucene.Net.Codecs/SimpleText/SimpleTextFieldsReader.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Codecs/SimpleText/SimpleTextFieldsReader.cs b/src/Lucene.Net.Codecs/SimpleText/SimpleTextFieldsReader.cs
index 1650adb..5d57d4f 100644
--- a/src/Lucene.Net.Codecs/SimpleText/SimpleTextFieldsReader.cs
+++ b/src/Lucene.Net.Codecs/SimpleText/SimpleTextFieldsReader.cs
@@ -768,9 +768,12 @@ namespace Lucene.Net.Codecs.SimpleText
             get { return -1; }
         }
 
-        public override void Dispose()
+        protected override void Dispose(bool disposing)
         {
-            _input.Dispose();
+            if (disposing)
+            {
+                _input.Dispose();
+            }
         }
 
         public override long RamBytesUsed()

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8105155d/src/Lucene.Net.Core/Codecs/BlockTreeTermsReader.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Codecs/BlockTreeTermsReader.cs b/src/Lucene.Net.Core/Codecs/BlockTreeTermsReader.cs
index 98bd36c..fedc3b0 100644
--- a/src/Lucene.Net.Core/Codecs/BlockTreeTermsReader.cs
+++ b/src/Lucene.Net.Core/Codecs/BlockTreeTermsReader.cs
@@ -263,17 +263,20 @@ namespace Lucene.Net.Codecs
         //   return "0x" + Integer.toHexString(v);
         // }
 
-        public override void Dispose()
+        protected override void Dispose(bool disposing)
         {
-            try
+            if (disposing)
             {
-                IOUtils.Close(@in, postingsReader);
-            }
-            finally
-            {
-                // Clear so refs to terms index is GCable even if
-                // app hangs onto us:
-                fields.Clear();
+                try
+                {
+                    IOUtils.Close(@in, postingsReader);
+                }
+                finally
+                {
+                    // Clear so refs to terms index is GCable even if
+                    // app hangs onto us:
+                    fields.Clear();
+                }
             }
         }
 

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8105155d/src/Lucene.Net.Core/Codecs/FieldsProducer.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Codecs/FieldsProducer.cs b/src/Lucene.Net.Core/Codecs/FieldsProducer.cs
index 540c124..d2b8f8a 100644
--- a/src/Lucene.Net.Core/Codecs/FieldsProducer.cs
+++ b/src/Lucene.Net.Core/Codecs/FieldsProducer.cs
@@ -38,7 +38,14 @@ namespace Lucene.Net.Codecs
         {
         }
 
-        public abstract void Dispose();
+        // LUCENENET specific - implementing proper dispose pattern
+        public void Dispose()
+        {
+            Dispose(true);
+            GC.SuppressFinalize(this);
+        }
+
+        protected abstract void Dispose(bool disposing);
 
         /// <summary>
         /// Returns approximate RAM bytes used </summary>

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8105155d/src/Lucene.Net.Core/Codecs/Lucene3x/Lucene3xFields.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Codecs/Lucene3x/Lucene3xFields.cs b/src/Lucene.Net.Core/Codecs/Lucene3x/Lucene3xFields.cs
index 648c5b2..a7a52a5 100644
--- a/src/Lucene.Net.Core/Codecs/Lucene3x/Lucene3xFields.cs
+++ b/src/Lucene.Net.Core/Codecs/Lucene3x/Lucene3xFields.cs
@@ -194,9 +194,12 @@ namespace Lucene.Net.Codecs.Lucene3x
             }
         }
 
-        public override void Dispose()
+        protected override void Dispose(bool disposing)
         {
-            IOUtils.Close(Tis, TisNoIndex, /*cfsReader,*/ FreqStream, ProxStream); // LUCENENET
NOTE: cfsReader not used
+            if (disposing)
+            {
+                IOUtils.Close(Tis, TisNoIndex, /*cfsReader,*/ FreqStream, ProxStream); //
LUCENENET NOTE: cfsReader not used
+            }
         }
 
         private class PreTerms : Terms

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8105155d/src/Lucene.Net.Core/Codecs/PerField/PerFieldPostingsFormat.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Codecs/PerField/PerFieldPostingsFormat.cs b/src/Lucene.Net.Core/Codecs/PerField/PerFieldPostingsFormat.cs
index 3674bd6..e45056f 100644
--- a/src/Lucene.Net.Core/Codecs/PerField/PerFieldPostingsFormat.cs
+++ b/src/Lucene.Net.Core/Codecs/PerField/PerFieldPostingsFormat.cs
@@ -254,9 +254,12 @@ namespace Lucene.Net.Codecs.PerField
                 get { return fields.Count; }
             }
 
-            public override void Dispose()
+            protected override void Dispose(bool disposing)
             {
-                IOUtils.Close(formats.Values);
+                if (disposing)
+                {
+                    IOUtils.Close(formats.Values);
+                }
             }
 
             public override long RamBytesUsed()

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8105155d/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingPostingsFormat.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingPostingsFormat.cs b/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingPostingsFormat.cs
index 840c5bc..d2b0a8c 100644
--- a/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingPostingsFormat.cs
+++ b/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingPostingsFormat.cs
@@ -64,15 +64,12 @@ namespace Lucene.Net.Codecs.Asserting
                 this.@in = @in;
             }
 
-            public override void Dispose()
-            {
-                Dispose(true);
-            }
-
-            protected void Dispose(bool disposing)
+            protected override void Dispose(bool disposing)
             {
                 if (disposing)
+                {
                     @in.Dispose();
+                }
             }
 
             public override IEnumerator<string> GetEnumerator()

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8105155d/src/Lucene.Net.TestFramework/Codecs/RAMOnly/RAMOnlyPostingsFormat.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.TestFramework/Codecs/RAMOnly/RAMOnlyPostingsFormat.cs b/src/Lucene.Net.TestFramework/Codecs/RAMOnly/RAMOnlyPostingsFormat.cs
index 712d18d..fbf5a25 100644
--- a/src/Lucene.Net.TestFramework/Codecs/RAMOnly/RAMOnlyPostingsFormat.cs
+++ b/src/Lucene.Net.TestFramework/Codecs/RAMOnly/RAMOnlyPostingsFormat.cs
@@ -129,7 +129,7 @@ namespace Lucene.Net.Codecs.RAMOnly
                 return FieldToTerms.Keys.GetEnumerator();
             }
 
-            public override void Dispose()
+            protected override void Dispose(bool disposing)
             {
             }
 


Mime
View raw message