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/10/16 20:45:47 UTC

[2/8] curator git commit: api for setting data

api for setting data


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

Branch: refs/heads/CURATOR-3.0
Commit: a49d2bb4f83a9627ad546a149a8f77831d174ef1
Parents: 3029856
Author: randgalt <ra...@apache.org>
Authored: Sun Sep 27 17:06:46 2015 -0500
Committer: randgalt <ra...@apache.org>
Committed: Sun Sep 27 17:06:46 2015 -0500

----------------------------------------------------------------------
 .../framework/recipes/nodes/GroupMember.java    | 22 ++++++++++++++++----
 .../recipes/nodes/PersistentEphemeralNode.java  |  7 ++++++-
 .../recipes/nodes/TestGroupMember.java          |  7 +++++++
 3 files changed, 31 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/a49d2bb4/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/GroupMember.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/GroupMember.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/GroupMember.java
index 5aa8ca2..b914ba4 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/GroupMember.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/GroupMember.java
@@ -28,7 +28,6 @@ import org.apache.curator.framework.recipes.cache.PathChildrenCache;
 import org.apache.curator.utils.CloseableUtils;
 import org.apache.curator.utils.ZKPaths;
 import java.io.Closeable;
-import java.util.Arrays;
 import java.util.Map;
 
 /**
@@ -40,7 +39,6 @@ public class GroupMember implements Closeable
     private final PersistentEphemeralNode pen;
     private final PathChildrenCache cache;
     private final String thisId;
-    private final byte[] payload;
 
     /**
      * @param client client
@@ -61,7 +59,6 @@ public class GroupMember implements Closeable
     public GroupMember(CuratorFramework client, String membershipPath, String thisId, byte[] payload)
     {
         this.thisId = Preconditions.checkNotNull(thisId, "thisId cannot be null");
-        this.payload = Arrays.copyOf(payload, payload.length);
 
         cache = newPathChildrenCache(client, membershipPath);
         pen = newPersistentEphemeralNode(client, membershipPath, thisId, payload);
@@ -85,6 +82,23 @@ public class GroupMember implements Closeable
     }
 
     /**
+     * Change the data stored in this instance's node
+     *
+     * @param data new data (cannot be null)
+     */
+    public void setThisData(byte[] data)
+    {
+        try
+        {
+            pen.setData(data);
+        }
+        catch ( Exception e )
+        {
+            Throwables.propagate(e);
+        }
+    }
+
+    /**
      * Have thisId leave the group and stop caching membership
      */
     @Override
@@ -112,7 +126,7 @@ public class GroupMember implements Closeable
         }
         if ( !thisIdAdded )
         {
-            builder.put(thisId, payload);   // this instance is always a member
+            builder.put(thisId, pen.getData());   // this instance is always a member
         }
         return builder.build();
     }

http://git-wip-us.apache.org/repos/asf/curator/blob/a49d2bb4/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
index 7e00e10..ff9cb12 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
@@ -352,7 +352,12 @@ public class PersistentEphemeralNode implements Closeable
         }
     }
 
-    private byte[] getData() {
+    /**
+     * Return the current value of our data
+     *
+     * @return our data
+     */
+    public byte[] getData() {
         return this.data.get();
     }
 

http://git-wip-us.apache.org/repos/asf/curator/blob/a49d2bb4/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestGroupMember.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestGroupMember.java b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestGroupMember.java
index 3d4b951..b67831d 100644
--- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestGroupMember.java
+++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestGroupMember.java
@@ -68,6 +68,13 @@ public class TestGroupMember extends BaseClassForTests
             Assert.assertEquals(currentMembers1.size(), 1);
             Assert.assertTrue(currentMembers1.containsKey("1"));
             Assert.assertFalse(currentMembers1.containsKey("2"));
+
+            groupMember1.setThisData("something".getBytes());
+
+            timing.sleepABit();
+            currentMembers1 = groupMember1.getCurrentMembers();
+            Assert.assertTrue(currentMembers1.containsKey("1"));
+            Assert.assertEquals(currentMembers1.get("1"), "something".getBytes());
         }
         finally
         {