lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nightowl...@apache.org
Subject [14/20] lucenenet git commit: Lucene.Net.Core.Store.IndexOutput: implemented .NET dispose pattern
Date Sun, 02 Apr 2017 07:06:14 GMT
Lucene.Net.Core.Store.IndexOutput: 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/0f71b2b1
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/0f71b2b1
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/0f71b2b1

Branch: refs/heads/api-work
Commit: 0f71b2b1814c535afd3654b60e89416043760c8f
Parents: 2ea33f7
Author: Shad Storhaug <shad@shadstorhaug.com>
Authored: Sun Apr 2 11:07:12 2017 +0700
Committer: Shad Storhaug <shad@shadstorhaug.com>
Committed: Sun Apr 2 11:58:35 2017 +0700

----------------------------------------------------------------------
 .../Store/BufferedIndexOutput.cs                |  7 ++--
 src/Lucene.Net.Core/Store/CompoundFileWriter.cs |  4 +--
 src/Lucene.Net.Core/Store/FSDirectory.cs        | 37 +++++++++++---------
 src/Lucene.Net.Core/Store/IndexOutput.cs        | 11 +++++-
 src/Lucene.Net.Core/Store/RAMOutputStream.cs    |  7 ++--
 .../Store/RateLimitedIndexOutput.cs             | 17 +++++----
 .../Store/MockDirectoryWrapper.cs               | 17 +++++----
 .../Store/MockIndexOutputWrapper.cs             | 31 ++++++++--------
 .../Util/ThrottledIndexOutput.cs                | 17 +++++----
 src/Lucene.Net.Tests.Facet/SlowRAMDirectory.cs  |  7 ++--
 10 files changed, 94 insertions(+), 61 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0f71b2b1/src/Lucene.Net.Core/Store/BufferedIndexOutput.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Store/BufferedIndexOutput.cs b/src/Lucene.Net.Core/Store/BufferedIndexOutput.cs
index 0acaf82..16fa6c8 100644
--- a/src/Lucene.Net.Core/Store/BufferedIndexOutput.cs
+++ b/src/Lucene.Net.Core/Store/BufferedIndexOutput.cs
@@ -148,9 +148,12 @@ namespace Lucene.Net.Store
         /// <param name="len"> the number of bytes to write </param>
         protected internal abstract void FlushBuffer(byte[] b, int offset, int len);
 
-        public override void Dispose()
+        protected override void Dispose(bool disposing)
         {
-            Flush();
+            if (disposing)
+            {
+                Flush();
+            }
         }
 
         public override long GetFilePointer()

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0f71b2b1/src/Lucene.Net.Core/Store/CompoundFileWriter.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Store/CompoundFileWriter.cs b/src/Lucene.Net.Core/Store/CompoundFileWriter.cs
index f2166b8..c4a6827 100644
--- a/src/Lucene.Net.Core/Store/CompoundFileWriter.cs
+++ b/src/Lucene.Net.Core/Store/CompoundFileWriter.cs
@@ -386,9 +386,9 @@ namespace Lucene.Net.Store
                 @delegate.Flush();
             }
 
-            public override void Dispose()
+            protected override void Dispose(bool disposing)
             {
-                if (!closed)
+                if (disposing && !closed)
                 {
                     closed = true;
                     entry.Length = writtenBytes;

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0f71b2b1/src/Lucene.Net.Core/Store/FSDirectory.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Store/FSDirectory.cs b/src/Lucene.Net.Core/Store/FSDirectory.cs
index 41334b3..c3a7086 100644
--- a/src/Lucene.Net.Core/Store/FSDirectory.cs
+++ b/src/Lucene.Net.Core/Store/FSDirectory.cs
@@ -483,25 +483,28 @@ namespace Lucene.Net.Store
                 //Debug.Assert(size == 0);
             }
 
-            public override void Dispose()
+            protected override void Dispose(bool disposing)
             {
-                parent.OnIndexOutputClosed(this);
-                // only close the file if it has not been closed yet
-                if (isOpen)
-                {
-                    IOException priorE = null;
-                    try
-                    {
-                        base.Dispose();
-                    }
-                    catch (IOException ioe)
-                    {
-                        priorE = ioe;
-                    }
-                    finally
+                if (disposing)
+                { 
+                    parent.OnIndexOutputClosed(this);
+                    // only close the file if it has not been closed yet
+                    if (isOpen)
                     {
-                        isOpen = false;
-                        IOUtils.CloseWhileHandlingException(priorE, file);
+                        IOException priorE = null;
+                        try
+                        {
+                            base.Dispose();
+                        }
+                        catch (IOException ioe)
+                        {
+                            priorE = ioe;
+                        }
+                        finally
+                        {
+                            isOpen = false;
+                            IOUtils.CloseWhileHandlingException(priorE, file);
+                        }
                     }
                 }
             }

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0f71b2b1/src/Lucene.Net.Core/Store/IndexOutput.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Store/IndexOutput.cs b/src/Lucene.Net.Core/Store/IndexOutput.cs
index 065f677..3c2adb2 100644
--- a/src/Lucene.Net.Core/Store/IndexOutput.cs
+++ b/src/Lucene.Net.Core/Store/IndexOutput.cs
@@ -39,7 +39,16 @@ namespace Lucene.Net.Store
 
         /// <summary>
         /// Closes this stream to further operations. </summary>
-        public abstract void Dispose();
+        // LUCENENET specific - implementing proper dispose pattern
+        public void Dispose()
+        {
+            Dispose(true);
+            GC.SuppressFinalize(this);
+        }
+
+        /// <summary>
+        /// Closes this stream to further operations. </summary>
+        protected abstract void Dispose(bool disposing);
 
         /// <summary>
         /// Returns the current position in this file, where the next write will

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0f71b2b1/src/Lucene.Net.Core/Store/RAMOutputStream.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Store/RAMOutputStream.cs b/src/Lucene.Net.Core/Store/RAMOutputStream.cs
index 19f6b89..4545392 100644
--- a/src/Lucene.Net.Core/Store/RAMOutputStream.cs
+++ b/src/Lucene.Net.Core/Store/RAMOutputStream.cs
@@ -120,9 +120,12 @@ namespace Lucene.Net.Store
             crc.Reset();
         }
 
-        public override void Dispose()
+        protected override void Dispose(bool disposing)
         {
-            Flush();
+            if (disposing)
+            {
+                Flush();
+            }
         }
 
         [Obsolete("(4.1) this method will be removed in Lucene 5.0")]

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0f71b2b1/src/Lucene.Net.Core/Store/RateLimitedIndexOutput.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Store/RateLimitedIndexOutput.cs b/src/Lucene.Net.Core/Store/RateLimitedIndexOutput.cs
index 93014a6..367a57d 100644
--- a/src/Lucene.Net.Core/Store/RateLimitedIndexOutput.cs
+++ b/src/Lucene.Net.Core/Store/RateLimitedIndexOutput.cs
@@ -89,15 +89,18 @@ namespace Lucene.Net.Store
             }
         }
 
-        public override void Dispose()
+        protected override void Dispose(bool disposing)
         {
-            try
-            {
-                base.Dispose();
-            }
-            finally
+            if (disposing)
             {
-                @delegate.Dispose();
+                try
+                {
+                    base.Dispose();
+                }
+                finally
+                {
+                    @delegate.Dispose();
+                }
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0f71b2b1/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs b/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs
index 1c6eb46..8f49463 100644
--- a/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs
+++ b/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs
@@ -1426,15 +1426,18 @@ namespace Lucene.Net.Store
                 }
             }
 
-            public override void Dispose()
+            protected override void Dispose(bool disposing)
             {
-                try
+                if (disposing)
                 {
-                    base.Dispose();
-                }
-                finally
-                {
-                    Io.Dispose();
+                    try
+                    {
+                        base.Dispose();
+                    }
+                    finally
+                    {
+                        Io.Dispose();
+                    }
                 }
             }
         }

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0f71b2b1/src/Lucene.Net.TestFramework/Store/MockIndexOutputWrapper.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.TestFramework/Store/MockIndexOutputWrapper.cs b/src/Lucene.Net.TestFramework/Store/MockIndexOutputWrapper.cs
index 1c935f3..610095a 100644
--- a/src/Lucene.Net.TestFramework/Store/MockIndexOutputWrapper.cs
+++ b/src/Lucene.Net.TestFramework/Store/MockIndexOutputWrapper.cs
@@ -101,26 +101,29 @@ namespace Lucene.Net.Store
             }
         }
 
-        public override void Dispose()
+        protected override void Dispose(bool disposing)
         {
-            try
+            if (disposing)
             {
-                Dir.MaybeThrowDeterministicException();
-            }
-            finally
-            {
-                @delegate.Dispose();
-                if (Dir.TrackDiskUsage_Renamed)
+                try
+                {
+                    Dir.MaybeThrowDeterministicException();
+                }
+                finally
                 {
-                    // Now compute actual disk usage & track the maxUsedSize
-                    // in the MockDirectoryWrapper:
-                    long size = Dir.RecomputedActualSizeInBytes;
-                    if (size > Dir.MaxUsedSize)
+                    @delegate.Dispose();
+                    if (Dir.TrackDiskUsage_Renamed)
                     {
-                        Dir.MaxUsedSize = size;
+                        // Now compute actual disk usage & track the maxUsedSize
+                        // in the MockDirectoryWrapper:
+                        long size = Dir.RecomputedActualSizeInBytes;
+                        if (size > Dir.MaxUsedSize)
+                        {
+                            Dir.MaxUsedSize = size;
+                        }
                     }
+                    Dir.RemoveIndexOutput(this, Name);
                 }
-                Dir.RemoveIndexOutput(this, Name);
             }
         }
 

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0f71b2b1/src/Lucene.Net.TestFramework/Util/ThrottledIndexOutput.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.TestFramework/Util/ThrottledIndexOutput.cs b/src/Lucene.Net.TestFramework/Util/ThrottledIndexOutput.cs
index 6d71bac..e169e50 100644
--- a/src/Lucene.Net.TestFramework/Util/ThrottledIndexOutput.cs
+++ b/src/Lucene.Net.TestFramework/Util/ThrottledIndexOutput.cs
@@ -78,15 +78,18 @@ namespace Lucene.Net.Util
             @delegate.Flush();
         }
 
-        public override void Dispose()
+        protected override void Dispose(bool disposing)
         {
-            try
-            {
-                Sleep(CloseDelayMillis + GetDelay(true));
-            }
-            finally
+            if (disposing)
             {
-                @delegate.Dispose();
+                try
+                {
+                    Sleep(CloseDelayMillis + GetDelay(true));
+                }
+                finally
+                {
+                    @delegate.Dispose();
+                }
             }
         }
 

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0f71b2b1/src/Lucene.Net.Tests.Facet/SlowRAMDirectory.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests.Facet/SlowRAMDirectory.cs b/src/Lucene.Net.Tests.Facet/SlowRAMDirectory.cs
index 205704f..782d376 100644
--- a/src/Lucene.Net.Tests.Facet/SlowRAMDirectory.cs
+++ b/src/Lucene.Net.Tests.Facet/SlowRAMDirectory.cs
@@ -230,9 +230,12 @@ namespace Lucene.Net.Facet
                 io.WriteBytes(b, offset, length);
             }
 
-            public override void Dispose()
+            protected override void Dispose(bool disposing)
             {
-                io.Dispose();
+                if (disposing)
+                {
+                    io.Dispose();
+                }
             }
             public override void Flush()
             {


Mime
View raw message