lucenenet-commits mailing list archives

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

Branch: refs/heads/api-work
Commit: de5e562a30e02e1d69df5e227ebae8bc79b76011
Parents: 6af91bd
Author: Shad Storhaug <shad@shadstorhaug.com>
Authored: Sun Apr 2 10:52:29 2017 +0700
Committer: Shad Storhaug <shad@shadstorhaug.com>
Committed: Sun Apr 2 11:58:34 2017 +0700

----------------------------------------------------------------------
 .../Codecs/MultiLevelSkipListReader.cs          |  7 ++-
 .../Store/BufferedChecksumIndexInput.cs         |  7 ++-
 .../Store/ByteBufferIndexInput.cs               | 61 ++++++++++----------
 src/Lucene.Net.Core/Store/Directory.cs          |  7 ++-
 src/Lucene.Net.Core/Store/IndexInput.cs         | 11 +++-
 src/Lucene.Net.Core/Store/MMapDirectory.cs      | 13 +++--
 src/Lucene.Net.Core/Store/NIOFSDirectory.cs     |  4 +-
 src/Lucene.Net.Core/Store/RAMInputStream.cs     |  2 +-
 src/Lucene.Net.Core/Store/SimpleFSDirectory.cs  |  4 +-
 .../Index/MockIndexInput.cs                     |  2 +-
 .../Store/MockIndexInputWrapper.cs              | 33 ++++++-----
 .../Store/SlowClosingMockIndexInputWrapper.cs   | 25 ++++----
 src/Lucene.Net.Tests.Facet/SlowRAMDirectory.cs  |  7 ++-
 src/Lucene.Net.Tests/Index/TestFieldsReader.cs  |  7 ++-
 .../Index/TestLazyProxSkipping.cs               |  7 ++-
 .../Index/TestMultiLevelSkipList.cs             |  7 ++-
 .../Store/TestBufferedIndexInput.cs             |  2 +-
 17 files changed, 124 insertions(+), 82 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/de5e562a/src/Lucene.Net.Core/Codecs/MultiLevelSkipListReader.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Codecs/MultiLevelSkipListReader.cs b/src/Lucene.Net.Core/Codecs/MultiLevelSkipListReader.cs
index 657b72d..0399aaa 100644
--- a/src/Lucene.Net.Core/Codecs/MultiLevelSkipListReader.cs
+++ b/src/Lucene.Net.Core/Codecs/MultiLevelSkipListReader.cs
@@ -338,9 +338,12 @@ namespace Lucene.Net.Codecs
                 input.ReadBytes(data, 0, length);
             }
 
-            public override void Dispose()
+            protected override void Dispose(bool disposing)
             {
-                data = null;
+                if (disposing)
+                {
+                    data = null;
+                }
             }
 
             public override long GetFilePointer()

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/de5e562a/src/Lucene.Net.Core/Store/BufferedChecksumIndexInput.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Store/BufferedChecksumIndexInput.cs b/src/Lucene.Net.Core/Store/BufferedChecksumIndexInput.cs
index ef91bc4..1099419 100644
--- a/src/Lucene.Net.Core/Store/BufferedChecksumIndexInput.cs
+++ b/src/Lucene.Net.Core/Store/BufferedChecksumIndexInput.cs
@@ -62,9 +62,12 @@ namespace Lucene.Net.Store
             }
         }
 
-        public override void Dispose()
+        protected override void Dispose(bool disposing)
         {
-            main.Dispose();
+            if (disposing)
+            {
+                main.Dispose();
+            }
         }
 
         public override long GetFilePointer()

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/de5e562a/src/Lucene.Net.Core/Store/ByteBufferIndexInput.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Store/ByteBufferIndexInput.cs b/src/Lucene.Net.Core/Store/ByteBufferIndexInput.cs
index d40b530..7b898ba 100644
--- a/src/Lucene.Net.Core/Store/ByteBufferIndexInput.cs
+++ b/src/Lucene.Net.Core/Store/ByteBufferIndexInput.cs
@@ -364,47 +364,50 @@ namespace Lucene.Net.Store
             curBufIndex = 0;
         }
 
-        public override void Dispose()
+        protected override void Dispose(bool disposing)
         {
-            try
+            if (disposing)
             {
-                if (buffers == null)
+                try
                 {
-                    return;
-                }
+                    if (buffers == null)
+                    {
+                        return;
+                    }
 
-                // make local copy, then un-set early
-                ByteBuffer[] bufs = buffers;
-                UnsetBuffers();
-                if (clones != null)
-                {
-                    clones.Remove(this);
-                }
+                    // make local copy, then un-set early
+                    ByteBuffer[] bufs = buffers;
+                    UnsetBuffers();
+                    if (clones != null)
+                    {
+                        clones.Remove(this);
+                    }
 
-                if (isClone)
-                {
-                    return;
-                }
+                    if (isClone)
+                    {
+                        return;
+                    }
 
-                // for extra safety unset also all clones' buffers:
-                if (clones != null)
-                {
-                    foreach (ByteBufferIndexInput clone in clones.Keys)
+                    // for extra safety unset also all clones' buffers:
+                    if (clones != null)
                     {
-                        clone.UnsetBuffers();
+                        foreach (ByteBufferIndexInput clone in clones.Keys)
+                        {
+                            clone.UnsetBuffers();
+                        }
+                        this.clones.Clear();
                     }
-                    this.clones.Clear();
-                }
 
-                foreach (ByteBuffer b in bufs)
+                    foreach (ByteBuffer b in bufs)
+                    {
+                        FreeBuffer(b);
+                    }
+                }
+                finally
                 {
-                    FreeBuffer(b);
+                    UnsetBuffers();
                 }
             }
-            finally
-            {
-                UnsetBuffers();
-            }
         }
 
         /// <summary>

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/de5e562a/src/Lucene.Net.Core/Store/Directory.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Store/Directory.cs b/src/Lucene.Net.Core/Store/Directory.cs
index fce0e46..2e848c9 100644
--- a/src/Lucene.Net.Core/Store/Directory.cs
+++ b/src/Lucene.Net.Core/Store/Directory.cs
@@ -389,9 +389,12 @@ namespace Lucene.Net.Store
             /// <summary>
             /// Closes the stream to further operations.
             /// </summary>
-            public override void Dispose()
+            protected override void Dispose(bool disposing)
             {
-                @base.Dispose();
+                if (disposing)
+                {
+                    @base.Dispose();
+                }
             }
 
             public override long Length

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/de5e562a/src/Lucene.Net.Core/Store/IndexInput.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Store/IndexInput.cs b/src/Lucene.Net.Core/Store/IndexInput.cs
index 99cd039..fae97a5 100644
--- a/src/Lucene.Net.Core/Store/IndexInput.cs
+++ b/src/Lucene.Net.Core/Store/IndexInput.cs
@@ -57,7 +57,16 @@ namespace Lucene.Net.Store
 
         /// <summary>
         /// Closes the 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 the stream to further operations. </summary>
+        protected abstract void Dispose(bool disposing);
 
         /// <summary>
         /// Returns the current position in this file, where the next read will

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/de5e562a/src/Lucene.Net.Core/Store/MMapDirectory.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Store/MMapDirectory.cs b/src/Lucene.Net.Core/Store/MMapDirectory.cs
index 9ebf25a..a0c920c 100644
--- a/src/Lucene.Net.Core/Store/MMapDirectory.cs
+++ b/src/Lucene.Net.Core/Store/MMapDirectory.cs
@@ -240,14 +240,17 @@ namespace Lucene.Net.Store
                 this.SetBuffers(outerInstance.Map(this, fc, 0, fc.Length));
             }
 
-            public override sealed void Dispose()
+            protected override /*sealed*/ void Dispose(bool disposing) // LUCENENET specific
- not marked sealed in case subclass needs to dispose
             {
-                if (null != this.memoryMappedFile)
+                if (disposing)
                 {
-                    this.memoryMappedFile.Dispose();
-                    this.memoryMappedFile = null;
+                    if (null != this.memoryMappedFile)
+                    {
+                        this.memoryMappedFile.Dispose();
+                        this.memoryMappedFile = null;
+                    }
+                    base.Dispose();
                 }
-                base.Dispose();
             }
 
             /// <summary>

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/de5e562a/src/Lucene.Net.Core/Store/NIOFSDirectory.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Store/NIOFSDirectory.cs b/src/Lucene.Net.Core/Store/NIOFSDirectory.cs
index faf27a9..6cc9aba 100644
--- a/src/Lucene.Net.Core/Store/NIOFSDirectory.cs
+++ b/src/Lucene.Net.Core/Store/NIOFSDirectory.cs
@@ -186,9 +186,9 @@ namespace Lucene.Net.Store
                 this.isClone = true;
             }
 
-            public override void Dispose()
+            protected override void Dispose(bool disposing)
             {
-                if (!isClone)
+                if (disposing && !isClone)
                 {
                     m_channel.Dispose();
                 }

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/de5e562a/src/Lucene.Net.Core/Store/RAMInputStream.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Store/RAMInputStream.cs b/src/Lucene.Net.Core/Store/RAMInputStream.cs
index 4a8a07e..623bd72 100644
--- a/src/Lucene.Net.Core/Store/RAMInputStream.cs
+++ b/src/Lucene.Net.Core/Store/RAMInputStream.cs
@@ -58,7 +58,7 @@ namespace Lucene.Net.Store
             currentBuffer = null;
         }
 
-        public override void Dispose()
+        protected override void Dispose(bool disposing)
         {
             // nothing to do here
         }

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/de5e562a/src/Lucene.Net.Core/Store/SimpleFSDirectory.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Store/SimpleFSDirectory.cs b/src/Lucene.Net.Core/Store/SimpleFSDirectory.cs
index e44e1b6..8bc927b 100644
--- a/src/Lucene.Net.Core/Store/SimpleFSDirectory.cs
+++ b/src/Lucene.Net.Core/Store/SimpleFSDirectory.cs
@@ -159,9 +159,9 @@ namespace Lucene.Net.Store
                 this.IsClone = true;
             }
 
-            public override void Dispose()
+            protected override void Dispose(bool disposing)
             {
-                if (!IsClone)
+                if (disposing && !IsClone)
                 {
                     m_file.Dispose();
                 }

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/de5e562a/src/Lucene.Net.TestFramework/Index/MockIndexInput.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.TestFramework/Index/MockIndexInput.cs b/src/Lucene.Net.TestFramework/Index/MockIndexInput.cs
index de745cb..88ab424 100644
--- a/src/Lucene.Net.TestFramework/Index/MockIndexInput.cs
+++ b/src/Lucene.Net.TestFramework/Index/MockIndexInput.cs
@@ -55,7 +55,7 @@ namespace Lucene.Net.Index
             Pointer += len;
         }
 
-        public override void Dispose()
+        protected override void Dispose(bool disposing)
         {
             // ignore
         }

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/de5e562a/src/Lucene.Net.TestFramework/Store/MockIndexInputWrapper.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.TestFramework/Store/MockIndexInputWrapper.cs b/src/Lucene.Net.TestFramework/Store/MockIndexInputWrapper.cs
index 45ec2dd..03c9ea1 100644
--- a/src/Lucene.Net.TestFramework/Store/MockIndexInputWrapper.cs
+++ b/src/Lucene.Net.TestFramework/Store/MockIndexInputWrapper.cs
@@ -43,24 +43,27 @@ namespace Lucene.Net.Store
             this.@delegate = @delegate;
         }
 
-        public override void Dispose()
+        protected override void Dispose(bool disposing)
         {
-            try
+            if (disposing)
             {
-                // turn on the following to look for leaks closing inputs,
-                // after fixing TestTransactions
-                // Dir.MaybeThrowDeterministicException();
-            }
-            finally
-            {
-                Closed = true;
-                @delegate.Dispose();
-                // Pending resolution on LUCENE-686 we may want to
-                // remove the conditional check so we also track that
-                // all clones get closed:
-                if (!IsClone)
+                try
+                {
+                    // turn on the following to look for leaks closing inputs,
+                    // after fixing TestTransactions
+                    // Dir.MaybeThrowDeterministicException();
+                }
+                finally
                 {
-                    Dir.RemoveIndexInput(this, Name);
+                    Closed = true;
+                    @delegate.Dispose();
+                    // Pending resolution on LUCENE-686 we may want to
+                    // remove the conditional check so we also track that
+                    // all clones get closed:
+                    if (!IsClone)
+                    {
+                        Dir.RemoveIndexInput(this, Name);
+                    }
                 }
             }
         }

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/de5e562a/src/Lucene.Net.TestFramework/Store/SlowClosingMockIndexInputWrapper.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.TestFramework/Store/SlowClosingMockIndexInputWrapper.cs b/src/Lucene.Net.TestFramework/Store/SlowClosingMockIndexInputWrapper.cs
index d59995d..72106a9 100644
--- a/src/Lucene.Net.TestFramework/Store/SlowClosingMockIndexInputWrapper.cs
+++ b/src/Lucene.Net.TestFramework/Store/SlowClosingMockIndexInputWrapper.cs
@@ -32,21 +32,24 @@ namespace Lucene.Net.Store
         {
         }
 
-        public override void Dispose()
+        protected override void Dispose(bool disposing)
         {
-            try
+            if (disposing)
             {
-                Thread.Sleep(50);
-            }
+                try
+                {
+                    Thread.Sleep(50);
+                }
 #if !NETSTANDARD
-            catch (ThreadInterruptedException ie)
-            {
-                throw new ThreadInterruptedException("Thread Interrupted Exception", ie);
-            }
+                catch (ThreadInterruptedException ie)
+                {
+                    throw new ThreadInterruptedException("Thread Interrupted Exception",
ie);
+                }
 #endif
-            finally
-            {
-                base.Dispose();
+                finally
+                {
+                    base.Dispose();
+                }
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/de5e562a/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 a137966..205704f 100644
--- a/src/Lucene.Net.Tests.Facet/SlowRAMDirectory.cs
+++ b/src/Lucene.Net.Tests.Facet/SlowRAMDirectory.cs
@@ -155,9 +155,12 @@ namespace Lucene.Net.Facet
             }
 
 
-            public override void Dispose()
+            protected override void Dispose(bool disposing)
             {
-                ii.Dispose();
+                if (disposing)
+                {
+                    ii.Dispose();
+                }
             }
             public override bool Equals(object o)
             {

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/de5e562a/src/Lucene.Net.Tests/Index/TestFieldsReader.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/Index/TestFieldsReader.cs b/src/Lucene.Net.Tests/Index/TestFieldsReader.cs
index 85379ce..c7b9619 100644
--- a/src/Lucene.Net.Tests/Index/TestFieldsReader.cs
+++ b/src/Lucene.Net.Tests/Index/TestFieldsReader.cs
@@ -205,9 +205,12 @@ namespace Lucene.Net.Index
                 get { return @delegate.Length; }
             }
 
-            public override void Dispose()
+            protected override void Dispose(bool disposing)
             {
-                @delegate.Dispose();
+                if (disposing)
+                {
+                    @delegate.Dispose();
+                }
             }
 
             public override object Clone()

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/de5e562a/src/Lucene.Net.Tests/Index/TestLazyProxSkipping.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/Index/TestLazyProxSkipping.cs b/src/Lucene.Net.Tests/Index/TestLazyProxSkipping.cs
index 04cf6b5..2553cf1 100644
--- a/src/Lucene.Net.Tests/Index/TestLazyProxSkipping.cs
+++ b/src/Lucene.Net.Tests/Index/TestLazyProxSkipping.cs
@@ -228,9 +228,12 @@ namespace Lucene.Net.Index
                 this.Input.ReadBytes(b, offset, len);
             }
 
-            public override void Dispose()
+            protected override void Dispose(bool disposing)
             {
-                this.Input.Dispose();
+                if (disposing)
+                {
+                    this.Input.Dispose();
+                }
             }
 
             public override long GetFilePointer()

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/de5e562a/src/Lucene.Net.Tests/Index/TestMultiLevelSkipList.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/Index/TestMultiLevelSkipList.cs b/src/Lucene.Net.Tests/Index/TestMultiLevelSkipList.cs
index b1de16d..11449d3 100644
--- a/src/Lucene.Net.Tests/Index/TestMultiLevelSkipList.cs
+++ b/src/Lucene.Net.Tests/Index/TestMultiLevelSkipList.cs
@@ -187,9 +187,12 @@ namespace Lucene.Net.Index
                 this.Input.ReadBytes(b, offset, len);
             }
 
-            public override void Dispose()
+            protected override void Dispose(bool disposing)
             {
-                this.Input.Dispose();
+                if (disposing)
+                {
+                    this.Input.Dispose();
+                }
             }
 
             public override long GetFilePointer()

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/de5e562a/src/Lucene.Net.Tests/Store/TestBufferedIndexInput.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/Store/TestBufferedIndexInput.cs b/src/Lucene.Net.Tests/Store/TestBufferedIndexInput.cs
index fad09a0..151c5df 100644
--- a/src/Lucene.Net.Tests/Store/TestBufferedIndexInput.cs
+++ b/src/Lucene.Net.Tests/Store/TestBufferedIndexInput.cs
@@ -252,7 +252,7 @@ namespace Lucene.Net.Store
                 this.Pos = pos;
             }
 
-            public override void Dispose()
+            protected override void Dispose(bool disposing)
             {
             }
 


Mime
View raw message