annotate src/treecms/memory/OnMemoryTree.java @ 9:17ed97ca9960

commit
author shoshi
date Mon, 18 Apr 2011 01:07:27 +0900
parents f96193babac0
children 85061e874775
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
1 package treecms.memory;
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
2
8
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
3 import java.nio.ByteBuffer;
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
4 import java.util.List;
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
5 import java.util.Map;
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
6 import java.util.concurrent.atomic.AtomicReference;
9
shoshi
parents: 8
diff changeset
7 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
3
5fa718b63cd5 finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents: 2
diff changeset
8
2
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
9 import treecms.api.Forest;
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
10 import treecms.api.Node;
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
11 import treecms.api.NodeData;
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
12 import treecms.api.NodeID;
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
13 import treecms.api.Tree;
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
14
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
15 /**
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
16 * オンメモリのTree実装です。
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
17 * 木構造のルートとなるNodeをメンバーとして持ち、操作はすべて転送します。
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
18 * @author shoshi
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
19 */
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
20 final class OnMemoryTree implements Tree
2
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
21 {
9
shoshi
parents: 8
diff changeset
22 private static final AtomicReferenceFieldUpdater<OnMemoryTree,OnMemoryNode> m_refUpdater = AtomicReferenceFieldUpdater.newUpdater(OnMemoryTree.class,OnMemoryNode.class,"m_root");
shoshi
parents: 8
diff changeset
23 private volatile OnMemoryNode m_root;
2
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
24
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
25 /**
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
26 * コンストラクタです。
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
27 * @param _newRoot 木構造のルートノードです。
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
28 */
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
29 public OnMemoryTree(OnMemoryNode _newRoot)
2
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
30 {
9
shoshi
parents: 8
diff changeset
31 m_root = _newRoot;
2
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
32 }
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
33
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
34 /**
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
35 * Nodeが属するForestを取得します.
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
36 * @return Nodeが属するForest
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
37 */
2
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
38 @Override
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
39 public Forest getForest()
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
40 {
9
shoshi
parents: 8
diff changeset
41 return m_root.getForest();
2
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
42 }
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
43
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
44 /**
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
45 * Nodeに対応するNodeIDを取得します.
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
46 * @return Nodeに対応するNodeID
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
47 */
2
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
48 @Override
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
49 public NodeID getID()
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
50 {
9
shoshi
parents: 8
diff changeset
51 return m_root.getID();
2
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
52 }
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
53
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
54 /**
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
55 * Nodeが保持するデータを取得します.クライアントはこのメソッドを用いて取得されるNodeDataを用いてNodeの内容を<b>変更できません</b>。
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
56 * @return Nodeが保持するNodeData
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
57 */
2
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
58 @Override
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
59 public NodeData getData()
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
60 {
9
shoshi
parents: 8
diff changeset
61 return m_root.getData();
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
62 }
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
63
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
64 /**
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
65 * この木構造のルートNodeを取得します.
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
66 * @return ルートNode
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
67 */
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
68 @Override
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
69 public Node getRoot()
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
70 {
9
shoshi
parents: 8
diff changeset
71 return m_root;
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
72 }
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
73
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
74 /**
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
75 * 指定されたNodeを子供Nodeとして追加します.
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
76 * @param _child
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
77 */
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
78 @Override
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
79 public void add(Node _child)
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
80 {
9
shoshi
parents: 8
diff changeset
81 m_root.add(_child);
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
82 }
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
83
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
84 /**
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
85 * 指定されたリストに含まれるNodeを,すべて子供Nodeとして追加します.
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
86 * @param _children 追加される子供Nodeを保持するリスト
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
87 */
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
88 @Override
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
89 public void addAll(List<Node> _children)
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
90 {
9
shoshi
parents: 8
diff changeset
91 m_root.addAll(_children);
2
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
92 }
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
93
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
94 /**
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
95 * 子供Nodeのリストを取得します..
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
96 * @return 子供Nodeのリスト
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
97 */
2
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
98 @Override
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
99 public List<Node> children()
2
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
100 {
9
shoshi
parents: 8
diff changeset
101 return m_root.children();
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
102 }
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
103
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
104 /**
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
105 * このNodeが保持する値の中で指定されたキーと対応する値を取得します.
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
106 * @param _key データに対応するキー
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
107 * @return キーと対応する値,見つからない場合はnull
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
108 */
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
109 @Override
8
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
110 public ByteBuffer get(ByteBuffer _key)
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
111 {
9
shoshi
parents: 8
diff changeset
112 return m_root.get(_key);
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
113 }
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
114
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
115 /**
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
116 * このNodeが保持するデータをマップとしてすべて取得します.
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
117 * @return Nodeが保持するすべてのデータのマップ
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
118 */
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
119 @Override
8
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
120 public Map<ByteBuffer,ByteBuffer> getAll()
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
121 {
9
shoshi
parents: 8
diff changeset
122 return m_root.getAll();
2
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
123 }
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
124
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
125 /**
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
126 * キーとそれに対応する値を保存します.キーが重複した場合は上書きされます.
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
127 * @param _key キー
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
128 * @param _value 値
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
129 */
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
130 @Override
8
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
131 public void put(ByteBuffer _key,ByteBuffer _value)
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
132 {
9
shoshi
parents: 8
diff changeset
133 m_root.put(_key,_value);
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
134 }
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
135
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
136 /**
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
137 * キーとそれに対応する値を複数保持するマップを引数としてとり,マップが保持する値をすべて追加します.
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
138 * @param _map 追加される値のマップ
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
139 */
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
140 @Override
8
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
141 public void putAll(Map<ByteBuffer, ByteBuffer> _map)
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
142 {
9
shoshi
parents: 8
diff changeset
143 m_root.putAll(_map);
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
144 }
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
145
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
146 /**
8
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
147 * キーとそれに対応する値を削除します。
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
148 * @param _key キー
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
149 */
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
150 @Override
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
151 public void remove(ByteBuffer _key)
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
152 {
9
shoshi
parents: 8
diff changeset
153 m_root.remove(_key);
8
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
154 }
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
155
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
156 /**
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
157 * 子供Nodeを削除します。
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
158 * @param _child
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
159 */
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
160 @Override
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
161 public void remove(Node _child)
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
162 {
9
shoshi
parents: 8
diff changeset
163 m_root.remove(_child);
8
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
164 }
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
165
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
166 /**
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
167 * ルートNodeを比較して置き換えます.
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
168 * @param _except 比較する対象
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
169 * @param _newRoot 一致した場合置き換える対象
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
170 */
9
shoshi
parents: 8
diff changeset
171 public boolean compareAndSwapRootNode(OnMemoryNode _expect,OnMemoryNode _newRoot,boolean _force)
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
172 {
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
173 if(_force){
9
shoshi
parents: 8
diff changeset
174 m_root = _newRoot;
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
175 }
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
176
9
shoshi
parents: 8
diff changeset
177 m_refUpdater.compareAndSet(this,_expect,_newRoot);
7
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
178 return true;
fc19e38b669b added concurrent access client for cassandr
shoshi
parents: 6
diff changeset
179 }
8
f96193babac0 changed byte[] to ByteBuffer
shoshi
parents: 7
diff changeset
180
2
4a5ee88f02cf added OnMemoryForest
shoshi
parents:
diff changeset
181 }