suneet-s commented on a change in pull request #9215: [0.17.0] Speed up String
first/last aggregators when folding isn't needed. (#9181)
URL: https://github.com/apache/druid/pull/9215#discussion_r368190414
##########
File path:
core/src/main/java/org/apache/druid/java/util/common/StringUtils.java
##########
@@ -546,4 +546,39 @@ public static String rpad(String base, Integer len,
String pad)
return new String(data);
}
+ /**
+ * Returns the string truncated to maxBytes.
+ * If given string input is shorter than maxBytes, then it remains the same.
+ *
+ * @param s The input string to possibly be truncated
+ * @param maxBytes The max bytes that string input will be truncated to
+ *
+ * @return the string after truncated to maxBytes
+ */
+ @Nullable
+ public static String chop(@Nullable final String s, final int maxBytes)
+ {
+ if (s == null) {
+ return null;
+ } else {
+ // Shorten firstValue to what could fit in maxBytes as UTF-8.
+ final byte[] bytes = new byte[maxBytes];
+ final int len = StringUtils.toUtf8WithLimit(s, ByteBuffer.wrap(bytes));
+ return new String(bytes, 0, len, StandardCharsets.UTF_8);
+ }
+ }
+
+ /**
+ * Shorten "s" to "maxBytes" chars. Fast and loose because these are *chars*
not *bytes*. Use
+ * {@link #chop(String, int)} for slower, but accurate chopping.
+ */
+ @Nullable
+ public static String fastLooseChop(@Nullable final String s, final int
maxBytes)
+ {
+ if (s == null || s.length() <= maxBytes) {
+ return s;
+ } else {
+ return s.substring(0, maxBytes);
+ }
Review comment:
To resolve I moved chop and fastLooseChop to StringUtils since this has
tests with them and renamed references in StringFirst/Last aggs
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]