Mercurial > hg > Members > shoshi > TreeCMSv2
view src/treecms/memory/OnMemoryMonotonicTreeNode.java @ 24:68021f7091e1
commit
author | shoshi |
---|---|
date | Sun, 12 Jun 2011 20:41:20 +0900 |
parents | 77a894c0b919 |
children | c1e7ec6b3d44 |
line wrap: on
line source
package treecms.memory; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import treecms.api.MonotonicTreeNode; import treecms.api.Node; import treecms.api.NodeAttributes; import treecms.api.NodeID; import treecms.api.NodeTable; import treecms.tree.util.NodeData; public class OnMemoryMonotonicTreeNode implements MonotonicTreeNode { private OnMemoryMonotonicTreeNode m_parent; private final NodeTable m_table; private final ConcurrentMap<String,MonotonicTreeNode> m_cache; private final OnMemoryNode m_node; private final LockableReference<OnMemoryNode> m_tip; public OnMemoryMonotonicTreeNode(OnMemoryNode _node,OnMemoryMonotonicTreeNode _parent,NodeTable _table) { m_parent = _parent; m_cache = new ConcurrentHashMap<String,MonotonicTreeNode>(); m_table = _table; m_node = _node; } @Override public NodeID getID() { OnMemoryNode n = (OnMemoryNode)m_table.tip(m_fid); return n.getID(); } @Override public MonotonicTreeNode getParent() { if(m_parent == null){ return null; } synchronized(m_parent){ OnMemoryNode node = (OnMemoryNode)getNode(); //check , does parent contain the node. if(!m_parent.contains(node.getID())){ m_parent = null; } return m_parent; } } @Override public ByteBuffer get(ByteBuffer _key) { OnMemoryNode node = (OnMemoryNode)m_table.tip(m_fid); return node.get(_key); } @Override public NodeAttributes getAll() { OnMemoryNode node = (OnMemoryNode)m_table.tip(m_fid); return node.getAll(); } @Override public synchronized void put(ByteBuffer _key, ByteBuffer _value) { OnMemoryNode n = (OnMemoryNode)m_table.tip(m_fid); NodeData<Node> d = new NodeData<Node>(n,n); d.put(_key,_value); cloneAndTransmit(d); } @Override public synchronized void putAll(NodeAttributes _map) { OnMemoryNode n = (OnMemoryNode)m_table.tip(m_fid); NodeData<Node> d = new NodeData<Node>(n,n); d.putAll(_map); cloneAndTransmit(d); } @Override public synchronized void remove(ByteBuffer _key) { OnMemoryNode n = (OnMemoryNode)m_table.tip(m_fid); NodeData<Node> d = new NodeData<Node>(n,n); d.remove(_key); cloneAndTransmit(d); } @Override public synchronized void removeAll(Set<ByteBuffer> _keys) { OnMemoryNode n = (OnMemoryNode)m_table.tip(m_fid); NodeData<Node> d = new NodeData<Node>(n,n); d.removeAll(_keys); cloneAndTransmit(d); } @Override public synchronized void clearAttributes() { OnMemoryNode n = (OnMemoryNode)m_table.tip(m_fid); NodeData<Node> d = new NodeData<Node>(n,n); d.clearAttributes(); cloneAndTransmit(d); } public synchronized void cloneAndTransmit(NodeData<Node> _d) { OnMemoryNode node = (OnMemoryNode)m_table.tip(m_fid); NodeID newID = node.getID().update(); Node clone = new OnMemoryNode(newID,_d); OnMemoryMonotonicTreeNode parent = (OnMemoryMonotonicTreeNode)getParent(); if(parent != null){ parent.transmit(node,clone); } m_table.register(clone); } public synchronized boolean transmit(Node _orig,Node _edit) { OnMemoryNode node = (OnMemoryNode)m_table.tip(m_fid); NodeData<Node> d = new NodeData<Node>(node,node); d.replace(_edit); NodeID newID = node.getID().update(); OnMemoryNode clone = new OnMemoryNode(newID,null); m_table.register(_newNode); OnMemoryMonotonicTreeNode parent = (OnMemoryMonotonicTreeNode)getParent(); if(parent != null){ return m_parent.transmit(node,clone); } return true; } @Override public synchronized Node getNode() { return m_table.tip(m_fid); } @Override public synchronized List<MonotonicTreeNode> getList() { //NodeのリストよりMonotonicTreeNodeのリストを作成する. OnMemoryNode node = m_table.tip(m_fid); int size = node.getList().size(); ArrayList<MonotonicTreeNode> list = new ArrayList<MonotonicTreeNode>(size); for(Iterator<Node> it = node.getList().iterator();it.hasNext();){ OnMemoryNode n = (OnMemoryNode)it.next(); list.add(getCache(n.getID().getFamilyID())); } return Collections.unmodifiableList(list); } public OnMemoryMonotonicTreeNode getCache(final String _fid) { OnMemoryMonotonicTreeNode newCache = new OnMemoryMonotonicTreeNode(m_fid,this,m_table); OnMemoryMonotonicTreeNode cache = (OnMemoryMonotonicTreeNode)m_cache.putIfAbsent(_fid,newCache); if(cache == null){ return newCache; } return cache; } @Override public synchronized MonotonicTreeNode remove(int _index) { OnMemoryNode n = m_table.tip(m_fid); NodeData<Node> d = new NodeData<Node>(n,n); OnMemoryNode deleted = (OnMemoryNode) d.remove(_index); if(n != null){ cloneAndTransmit(d); OnMemoryMonotonicTreeNode tn = getCache(deleted.getID().getFamilyID()); return tn; } return null; } @Override public synchronized void clearChildren() { OnMemoryNode node = m_table.tip(m_fid); NodeData<Node> d = new NodeData<Node>(node,node); d.clearChildren(); cloneAndTransmit(d); } @Override public Map<ByteBuffer, ByteBuffer> asMap() { OnMemoryNode node = m_table.tip(m_fid); return node.asMap(); } @Override public Set<ByteBuffer> getKeySet() { OnMemoryNode node = m_table.tip(m_fid); return node.getKeySet(); } @Override public synchronized MonotonicTreeNode get(int _index) { OnMemoryNode node = m_table.tip(m_fid); return getCache(node.getID().getFamilyID()); } @Override public boolean contains(NodeID _id) { OnMemoryNode node = m_table.tip(m_fid); return node.contains(_id); } @Override public boolean swap(String _fid1,String _fid2) { OnMemoryNode node = m_table.tip(m_fid); return node.swap(_fid1,_fid2); } @Override public Set<String> getFamilyIDSet() { OnMemoryNode node = m_table.tip(m_fid); return node.getFamilyIDSet(); } @Override public synchronized MonotonicTreeNode get(String _fid) { OnMemoryMonotonicTreeNode mono = getCache(_fid); return mono; } @Override public synchronized MonotonicTreeNode remove(String _fid) { OnMemoryMonotonicTreeNode tnode = getCache(_fid); OnMemoryNode node = (OnMemoryNode)tnode.getNode(); NodeData<Node> d = new NodeData<Node>(node,node); d.remove(_fid); cloneAndTransmit(d); return tnode; } @Override public synchronized MonotonicTreeNode create(NodeAttributes _attr) { NodeID newID = getNode().getID().create(); OnMemoryNode newNode = m_table.createNode(newID,new NodeData<Node>(null,_attr)); OnMemoryNode thisNode = (OnMemoryNode)getNode(); NodeData<Node> d = new NodeData<Node>(thisNode,thisNode); d.add(newNode); cloneAndTransmit(d); OnMemoryMonotonicTreeNode tn = getCache(newID.getFamilyID()); return tn; } }