Return-Path: X-Original-To: apmail-lucenenet-commits-archive@www.apache.org Delivered-To: apmail-lucenenet-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id CE85A17F79 for ; Tue, 6 Jan 2015 03:46:17 +0000 (UTC) Received: (qmail 4953 invoked by uid 500); 6 Jan 2015 03:46:18 -0000 Delivered-To: apmail-lucenenet-commits-archive@lucenenet.apache.org Received: (qmail 4847 invoked by uid 500); 6 Jan 2015 03:46:18 -0000 Mailing-List: contact commits-help@lucenenet.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: lucene-net-dev@lucenenet.apache.org Delivered-To: mailing list commits@lucenenet.apache.org Received: (qmail 4670 invoked by uid 99); 6 Jan 2015 03:46:18 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 06 Jan 2015 03:46:18 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 825588C6411; Tue, 6 Jan 2015 03:46:18 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: synhershko@apache.org To: commits@lucenenet.apache.org Date: Tue, 06 Jan 2015 03:46:27 -0000 Message-Id: <20c5f3065f8e43c9b27255e40a10a0ce@git.apache.org> In-Reply-To: <182cdbb11c7749c6bea4975c0322f92a@git.apache.org> References: <182cdbb11c7749c6bea4975c0322f92a@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [11/18] lucenenet git commit: Better NativeFSLock, phase 2 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 Authored: Mon Jan 5 18:24:55 2015 +0200 Committer: Itamar Syn-Hershko 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> LOCKS_HELD = new ConcurrentDictionary>(); - 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 close() on public static void CloseWhileHandlingException(params IDisposable[] objects) { - foreach (IDisposable o in objects) + foreach (var o in objects) { try {