配置存储主要维护了一个树结构,实现了类似文件系统的读写修改操作。
类图结构
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为树结构的一个节点,内部存储了父节点、子节点、节点数据、节点权限、持久化信息。