You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sqoop.apache.org by stanleyxu2005 <gi...@git.apache.org> on 2016/04/12 13:30:34 UTC

[GitHub] sqoop pull request: [SQOOP-2906] Optimize toAvroIdentifier

Github user stanleyxu2005 commented on a diff in the pull request:

    https://github.com/apache/sqoop/pull/18#discussion_r59360059
  
    --- Diff: src/java/org/apache/sqoop/avro/AvroUtil.java ---
    @@ -114,11 +114,20 @@ public static String toAvroColumn(String column) {
        * Format candidate to avro specifics
        */
       public static String toAvroIdentifier(String candidate) {
    -    String formattedCandidate = candidate.replaceAll("\\W+", "_");
    -    if (formattedCandidate.substring(0,1).matches("[a-zA-Z_]")) {
    -      return formattedCandidate;
    +    char[] data = candidate.toCharArray();
    +    int stringIndex = 0;
    +
    +    for (char c:data) {
    +      if (Character.isLetterOrDigit(c) || c == '_') {
    +        data[stringIndex++] = c;
    +      }
    +    }
    +
    +    char initial = data[0];
    +    if (Character.isLetter(initial) || initial == '_') {
    +      return new String(data, 0, stringIndex);
    --- End diff --
    
    Your code will first create a char array and then eventually update char in the array. As result you will create another copy as a new String. Have you thought about using a `StringBuilder` directly?
    ```
      final StringBuilder sb = new StringBuilder();
      for (char c : candidate) {
        if (Character.isLetterOrDigit(c) || c == '_') {
          sb.append(c);
        }
      }
      ...
        return sb.toString();
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---