curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From randg...@apache.org
Subject [10/14] git commit: CURATOR-35 - added a full featured sync() method and deprecated the previous method
Date Thu, 20 Jun 2013 17:50:56 GMT
CURATOR-35 - added a full featured sync() method and deprecated the previous method


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

Branch: refs/heads/master
Commit: d9d0dc2fdb63ae0f2754144a250348774454d701
Parents: 6a26d4f
Author: randgalt <randgalt@apache.org>
Authored: Tue Jun 18 14:11:42 2013 -0700
Committer: randgalt <randgalt@apache.org>
Committed: Tue Jun 18 14:11:42 2013 -0700

----------------------------------------------------------------------
 .../curator/framework/CuratorFramework.java     | 29 ++++--
 .../curator/framework/api/SyncBuilder.java      | 24 +++++
 .../framework/imps/CuratorFrameworkImpl.java    |  6 ++
 .../curator/framework/imps/SyncBuilderImpl.java | 92 ++++++++++++++++++++
 .../curator/framework/imps/TestFramework.java   | 31 +++++++
 5 files changed, 173 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/d9d0dc2f/curator-framework/src/main/java/org/apache/curator/framework/CuratorFramework.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/CuratorFramework.java
b/curator-framework/src/main/java/org/apache/curator/framework/CuratorFramework.java
index 0ca9b23..e49386e 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/CuratorFramework.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/CuratorFramework.java
@@ -16,6 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+
 package org.apache.curator.framework;
 
 import org.apache.curator.CuratorZookeeperClient;
@@ -35,12 +36,12 @@ public interface CuratorFramework extends Closeable
     /**
      * Start the client. Most mutator methods will not work until the client is started
      */
-    public void     start();
+    public void start();
 
     /**
      * Stop the client
      */
-    public void     close();
+    public void close();
 
     /**
      * Returns the state of this instance
@@ -55,7 +56,7 @@ public interface CuratorFramework extends Closeable
      * @return true/false
      * @deprecated use {@link #getState()} instead
      */
-    public boolean  isStarted();
+    public boolean isStarted();
 
     /**
      * Start a create builder
@@ -73,7 +74,7 @@ public interface CuratorFramework extends Closeable
 
     /**
      * Start an exists builder
-     *
+     * <p/>
      * The builder will return a Stat object as if org.apache.zookeeper.ZooKeeper.exists()
were called.  Thus, a null
      * means that it does not exist and an actual Stat object means it does exist.
      *
@@ -126,10 +127,19 @@ public interface CuratorFramework extends Closeable
     /**
      * Perform a sync on the given path - syncs are always in the background
      *
-     * @param path the path
+     * @param path                    the path
      * @param backgroundContextObject optional context
+     * @deprecated use {@link #sync()} instead
+     */
+    public void sync(String path, Object backgroundContextObject);
+
+    /**
+     * Start a sync builder. Note: sync is ALWAYS in the background even
+     * if you don't use one of the background() methods
+     *
+     * @return builder object
      */
-    public void     sync(String path, Object backgroundContextObject);
+    public SyncBuilder sync();
 
     /**
      * Returns the listenable interface for the Connect State
@@ -143,14 +153,14 @@ public interface CuratorFramework extends Closeable
      *
      * @return listenable
      */
-    public Listenable<CuratorListener>         getCuratorListenable();
+    public Listenable<CuratorListener> getCuratorListenable();
 
     /**
      * Returns the listenable interface for unhandled errors
      *
      * @return listenable
      */
-    public Listenable<UnhandledErrorListener>  getUnhandledErrorListenable();
+    public Listenable<UnhandledErrorListener> getUnhandledErrorListenable();
 
     /**
      * Returns a facade of the current instance that does _not_ automatically
@@ -172,6 +182,7 @@ public interface CuratorFramework extends Closeable
 
     /**
      * Return the current namespace or "" if none
+     *
      * @return namespace
      */
     public String getNamespace();
@@ -189,5 +200,5 @@ public interface CuratorFramework extends Closeable
      * @param path path to ensure
      * @return new EnsurePath instance
      */
-    public EnsurePath    newNamespaceAwareEnsurePath(String path);
+    public EnsurePath newNamespaceAwareEnsurePath(String path);
 }

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/d9d0dc2f/curator-framework/src/main/java/org/apache/curator/framework/api/SyncBuilder.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/api/SyncBuilder.java
b/curator-framework/src/main/java/org/apache/curator/framework/api/SyncBuilder.java
new file mode 100644
index 0000000..6ae3586
--- /dev/null
+++ b/curator-framework/src/main/java/org/apache/curator/framework/api/SyncBuilder.java
@@ -0,0 +1,24 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.curator.framework.api;
+
+public interface SyncBuilder extends
+    BackgroundPathable<Void>
+{
+}

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/d9d0dc2f/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java
b/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java
index 28cad83..c3b0992 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java
@@ -411,6 +411,12 @@ public class CuratorFrameworkImpl implements CuratorFramework
         internalSync(this, path, context);
     }
 
+    @Override
+    public SyncBuilder sync()
+    {
+        return new SyncBuilderImpl(this);
+    }
+
     protected void internalSync(CuratorFrameworkImpl impl, String path, Object context)
     {
         BackgroundOperation<String> operation = new BackgroundSyncImpl(impl, context);

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/d9d0dc2f/curator-framework/src/main/java/org/apache/curator/framework/imps/SyncBuilderImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/SyncBuilderImpl.java
b/curator-framework/src/main/java/org/apache/curator/framework/imps/SyncBuilderImpl.java
new file mode 100644
index 0000000..7c74abc
--- /dev/null
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/SyncBuilderImpl.java
@@ -0,0 +1,92 @@
+package org.apache.curator.framework.imps;
+
+import org.apache.curator.TimeTrace;
+import org.apache.curator.framework.api.BackgroundCallback;
+import org.apache.curator.framework.api.CuratorEvent;
+import org.apache.curator.framework.api.CuratorEventType;
+import org.apache.curator.framework.api.Pathable;
+import org.apache.curator.framework.api.SyncBuilder;
+import org.apache.zookeeper.AsyncCallback;
+import java.util.concurrent.Executor;
+
+public class SyncBuilderImpl implements SyncBuilder, BackgroundOperation<String>
+{
+    private final CuratorFrameworkImpl client;
+    private Backgrounding backgrounding = new Backgrounding();
+
+    public SyncBuilderImpl(CuratorFrameworkImpl client)
+    {
+        //To change body of created methods use File | Settings | File Templates.
+        this.client = client;
+    }
+
+    @Override
+    public Pathable<Void> inBackground()
+    {
+        // NOP always in background
+        return this;
+    }
+
+    @Override
+    public Pathable<Void> inBackground(Object context)
+    {
+        backgrounding = new Backgrounding(context);
+        return this;
+    }
+
+    @Override
+    public Pathable<Void> inBackground(BackgroundCallback callback)
+    {
+        backgrounding = new Backgrounding(callback);
+        return this;
+    }
+
+    @Override
+    public Pathable<Void> inBackground(BackgroundCallback callback, Object context)
+    {
+        backgrounding = new Backgrounding(callback, context);
+        return this;
+    }
+
+    @Override
+    public Pathable<Void> inBackground(BackgroundCallback callback, Executor executor)
+    {
+        backgrounding = new Backgrounding(callback, executor);
+        return this;
+    }
+
+    @Override
+    public Pathable<Void> inBackground(BackgroundCallback callback, Object context,
Executor executor)
+    {
+        backgrounding = new Backgrounding(client, callback, context, executor);
+        return this;
+    }
+
+    @Override
+    public void performBackgroundOperation(final OperationAndData<String> operationAndData)
throws Exception
+    {
+        final TimeTrace trace = client.getZookeeperClient().startTracer("SyncBuilderImpl-Background");
+        final String path = operationAndData.getData();
+        String adjustedPath = client.fixForNamespace(path);
+
+        AsyncCallback.VoidCallback voidCallback = new AsyncCallback.VoidCallback()
+        {
+            @Override
+            public void processResult(int rc, String path, Object ctx)
+            {
+                trace.commit();
+                CuratorEvent event = new CuratorEventImpl(client, CuratorEventType.SYNC,
rc, path, path, ctx, null, null, null, null, null);
+                client.processBackgroundOperation(operationAndData, event);
+            }
+        };
+        client.getZooKeeper().sync(adjustedPath, voidCallback, backgrounding.getContext());
+    }
+
+    @Override
+    public Void forPath(String path) throws Exception
+    {
+        OperationAndData<String> operationAndData = new OperationAndData<String>(this,
path, backgrounding.getCallback(), null);
+        client.processBackgroundOperation(operationAndData, null);
+        return null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/d9d0dc2f/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFramework.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFramework.java
b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFramework.java
index a3bf73b..f7ef6e9 100644
--- a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFramework.java
+++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFramework.java
@@ -427,6 +427,37 @@ public class TestFramework extends BaseClassForTests
     }
 
     @Test
+    public void     testSyncNew() throws Exception
+    {
+        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(),
new RetryOneTime(1));
+        client.start();
+        try
+        {
+            client.create().forPath("/head");
+            Assert.assertNotNull(client.checkExists().forPath("/head"));
+
+            final CountDownLatch      latch = new CountDownLatch(1);
+            BackgroundCallback callback = new BackgroundCallback()
+            {
+                @Override
+                public void processResult(CuratorFramework client, CuratorEvent event) throws
Exception
+                {
+                    if ( event.getType() == CuratorEventType.SYNC )
+                    {
+                        latch.countDown();
+                    }
+                }
+            };
+            client.sync().inBackground(callback).forPath("/head");
+            Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
+        }
+        finally
+        {
+            client.close();
+        }
+    }
+
+    @Test
     public void     testBackgroundDelete() throws Exception
     {
         CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(),
new RetryOneTime(1));


Mime
View raw message