Mercurial > hg > Members > tatsuki > bench > jungle-core
annotate src/main/java/jungle/core/graph/simple/SimpleVertex.java @ 8:abed5bd92fcb
commit
author | shoshi <shoshi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 03 Jul 2012 18:59:28 +0900 |
parents | c3c65308a11b |
children |
rev | line source |
---|---|
4
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 package jungle.core.graph.simple; |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 |
8 | 3 import java.util.Iterator; |
4 import java.util.concurrent.atomic.AtomicReference; | |
5 import fj.Ord; | |
6 import fj.P2; | |
7 import fj.data.Option; | |
8 import fj.data.TreeMap; | |
4
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 import jungle.core.graph.Graph; |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 import jungle.core.graph.Vertex; |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 import jungle.core.graph.Vertexes; |
8 | 12 import jungle.util.Pair; |
4
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 public class SimpleVertex implements Vertex |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 { |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 private String id; |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 private final SimpleGraph parent; |
8 | 18 private final AtomicReference<TreeMap<String,String>> propertiesHolder; |
19 private final AtomicReference<TreeMap<String,SimpleVertexes>> relationsHolder; | |
20 | |
21 private static final TreeMap<String,String> EMPTY_PROPERTIES_MAP = TreeMap.empty(Ord.stringOrd); | |
22 private static final TreeMap<String,SimpleVertexes> EMPTY_RELATIONS_MAP = TreeMap.empty(Ord.stringOrd); | |
4
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 public SimpleVertex(SimpleGraph _parent,String _id) |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25 { |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
26 id = _id; |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27 parent = _parent; |
8 | 28 propertiesHolder = new AtomicReference<TreeMap<String,String>>(EMPTY_PROPERTIES_MAP); |
29 relationsHolder = new AtomicReference<TreeMap<String,SimpleVertexes>>(EMPTY_RELATIONS_MAP); | |
30 } | |
31 | |
32 public SimpleVertex(SimpleGraph _parent,String _id,SimpleVertex _template) | |
33 { | |
34 id = _id; | |
35 parent = _parent; | |
36 | |
37 //get snapshot | |
38 TreeMap<String,String> snapshotProperties = _template. | |
39 | |
40 propertiesHolder = new AtomicReference<TreeMap<String,String>>(); | |
41 relationsHolder = new AtomicReference<TreeMap<String,SimpleVertexes>>(); | |
42 } | |
43 | |
44 private Pair<TreeMap<String,String>,TreeMap<String,SimpleVertexes>> createVertexSnapshot() | |
45 { | |
46 TreeMap<String,String> snapshotProperties = propertiesHolder.get(); | |
47 TreeMap<String,SimpleVertexes> snapshotRelations = relationsHolder.get(); | |
48 | |
49 return new Pair<TreeMap<Stirng,String>> | |
4
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
50 } |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
51 |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
52 void setID(String _id) |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
53 { |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
54 id = _id; |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
55 } |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
56 |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
57 @Override |
7
c3c65308a11b
removed some package and added Graph API
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
58 public String setPropertyIfAbsent(String _key,String _value) |
c3c65308a11b
removed some package and added Graph API
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
59 { |
8 | 60 TreeMap<String,String> current,update; |
61 do{ | |
62 current = propertiesHolder.get(); | |
63 Option<String> value = current.get(_key); | |
64 if(value.isSome()){ | |
65 return value.some(); | |
66 } | |
67 | |
68 update = current.set(_key,_value); | |
69 }while(!propertiesHolder.compareAndSet(current,update)); | |
70 | |
71 return null; | |
7
c3c65308a11b
removed some package and added Graph API
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
72 } |
c3c65308a11b
removed some package and added Graph API
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
73 |
c3c65308a11b
removed some package and added Graph API
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
74 @Override |
5
07b26b4b21e0
modified AbstractVertexesTest
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
75 public String toString() |
07b26b4b21e0
modified AbstractVertexesTest
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
76 { |
07b26b4b21e0
modified AbstractVertexesTest
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
77 return id; |
07b26b4b21e0
modified AbstractVertexesTest
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
78 } |
07b26b4b21e0
modified AbstractVertexesTest
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
79 |
07b26b4b21e0
modified AbstractVertexesTest
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
80 @Override |
4
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
81 public String getID() |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
82 { |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
83 return id; |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
84 } |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
85 |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
86 @Override |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
87 public String getProperty(String _key) |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
88 { |
8 | 89 TreeMap<String,String> current = propertiesHolder.get(); |
90 Option<String> nullOrValue = current.get(_key); | |
91 if(nullOrValue.isNone()){ | |
92 return null; | |
93 } | |
94 | |
95 return nullOrValue.some(); | |
4
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
96 } |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
97 |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
98 @Override |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
99 public void setProperty(String _key, String _value) |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
100 { |
8 | 101 TreeMap<String,String> current , update; |
102 do{ | |
103 current = propertiesHolder.get(); | |
104 update = current.set(_key,_value); | |
105 }while(!propertiesHolder.compareAndSet(current,update)); | |
4
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
106 } |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
107 |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
108 @Override |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
109 public String removeProperty(String _key) |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
110 { |
8 | 111 if(_key == null){ |
112 throw new NullPointerException("_key is null"); | |
113 } | |
114 | |
115 String remove; | |
116 TreeMap<String,String> current , update; | |
117 do{ | |
118 current = propertiesHolder.get(); | |
119 Option<String> nullOrValue = current.get(_key); | |
120 if(nullOrValue.isNone()){ | |
121 return null; | |
122 } | |
123 | |
124 remove = nullOrValue.some(); | |
125 update = current.delete(_key); | |
126 }while(!propertiesHolder.compareAndSet(current,update)); | |
127 | |
128 return remove; | |
4
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
129 } |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
130 |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
131 @Override |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
132 public Vertexes getVertexes(String _key) |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
133 { |
8 | 134 if(_key == null){ |
135 throw new NullPointerException("_key is null"); | |
136 } | |
137 | |
138 TreeMap<String,SimpleVertexes> current = relationsHolder.get(); | |
139 Option<SimpleVertexes> nullOrValue = current.get(_key); | |
140 if(nullOrValue.isNone()){ | |
141 return null; | |
142 } | |
143 | |
144 return nullOrValue.some(); | |
4
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
145 } |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
146 |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
147 @Override |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
148 public Vertexes removeVertexes(String _type) |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
149 { |
8 | 150 if(_type == null){ |
151 throw new NullPointerException("_type is null"); | |
152 } | |
153 | |
154 SimpleVertexes remove; | |
155 TreeMap<String,SimpleVertexes> current , update; | |
156 do{ | |
157 current = relationsHolder.get(); | |
158 Option<SimpleVertexes> nullOrValue = current.get(_type); | |
159 if(nullOrValue.isNone()){ | |
160 return null; | |
161 } | |
162 | |
163 remove = nullOrValue.some(); | |
164 update = current.delete(_type); | |
165 }while(!relationsHolder.compareAndSet(current,update)); | |
166 | |
167 return remove; | |
4
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
168 } |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
169 |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
170 @Override |
8 | 171 public boolean compareAndSwapProprety(String _key,final String _except,final String _value) |
4
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
172 { |
8 | 173 if(_key == null || _except == null || _value == null){ |
174 throw new NullPointerException("_key or _except or _value is null"); | |
175 } | |
176 | |
177 TreeMap<String,String> current , update; | |
178 do{ | |
179 current = propertiesHolder.get(); | |
180 Option<String> nullOrValue = current.get(_key); | |
181 if(nullOrValue.isNone()){ | |
182 return false; | |
183 } | |
184 | |
185 String value = nullOrValue.some(); | |
186 if(!value.equals(_except)){ | |
187 return false; | |
188 } | |
189 | |
190 update = current.set(_key,_value); | |
191 }while(!propertiesHolder.compareAndSet(current,update)); | |
192 | |
193 return true; | |
4
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
194 } |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
195 |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
196 @Override |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
197 public Graph getGraph() |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
198 { |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
199 return parent; |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
200 } |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
201 |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
202 @Override |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
203 public Vertexes createVertexes(String _type) |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
204 { |
8 | 205 TreeMap<String,SimpleVertexes> current , update; |
206 | |
207 SimpleVertexes newVertexes; | |
208 do{ | |
209 current = relationsHolder.get(); | |
210 Option<SimpleVertexes> nullOrValue = current.get(_type); | |
211 if(nullOrValue.isSome()){ | |
212 return null; | |
213 } | |
214 | |
215 newVertexes = new SimpleVertexes(_type,parent); | |
216 update = current.set(_type,newVertexes); | |
217 }while(!relationsHolder.compareAndSet(current,update)); | |
218 | |
219 return newVertexes; | |
220 } | |
221 | |
222 @Override | |
223 public Iterable<Pair<String,String>> getProperties() | |
224 { | |
225 final TreeMap<String,String> snapshot = propertiesHolder.get(); | |
226 Iterable<Pair<String,String>> snapshotIterableWrapper = new Iterable<Pair<String,String>>(){ | |
227 @Override | |
228 public Iterator<Pair<String, String>> iterator(){ | |
229 return new Iterator<Pair<String,String>>(){ | |
230 Iterator<P2<String,String>> snapshotKeyValueIterator = snapshot.iterator(); | |
231 @Override | |
232 public boolean hasNext(){ | |
233 return snapshotKeyValueIterator.hasNext(); | |
234 } | |
235 @Override | |
236 public Pair<String, String> next(){ | |
237 P2<String,String> keyValue = snapshotKeyValueIterator.next(); | |
238 return new Pair<String,String>(keyValue._1(),keyValue._2()); | |
239 } | |
240 @Override | |
241 public void remove() { | |
242 throw new UnsupportedOperationException("removing is not supported."); | |
243 } | |
244 }; | |
245 } | |
246 }; | |
247 return snapshotIterableWrapper; | |
4
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
248 } |
761d04aecfcb
added Graph API and some implementation
shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
249 } |