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 */
|
|
63 public JungleTreeEditor update(final UpdateQuery query) {
|
|
64 Iterator<Pair<TreeNode, NodePath>> findNode = find(query);
|
|
65 //do {
|
|
66 for (; findNode.hasNext();) {
|
|
67 Either<Error, JungleTreeEditor> either = editor.putAttribute(findNode.next().right(), "KEY", query.getUpdateAttribute());
|
|
68 if (either.isA())
|
|
69 ;// wait delay write
|
|
70 editor = either.b();
|
97
|
71 }
|
101
|
72 //} while (editor.success().isA());
|
|
73
|
|
74 return editor;
|
97
|
75 }
|
101
|
76
|
|
77 public Iterator<Pair<TreeNode, NodePath>> find(final SearchQuery query) {
|
99
|
78 final PathNodeIterator itNode = new PathNodeIterator(node);
|
101
|
79
|
99
|
80 return new Iterator<Pair<TreeNode, NodePath>>() {
|
|
81
|
101
|
82 private Pair<TreeNode, NodePath> matchPair = nextmatch(itNode);
|
|
83
|
|
84 private Pair<TreeNode, NodePath> nextmatch(PathNodeIterator itNode) {
|
|
85 for (; itNode.hasNext();) {
|
|
86 Pair<TreeNode, NodePath> pathNode = itNode.next();
|
|
87 if (query.condition(pathNode.left()))
|
|
88 return pathNode;
|
|
89 }
|
|
90 return null;
|
|
91 }
|
99
|
92
|
101
|
93 @Override
|
|
94 public boolean hasNext() {
|
|
95 return matchPair != null;
|
|
96 }
|
99
|
97
|
101
|
98 @Override
|
|
99 public Pair<TreeNode, NodePath> next() {
|
|
100 Pair<TreeNode, NodePath> currentPair = matchPair;
|
|
101 matchPair = nextmatch(itNode);
|
|
102 return currentPair;
|
|
103 }
|
99
|
104
|
101
|
105 @Override
|
|
106 public void remove() {
|
|
107 // TODO Auto-generated method stub
|
|
108
|
|
109 }
|
99
|
110
|
|
111 };
|
|
112
|
98
|
113 }
|
97
|
114
|
|
115 }
|