97
|
1 package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser;
|
|
2
|
98
|
3 import java.util.Iterator;
|
|
4
|
97
|
5 import fj.data.TreeMap;
|
98
|
6 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
|
101
|
7 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
|
97
|
8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
|
|
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
|
101
|
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
|
|
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
|
97
|
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair;
|
99
|
13 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.PathNodeIterator;
|
101
|
14 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.SearchQuery;
|
|
15 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.UpdateQuery;
|
97
|
16
|
100
|
17 public class InterfaceTraverser {
|
|
18 InterfaceTraverser traverser;
|
101
|
19
|
98
|
20 TreeNode node;
|
101
|
21 TreeMap<String, TreeNode> nodeIndex;
|
|
22 TreeMap<String, String> attributeIndex;
|
|
23 JungleTreeEditor editor;
|
|
24
|
|
25 public InterfaceTraverser(TreeNode _root,
|
|
26 Pair<TreeMap<String, TreeNode>, TreeMap<String, String>> index,
|
|
27 JungleTreeEditor editor) {
|
|
28 this.node = _root;
|
|
29 this.nodeIndex = index.left();
|
|
30 this.attributeIndex = index.right();
|
|
31 this.editor = editor;
|
98
|
32 }
|
101
|
33
|
100
|
34 public InterfaceTraverser getTraverser(JungleTree tree) {
|
101
|
35 return new InterfaceTraverser(tree.getRootNode(), tree.getIndex(),
|
|
36 tree.getTreeEditor());
|
97
|
37 }
|
98
|
38
|
101
|
39 public void set(TreeNode root){
|
|
40 this.node = root;
|
|
41 }
|
|
42 /*
|
|
43 * public IteratorPathNode traverse(TreeNode _node ,NodePath _path ,int
|
|
44 * _pos){
|
|
45 *
|
|
46 * Children children = _node.getChildren(); Either<Error,TreeNode> either =
|
|
47 * children.at(0); IteratorPathNode list = new IteratorPathNodeImpl(); int
|
|
48 * pathCount = _pos; if(children.size() == 0){ list = list.add(new
|
|
49 * Pair<TreeNode, NodePath>(node, _path.add(_pos))); return list; }
|
|
50 *
|
|
51 * for(TreeNode child : children){ list =
|
|
52 * list.append(traverse(child,_path,pathCount)); pathCount++; }
|
|
53 *
|
|
54 * list = list.add(new Pair<TreeNode,NodePath>(_node, _path.add(_pos)));
|
|
55 * return list; }
|
|
56 *
|
|
57 * public int count(Query _query, String _key, String _attribute){ return
|
|
58 * this.find(_query,_key,_attribute); }
|
|
59 *
|
|
60 * public List<Pair<NodePath,TreeNode>> distinct(String _key ,String...
|
|
61 * _attribute){ return null; }
|
|
62 */
|
102
|
63
|
|
64 public JungleTreeEditor update(UpdateQuery query) {
|
101
|
65 Iterator<Pair<TreeNode, NodePath>> findNode = find(query);
|
|
66 //do {
|
|
67 for (; findNode.hasNext();) {
|
|
68 Either<Error, JungleTreeEditor> either = editor.putAttribute(findNode.next().right(), "KEY", query.getUpdateAttribute());
|
|
69 if (either.isA())
|
|
70 ;// wait delay write
|
|
71 editor = either.b();
|
97
|
72 }
|
101
|
73 //} while (editor.success().isA());
|
|
74
|
|
75 return editor;
|
97
|
76 }
|
101
|
77
|
|
78 public Iterator<Pair<TreeNode, NodePath>> find(final SearchQuery query) {
|
99
|
79 final PathNodeIterator itNode = new PathNodeIterator(node);
|
101
|
80
|
99
|
81 return new Iterator<Pair<TreeNode, NodePath>>() {
|
|
82
|
101
|
83 private Pair<TreeNode, NodePath> matchPair = nextmatch(itNode);
|
|
84
|
|
85 private Pair<TreeNode, NodePath> nextmatch(PathNodeIterator itNode) {
|
|
86 for (; itNode.hasNext();) {
|
|
87 Pair<TreeNode, NodePath> pathNode = itNode.next();
|
|
88 if (query.condition(pathNode.left()))
|
|
89 return pathNode;
|
|
90 }
|
|
91 return null;
|
|
92 }
|
99
|
93
|
101
|
94 @Override
|
|
95 public boolean hasNext() {
|
|
96 return matchPair != null;
|
|
97 }
|
99
|
98
|
101
|
99 @Override
|
|
100 public Pair<TreeNode, NodePath> next() {
|
|
101 Pair<TreeNode, NodePath> currentPair = matchPair;
|
|
102 matchPair = nextmatch(itNode);
|
|
103 return currentPair;
|
|
104 }
|
99
|
105
|
101
|
106 @Override
|
|
107 public void remove() {
|
|
108 // TODO Auto-generated method stub
|
|
109
|
|
110 }
|
99
|
111
|
|
112 };
|
|
113
|
98
|
114 }
|
97
|
115
|
|
116 }
|