rickyma commented on code in PR #1659:
URL: 
https://github.com/apache/incubator-uniffle/pull/1659#discussion_r1570698857


##########
common/src/main/java/org/apache/uniffle/common/netty/buffer/FileSegmentManagedBuffer.java:
##########
@@ -39,6 +39,8 @@ public class FileSegmentManagedBuffer extends ManagedBuffer {
   private final File file;
   private final long offset;
   private final int length;
+  private boolean isFilled;

Review Comment:
   Maybe we don't need `isFilled` at all. I think it can be replaced by 
`readByteBuffer != null`. 



##########
common/src/main/java/org/apache/uniffle/common/netty/buffer/FileSegmentManagedBuffer.java:
##########
@@ -58,27 +60,32 @@ public ByteBuf byteBuf() {
 
   @Override
   public ByteBuffer nioByteBuffer() {
+    if (isFilled) {
+      return readByteBuffer;
+    }
     FileChannel channel = null;
     try {
       channel = new RandomAccessFile(file, "r").getChannel();
-      ByteBuffer buf = ByteBuffer.allocate(length);
+      readByteBuffer = ByteBuffer.allocate(length);
       channel.position(offset);
-      while (buf.remaining() != 0) {
-        if (channel.read(buf) == -1) {
+      while (readByteBuffer.remaining() != 0) {
+        if (channel.read(readByteBuffer) == -1) {
           throw new IOException(
               String.format(
                   "Reached EOF before filling 
buffer.offset=%s,file=%s,buf.remaining=%s",
-                  offset, file.getAbsoluteFile(), buf.remaining()));
+                  offset, file.getAbsoluteFile(), readByteBuffer.remaining()));
         }
       }
-      buf.flip();
-      return buf;
+      readByteBuffer.flip();
+      isFilled = true;
+      return readByteBuffer;
     } catch (IOException e) {
-      String errorMessage = "Error in reading " + this;
+      String errorMessage = "Error in reading file " + file + " " + this;

Review Comment:
   It's better if we use `String.format`.



##########
common/src/main/java/org/apache/uniffle/common/netty/buffer/FileSegmentManagedBuffer.java:
##########
@@ -58,27 +60,32 @@ public ByteBuf byteBuf() {
 
   @Override
   public ByteBuffer nioByteBuffer() {
+    if (isFilled) {
+      return readByteBuffer;
+    }
     FileChannel channel = null;
     try {
       channel = new RandomAccessFile(file, "r").getChannel();
-      ByteBuffer buf = ByteBuffer.allocate(length);
+      readByteBuffer = ByteBuffer.allocate(length);
       channel.position(offset);
-      while (buf.remaining() != 0) {
-        if (channel.read(buf) == -1) {
+      while (readByteBuffer.remaining() != 0) {
+        if (channel.read(readByteBuffer) == -1) {
           throw new IOException(
               String.format(
                   "Reached EOF before filling 
buffer.offset=%s,file=%s,buf.remaining=%s",
-                  offset, file.getAbsoluteFile(), buf.remaining()));
+                  offset, file.getAbsoluteFile(), readByteBuffer.remaining()));
         }
       }
-      buf.flip();
-      return buf;
+      readByteBuffer.flip();
+      isFilled = true;
+      return readByteBuffer;
     } catch (IOException e) {
-      String errorMessage = "Error in reading " + this;
+      String errorMessage = "Error in reading file " + file + " " + this;
       try {
         if (channel != null) {
           long size = channel.size();
-          errorMessage = "Error in reading " + this + " (actual file length " 
+ size + ")";
+          errorMessage =

Review Comment:
   ditto



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to