You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "robertwb (via GitHub)" <gi...@apache.org> on 2023/05/01 19:03:33 UTC

[GitHub] [beam] robertwb commented on a diff in pull request #26370: Ensure FnApi bag appends are not batched if it would exceed 10MB limit

robertwb commented on code in PR #26370:
URL: https://github.com/apache/beam/pull/26370#discussion_r1181805751


##########
sdks/java/core/src/main/java/org/apache/beam/sdk/util/ByteStringOutputStream.java:
##########
@@ -153,6 +157,47 @@ public ByteString toByteStringAndReset() {
     return rval;
   }
 
+  /**
+   * Creates a byte string with the given size containing the prefix of the contents of this output
+   * stream.
+   */
+  public ByteString consumePrefixToByteString(int prefixSize) {
+    ByteString rval;
+    Preconditions.checkArgument(prefixSize <= size());
+    if (prefixSize == size()) {
+      return toByteStringAndReset();
+    }
+    int bytesFromBuffer = prefixSize - result.size();
+    if (bytesFromBuffer == 0) {
+      rval = result;
+      result = ByteString.EMPTY;
+      return rval;
+    }
+    if (bytesFromBuffer < 0) {
+      rval = result.substring(0, prefixSize);
+      result = result.substring(prefixSize);
+      return rval;
+    }
+    // Copy or reference the kept bytes for the rval.
+    if (shouldCopy(buffer.length, bytesFromBuffer)) {
+      byte[] bufferCopy = new byte[bytesFromBuffer];
+      System.arraycopy(buffer, 0, bufferCopy, 0, bytesFromBuffer);
+      rval = result.concat(UnsafeByteOperations.unsafeWrap(bufferCopy));

Review Comment:
   Any reason not to us ByteString.copyFrom here? 



-- 
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: github-unsubscribe@beam.apache.org

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