Move sequential suffix extraction logic into ZKPaths helper method

Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/eebff920
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/eebff920
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/eebff920

Branch: refs/heads/master
Commit: eebff920c99f1d8d3d7527deed9958bb4595e59a
Parents: 4d6069a
Author: nickhill <[email protected]>
Authored: Thu Nov 15 10:19:51 2018 -0800
Committer: nickhill <[email protected]>
Committed: Thu Nov 15 10:19:51 2018 -0800

----------------------------------------------------------------------
 .../main/java/org/apache/curator/utils/ZKPaths.java  | 15 +++++++++++++++
 .../framework/recipes/nodes/PersistentNode.java      | 10 ++++------
 2 files changed, 19 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/eebff920/curator-client/src/main/java/org/apache/curator/utils/ZKPaths.java
----------------------------------------------------------------------
diff --git a/curator-client/src/main/java/org/apache/curator/utils/ZKPaths.java 
b/curator-client/src/main/java/org/apache/curator/utils/ZKPaths.java
index 9d6ea9d..8e94d2b 100644
--- a/curator-client/src/main/java/org/apache/curator/utils/ZKPaths.java
+++ b/curator-client/src/main/java/org/apache/curator/utils/ZKPaths.java
@@ -177,6 +177,21 @@ public class ZKPaths
         return new PathAndNode(parentPath, node);
     }
 
+    // Hardcoded in {@link org.apache.zookeeper.server.PrepRequestProcessor}
+    static final int SEQUENTIAL_SUFFIX_DIGITS = 10;
+
+    /**
+     * Extracts the ten-digit suffix from a sequential znode path. Does not 
currently perform validation on the
+     * provided path; it will just return a string comprising the last ten 
characters.
+     * 
+     * @param path the path of a sequential znodes
+     * @return the sequential suffix
+     */
+    public static String extractSequentialSuffix(String path) {
+        int length = path.length();
+        return length > SEQUENTIAL_SUFFIX_DIGITS ? path.substring(length - 
SEQUENTIAL_SUFFIX_DIGITS) : path;
+    }
+
     private static final Splitter PATH_SPLITTER = 
Splitter.on(PATH_SEPARATOR).omitEmptyStrings();
 
     /**

http://git-wip-us.apache.org/repos/asf/curator/blob/eebff920/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java
----------------------------------------------------------------------
diff --git 
a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java
 
b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java
index 80b07e8..81e8dd9 100644
--- 
a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java
+++ 
b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java
@@ -37,6 +37,7 @@ import org.apache.curator.framework.state.ConnectionState;
 import org.apache.curator.framework.state.ConnectionStateListener;
 import org.apache.curator.utils.PathUtils;
 import org.apache.curator.utils.ThreadUtils;
+import org.apache.curator.utils.ZKPaths;
 import org.apache.zookeeper.CreateMode;
 import org.apache.zookeeper.KeeperException;
 import org.apache.zookeeper.WatchedEvent;
@@ -422,9 +423,6 @@ public class PersistentNode implements Closeable
         }
     }
 
-    // Hardcoded in {@link org.apache.zookeeper.server.PrepRequestProcessor}
-    static final int SEQUENTIAL_SUFFIX_DIGITS = 10;
-
     private void createNode()
     {
         if ( !isActive() )
@@ -452,13 +450,13 @@ public class PersistentNode implements Closeable
             {
                 createPath = existingPath;
             }
-            else if ( existingPath == null || !mode.isSequential() )
+            else if ( existingPath != null && mode.isSequential() )
             {
-                createPath = basePath;
+                createPath = basePath + 
ZKPaths.extractSequentialSuffix(existingPath);
             }
             else
             {
-                createPath = basePath + 
existingPath.substring(existingPath.length() - SEQUENTIAL_SUFFIX_DIGITS);
+                createPath = basePath;
             }
 
             CreateModable<ACLBackgroundPathAndBytesable<String>> 
localCreateMethod = createMethod.get();

Reply via email to