This is an automated email from the ASF dual-hosted git repository.

chengpan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kyuubi.git


The following commit(s) were added to refs/heads/master by this push:
     new bb9212813 [KYUUBI #6447] Use static regex Pattern instances in 
JavaUtils.timeStringAs and JavaUtils.byteStringAs
bb9212813 is described below

commit bb921281319541b212ffafbe60e5492a15f85fc5
Author: senmiaoliu <[email protected]>
AuthorDate: Wed Jun 5 13:29:26 2024 +0800

    [KYUUBI #6447] Use static regex Pattern instances in JavaUtils.timeStringAs 
and JavaUtils.byteStringAs
    
    # :mag: Description
    ## Issue References ๐Ÿ”—
    
    This pull request fixes #6447
    
    ## Describe Your Solution ๐Ÿ”ง
    Use static regex Pattern instances in JavaUtils.timeStringAs and 
JavaUtils.byteStringAs
    
    ## Types of changes :bookmark:
    
    - [ ] Bugfix (non-breaking change which fixes an issue)
    - [ ] New feature (non-breaking change which adds functionality)
    - [ ] Breaking change (fix or feature that would cause existing 
functionality to change)
    
    ## Test Plan ๐Ÿงช
    
    #### Behavior Without This Pull Request :coffin:
    
    #### Behavior With This Pull Request :tada:
    
    #### Related Unit Tests
    
    ---
    
    # Checklist ๐Ÿ“
    
    - [ ] This patch was not authored or co-authored using [Generative 
Tooling](https://www.apache.org/legal/generative-tooling.html)
    
    **Be nice. Be informative.**
    
    Closes #6448 from lsm1/branch-kyuubi-6447.
    
    Closes #6447
    
    467066ce5 [senmiaoliu] Use static regex Pattern instances in JavaUtils
    
    Authored-by: senmiaoliu <[email protected]>
    Signed-off-by: Cheng Pan <[email protected]>
---
 .../org/apache/kyuubi/spark/connector/common/JavaUtils.java  | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git 
a/extensions/spark/kyuubi-spark-connector-common/src/main/java/org/apache/kyuubi/spark/connector/common/JavaUtils.java
 
b/extensions/spark/kyuubi-spark-connector-common/src/main/java/org/apache/kyuubi/spark/connector/common/JavaUtils.java
index 7f3d60525..81851f64e 100644
--- 
a/extensions/spark/kyuubi-spark-connector-common/src/main/java/org/apache/kyuubi/spark/connector/common/JavaUtils.java
+++ 
b/extensions/spark/kyuubi-spark-connector-common/src/main/java/org/apache/kyuubi/spark/connector/common/JavaUtils.java
@@ -58,6 +58,8 @@ public class JavaUtils {
     byteSuffixes.put("pb", ByteUnit.PiB);
   }
 
+  private static final Pattern TIME_STRING_PATTERN = 
Pattern.compile("(-?[0-9]+)([a-z]+)?");
+
   /**
    * Convert a passed time string (e.g. 50s, 100ms, or 250us) to a time count 
in the given unit. The
    * unit is also considered the default if the given string does not specify 
a unit.
@@ -66,7 +68,7 @@ public class JavaUtils {
     String lower = str.toLowerCase(Locale.ROOT).trim();
 
     try {
-      Matcher m = Pattern.compile("(-?[0-9]+)([a-z]+)?").matcher(lower);
+      Matcher m = TIME_STRING_PATTERN.matcher(lower);
       if (!m.matches()) {
         throw new NumberFormatException("Failed to parse time string: " + str);
       }
@@ -107,6 +109,10 @@ public class JavaUtils {
     return timeStringAs(str, TimeUnit.SECONDS);
   }
 
+  private static final Pattern BYTE_STRING_PATTERN = 
Pattern.compile("([0-9]+)([a-z]+)?");
+  private static final Pattern BYTE_STRING_FRACTION_PATTERN =
+      Pattern.compile("([0-9]+\\.[0-9]+)([a-z]+)?");
+
   /**
    * Convert a passed byte string (e.g. 50b, 100kb, or 250mb) to the given. If 
no suffix is
    * provided, a direct conversion to the provided unit is attempted.
@@ -115,8 +121,8 @@ public class JavaUtils {
     String lower = str.toLowerCase(Locale.ROOT).trim();
 
     try {
-      Matcher m = Pattern.compile("([0-9]+)([a-z]+)?").matcher(lower);
-      Matcher fractionMatcher = 
Pattern.compile("([0-9]+\\.[0-9]+)([a-z]+)?").matcher(lower);
+      Matcher m = BYTE_STRING_PATTERN.matcher(lower);
+      Matcher fractionMatcher = BYTE_STRING_FRACTION_PATTERN.matcher(lower);
 
       if (m.matches()) {
         long val = Long.parseLong(m.group(1));

Reply via email to