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

ctubbsii pushed a commit to branch 2.1
in repository https://gitbox.apache.org/repos/asf/accumulo.git


The following commit(s) were added to refs/heads/2.1 by this push:
     new eeff601b07 Pre-compile Patterns and avoid String.matches() (#3554)
eeff601b07 is described below

commit eeff601b071f787461e2219095abfd76eb0bbcc5
Author: Christopher Tubbs <[email protected]>
AuthorDate: Thu Jun 29 08:11:24 2023 -0400

    Pre-compile Patterns and avoid String.matches() (#3554)
    
    * Pre-compile java.util.regex.Pattern instead of using
      `String.matches()`, which is less efficient when called more than once
    * Remove unneeded TODO from ValidationUtil that was decided against in
      PR #3511
    * Use Guava Preconditions to format validation messages in
      ValidationUtil and to make the pattern checking logic there more
      concise
---
 .../accumulo/core/metadata/ValidationUtil.java     | 23 +++++++++++-----------
 .../core/metadata/schema/MetadataSchema.java       |  6 +++++-
 .../accumulo/test/conf/PropStoreConfigIT.java      |  5 ++++-
 3 files changed, 20 insertions(+), 14 deletions(-)

diff --git 
a/core/src/main/java/org/apache/accumulo/core/metadata/ValidationUtil.java 
b/core/src/main/java/org/apache/accumulo/core/metadata/ValidationUtil.java
index f4bfa4358a..db60a89200 100644
--- a/core/src/main/java/org/apache/accumulo/core/metadata/ValidationUtil.java
+++ b/core/src/main/java/org/apache/accumulo/core/metadata/ValidationUtil.java
@@ -19,10 +19,13 @@
 package org.apache.accumulo.core.metadata;
 
 import java.util.Objects;
+import java.util.regex.Pattern;
 
 import org.apache.accumulo.core.gc.ReferenceFile;
 import org.apache.hadoop.fs.Path;
 
+import com.google.common.base.Preconditions;
+
 /**
  * Utility class for validation of tablet file paths.
  */
@@ -45,26 +48,22 @@ public class ValidationUtil {
   }
 
   public static Path validate(Path path) {
-    if (path.toUri().getScheme() == null) {
-      throw new IllegalArgumentException("Invalid path provided, no scheme in 
" + path);
-    }
+    Preconditions.checkArgument(path.toUri().getScheme() != null,
+        "Invalid path provided, no scheme in %s", path);
     return path;
   }
 
   public static void validateRFileName(String fileName) {
     Objects.requireNonNull(fileName);
-    // TODO: In 3.0.0 validate that filename starts with FilePrefix
-    if (!fileName.endsWith(".rf") && !fileName.endsWith("_tmp")) {
-      throw new IllegalArgumentException(
-          "Provided filename (" + fileName + ") does not end with '.rf' or 
'_tmp'");
-    }
+    Preconditions.checkArgument(fileName.endsWith(".rf") || 
fileName.endsWith("_tmp"),
+        "Provided filename (%s) does not end with '.rf' or '_tmp'", fileName);
   }
 
+  private static final Pattern VALID_FILE_NAME_MATCH_PATTERN = 
Pattern.compile("[\\dA-Za-z._-]+");
+
   public static void validateFileName(String fileName) {
     Objects.requireNonNull(fileName);
-    if (!fileName.matches("[\\dA-Za-z._-]+")) {
-      throw new IllegalArgumentException(
-          "Provided filename (" + fileName + ") contains invalid characters.");
-    }
+    
Preconditions.checkArgument(VALID_FILE_NAME_MATCH_PATTERN.matcher(fileName).matches(),
+        "Provided filename (%s) is empty or contains invalid characters", 
fileName);
   }
 }
diff --git 
a/core/src/main/java/org/apache/accumulo/core/metadata/schema/MetadataSchema.java
 
b/core/src/main/java/org/apache/accumulo/core/metadata/schema/MetadataSchema.java
index 6676d44943..b1b306b083 100644
--- 
a/core/src/main/java/org/apache/accumulo/core/metadata/schema/MetadataSchema.java
+++ 
b/core/src/main/java/org/apache/accumulo/core/metadata/schema/MetadataSchema.java
@@ -22,6 +22,8 @@ import static 
com.google.common.base.Preconditions.checkArgument;
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static java.util.Objects.requireNonNull;
 
+import java.util.regex.Pattern;
+
 import org.apache.accumulo.core.client.admin.TimeType;
 import org.apache.accumulo.core.data.ArrayByteSequence;
 import org.apache.accumulo.core.data.Key;
@@ -191,6 +193,8 @@ public class MetadataSchema {
        */
       public static final String DEFAULT_TABLET_DIR_NAME = "default_tablet";
 
+      private static final Pattern DIRCOL_MATCH_PATTERN = 
Pattern.compile("[\\dA-Za-z_-]+");
+
       /**
        * Matches regex for a tablet directory like "default_tablet" or 
"t-000009x"
        *
@@ -198,7 +202,7 @@ public class MetadataSchema {
        *         table. Returns false otherwise.
        */
       public static boolean isValidDirCol(String dirName) {
-        return dirName.matches("[\\dA-Za-z_-]+");
+        return DIRCOL_MATCH_PATTERN.matcher(dirName).matches();
       }
 
       /**
diff --git 
a/test/src/main/java/org/apache/accumulo/test/conf/PropStoreConfigIT.java 
b/test/src/main/java/org/apache/accumulo/test/conf/PropStoreConfigIT.java
index 1babbf24d5..6cb8b1972f 100644
--- a/test/src/main/java/org/apache/accumulo/test/conf/PropStoreConfigIT.java
+++ b/test/src/main/java/org/apache/accumulo/test/conf/PropStoreConfigIT.java
@@ -39,6 +39,7 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.function.Consumer;
 import java.util.function.Supplier;
+import java.util.regex.Pattern;
 
 import org.apache.accumulo.core.client.Accumulo;
 import org.apache.accumulo.core.client.AccumuloException;
@@ -629,9 +630,11 @@ public class PropStoreConfigIT extends 
SharedMiniClusterBase {
     expected.put("table.custom.D", iterations * 7 + "");
     expected.put("table.custom.E", iterations * 19 + "");
 
+    final var IS_NOT_CUSTOM_TABLE_PROP =
+        
Pattern.compile("table[.]custom[.][ABCDEF]").asMatchPredicate().negate();
     assertTrue(Wait.waitFor(() -> {
       var tableProps = new HashMap<>(propShim.getProperties());
-      tableProps.keySet().removeIf(key -> 
!key.matches("table[.]custom[.][ABCDEF]"));
+      tableProps.keySet().removeIf(IS_NOT_CUSTOM_TABLE_PROP);
       boolean equal = expected.equals(tableProps);
       if (!equal) {
         log.info(

Reply via email to