You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by GitBox <gi...@apache.org> on 2022/05/21 08:06:16 UTC

[GitHub] [lucene] uschindler commented on a diff in pull request #912: Initial rewrite of MMapDirectory for JDK-19 preview Panama APIs (>= JDK-19-ea+23)

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


##########
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 just rrwrote the old code. Yes could be final. We could also use `final var` which looks like bullshit :-)
   
   Previously it was `int` because it was about bytebufers, which are limited to 2 Gigabytes.



-- 
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-unsubscribe@lucene.apache.org

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


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