From ae1fc2a32b4eb3a347b301c8cb62dfad85acd6db Mon Sep 17 00:00:00 2001 From: Laimonas Simutis Date: Sun, 17 May 2015 12:10:26 -0400 Subject: [PATCH] synchronize access to underlying file stream for NIOFSDirectory --- src/Lucene.Net.Core/Store/NIOFSDirectory.cs | 22 ++++------- .../Support/FileStreamExtensions.cs | 37 +++++++++++-------- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/src/Lucene.Net.Core/Store/NIOFSDirectory.cs b/src/Lucene.Net.Core/Store/NIOFSDirectory.cs index 42fa5e6932..e2a0a95e8a 100644 --- a/src/Lucene.Net.Core/Store/NIOFSDirectory.cs +++ b/src/Lucene.Net.Core/Store/NIOFSDirectory.cs @@ -77,36 +77,28 @@ public NIOFSDirectory(DirectoryInfo path) 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 @@ public override IndexInput OpenFullSlice() { return OpenSlice("full-slice", 0, Descriptor.Length); } - catch (System.IO.IOException ex) + catch (IOException ex) { throw new Exception(ex.Message, ex); } diff --git a/src/Lucene.Net.Core/Support/FileStreamExtensions.cs b/src/Lucene.Net.Core/Support/FileStreamExtensions.cs index c305d85494..1e9fc147f8 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