You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by ra...@apache.org on 2015/01/12 21:49:33 UTC

[1/4] curator git commit: CURATOR-166: Allow ZKPaths to build paths with an arbitrary number of children.

Repository: curator
Updated Branches:
  refs/heads/CURATOR-166 [created] 84699d336


CURATOR-166: Allow ZKPaths to build paths with an arbitrary number of children.


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

Branch: refs/heads/CURATOR-166
Commit: 01a23e9c043277ab80d9f78b8c58b7aab06c902b
Parents: ef2ca57
Author: Ricardo Ferreira <ri...@feedzai.com>
Authored: Fri Nov 14 23:48:31 2014 +0000
Committer: Ricardo Ferreira <ri...@feedzai.com>
Committed: Fri Nov 14 23:48:31 2014 +0000

----------------------------------------------------------------------
 .../java/org/apache/curator/utils/ZKPaths.java  | 71 +++++++++++++-------
 .../org/apache/curator/utils/TestZKPaths.java   | 10 +++
 2 files changed, 57 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/01a23e9c/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 352bfd6..300913b 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
@@ -32,6 +32,12 @@ import java.util.List;
 public class ZKPaths
 {
     /**
+     * Zookeeper's path separator character.
+     */
+    public static final String PATH_SEPARATOR = "/";
+    
+    
+    /**
      * Apply the namespace to the given path
      *
      * @param namespace namespace (can be null)
@@ -59,7 +65,7 @@ public class ZKPaths
     public static String getNodeFromPath(String path)
     {
         PathUtils.validatePath(path);
-        int i = path.lastIndexOf('/');
+        int i = path.lastIndexOf(PATH_SEPARATOR);
         if ( i < 0 )
         {
             return path;
@@ -102,21 +108,21 @@ public class ZKPaths
     public static PathAndNode getPathAndNode(String path)
     {
         PathUtils.validatePath(path);
-        int i = path.lastIndexOf('/');
+        int i = path.lastIndexOf(PATH_SEPARATOR);
         if ( i < 0 )
         {
             return new PathAndNode(path, "");
         }
         if ( (i + 1) >= path.length() )
         {
-            return new PathAndNode("/", "");
+            return new PathAndNode(PATH_SEPARATOR, "");
         }
         String node = path.substring(i + 1);
-        String parentPath = (i > 0) ? path.substring(0, i) : "/";
+        String parentPath = (i > 0) ? path.substring(0, i) : PATH_SEPARATOR;
         return new PathAndNode(parentPath, node);
     }
 
-    private static final Splitter PATH_SPLITTER = Splitter.on('/').omitEmptyStrings();
+    private static final Splitter PATH_SPLITTER = Splitter.on(PATH_SEPARATOR).omitEmptyStrings();
 
     /**
      * Given a full path, return the the individual parts, without slashes.
@@ -178,7 +184,7 @@ public class ZKPaths
         int pos = 1; // skip first slash, root is guaranteed to exist
         do
         {
-            pos = path.indexOf('/', pos + 1);
+            pos = path.indexOf(PATH_SEPARATOR, pos + 1);
 
             if ( pos == -1 )
             {
@@ -276,26 +282,26 @@ public class ZKPaths
         Collections.sort(sortedList);
         return sortedList;
     }
-
+    
     /**
-     * Given a parent path and a child node, create a combined full path
+     * Given a parent path and a list of children nodes, create a combined full path
      *
      * @param parent the parent
-     * @param child  the child
+     * @param children  the children
      * @return full path
      */
-    public static String makePath(String parent, String child)
+    public static String makePath(String parent, String... children)
     {
         StringBuilder path = new StringBuilder();
 
         // Add parent piece, with no trailing slash.
         if ( (parent != null) && (parent.length() > 0) )
         {
-            if ( !parent.startsWith("/") )
+            if ( !parent.startsWith(PATH_SEPARATOR) )
             {
-                path.append('/');
+                path.append(PATH_SEPARATOR);
             }
-            if ( parent.endsWith("/") )
+            if ( parent.endsWith(PATH_SEPARATOR) )
             {
                 path.append(parent.substring(0, parent.length() - 1));
             }
@@ -305,26 +311,43 @@ public class ZKPaths
             }
         }
 
-        if ( (child == null) || (child.length() == 0) || (child.equals("/")) )
+        if (children == null || children.length == 0)
         {
             // Special case, empty parent and child
             if ( path.length() == 0 )
             {
-                return "/";
+                return PATH_SEPARATOR;
             }
             return path.toString();
         }
 
-        // Now add the separator between parent and child.
-        path.append('/');
-
-        // Finally, add the child.
-        if ( child.startsWith("/") )
-        {
-            path.append(child.substring(1));
-        }
-        else
+        for (String child : children)
         {
+            if ( (child == null) || (child.length() == 0) || (child.equals(PATH_SEPARATOR)) )
+            {
+                // Special case, empty parent and child
+                if ( path.length() == 0 )
+                {
+                    path.append(PATH_SEPARATOR);
+                }
+
+                continue;
+            }
+
+            // Now add the separator between parent and child.
+            path.append(PATH_SEPARATOR);
+
+            if ( child.startsWith(PATH_SEPARATOR) )
+            {
+                child = child.substring(1);
+            }
+
+            if ( child.endsWith(PATH_SEPARATOR) )
+            {
+                child = child.substring(0, child.length() - 1);
+            }
+
+            // Finally, add the child.
             path.append(child);
         }
 

http://git-wip-us.apache.org/repos/asf/curator/blob/01a23e9c/curator-client/src/test/java/org/apache/curator/utils/TestZKPaths.java
----------------------------------------------------------------------
diff --git a/curator-client/src/test/java/org/apache/curator/utils/TestZKPaths.java b/curator-client/src/test/java/org/apache/curator/utils/TestZKPaths.java
index 04d07c5..2e8dc73 100644
--- a/curator-client/src/test/java/org/apache/curator/utils/TestZKPaths.java
+++ b/curator-client/src/test/java/org/apache/curator/utils/TestZKPaths.java
@@ -63,6 +63,16 @@ public class TestZKPaths
         Assert.assertEquals(ZKPaths.makePath("/foo", "bar"), "/foo/bar");
         Assert.assertEquals(ZKPaths.makePath("foo", "/bar"), "/foo/bar");
         Assert.assertEquals(ZKPaths.makePath("/foo", "/bar"), "/foo/bar");
+        Assert.assertEquals(ZKPaths.makePath("/foo", "bar/"), "/foo/bar");
+        Assert.assertEquals(ZKPaths.makePath("/foo/", "/bar/"), "/foo/bar");
+
+        Assert.assertEquals(ZKPaths.makePath("foo", "bar", "baz"), "/foo/bar/baz");
+        Assert.assertEquals(ZKPaths.makePath("foo", "bar", "baz", "qux"), "/foo/bar/baz/qux");
+        Assert.assertEquals(ZKPaths.makePath("/foo", "/bar", "/baz"), "/foo/bar/baz");
+        Assert.assertEquals(ZKPaths.makePath("/foo/", "/bar/", "/baz/"), "/foo/bar/baz");
+        Assert.assertEquals(ZKPaths.makePath("foo", null, null), "/foo");
+        Assert.assertEquals(ZKPaths.makePath("foo", "bar", null), "/foo/bar");
+        Assert.assertEquals(ZKPaths.makePath("foo", null, "baz"), "/foo/baz");
     }
 
     @Test


[3/4] curator git commit: Merge branch 'CURATOR-166' of https://github.com/rfer/curator into CURATOR-166

Posted by ra...@apache.org.
Merge branch 'CURATOR-166' of https://github.com/rfer/curator into CURATOR-166


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

Branch: refs/heads/CURATOR-166
Commit: 687672a390033203661aa368fa32a4d1673761e8
Parents: 7a97315 b387cf0
Author: randgalt <ra...@apache.org>
Authored: Mon Jan 12 15:41:13 2015 -0500
Committer: randgalt <ra...@apache.org>
Committed: Mon Jan 12 15:41:13 2015 -0500

----------------------------------------------------------------------
 .../java/org/apache/curator/utils/ZKPaths.java  | 88 +++++++++++++++-----
 .../org/apache/curator/utils/TestZKPaths.java   | 10 +++
 2 files changed, 79 insertions(+), 19 deletions(-)
----------------------------------------------------------------------



[2/4] curator git commit: CURATOR-166: Reverted to using two overloads in order to keep binary compatibiity and avoid uneeded array allocation.

Posted by ra...@apache.org.
CURATOR-166: Reverted to using two overloads in order to keep binary compatibiity and avoid uneeded array allocation.


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

Branch: refs/heads/CURATOR-166
Commit: b387cf0963d184c9b2aad88a4947e1bb5ed1275d
Parents: 01a23e9
Author: Ricardo Ferreira <ri...@feedzai.com>
Authored: Mon Nov 17 23:02:51 2014 +0000
Committer: Ricardo Ferreira <ri...@feedzai.com>
Committed: Mon Nov 17 23:02:51 2014 +0000

----------------------------------------------------------------------
 .../java/org/apache/curator/utils/ZKPaths.java  | 95 +++++++++++++-------
 1 file changed, 61 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/b387cf09/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 300913b..f485c62 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
@@ -282,18 +282,61 @@ public class ZKPaths
         Collections.sort(sortedList);
         return sortedList;
     }
-    
+
     /**
-     * Given a parent path and a list of children nodes, create a combined full path
+     * Given a parent path and a child node, create a combined full path
      *
      * @param parent the parent
-     * @param children  the children
+     * @param child  the child
      * @return full path
      */
-    public static String makePath(String parent, String... children)
+    public static String makePath(String parent, String child)
     {
         StringBuilder path = new StringBuilder();
 
+        joinPath(path, parent, child);
+
+        return path.toString();
+    }
+
+    /**
+     * Given a parent path and a list of children nodes, create a combined full path
+     *
+     * @param parent       the parent
+     * @param firstChild   the first children in the path
+     * @param restChildren the rest of the children in the path
+     * @return full path
+     */
+    public static String makePath(String parent, String firstChild, String... restChildren)
+    {
+        StringBuilder path = new StringBuilder();
+
+        joinPath(path, parent, firstChild);
+
+        if ( restChildren == null )
+        {
+            return path.toString();
+        }
+        else
+        {
+            for ( String child : restChildren )
+            {
+                joinPath(path, "", child);
+            }
+
+            return path.toString();
+        }
+    }
+
+    /**
+     * Given a parent and a child node, join them in the given {@link StringBuilder path}
+     *
+     * @param path   the {@link StringBuilder} used to make the path
+     * @param parent the parent
+     * @param child  the child
+     */
+    private static void joinPath(StringBuilder path, String parent, String child)
+    {
         // Add parent piece, with no trailing slash.
         if ( (parent != null) && (parent.length() > 0) )
         {
@@ -311,47 +354,31 @@ public class ZKPaths
             }
         }
 
-        if (children == null || children.length == 0)
+        if ( (child == null) || (child.length() == 0) || (child.equals(PATH_SEPARATOR)) )
         {
             // Special case, empty parent and child
             if ( path.length() == 0 )
             {
-                return PATH_SEPARATOR;
+                path.append(PATH_SEPARATOR);
             }
-            return path.toString();
+            return;
         }
 
-        for (String child : children)
-        {
-            if ( (child == null) || (child.length() == 0) || (child.equals(PATH_SEPARATOR)) )
-            {
-                // Special case, empty parent and child
-                if ( path.length() == 0 )
-                {
-                    path.append(PATH_SEPARATOR);
-                }
+        // Now add the separator between parent and child.
+        path.append(PATH_SEPARATOR);
 
-                continue;
-            }
-
-            // Now add the separator between parent and child.
-            path.append(PATH_SEPARATOR);
-
-            if ( child.startsWith(PATH_SEPARATOR) )
-            {
-                child = child.substring(1);
-            }
-
-            if ( child.endsWith(PATH_SEPARATOR) )
-            {
-                child = child.substring(0, child.length() - 1);
-            }
+        if ( child.startsWith(PATH_SEPARATOR) )
+        {
+            child = child.substring(1);
+        }
 
-            // Finally, add the child.
-            path.append(child);
+        if ( child.endsWith(PATH_SEPARATOR) )
+        {
+            child = child.substring(0, child.length() - 1);
         }
 
-        return path.toString();
+        // Finally, add the child.
+        path.append(child);
     }
 
     private ZKPaths()


[4/4] curator git commit: eliminated warning

Posted by ra...@apache.org.
eliminated warning


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

Branch: refs/heads/CURATOR-166
Commit: 84699d336fa96f78cbeca25b159091acd95384df
Parents: 687672a
Author: randgalt <ra...@apache.org>
Authored: Mon Jan 12 15:49:19 2015 -0500
Committer: randgalt <ra...@apache.org>
Committed: Mon Jan 12 15:49:19 2015 -0500

----------------------------------------------------------------------
 .../src/test/java/org/apache/curator/utils/TestZKPaths.java         | 1 +
 1 file changed, 1 insertion(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/84699d33/curator-client/src/test/java/org/apache/curator/utils/TestZKPaths.java
----------------------------------------------------------------------
diff --git a/curator-client/src/test/java/org/apache/curator/utils/TestZKPaths.java b/curator-client/src/test/java/org/apache/curator/utils/TestZKPaths.java
index 2e8dc73..ef05d34 100644
--- a/curator-client/src/test/java/org/apache/curator/utils/TestZKPaths.java
+++ b/curator-client/src/test/java/org/apache/curator/utils/TestZKPaths.java
@@ -26,6 +26,7 @@ import java.util.Collections;
 
 public class TestZKPaths
 {
+    @SuppressWarnings("NullArgumentToVariableArgMethod")
     @Test
     public void testMakePath()
     {