szetszwo commented on code in PR #1631:
URL: https://github.com/apache/ratis/pull/1631#discussion_r4232532109


##########
ratis-common/src/main/java/org/apache/ratis/protocol/Message.java:
##########
@@ -68,14 +69,56 @@ static int getSize(Message message) {
     return Optional.ofNullable(message).map(Message::size).orElse(0);
   }
 
+  /**
+   * Convert the content of the given message to a {@link ByteString}.
+   * When the message supports {@link ByteBuffer},
+   * the buffer is wrapped without copying and {@link #getContent()} is not 
invoked.
+   *
+   * @return the content as a {@link ByteString}, or null if the content is 
null.
+   */
+  static ByteString toByteString(Message message) {
+    if (!message.isByteBufferSupported()) {
+      return message.getContent();
+    }
+    final ByteBuffer buffer = message.getContentAsByteBuffer();
+    return buffer == null ? null : UnsafeByteOperations.unsafeWrap(buffer);
+  }
+
   Message EMPTY = valueOf(ByteString.EMPTY);
 
   /**
-   * @return the content of the message
+   * @return the content of the message.
+   *         When {@link #isByteBufferSupported()} returns true,
+   *         Ratis never invokes this method and the implementation may throw 
{@link UnsupportedOperationException}.
    */
   ByteString getContent();
 
+  /**
+   * Does this message support {@link #getContentAsByteBuffer()} natively?
+   * When it returns true, Ratis uses {@link #getContentAsByteBuffer()}
+   * and never invokes {@link #getContent()}.
+   *
+   * @return true if this message supports {@link ByteBuffer}; otherwise, 
return false.
+   */
+  default boolean isByteBufferSupported() {
+    return false;
+  }
+
+  /**
+   * Ratis may use the returned buffer without copying it
+   * but it does not change the buffer's content, position or limit.
+   * The implementation must not change the content of the buffer after 
returning it.
+   *
+   * @return the content of the message as a {@link ByteBuffer}, from its 
position to its limit.
+   */
+  default ByteBuffer getContentAsByteBuffer() {
+    return 
Optional.ofNullable(getContent()).map(ByteString::asReadOnlyByteBuffer).orElse(null);
+  }

Review Comment:
   Let's rename it to `asReadOnlyBuffer()`:
   ```java
     /** The same as {@link ByteBuffer#asReadOnlyBuffer()}. */
     default ByteBuffer asReadOnlyByteBuffer() {
       final ByteString content = getContent();
       return content == null ? null : content.asReadOnlyByteBuffer();
     }
   ```



##########
ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java:
##########
@@ -1096,7 +1096,7 @@ private CompletableFuture<RaftClientReply> 
writeAsyncImpl(RaftClientRequest requ
   }
 
   private CompletableFuture<RaftClientReply> watchAsync(RaftClientRequest 
request) {
-    if 
(OrderedAsync.DUMMY.getContent().equals(request.getMessage().getContent())) {
+    if 
(OrderedAsync.DUMMY.getContent().equals(Message.toByteString(request.getMessage())))
 {

Review Comment:
   Use isDummyRead(request)



##########
ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java:
##########
@@ -1212,7 +1212,8 @@ private CompletableFuture<RaftClientReply> 
readAsync(RaftClientRequest request)
     }
   }
   private static boolean isDummyRead(RaftClientRequest request) {
-    return request.getMessage() != null && 
OrderedAsync.DUMMY.getContent().equals(request.getMessage().getContent());
+    return request.getMessage() != null
+        && 
OrderedAsync.DUMMY.getContent().equals(Message.toByteString(request.getMessage()));

Review Comment:
   Let's implement hashCode() and equal(..):
   
   ```java
     private static boolean isDummyRead(RaftClientRequest request) {
       return OrderedAsync.DUMMY.equals(request.getMessage());
     }
   ```
   
   ```java
   //Message.java
     static Message valueOf(ByteString bytes, Supplier<String> stringSupplier) {
       return new Message() {
         private final MemoizedSupplier<String> memoized = 
MemoizedSupplier.valueOf(stringSupplier);
   
         @Override
         public ByteString getContent() {
           return bytes;
         }
   
         @Override
         public int hashCode() {
           return Objects.hashCode(bytes);
         }
   
         @Override
         public boolean equals(Object obj) {
           if (obj == this) {
             return true;
           } else if (!(obj instanceof Message)) {
             return false;
           }
           final Message that = (Message)obj;
           if (that.isByteBufferSupported()) {
             return Objects.equals(this.asReadOnlyByteBuffer(), 
that.asReadOnlyByteBuffer());
           } else {
             return Objects.equals(this.getContent(), that.getContent());
           }
         }
   
         @Override
         public String toString() {
           return memoized.get();
         }
       };
     }
   ```



##########
ratis-common/src/main/java/org/apache/ratis/protocol/Message.java:
##########
@@ -68,14 +69,56 @@ static int getSize(Message message) {
     return Optional.ofNullable(message).map(Message::size).orElse(0);
   }
 
+  /**
+   * Convert the content of the given message to a {@link ByteString}.
+   * When the message supports {@link ByteBuffer},
+   * the buffer is wrapped without copying and {@link #getContent()} is not 
invoked.
+   *
+   * @return the content as a {@link ByteString}, or null if the content is 
null.
+   */
+  static ByteString toByteString(Message message) {
+    if (!message.isByteBufferSupported()) {
+      return message.getContent();
+    }
+    final ByteBuffer buffer = message.getContentAsByteBuffer();
+    return buffer == null ? null : UnsafeByteOperations.unsafeWrap(buffer);
+  }
+
   Message EMPTY = valueOf(ByteString.EMPTY);
 
   /**
-   * @return the content of the message
+   * @return the content of the message.
+   *         When {@link #isByteBufferSupported()} returns true,
+   *         Ratis never invokes this method and the implementation may throw 
{@link UnsupportedOperationException}.
    */
   ByteString getContent();
 
+  /**
+   * Does this message support {@link #getContentAsByteBuffer()} natively?
+   * When it returns true, Ratis uses {@link #getContentAsByteBuffer()}
+   * and never invokes {@link #getContent()}.
+   *
+   * @return true if this message supports {@link ByteBuffer}; otherwise, 
return false.
+   */
+  default boolean isByteBufferSupported() {
+    return false;
+  }
+
+  /**
+   * Ratis may use the returned buffer without copying it
+   * but it does not change the buffer's content, position or limit.
+   * The implementation must not change the content of the buffer after 
returning it.
+   *
+   * @return the content of the message as a {@link ByteBuffer}, from its 
position to its limit.
+   */
+  default ByteBuffer getContentAsByteBuffer() {
+    return 
Optional.ofNullable(getContent()).map(ByteString::asReadOnlyByteBuffer).orElse(null);
+  }
+
   default int size() {
+    if (isByteBufferSupported()) {
+      return 
Optional.ofNullable(getContentAsByteBuffer()).map(ByteBuffer::remaining).orElse(0);
+    }
     return Optional.ofNullable(getContent()).map(ByteString::size).orElse(0);

Review Comment:
   Since `Optional` is inefficient, let's take this chance to get rid of it.
   
   ```java
     static int getSize(Message message) {
       return message == null ? 0 : message.size();
     }
   
     static int getSize(ByteBuffer buffer) {
       return buffer == null ? 0 : buffer.remaining();
     }
   
     static int getSize(ByteString bytes) {
       return bytes == null ? 0 : bytes.size();
     }
   
     default int size() {
       return isByteBufferSupported() ? getSize(asReadOnlyByteBuffer()) : 
getSize(getContent());
     }
   ```



-- 
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]

Reply via email to