jira-importer commented on issue #554:
URL: https://github.com/apache/curator/issues/554#issuecomment-2604695695

   <i><a 
href="https://issues.apache.org/jira/secure/ViewProfile.jspa?name=githubbot";>githubbot</a>:</i>
   <p>Github user Randgalt commented on a diff in the pull request:</p>
   
   <p>    <a 
href="https://github.com/apache/curator/pull/17#discussion_r15658282"; 
class="external-link" target="_blank" rel="nofollow 
noopener">https://github.com/apache/curator/pull/17#discussion_r15658282</a></p>
   
   <p>    &#8212; Diff: 
curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
 &#8212;<br/>
       @@ -0,0 +1,605 @@<br/>
       +/**<br/>
       + * Licensed to the Apache Software Foundation (ASF) under one<br/>
       + * or more contributor license agreements.  See the NOTICE file<br/>
       + * distributed with this work for additional information<br/>
       + * regarding copyright ownership.  The ASF licenses this file<br/>
       + * to you under the Apache License, Version 2.0 (the<br/>
       + * "License"); you may not use this file except in compliance<br/>
       + * with the License.  You may obtain a copy of the License at<br/>
       + *<br/>
       + *   <a href="http://www.apache.org/licenses/LICENSE-2.0"; 
class="external-link" target="_blank" rel="nofollow 
noopener">http://www.apache.org/licenses/LICENSE-2.0</a><br/>
       + *<br/>
       + * Unless required by applicable law or agreed to in writing,<br/>
       + * software distributed under the License is distributed on an<br/>
       + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY<br/>
       + * KIND, either express or implied.  See the License for the<br/>
       + * specific language governing permissions and limitations<br/>
       + * under the License.<br/>
       + */<br/>
       +<br/>
       +package org.apache.curator.framework.recipes.cache;<br/>
       +<br/>
       +import com.google.common.base.Function;<br/>
       +import com.google.common.collect.ImmutableSortedSet;<br/>
       +import com.google.common.collect.Maps;<br/>
       +import org.apache.curator.framework.CuratorFramework;<br/>
       +import org.apache.curator.framework.api.BackgroundCallback;<br/>
       +import org.apache.curator.framework.api.CuratorEvent;<br/>
       +import org.apache.curator.framework.listen.ListenerContainer;<br/>
       +import org.apache.curator.framework.state.ConnectionState;<br/>
       +import org.apache.curator.framework.state.ConnectionStateListener;<br/>
       +import org.apache.curator.utils.CloseableExecutorService;<br/>
       +import org.apache.curator.utils.ThreadUtils;<br/>
       +import org.apache.curator.utils.ZKPaths;<br/>
       +import org.apache.zookeeper.KeeperException;<br/>
       +import org.apache.zookeeper.WatchedEvent;<br/>
       +import org.apache.zookeeper.Watcher;<br/>
       +import org.apache.zookeeper.data.Stat;<br/>
       +import org.slf4j.Logger;<br/>
       +import org.slf4j.LoggerFactory;<br/>
       +import java.io.Closeable;<br/>
       +import java.io.IOException;<br/>
       +import java.util.ArrayList;<br/>
       +import java.util.Collections;<br/>
       +import java.util.List;<br/>
       +import java.util.SortedSet;<br/>
       +import java.util.concurrent.ConcurrentMap;<br/>
       +import java.util.concurrent.ExecutorService;<br/>
       +import java.util.concurrent.Executors;<br/>
       +import java.util.concurrent.ThreadFactory;<br/>
       +import java.util.concurrent.atomic.AtomicLong;<br/>
       +import java.util.concurrent.atomic.AtomicReference;<br/>
       +<br/>
       +/**<br/>
       + * <p>A utility that attempts to keep all data from all children of a 
ZK path locally cached. This class<br/>
       + * will watch the ZK path, respond to update/create/delete events, pull 
down the data, etc. You can<br/>
       + * register a listener that will get notified when changes 
occur.</p><br/>
       + * <p></p><br/>
       + * <p><b>IMPORTANT</b> - it's not possible to stay transactionally in 
sync. Users of this class must<br/>
       + * be prepared for false-positives and false-negatives. Additionally, 
always use the version number<br/>
       + * when updating data to avoid overwriting another process' 
change.</p><br/>
       + */<br/>
       +public class TreeCache implements Closeable<br/>
       +{<br/>
       +    private static final Logger LOG = 
LoggerFactory.getLogger(TreeCache.class);<br/>
       +<br/>
       +    private enum NodeState<br/>
       +    </p>
   {
       +PENDING, LIVE, DEAD
       +    }
   <p>    +<br/>
       +    final class TreeNode implements Watcher, BackgroundCallback<br/>
       +    {<br/>
       +private final AtomicReference<NodeState> nodeState = new 
AtomicReference<NodeState>(NodeState.PENDING);<br/>
       +private final String path;<br/>
       +private final TreeNode parent;<br/>
       +private final AtomicReference<Stat> stat = new 
AtomicReference<Stat>();<br/>
       +private final AtomicReference<byte[]> data = new 
AtomicReference<byte[]>();<br/>
       +private final AtomicReference<ConcurrentMap<String, TreeNode>> children 
= new AtomicReference<ConcurrentMap<String, TreeNode>>();<br/>
       +<br/>
       +TreeNode(String path, TreeNode parent)<br/>
       +</p>
   {
       +    this.path = path;
       +    this.parent = parent;
       +}
   <p>    +<br/>
       +private void refreshChildren() throws Exception<br/>
       +</p>
   {
       +    outstandingOps.incrementAndGet();
       +    
client.getChildren().usingWatcher(this).inBackground(this).forPath(path);
       +}
   <p>    +<br/>
       +private void refreshData() throws Exception<br/>
       +{<br/>
       +    outstandingOps.incrementAndGet();<br/>
       +    if ( dataIsCompressed )<br/>
       +    </p>
   {
       
+client.getData().decompressed().usingWatcher(this).inBackground(this).forPath(path);
       +    }
   <p>    +    else<br/>
       +    </p>
   {
       +client.getData().usingWatcher(this).inBackground(this).forPath(path);
       +    }
   <p>    +}<br/>
       +<br/>
       +private void wasReconnected() throws Exception<br/>
       +{<br/>
       +    refreshData();<br/>
       +    refreshChildren();<br/>
       +    ConcurrentMap<String, TreeNode> childMap = children.get();<br/>
       +    if ( childMap != null )<br/>
       +    {<br/>
       +for ( TreeNode child : childMap.values() )<br/>
       +</p>
   {
       +    child.wasReconnected();
       +}
   <p>    +    }<br/>
       +}<br/>
       +<br/>
       +private void wasCreated() throws Exception<br/>
       +</p>
   {
       +    refreshData();
       +    refreshChildren();
       +}
   <p>    +<br/>
       +private void wasDeleted() throws Exception<br/>
       +{<br/>
       +    stat.set(null);<br/>
       +    data.set(null);<br/>
       +    client.clearWatcherReferences(this);<br/>
       +    ConcurrentMap<String, TreeNode> childMap = 
children.getAndSet(null);<br/>
       +    if ( childMap != null )<br/>
       +    {<br/>
       +ArrayList<TreeNode> childCopy = new 
ArrayList<TreeNode>(childMap.values());<br/>
       +childMap.clear();<br/>
       +for ( TreeNode child : childCopy )<br/>
       +</p>
   {
       +    child.wasDeleted();
       +}
   <p>    +    }<br/>
       +<br/>
       +    if ( treeState.get() == TreeState.CLOSED )<br/>
       +    </p>
   {
       +return;
       +    }
   <p>    +<br/>
       +    if ( nodeState.compareAndSet(NodeState.LIVE, NodeState.DEAD) )<br/>
       +    </p>
   {
       +publishEvent(TreeCacheEvent.Type.NODE_REMOVED, path);
       +    }
   <p>    +<br/>
       +    if ( parent == null )<br/>
       +    </p>
   {
       +// Root node; use an exist query to watch for existence.
       +client.checkExists().usingWatcher(this).inBackground().forPath(path);
       +    }
   <p>    +    else<br/>
       +    {<br/>
       +// Remove from parent if we're currently a child<br/>
       +ConcurrentMap<String, TreeNode> parentChildMap = 
parent.children.get();<br/>
       +if ( parentChildMap != null )<br/>
       +</p>
   {
       +    parentChildMap.remove(ZKPaths.getNodeFromPath(path), this);
       +}
   <p>    +    }<br/>
       +}<br/>
       +<br/>
       +@Override<br/>
       +public void process(WatchedEvent event)<br/>
       +{<br/>
       +    try<br/>
       +    {<br/>
       +switch ( event.getType() )<br/>
       +{<br/>
       +case NodeCreated:<br/>
       +    assert parent == null;<br/>
       &#8212; End diff &#8211;</p>
   
   <p>    Use Guava Preconditions instead with a reasonable message</p>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@curator.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to