Mercurial > hg > Members > tatsuki > bench > jungle-core
annotate src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/InterfaceTraverser.java @ 153:20af7f25ef32
miner change
author | one |
---|---|
date | Tue, 25 Nov 2014 17:52:41 +0900 |
parents | 8a0aa8fc137c |
children | b8cef4b640a3 |
rev | line source |
---|---|
97 | 1 package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser; |
2 | |
98 | 3 import java.util.Iterator; |
4 | |
104 | 5 import fj.data.List; |
141 | 6 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NulIterator; |
97 | 7 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode; |
99 | 8 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.PathNodeIterator; |
110 | 9 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.Query; |
151 | 10 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index.Index; |
134 | 11 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index.IndexManager; |
153 | 12 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index.ParentIndex; |
97 | 13 |
100 | 14 public class InterfaceTraverser { |
124 | 15 |
139 | 16 TreeNode node; |
151 | 17 Index index; |
153 | 18 ParentIndex parentIndex; |
19 boolean parentUpdateFlag; | |
139 | 20 IndexManager indexManager; |
153 | 21 boolean indexFlag; |
139 | 22 |
153 | 23 public InterfaceTraverser(TreeNode _root, Index index, ParentIndex parentIndex, IndexManager indexManager, |
24 boolean indexFlag) { | |
139 | 25 this.node = _root; |
26 this.index = index; | |
27 this.indexManager = indexManager; | |
153 | 28 this.parentIndex = parentIndex; |
29 if (parentIndex.isEmpty()) | |
30 parentUpdateFlag = true; | |
31 else | |
32 parentUpdateFlag = false; | |
33 this.indexFlag = indexFlag; | |
139 | 34 } |
35 | |
151 | 36 public Index getIndex() { |
139 | 37 return index; |
38 } | |
127 | 39 |
153 | 40 public void commit() { |
41 parentUpdateFlag = false; | |
42 indexManager.commit(index, parentIndex); | |
43 } | |
44 | |
45 public ParentIndex getParentIndex() { | |
46 return parentIndex; | |
47 } | |
48 | |
151 | 49 public void setIndex(Index index) { |
139 | 50 this.index = index; |
51 } | |
127 | 52 |
153 | 53 public Iterator<TreeNode> emptyQuery() { |
54 | |
55 final PathNodeIterator itNode = new PathNodeIterator(node); | |
56 return new Iterator<TreeNode>() { | |
57 | |
58 private TreeNode matchNode = nextmatch(itNode); | |
59 | |
60 private TreeNode nextmatch(PathNodeIterator itNode) { | |
61 | |
62 for (; itNode.hasNext();) { | |
63 TreeNode targetNode = itNode.next(); | |
64 List<String> keys = targetNode.getAttributes().getKeys(); | |
65 for (String key : keys) { | |
66 String value = targetNode.getAttributes().getString(key); | |
67 if (value != null) | |
68 index = index.set(key, value, targetNode); | |
69 if (parentUpdateFlag) | |
70 parentIndex = parentIndex.set(targetNode); | |
71 } | |
72 } | |
73 commit(); | |
74 return null; | |
75 } | |
76 | |
77 @Override | |
78 public boolean hasNext() { | |
79 if (matchNode == null) { | |
80 return false; | |
81 } | |
82 return true; | |
83 } | |
84 | |
85 @Override | |
86 public TreeNode next() { | |
87 TreeNode currentPair = matchNode; | |
88 matchNode = nextmatch(itNode); | |
89 return currentPair; | |
90 } | |
91 | |
92 @Override | |
93 public void remove() { | |
94 } | |
95 | |
96 }; | |
97 | |
98 } | |
99 | |
139 | 100 /** |
101 * subTree以下のNodeに対してKey,Valueのペアでindexを使って探索を行う | |
102 * | |
103 * @param query | |
104 * @param subTree | |
105 * @param key | |
106 * @param searchValue | |
107 * @return | |
108 */ | |
153 | 109 public Iterator<TreeNode> findInSubTree(Query query, TreeNode subTree, String key, String searchValue) { |
139 | 110 /* |
111 * indexを使って取ってきたNodeのPathと、subTreeのPathを先頭から1つずつ比較し、 | |
112 * indexから取ってきたNodeのPathが一致した場合、そのNodeを返す | |
113 */ | |
153 | 114 List<TreeNode> nodeList = index.get(key, searchValue); |
115 if (nodeList != null) { | |
139 | 116 |
153 | 117 if (nodeList.isEmpty()) |
118 return new NulIterator<TreeNode>();// 空のIteratorを返す | |
137 | 119 |
153 | 120 // ここでNode以下にあるか調べる |
121 List<TreeNode> filteredList = List.nil(); | |
139 | 122 |
153 | 123 for (TreeNode targetNode : nodeList) { |
124 TreeNode parent = targetNode; | |
125 while (parent != null) { | |
126 parent = parentIndex.get(parent); | |
127 if (parent.equals(subTree)) | |
128 filteredList = filteredList.cons(targetNode); | |
129 } | |
141 | 130 } |
131 | |
132 return filteredList.iterator(); | |
139 | 133 |
134 } else { | |
141 | 135 final PathNodeIterator itNode = new PathNodeIterator(subTree); |
153 | 136 return new Iterator<TreeNode>() { |
139 | 137 |
153 | 138 private TreeNode matchNode = nextmatch(itNode); |
139 | 139 |
153 | 140 private TreeNode nextmatch(PathNodeIterator itNode) { |
127 | 141 |
139 | 142 for (; itNode.hasNext();) { |
153 | 143 TreeNode targetNode = itNode.next(); |
144 if (query.condition(targetNode)) | |
145 return targetNode; | |
139 | 146 } |
147 return null; | |
148 } | |
125 | 149 |
139 | 150 @Override |
151 public boolean hasNext() { | |
153 | 152 if (matchNode == null) { |
139 | 153 return false; |
154 } | |
155 return true; | |
156 } | |
157 | |
158 @Override | |
153 | 159 public TreeNode next() { |
160 TreeNode currentNode = matchNode; | |
161 matchNode = nextmatch(itNode); | |
162 return currentNode; | |
139 | 163 } |
164 | |
165 @Override | |
166 public void remove() { | |
167 } | |
168 | |
169 }; | |
137 | 170 } |
139 | 171 } |
137 | 172 |
139 | 173 /** |
174 * subTree以下のNodeに対してKeyに対応する値をindexを使って探索する | |
175 * | |
176 * @param query | |
177 * @param subTree | |
178 * @param key | |
179 * @param searchValue | |
180 * @return | |
181 */ | |
153 | 182 public Iterator<TreeNode> findInSubTreeAllValue(Query query, TreeNode subTree, String key) { |
139 | 183 /* |
184 * indexからinnerIndexを取得 取得したinnerIndexが保有するKeyを取得 | |
185 * そのKeyを保有するNodeとNodeのPathを取得する | |
186 * indexを使って取ってきたNodeのPathと、subTreeのPathを先頭から1つずつ比較し、 | |
187 * indexから取ってきたNodeのPathが一致した場合、そのNodeを返す | |
137 | 188 */ |
153 | 189 Iterator<TreeNode> NodeIterator = index.getAll(key); |
190 if (NodeIterator != null) { | |
191 List<TreeNode> filteredList = List.nil(); | |
192 for (; NodeIterator.hasNext();) { | |
193 TreeNode targetNode = NodeIterator.next(); | |
194 TreeNode parent = targetNode; | |
195 while (parent != null) { | |
196 parent = parentIndex.get(parent); | |
197 if (parent.equals(subTree)) | |
198 filteredList = filteredList.cons(targetNode); | |
143
afbe19c98f53
change Index form TreeMap<String,TreeMap<String<List<Pair<TreeNode,NodePath>>>> → TreeMap<String,TreeMap<String<List<NodePath>>>
one
parents:
141
diff
changeset
|
199 } |
139 | 200 } |
201 return filteredList.iterator(); | |
137 | 202 |
139 | 203 } else { |
153 | 204 |
141 | 205 final PathNodeIterator itNode = new PathNodeIterator(subTree); |
153 | 206 return new Iterator<TreeNode>() { |
139 | 207 |
153 | 208 private TreeNode matchPair = nextmatch(itNode); |
139 | 209 |
153 | 210 private TreeNode nextmatch(PathNodeIterator itNode) { |
137 | 211 |
139 | 212 for (; itNode.hasNext();) { |
153 | 213 TreeNode targetNode = itNode.next(); |
214 if (query.condition(targetNode)) | |
215 return targetNode; | |
139 | 216 } |
217 return null; | |
218 } | |
219 | |
220 @Override | |
221 public boolean hasNext() { | |
222 if (matchPair == null) { | |
223 return false; | |
224 } | |
225 return true; | |
226 } | |
124 | 227 |
139 | 228 @Override |
153 | 229 public TreeNode next() { |
230 TreeNode currentNode = matchPair; | |
139 | 231 matchPair = nextmatch(itNode); |
153 | 232 return currentNode; |
233 } | |
234 | |
235 @Override | |
236 public void remove() { | |
237 } | |
238 | |
239 }; | |
240 } | |
241 } | |
242 | |
243 public Iterator<TreeNode> find(Query query, String key, String searchValue) { | |
244 | |
245 List<TreeNode> nodeList = index.get(key, searchValue); | |
246 if (nodeList != null) { | |
247 return nodeList.iterator(); | |
248 } else { | |
249 | |
250 final PathNodeIterator itNode = new PathNodeIterator(node); | |
251 return new Iterator<TreeNode>() { | |
252 | |
253 private TreeNode matchNode = nextmatch(itNode); | |
254 | |
255 private TreeNode nextmatch(PathNodeIterator itNode) { | |
256 | |
257 for (; itNode.hasNext();) { | |
258 TreeNode targetNode = itNode.next(); | |
259 String value = targetNode.getAttributes().getString(key); | |
260 if (indexFlag) { | |
261 if (value != null) | |
262 index = index.set(key, value, targetNode); | |
263 } | |
264 if (parentUpdateFlag) | |
265 parentIndex = parentIndex.set(targetNode); | |
266 if (query.condition(targetNode)) | |
267 return targetNode; | |
268 } | |
269 commit(); | |
270 return null; | |
271 } | |
272 | |
273 @Override | |
274 public boolean hasNext() { | |
275 if (matchNode == null) { | |
276 return false; | |
277 } | |
278 return true; | |
279 } | |
280 | |
281 @Override | |
282 public TreeNode next() { | |
283 TreeNode currentPair = matchNode; | |
284 matchNode = nextmatch(itNode); | |
139 | 285 return currentPair; |
286 } | |
287 | |
288 @Override | |
289 public void remove() { | |
290 } | |
291 | |
292 }; | |
293 } | |
294 } | |
295 | |
153 | 296 public Iterator<TreeNode> findAll(Query query, String key) { |
139 | 297 |
153 | 298 Iterator<TreeNode> nodeList = index.getAll(key); |
299 if (nodeList != null) { | |
139 | 300 |
153 | 301 return nodeList; |
139 | 302 |
303 } else { | |
153 | 304 |
305 final PathNodeIterator itNode = new PathNodeIterator(node); | |
306 return new Iterator<TreeNode>() { | |
137 | 307 |
153 | 308 private TreeNode matchNode = nextmatch(itNode); |
139 | 309 |
153 | 310 private TreeNode nextmatch(PathNodeIterator itNode) { |
139 | 311 |
312 for (; itNode.hasNext();) { | |
153 | 313 TreeNode targetNode = itNode.next(); |
314 String value = targetNode.getAttributes().getString(key); | |
315 if (indexFlag) { | |
316 if (value != null) | |
317 index = index.set(key, value, targetNode); | |
137 | 318 } |
153 | 319 if (parentUpdateFlag) |
320 parentIndex = parentIndex.set(targetNode); | |
321 if (query.condition(targetNode)) | |
322 return targetNode; | |
139 | 323 } |
153 | 324 commit(); |
139 | 325 return null; |
326 } | |
136 | 327 |
139 | 328 @Override |
329 public boolean hasNext() { | |
153 | 330 if (matchNode == null) { |
139 | 331 return false; |
332 } | |
333 return true; | |
334 } | |
136 | 335 |
139 | 336 @Override |
153 | 337 public TreeNode next() { |
338 TreeNode currentPair = matchNode; | |
339 matchNode = nextmatch(itNode); | |
139 | 340 return currentPair; |
341 } | |
342 | |
343 @Override | |
344 public void remove() { | |
345 } | |
346 | |
347 }; | |
137 | 348 } |
139 | 349 } |
97 | 350 } |