Repository: curator
Updated Branches:
  refs/heads/CURATOR-397 6428a9238 -> 523f0c809


Added readThrough methods


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

Branch: refs/heads/CURATOR-397
Commit: 523f0c8095fcde7033b9caa4c5423301085b0e16
Parents: 6428a92
Author: randgalt <[email protected]>
Authored: Fri Jun 9 18:01:58 2017 -0500
Committer: randgalt <[email protected]>
Committed: Fri Jun 9 18:01:58 2017 -0500

----------------------------------------------------------------------
 .../modeled/cached/CachedModeledFramework.java  | 33 ++++++++++++++++++-
 .../details/CachedModeledFrameworkImpl.java     | 34 ++++++++++++++++----
 2 files changed, 59 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/523f0c80/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/cached/CachedModeledFramework.java
----------------------------------------------------------------------
diff --git 
a/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/cached/CachedModeledFramework.java
 
b/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/cached/CachedModeledFramework.java
index c08394a..f0d1d1b 100644
--- 
a/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/cached/CachedModeledFramework.java
+++ 
b/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/cached/CachedModeledFramework.java
@@ -19,10 +19,12 @@
 package org.apache.curator.x.async.modeled.cached;
 
 import org.apache.curator.framework.listen.Listenable;
+import org.apache.curator.x.async.AsyncStage;
 import org.apache.curator.x.async.modeled.ModeledFramework;
+import org.apache.curator.x.async.modeled.ZNode;
 import org.apache.curator.x.async.modeled.ZPath;
+import org.apache.zookeeper.data.Stat;
 import java.io.Closeable;
-import java.util.concurrent.Executor;
 
 public interface CachedModeledFramework<T> extends ModeledFramework<T>, 
Closeable
 {
@@ -72,4 +74,33 @@ public interface CachedModeledFramework<T> extends 
ModeledFramework<T>, Closeabl
      */
     @Override
     CachedModeledFramework<T> withPath(ZPath path);
+
+    /**
+     * Same as {@link #read()} except that if the cache does not have a value
+     * for this path a direct query is made.
+     *
+     * @return AsyncStage
+     * @see org.apache.curator.x.async.AsyncStage
+     */
+    AsyncStage<T> readThrough();
+
+    /**
+     * Same as {@link #read(org.apache.zookeeper.data.Stat)} except that if 
the cache does not have a value
+     * for this path a direct query is made.
+     *
+     * @param storingStatIn the stat for the new ZNode is stored here
+     * @return AsyncStage
+     * @see org.apache.curator.x.async.AsyncStage
+     */
+    AsyncStage<T> readThrough(Stat storingStatIn);
+
+    /**
+     * Same as {@link #readAsZNode()} except that if the cache does not have a 
value
+     * for this path a direct query is made.
+     *
+     *
+     * @return AsyncStage
+     * @see org.apache.curator.x.async.AsyncStage
+     */
+    AsyncStage<ZNode<T>> readThroughAsZNode();
 }

http://git-wip-us.apache.org/repos/asf/curator/blob/523f0c80/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/CachedModeledFrameworkImpl.java
----------------------------------------------------------------------
diff --git 
a/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/CachedModeledFrameworkImpl.java
 
b/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/CachedModeledFrameworkImpl.java
index 5ab925a..3893f47 100644
--- 
a/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/CachedModeledFrameworkImpl.java
+++ 
b/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/CachedModeledFrameworkImpl.java
@@ -41,6 +41,7 @@ import java.util.Set;
 import java.util.concurrent.Executor;
 import java.util.concurrent.ExecutorService;
 import java.util.function.Function;
+import java.util.function.Supplier;
 
 class CachedModeledFrameworkImpl<T> implements CachedModeledFramework<T>
 {
@@ -167,7 +168,7 @@ class CachedModeledFrameworkImpl<T> implements 
CachedModeledFramework<T>
     @Override
     public AsyncStage<T> read()
     {
-        return internalRead(ZNode::model);
+        return internalRead(ZNode::model, this::exceptionally);
     }
 
     @Override
@@ -179,13 +180,31 @@ class CachedModeledFrameworkImpl<T> implements 
CachedModeledFramework<T>
                 DataTree.copyStat(n.stat(), storingStatIn);
             }
             return n.model();
-        });
+        }, this::exceptionally);
     }
 
     @Override
     public AsyncStage<ZNode<T>> readAsZNode()
     {
-        return internalRead(Function.identity());
+        return internalRead(Function.identity(), this::exceptionally);
+    }
+
+    @Override
+    public AsyncStage<T> readThrough()
+    {
+        return internalRead(ZNode::model, client::read);
+    }
+
+    @Override
+    public AsyncStage<T> readThrough(Stat storingStatIn)
+    {
+        return internalRead(ZNode::model, () -> client.read(storingStatIn));
+    }
+
+    @Override
+    public AsyncStage<ZNode<T>> readThroughAsZNode()
+    {
+        return internalRead(Function.identity(), client::readAsZNode);
     }
 
     @Override
@@ -287,16 +306,17 @@ class CachedModeledFrameworkImpl<T> implements 
CachedModeledFramework<T>
         return asyncDefaultMode ? ModelStage.asyncCompleted(value, executor) : 
ModelStage.completed(value);
     }
 
-    private <U> AsyncStage<U> exceptionally(Exception e)
+    private <U> AsyncStage<U> exceptionally()
     {
-        return asyncDefaultMode ? ModelStage.asyncExceptionally(e, executor) : 
ModelStage.exceptionally(e);
+        KeeperException.NoNodeException exception = new 
KeeperException.NoNodeException(client.modelSpec().path().fullPath());
+        return asyncDefaultMode ? ModelStage.asyncExceptionally(exception, 
executor) : ModelStage.exceptionally(exception);
     }
 
-    private <U> AsyncStage<U> internalRead(Function<ZNode<T>, U> resolver)
+    private <U> AsyncStage<U> internalRead(Function<ZNode<T>, U> resolver, 
Supplier<AsyncStage<U>> elseProc)
     {
         ZPath path = client.modelSpec().path();
         Optional<ZNode<T>> data = cache.currentData(path);
         return data.map(node -> completed(resolver.apply(node)))
-            .orElseGet(() -> exceptionally(new 
KeeperException.NoNodeException(path.fullPath())));
+            .orElseGet(elseProc);
     }
 }

Reply via email to