You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@curator.apache.org by GitBox <gi...@apache.org> on 2020/05/11 04:15:28 UTC

[GitHub] [curator] TisonKun commented on a change in pull request #366: [CURATOR-569] API TO HANDLE PROTECTED MODE ZNODE NAMES

TisonKun commented on a change in pull request #366:
URL: https://github.com/apache/curator/pull/366#discussion_r422768298



##########
File path: curator-framework/src/main/java/org/apache/curator/framework/api/CreateBuilderMain.java
##########
@@ -27,6 +29,56 @@
     Compressible<CreateBackgroundModeStatACLable>,
     Statable<CreateProtectACLCreateModePathAndBytesable<String>>
 {
+
+    /**
+     * First 3 characters in the prefix on ZNode names when using {@link #withProtection()}
+     */
+    static final String PROTECTED_PREFIX = "_c_";
+
+    /**
+     * Last character used in the prefix on ZNode names when using {@link #withProtection()}
+     */
+    static final char PROTECTED_SEPARATOR = '-';
+
+    /**
+     * Prefix length on ZNode name when using {@link #withProtection()}
+     */
+    static final int PROTECTED_PREFIX_WITH_UUID_LENGTH = 
+            PROTECTED_PREFIX.length() 
+            + 36 // UUID canonical text representation produced by {@link UUID#toString()}
+            + 1; // Separator length
+
+    /**
+     * Utility method to determine if a given ZNode name starts with Curator's generated protected prefix.
+     * 
+     * @param znodeName ZNode name
+     * @return {@code true} if the given ZNode name starts with Curator's generated protected prefix
+     */
+    public static boolean isProtectedZNode(final String znodeName) {
+        if (znodeName.length() > PROTECTED_PREFIX_WITH_UUID_LENGTH
+                && znodeName.startsWith(PROTECTED_PREFIX)
+                && znodeName.charAt(PROTECTED_PREFIX_WITH_UUID_LENGTH-1) == PROTECTED_SEPARATOR
+           ) {
+            try {
+                UUID.fromString(znodeName.substring(PROTECTED_PREFIX.length(), PROTECTED_PREFIX_WITH_UUID_LENGTH-2));
+                return true;
+            } catch (IllegalArgumentException e) {
+                // Just false

Review comment:
       nit - (personal taste) explicitly return `false` does no harm and more expressive

##########
File path: curator-framework/src/main/java/org/apache/curator/framework/api/CreateBuilderMain.java
##########
@@ -27,6 +29,56 @@
     Compressible<CreateBackgroundModeStatACLable>,
     Statable<CreateProtectACLCreateModePathAndBytesable<String>>
 {
+
+    /**
+     * First 3 characters in the prefix on ZNode names when using {@link #withProtection()}
+     */
+    static final String PROTECTED_PREFIX = "_c_";
+
+    /**
+     * Last character used in the prefix on ZNode names when using {@link #withProtection()}
+     */
+    static final char PROTECTED_SEPARATOR = '-';
+
+    /**
+     * Prefix length on ZNode name when using {@link #withProtection()}
+     */
+    static final int PROTECTED_PREFIX_WITH_UUID_LENGTH = 
+            PROTECTED_PREFIX.length() 
+            + 36 // UUID canonical text representation produced by {@link UUID#toString()}
+            + 1; // Separator length
+
+    /**
+     * Utility method to determine if a given ZNode name starts with Curator's generated protected prefix.
+     * 
+     * @param znodeName ZNode name
+     * @return {@code true} if the given ZNode name starts with Curator's generated protected prefix
+     */
+    public static boolean isProtectedZNode(final String znodeName) {
+        if (znodeName.length() > PROTECTED_PREFIX_WITH_UUID_LENGTH
+                && znodeName.startsWith(PROTECTED_PREFIX)
+                && znodeName.charAt(PROTECTED_PREFIX_WITH_UUID_LENGTH-1) == PROTECTED_SEPARATOR
+           ) {
+            try {
+                UUID.fromString(znodeName.substring(PROTECTED_PREFIX.length(), PROTECTED_PREFIX_WITH_UUID_LENGTH-2));
+                return true;
+            } catch (IllegalArgumentException e) {
+                // Just false
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Utility method to remove Curator's generated protected prefix if present
+     * 
+     * @param znodeName ZNode name
+     * @return string without Curator's generated protected prefix if present; original string if prefix not present
+     */
+    public static String removeProtectedPrefix(final String znodeName) {

Review comment:
       What if returns `Optional<String>` which equals `isProtectedZNode(znodeName) ? Optional.of(znodeName.substring(PROTECTED_PREFIX_WITH_UUID_LENGTH)) : Optional.empty()`.
   
   For API semantic it fits in "if present" and user can easily get the original sting by `.orElse` but hard to filter non-protected znodes with this API(the user of this method seems interested in protected znodes only.)




----------------------------------------------------------------
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:
users@infra.apache.org