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/05/18 08:27:18 UTC

lucenenet git commit: synchronize access to underlying file stream for NIOFSDirectory

Repository: lucenenet
Updated Branches:
  refs/heads/master d00a4262b -> cd0a14aea


synchronize access to underlying file stream for NIOFSDirectory


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

Branch: refs/heads/master
Commit: cd0a14aeaebf14f0549c92d6fd7ae1b3331eb638
Parents: d00a426
Author: Laimonas Simutis <la...@gmail.com>
Authored: Sun May 17 12:10:26 2015 -0400
Committer: Itamar Syn-Hershko <it...@code972.com>
Committed: Mon May 18 09:26:58 2015 +0300

----------------------------------------------------------------------
 src/Lucene.Net.Core/Store/NIOFSDirectory.cs     | 22 ++++--------
 .../Support/FileStreamExtensions.cs             | 37 +++++++++++---------
 2 files changed, 28 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/cd0a14ae/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 42fa5e6..e2a0a95 100644
--- a/src/Lucene.Net.Core/Store/NIOFSDirectory.cs
+++ b/src/Lucene.Net.Core/Store/NIOFSDirectory.cs
@@ -77,36 +77,28 @@ namespace Lucene.Net.Store
         public override IndexInput OpenInput(string name, IOContext context)
         {
             EnsureOpen();
-            //File path = new File(Directory, name);
-            FileInfo path = new FileInfo(Path.Combine(Directory.FullName, name));
-            //path.Create();
-            FileStream fc = new FileStream(path.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);//FileChannel.open(path.toPath(), StandardOpenOption.READ);
+            var path = new FileInfo(Path.Combine(Directory.FullName, name));
+            var fc = new FileStream(path.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
             return new NIOFSIndexInput("NIOFSIndexInput(path=\"" + path + "\")", fc, context);
-            //return new NIOFSIndexInput(new FileInfo(Path.Combine(Directory.FullName, name)), context, ReadChunkSize);
         }
 
         public override IndexInputSlicer CreateSlicer(string name, IOContext context)
         {
             EnsureOpen();
-            //File path = new File(Directory, name);
-            //FileStream descriptor = FileChannel.open(path.toPath(), StandardOpenOption.READ);
-            FileInfo path = new FileInfo(Path.Combine(Directory.FullName, name));
-            FileStream fc = new FileStream(path.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
+            var path = new FileInfo(Path.Combine(Directory.FullName, name));
+            var fc = new FileStream(path.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
             return new IndexInputSlicerAnonymousInnerClassHelper(this, context, path, fc);
         }
 
-        private class IndexInputSlicerAnonymousInnerClassHelper : Directory.IndexInputSlicer
+        private class IndexInputSlicerAnonymousInnerClassHelper : IndexInputSlicer
         {
-            private readonly NIOFSDirectory OuterInstance;
-
             private readonly IOContext Context;
             private readonly FileInfo Path;
             private readonly FileStream Descriptor;
 
-            public IndexInputSlicerAnonymousInnerClassHelper(NIOFSDirectory outerInstance, Lucene.Net.Store.IOContext context, FileInfo path, FileStream descriptor)
+            public IndexInputSlicerAnonymousInnerClassHelper(NIOFSDirectory outerInstance, IOContext context, FileInfo path, FileStream descriptor)
                 : base(outerInstance)
             {
-                this.OuterInstance = outerInstance;
                 this.Context = context;
                 this.Path = path;
                 this.Descriptor = descriptor;
@@ -131,7 +123,7 @@ namespace Lucene.Net.Store
                 {
                     return OpenSlice("full-slice", 0, Descriptor.Length);
                 }
-                catch (System.IO.IOException ex)
+                catch (IOException ex)
                 {
                     throw new Exception(ex.Message, ex);
                 }

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/cd0a14ae/src/Lucene.Net.Core/Support/FileStreamExtensions.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Support/FileStreamExtensions.cs b/src/Lucene.Net.Core/Support/FileStreamExtensions.cs
index c305d85..1e9fc14 100644
--- a/src/Lucene.Net.Core/Support/FileStreamExtensions.cs
+++ b/src/Lucene.Net.Core/Support/FileStreamExtensions.cs
@@ -4,31 +4,36 @@ namespace Lucene.Net.Support
 {
     public static class FileStreamExtensions
     {
+        private static object _fsReadLock = new object();
+
         //Reads bytes from the Filestream into the bytebuffer
         public static int Read(this FileStream file, ByteBuffer dst, long position)
         {
-            // TODO: check this logic, could probably optimize
-            if (position >= file.Length)
-                return 0;
+            lock (_fsReadLock)
+            {
+                // TODO: check this logic, could probably optimize
+                if (position >= file.Length)
+                    return 0;
 
-            var original = file.Position;
+                var original = file.Position;
 
-            file.Seek(position, SeekOrigin.Begin);
+                file.Seek(position, SeekOrigin.Begin);
 
-            int count = 0;
+                int count = 0;
 
-            for (int i = dst.Position; i < dst.Limit; i++)
-            {
-                int v = file.ReadByte();
-                if (v == -1)
-                    break;
-                dst.Put((byte)v);
-                count++;
-            }
+                for (int i = dst.Position; i < dst.Limit; i++)
+                {
+                    int v = file.ReadByte();
+                    if (v == -1)
+                        break;
+                    dst.Put((byte) v);
+                    count++;
+                }
 
-            file.Seek(original, SeekOrigin.Begin);
+                file.Seek(original, SeekOrigin.Begin);
 
-            return count;
+                return count;
+            }
         }
     }
 }
\ No newline at end of file