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 2014/03/10 02:26:44 UTC

git commit: refined NodeCache APIs/data structures

Repository: curator
Updated Branches:
  refs/heads/CURATOR-88 982251cc3 -> a8f716d7a


refined NodeCache APIs/data structures


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

Branch: refs/heads/CURATOR-88
Commit: a8f716d7a467e371d2730bdd4c9491ddf03d7aa8
Parents: 982251c
Author: randgalt <ra...@apache.org>
Authored: Sun Mar 9 20:26:37 2014 -0500
Committer: randgalt <ra...@apache.org>
Committed: Sun Mar 9 20:26:37 2014 -0500

----------------------------------------------------------------------
 .../org/apache/curator/x/rest/api/Constants.java    |  6 +++++-
 .../apache/curator/x/rest/entities/NodeData.java    | 16 ++++++++++++++--
 .../apache/curator/x/rest/api/TestNodeCache.java    |  8 ++++----
 .../curator/x/rest/support/NodeCacheBridge.java     | 10 +++-------
 4 files changed, 26 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/a8f716d7/curator-x-rest/src/main/java/org/apache/curator/x/rest/api/Constants.java
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/main/java/org/apache/curator/x/rest/api/Constants.java b/curator-x-rest/src/main/java/org/apache/curator/x/rest/api/Constants.java
index 5ee1982..d8c8a3d 100644
--- a/curator-x-rest/src/main/java/org/apache/curator/x/rest/api/Constants.java
+++ b/curator-x-rest/src/main/java/org/apache/curator/x/rest/api/Constants.java
@@ -73,8 +73,12 @@ class Constants
 
     static NodeData toNodeData(ChildData c)
     {
+        if ( c == null )
+        {
+            return new NodeData();
+        }
         String payload = (c.getData() != null) ? new String(c.getData()) : "";
-        return new NodeData(c.getPath(), c.getStat(), payload);
+        return new NodeData(c.getPath(), c.getStat(), payload, true);
     }
 
     public static String newId()

http://git-wip-us.apache.org/repos/asf/curator/blob/a8f716d7/curator-x-rest/src/main/java/org/apache/curator/x/rest/entities/NodeData.java
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/main/java/org/apache/curator/x/rest/entities/NodeData.java b/curator-x-rest/src/main/java/org/apache/curator/x/rest/entities/NodeData.java
index 8326165..d1a4ea1 100644
--- a/curator-x-rest/src/main/java/org/apache/curator/x/rest/entities/NodeData.java
+++ b/curator-x-rest/src/main/java/org/apache/curator/x/rest/entities/NodeData.java
@@ -27,17 +27,19 @@ public class NodeData
     private String path;
     private Stat stat;
     private String data;
+    private boolean created;
 
     public NodeData()
     {
-        this("/", new Stat(), "");
+        this("/", new Stat(), "", false);
     }
 
-    public NodeData(String path, Stat stat, String data)
+    public NodeData(String path, Stat stat, String data, boolean created)
     {
         this.path = path;
         this.stat = stat;
         this.data = data;
+        this.created = created;
     }
 
     public String getPath()
@@ -69,4 +71,14 @@ public class NodeData
     {
         this.data = data;
     }
+
+    public boolean isCreated()
+    {
+        return created;
+    }
+
+    public void setCreated(boolean created)
+    {
+        this.created = created;
+    }
 }

http://git-wip-us.apache.org/repos/asf/curator/blob/a8f716d7/curator-x-rest/src/test/java/org/apache/curator/x/rest/api/TestNodeCache.java
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/test/java/org/apache/curator/x/rest/api/TestNodeCache.java b/curator-x-rest/src/test/java/org/apache/curator/x/rest/api/TestNodeCache.java
index 7982f18..4f08923 100644
--- a/curator-x-rest/src/test/java/org/apache/curator/x/rest/api/TestNodeCache.java
+++ b/curator-x-rest/src/test/java/org/apache/curator/x/rest/api/TestNodeCache.java
@@ -60,25 +60,25 @@ public class TestNodeCache extends BaseClassForTests
                 }
             );
 
-        Assert.assertNull(cache.getCurrentData());
+        Assert.assertFalse(cache.getCurrentData().isCreated());
 
         createSpec.setPath("/test/node");
         createSpec.setData("a");
         restClient.resource(uriMaker.getMethodUri("create")).type(MediaType.APPLICATION_JSON).post(PathAndId.class, createSpec);
         Assert.assertTrue(timing.acquireSemaphore(semaphore));
-        Assert.assertEquals(cache.getCurrentData().getData(), "a".getBytes());
+        Assert.assertEquals(cache.getCurrentData().getData(), "a");
 
         SetDataSpec setDataSpec = new SetDataSpec();
         setDataSpec.setPath("/test/node");
         setDataSpec.setData("b");
         restClient.resource(uriMaker.getMethodUri("setData")).type(MediaType.APPLICATION_JSON).post(setDataSpec);
         Assert.assertTrue(timing.acquireSemaphore(semaphore));
-        Assert.assertEquals(cache.getCurrentData().getData(), "b".getBytes());
+        Assert.assertEquals(cache.getCurrentData().getData(), "b");
 
         DeleteSpec deleteSpec = new DeleteSpec();
         deleteSpec.setPath("/test/node");
         restClient.resource(uriMaker.getMethodUri("delete")).type(MediaType.APPLICATION_JSON).post(deleteSpec);
         Assert.assertTrue(timing.acquireSemaphore(semaphore));
-        Assert.assertNull(cache.getCurrentData());
+        Assert.assertFalse(cache.getCurrentData().isCreated());
     }
 }

http://git-wip-us.apache.org/repos/asf/curator/blob/a8f716d7/curator-x-rest/src/test/java/org/apache/curator/x/rest/support/NodeCacheBridge.java
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/test/java/org/apache/curator/x/rest/support/NodeCacheBridge.java b/curator-x-rest/src/test/java/org/apache/curator/x/rest/support/NodeCacheBridge.java
index c88764d..476e05e 100644
--- a/curator-x-rest/src/test/java/org/apache/curator/x/rest/support/NodeCacheBridge.java
+++ b/curator-x-rest/src/test/java/org/apache/curator/x/rest/support/NodeCacheBridge.java
@@ -22,10 +22,10 @@ package org.apache.curator.x.rest.support;
 import com.google.common.base.Function;
 import com.sun.jersey.api.client.Client;
 import org.apache.curator.framework.listen.ListenerContainer;
-import org.apache.curator.framework.recipes.cache.ChildData;
 import org.apache.curator.framework.recipes.cache.NodeCacheListener;
 import org.apache.curator.x.rest.api.NodeCacheResource;
 import org.apache.curator.x.rest.entities.NodeCacheSpec;
+import org.apache.curator.x.rest.entities.NodeData;
 import org.apache.curator.x.rest.entities.PathAndId;
 import org.apache.curator.x.rest.entities.Status;
 import org.apache.curator.x.rest.entities.StatusMessage;
@@ -36,13 +36,11 @@ import javax.ws.rs.core.MediaType;
 import java.io.Closeable;
 import java.io.IOException;
 import java.util.List;
-import java.util.concurrent.atomic.AtomicReference;
 
 public class NodeCacheBridge implements Closeable, StatusListener
 {
     private final Logger log = LoggerFactory.getLogger(getClass());
     private final ListenerContainer<NodeCacheListener> listeners = new ListenerContainer<NodeCacheListener>();
-    private final AtomicReference<ChildData> data = new AtomicReference<ChildData>(null);
     private final Client restClient;
     private final SessionManager sessionManager;
     private final UriMaker uriMaker;
@@ -79,9 +77,9 @@ public class NodeCacheBridge implements Closeable, StatusListener
         return listeners;
     }
 
-    public ChildData getCurrentData()
+    public NodeData getCurrentData() throws Exception
     {
-        return data.get();
+        return restClient.resource(uriMaker.getMethodUri(NodeCacheResource.class, null)).path(id).type(MediaType.APPLICATION_JSON).get(NodeData.class);
     }
 
     @Override
@@ -91,8 +89,6 @@ public class NodeCacheBridge implements Closeable, StatusListener
         {
             if ( statusMessage.getType().equals("node-cache") && statusMessage.getSourceId().equals(id) )
             {
-                ChildData newData = (statusMessage.getMessage().length() > 0) ? new ChildData(path, null, statusMessage.getMessage().getBytes()) : null;
-                data.set(newData);
                 listeners.forEach(new Function<NodeCacheListener, Void>()
                 {
                     @Nullable