lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From synhers...@apache.org
Subject [1/2] lucenenet git commit: synchronize access to underlying file stream for NIOFSDirectory
Date Mon, 18 May 2015 06:28:00 GMT
Repository: lucenenet
Updated Branches:
  refs/heads/master cd0a14aea -> 40684b823


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/ae1fc2a3
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/ae1fc2a3
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/ae1fc2a3

Branch: refs/heads/master
Commit: ae1fc2a32b4eb3a347b301c8cb62dfad85acd6db
Parents: e3833d6
Author: Laimonas Simutis <laimis@gmail.com>
Authored: Sun May 17 12:10:26 2015 -0400
Committer: Laimonas Simutis <laimis@gmail.com>
Committed: Sun May 17 12:10:26 2015 -0400

----------------------------------------------------------------------
 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/ae1fc2a3/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/ae1fc2a3/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


Mime
View raw message