mocobeta commented on code in PR #912:
URL: https://github.com/apache/lucene/pull/912#discussion_r878666674


##########
lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java:
##########
@@ -232,55 +224,62 @@ public IndexInput openInput(String name, IOContext 
context) throws IOException {
     ensureOpen();
     ensureCanRead(name);
     Path path = directory.resolve(name);
-    try (FileChannel c = FileChannel.open(path, StandardOpenOption.READ)) {
-      final String resourceDescription = "MMapIndexInput(path=\"" + 
path.toString() + "\")";
-      final boolean useUnmap = getUseUnmap();
-      return ByteBufferIndexInput.newInstance(
-          resourceDescription,
-          map(resourceDescription, c, 0, c.size()),
-          c.size(),
-          chunkSizePower,
-          new ByteBufferGuard(resourceDescription, useUnmap ? CLEANER : null));
+    final String resourceDescription = "MemorySegmentIndexInput(path=\"" + 
path.toString() + "\")";
+
+    // Work around for JDK-8259028: we need to unwrap our test-only file 
system layers
+    path = Unwrappable.unwrapAll(path);
+
+    boolean success = false;
+    final MemorySession session = MemorySession.openShared();
+    try (var fc = FileChannel.open(path)) {
+      final long fileSize = fc.size();
+      final MemorySegment[] segments = map(session, resourceDescription, fc, 
fileSize);
+      final IndexInput in =
+          MemorySegmentIndexInput.newInstance(
+              resourceDescription, session, segments, fileSize, 
chunkSizePower);
+      success = true;
+      return in;
+    } finally {
+      if (success == false) {
+        session.close();
+      }
     }
   }
 
-  /** Maps a file into a set of buffers */
-  final ByteBuffer[] map(String resourceDescription, FileChannel fc, long 
offset, long length)
+  /** Maps a file into a set of segments */
+  final MemorySegment[] map(
+      MemorySession session, String resourceDescription, FileChannel fc, long 
length)
       throws IOException {
     if ((length >>> chunkSizePower) >= Integer.MAX_VALUE)
-      throw new IllegalArgumentException(
-          "RandomAccessFile too big for chunk size: " + resourceDescription);
+      throw new IllegalArgumentException("File too big for chunk size: " + 
resourceDescription);
 
     final long chunkSize = 1L << chunkSizePower;
 
-    // we always allocate one more buffer, the last one may be a 0 byte one
-    final int nrBuffers = (int) (length >>> chunkSizePower) + 1;
+    // we always allocate one more segments, the last one may be a 0 byte one
+    final int nrSegments = (int) (length >>> chunkSizePower) + 1;
 
-    ByteBuffer[] buffers = new ByteBuffer[nrBuffers];
+    final MemorySegment[] segments = new MemorySegment[nrSegments];
 
-    long bufferStart = 0L;
-    for (int bufNr = 0; bufNr < nrBuffers; bufNr++) {
-      int bufSize =
-          (int) ((length > (bufferStart + chunkSize)) ? chunkSize : (length - 
bufferStart));
-      MappedByteBuffer buffer;
+    long startOffset = 0L;
+    for (int segNr = 0; segNr < nrSegments; segNr++) {
+      long segSize = (length > (startOffset + chunkSize)) ? chunkSize : 
(length - startOffset);

Review Comment:
   I'm resolving this - it's a local variable in the loop with a smaller scope, 
I just noticed that it looks like that other parts of this patch try to use 
final as far as possible.



-- 
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: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to