You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucenenet.apache.org by "paulirwin (via GitHub)" <gi...@apache.org> on 2026/06/15 14:26:55 UTC
Re: [PR] Overhaul MMapDirectory with MemoryMappedViewAccessor, #1013, #1151 [lucenenet]
paulirwin commented on code in PR #1267:
URL: https://github.com/apache/lucenenet/pull/1267#discussion_r3414219992
##########
src/Lucene.Net/Store/MMapDirectory.cs:
##########
@@ -193,39 +189,98 @@ public override IndexInput OpenInput(string name, IOContext context)
{
EnsureOpen();
var file = Path.Combine(Directory.FullName, name); // LUCENENET specific: changed to use string file name instead of allocating a FileInfo (#832)
- var fc = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
- return new MMapIndexInput(this, "MMapIndexInput(path=\"" + file + "\")", fc);
+ // LUCENENET specific: a fresh SharedMapping per OpenInput call.
+ // Matches upstream Java (openInput creates a new FileChannel +
+ // fc.map()) and ensures Length reflects the file's current size.
+ SharedMapping mapping = SharedMapping.Create(file, chunkSizePower);
+ try
+ {
+ return new MMapIndexInput($"MMapIndexInput(path=\"{file}\")", ownsMapping: true, mapping, 0, mapping.Length, chunkSizePower);
+ }
+ catch
+ {
+ mapping.Dispose();
+ throw;
+ }
}
public override IndexInputSlicer CreateSlicer(string name, IOContext context)
{
- var full = (MMapIndexInput)OpenInput(name, context);
- return new IndexInputSlicerAnonymousClass(this, full);
+ EnsureOpen();
+ var file = Path.Combine(Directory.FullName, name);
+ SharedMapping mapping = SharedMapping.Create(file, chunkSizePower);
+ try
+ {
+ return new IndexInputSlicerAnonymousClass(this, file, mapping);
+ }
+ catch
+ {
+ mapping.Dispose();
+ throw;
+ }
}
private sealed class IndexInputSlicerAnonymousClass : IndexInputSlicer
{
private readonly MMapDirectory outerInstance;
- private readonly MMapIndexInput full;
- private int disposed = 0; // LUCENENET specific - allow double-dispose
+ private readonly string file;
+
+ // The slicer owns the shared mapping; all slices issued from it
+ // piggyback on the slicer's ownership. Disposing the slicer
+ // tears the mapping down once issued slices have been disposed.
+ private readonly SharedMapping mapping;
+
+ private int disposed /* = 0 */; // LUCENENET specific - allow double-dispose
+
+ // Track issued slices so that Dispose cascades. Lucene's
+ // contract is that after slicer.Dispose, reads from any slice
+ // (or clone of a slice) throw AlreadyClosedException.
+ private readonly SCG.List<MMapIndexInput> issuedSlices = new SCG.List<MMapIndexInput>();
+ private readonly object issuedSlicesLock = new object();
- public IndexInputSlicerAnonymousClass(MMapDirectory outerInstance, MMapIndexInput full)
+ public IndexInputSlicerAnonymousClass(MMapDirectory outerInstance, string file, SharedMapping mapping)
{
this.outerInstance = outerInstance;
- this.full = full;
+ this.file = file;
+ this.mapping = mapping;
}
public override IndexInput OpenSlice(string sliceDescription, long offset, long length)
{
outerInstance.EnsureOpen();
- return full.Slice(sliceDescription, offset, length);
+ if (offset < 0 || length < 0 || offset + length > mapping.Length)
+ {
+ throw new ArgumentException(
+ "slice() " + sliceDescription + " out of bounds: offset=" + offset
+ + ",length=" + length + ",fileLength=" + mapping.Length + ": " + this);
+ }
+ // Slices reference the slicer's mapping; only the slicer
+ // itself owns the mapping and will dispose it.
+ var input = new MMapIndexInput(
+ $"MMapIndexInput({sliceDescription} in path=\"{file}\" slice={offset}:{offset + length})", ownsMapping: false, mapping,
+ offset, length,
+ outerInstance.chunkSizePower);
+ lock (issuedSlicesLock)
+ {
+ if (Volatile.Read(ref disposed) != 0)
+ {
+ // Slicer was disposed after EnsureOpen but before we
+ // got the lock. Tear down what we just allocated.
+ input.Dispose();
+ throw AlreadyClosedException.Create(nameof(IndexInputSlicer), "this IndexInputSlicer is closed");
+ }
+ issuedSlices.Add(input);
+ }
+ return input;
}
[Obsolete("Only for reading CFS files from 3.x indexes.")]
public override IndexInput OpenFullSlice()
{
outerInstance.EnsureOpen();
- return (IndexInput)full.Clone();
+ // The shared mapping's Length was captured at creation time,
+ // so we don't need to touch any FileStream here.
+ return OpenSlice("full-slice", 0, mapping.Length);
Review Comment:
Please try now with the latest changes; I ran many tests and added some new ones, and cannot reproduce any file handle leaks.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org