码迷,mamicode.com
首页 > 其他好文 > 详细

zookeeper源码之配置存储

时间:2018-01-30 20:02:36      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:配置   height   throws   gif   复制   addchild   权限   toolbar   time   

  配置存储主要维护了一个树结构,实现了类似文件系统的读写修改操作。 

类图结构

技术分享图片

  DataTree内部维护了一个树结构,并且也维护了一个hastable结构,key为路径,value为节点,方便根据路径查询。 

技术分享图片
public class DataTree {
...
private final ConcurrentHashMap<String, DataNode> nodes =
        new ConcurrentHashMap<String, DataNode>();
...
public void addDataNode(String path, DataNode node) {
        nodes.put(path, node);
}
public DataNode getNode(String path) {
     return nodes.get(path);
}
public int getNodeCount() {
    return nodes.size();
}
public String createNode(String path, byte data[], List<ACL> acl,
            long ephemeralOwner, long zxid, long time)
            throws KeeperException.NoNodeException,
            KeeperException.NodeExistsException {
        ...
        String parentName = path.substring(0, lastSlash);
        ...
        DataNode parent = nodes.get(parentName);
        ...
        synchronized (parent) {
            Set<String> children = parent.getChildren();
            ...
            DataNode child = new DataNode(parent, data, longval, stat);
            parent.addChild(childName);
            nodes.put(path, child);
            ...
        }
       ...
        return path;
    }
public void deleteNode(String path, long zxid)
            throws KeeperException.NoNodeException {
        ...
        String parentName = path.substring(0, lastSlash);
        String childName = path.substring(lastSlash + 1);
        DataNode node = nodes.get(path);
        ...
        nodes.remove(path);
        DataNode parent = nodes.get(parentName);
        ...
        synchronized (parent) {
            parent.removeChild(childName);
            ....
            node.parent = null;
        }
        ...
    }
public Stat setData(String path, byte data[], int version, long zxid,
            long time) throws KeeperException.NoNodeException {
        Stat s = new Stat();
        DataNode n = nodes.get(path);
        ...
        synchronized (n) {
            lastdata = n.data;
            n.data = data;
            ...
            n.copyStat(s);
        }
        ...
        return s;
    }
public byte[] getData(String path, Stat stat, Watcher watcher)
            throws KeeperException.NoNodeException {
        DataNode n = nodes.get(path);
        ...
        return n.data;
    }
}
技术分享图片

   DataNode为树结构的一个节点,内部存储了父节点、子节点、节点数据、节点权限、持久化信息。

技术分享图片
public class DataNode{
    DataNode parent;
    byte data[];
    Long acl;
    public StatPersisted stat;
    private Set<String> children = null;
}
技术分享图片

 

zookeeper源码之配置存储

标签:配置   height   throws   gif   复制   addchild   权限   toolbar   time   

原文地址:https://www.cnblogs.com/zhangwanhua/p/8385962.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!