You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by sy...@apache.org on 2015/01/06 04:46:30 UTC

[14/18] lucenenet git commit: NativeFSLocks in Java are de facto singletons

NativeFSLocks in Java are de facto singletons

We need to mimick that behavior otherwise IW.IsLocked and IW.Unlock aren't going to work

There may be holes in this current implementation since we may be storing Lock instances of failed locks, but lets go with this for now


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

Branch: refs/heads/master
Commit: fbabfe9ecedd10837136d0aa37011dbfacebfc67
Parents: b0bbd9e
Author: Itamar Syn-Hershko <it...@code972.com>
Authored: Mon Jan 5 19:05:40 2015 +0200
Committer: Itamar Syn-Hershko <it...@code972.com>
Committed: Mon Jan 5 19:05:40 2015 +0200

----------------------------------------------------------------------
 .../Store/NativeFSLockFactory.cs                | 21 +++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/fbabfe9e/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 f7c370e..0baf36c 100644
--- a/src/Lucene.Net.Core/Store/NativeFSLockFactory.cs
+++ b/src/Lucene.Net.Core/Store/NativeFSLockFactory.cs
@@ -96,16 +96,18 @@ namespace Lucene.Net.Store
             LockDir = lockDir;
         }
 
+        // LUCENENET NativeFSLocks in Java are infact singletons; this is how we mimick that to track instances and make sure
+        // IW.Unlock and IW.IsLocked works correctly
+        internal readonly ConcurrentDictionary<string, NativeFSLock> _locks = new ConcurrentDictionary<string, NativeFSLock>(); 
+
         public override Lock MakeLock(string lockName)
         {
-            lock (this)
+            if (LockPrefix_Renamed != null)
             {
-                if (LockPrefix_Renamed != null)
-                {
-                    lockName = LockPrefix_Renamed + "-" + lockName;
-                }
-                return new NativeFSLock(LockDir_Renamed, lockName);
+                lockName = LockPrefix_Renamed + "-" + lockName;
             }
+
+            return _locks.GetOrAdd(lockName, (s) => new NativeFSLock(this, LockDir_Renamed, s));
         }
 
         public override void ClearLock(string lockName)
@@ -118,10 +120,12 @@ namespace Lucene.Net.Store
     {
         private FileStream Channel;
         private readonly DirectoryInfo Path;
+        private readonly NativeFSLockFactory _creatingInstance;
         private readonly DirectoryInfo LockDir;
 
-        public NativeFSLock(DirectoryInfo lockDir, string lockFileName)
+        public NativeFSLock(NativeFSLockFactory creatingInstance, DirectoryInfo lockDir, string lockFileName)
         {
+            _creatingInstance = creatingInstance;
             this.LockDir = lockDir;
             Path = new DirectoryInfo(System.IO.Path.Combine(lockDir.FullName, lockFileName));
         }
@@ -200,6 +204,9 @@ namespace Lucene.Net.Store
                     try
                     {
                         Channel.Unlock(0, Channel.Length);
+
+                        NativeFSLock _;
+                        _creatingInstance._locks.TryRemove(Path.FullName, out _);
                     }
                     finally
                     {