lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From synhers...@apache.org
Subject [11/18] lucenenet git commit: Better NativeFSLock, phase 2
Date Tue, 06 Jan 2015 03:46:27 GMT
Better NativeFSLock, phase 2

Now properly dispose of unused FileStreams


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

Branch: refs/heads/master
Commit: d71f71434887749e3dfcde0f53ef3967c230b19d
Parents: ca7e09b
Author: Itamar Syn-Hershko <itamar@code972.com>
Authored: Mon Jan 5 18:24:55 2015 +0200
Committer: Itamar Syn-Hershko <itamar@code972.com>
Committed: Mon Jan 5 18:24:55 2015 +0200

----------------------------------------------------------------------
 .../Store/NativeFSLockFactory.cs                | 52 ++++++--------------
 src/Lucene.Net.Core/Util/IOUtils.cs             |  2 +-
 2 files changed, 15 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/d71f7143/src/Lucene.Net.Core/Store/NativeFSLockFactory.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Store/NativeFSLockFactory.cs b/src/Lucene.Net.Core/Store/NativeFSLockFactory.cs
index fd98b94..f7c370e 100644
--- a/src/Lucene.Net.Core/Store/NativeFSLockFactory.cs
+++ b/src/Lucene.Net.Core/Store/NativeFSLockFactory.cs
@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Concurrent;
+using Lucene.Net.Support;
 using Lucene.Net.Util;
 
 namespace Lucene.Net.Store
@@ -119,18 +120,6 @@ namespace Lucene.Net.Store
         private readonly DirectoryInfo Path;
         private readonly DirectoryInfo LockDir;
 
-      /*
-       * The javadocs for FileChannel state that you should have
-       * a single instance of a FileChannel (per JVM) for all
-       * locking against a given file.  To ensure this, we have
-       * a single (static) HashSet that contains the file paths
-       * of all currently locked locks.  This protects against
-       * possible cases where different Directory instances in
-       * one JVM (each with their own NativeFSLockFactory
-       * instance) have set the same lock dir and lock prefix.
-       */
-        private static readonly ConcurrentDictionary<string, WeakReference<FileStream>>
LOCKS_HELD = new ConcurrentDictionary<string, WeakReference<FileStream>>();
-
         public NativeFSLock(DirectoryInfo lockDir, string lockFileName)
         {
             this.LockDir = lockDir;
@@ -141,14 +130,14 @@ namespace Lucene.Net.Store
         {
             lock (this)
             {
+                FailureReason = null;
+
                 if (Channel != null)
                 {
                     // Our instance is already locked:
                     return false;
                 }
 
-                //LOCKS_HELD.GetOrAdd(Path.FullName)
-
                 if (!System.IO.Directory.Exists(LockDir.FullName))
                 {
                     try
@@ -160,12 +149,12 @@ namespace Lucene.Net.Store
                         throw new System.IO.IOException("Cannot create directory: " + LockDir.FullName);
                     }
                 }
-                else if (System.IO.File.Exists(LockDir.FullName))
+                else if (File.Exists(LockDir.FullName))
                 {
-                    throw new System.IO.IOException("Found regular file where directory expected:
" + LockDir.FullName);
+                    throw new IOException("Found regular file where directory expected: "
+ LockDir.FullName);
                 }
 
-                bool success = false;
+                var success = false;
                 try
                 {
                     Channel = new FileStream(Path.FullName, FileMode.Create, FileAccess.Write,
FileShare.None);
@@ -175,30 +164,26 @@ namespace Lucene.Net.Store
                 catch (IOException e)
                 {
                     FailureReason = e;
+                    IOUtils.CloseWhileHandlingException(Channel);
                     Channel = null;
                 }
                 // LUCENENET: UnauthorizedAccessException does not derive from IOException
like in java
-                catch (System.UnauthorizedAccessException e)
+                catch (UnauthorizedAccessException e)
                 {
                     // On Windows, we can get intermittent "Access
                     // Denied" here.  So, we treat this as failure to
                     // acquire the lock, but, store the reason in case
                     // there is in fact a real error case.
                     FailureReason = e;
+                    IOUtils.CloseWhileHandlingException(Channel);
                     Channel = null;
                 }
                 finally
                 {
                     if (!success)
                     {
-                        try
-                        {
-                            IOUtils.CloseWhileHandlingException(Channel);
-                        }
-                        finally
-                        {
-                            Channel = null;
-                        }
+                        IOUtils.CloseWhileHandlingException(Channel);
+                        Channel = null;
                     }
                 }
 
@@ -218,19 +203,10 @@ namespace Lucene.Net.Store
                     }
                     finally
                     {
-                        try
-                        {
-                            Channel.Close();
-                        }
-                        finally
-                        {
-                            Channel = null;
-//                            lock (LOCK_HELD)
-//                            {
-//                                LOCK_HELD.Remove(Path.FullName);
-//                            }
-                        }
+                        IOUtils.CloseWhileHandlingException(Channel);
+                        Channel = null;
                     }
+
                     bool tmpBool;
                     if (File.Exists(Path.FullName))
                     {

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/d71f7143/src/Lucene.Net.Core/Util/IOUtils.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Util/IOUtils.cs b/src/Lucene.Net.Core/Util/IOUtils.cs
index e23d3cd..1c8e952 100644
--- a/src/Lucene.Net.Core/Util/IOUtils.cs
+++ b/src/Lucene.Net.Core/Util/IOUtils.cs
@@ -213,7 +213,7 @@ namespace Lucene.Net.Util
         ///          objects to call <tt>close()</tt> on </param>
         public static void CloseWhileHandlingException(params IDisposable[] objects)
         {
-            foreach (IDisposable o in objects)
+            foreach (var o in objects)
             {
                 try
                 {


Mime
View raw message