97
|
1 package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser;
|
|
2
|
98
|
3 import java.util.Iterator;
|
132
|
4 import java.util.concurrent.atomic.AtomicReference;
|
98
|
5
|
112
|
6 import fj.Ord;
|
104
|
7 import fj.data.List;
|
124
|
8 import fj.data.Option;
|
97
|
9 import fj.data.TreeMap;
|
98
|
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
|
101
|
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
|
132
|
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeList;
|
|
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.ChangeSet;
|
97
|
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
|
|
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
|
132
|
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.AtomicReservableReference;
|
|
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.DefaultChangeSet;
|
|
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.DefaultTreeContext;
|
127
|
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.IndexJungleTreeEditor;
|
132
|
20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.TreeContext;
|
97
|
21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair;
|
99
|
22 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.PathNodeIterator;
|
110
|
23 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.Query;
|
134
|
24 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index.IndexManager;
|
97
|
25
|
100
|
26 public class InterfaceTraverser {
|
124
|
27
|
137
|
28 //InterfaceTraverser traverser;
|
|
29 TreeNode node;
|
|
30 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index;
|
|
31 IndexManager indexManager;
|
127
|
32
|
137
|
33 public InterfaceTraverser(TreeNode _root, IndexManager indexManager) {
|
|
34 this.node = _root;
|
|
35 this.index = TreeMap.empty(Ord.stringOrd);
|
|
36 this.indexManager = indexManager;
|
|
37 }
|
127
|
38
|
137
|
39 public InterfaceTraverser(
|
|
40 TreeNode _root,
|
|
41 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index,
|
|
42 IndexManager indexManager) {
|
|
43 this.node = _root;
|
|
44 this.index = index;
|
|
45 this.indexManager = indexManager;
|
|
46 }
|
|
47
|
|
48 public void commitIndex(){
|
|
49 indexManager.commit(index);
|
|
50 }
|
127
|
51
|
137
|
52 public TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> getIndex() {
|
|
53 return index;
|
|
54 }
|
125
|
55
|
137
|
56 public void setIndex(
|
|
57 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) {
|
|
58 this.index = index;
|
|
59 }
|
125
|
60
|
137
|
61
|
|
62
|
|
63 /* public InterfaceTraverser getTraverser(JungleTree tree) {
|
124
|
64 return new InterfaceTraverser(tree.getRootNode(), tree.getIndex(),
|
127
|
65 tree.getIndexTreeEditor());
|
132
|
66 }*/
|
98
|
67
|
137
|
68 public void set(TreeNode root) {
|
|
69 this.node = root;
|
|
70 }
|
|
71
|
|
72
|
|
73 /**
|
|
74 * subTree以下のNodeに対してKey,Valueのペアでindexを使って探索を行う
|
|
75 * @param query
|
|
76 * @param subTree
|
|
77 * @param key
|
|
78 * @param searchValue
|
|
79 * @return
|
|
80 */
|
|
81 public Iterator<Pair<TreeNode, NodePath>> findInSubTree(Query query, TreeNode subTree, String key, String searchValue){
|
|
82 /*
|
|
83 * indexを使って取ってきたNodeのPathと、subTreeのPathを先頭から1つずつ比較し、
|
|
84 * indexから取ってきたNodeのPathが一致した場合、そのNodeを返す
|
|
85 */
|
|
86 final PathNodeIterator itNode = new PathNodeIterator(subTree);
|
|
87 return new Iterator<Pair<TreeNode, NodePath>>() {
|
|
88
|
|
89 private Pair<TreeNode, NodePath> matchPair = nextmatch(itNode);
|
|
90
|
|
91 private Pair<TreeNode, NodePath> nextmatch(
|
|
92 PathNodeIterator itNode) {
|
124
|
93
|
137
|
94 for (; itNode.hasNext();) {
|
|
95 Pair<TreeNode, NodePath> pathNode = itNode.next();
|
|
96 if (query.condition(pathNode.left()))
|
|
97 return pathNode;
|
|
98 }
|
|
99 return null;
|
|
100 }
|
|
101
|
|
102 @Override
|
|
103 public boolean hasNext() {
|
|
104 if (matchPair == null) {
|
|
105 // index = itNode.getIndex();
|
|
106 return false;
|
|
107 }
|
|
108 return true;
|
|
109 }
|
|
110
|
|
111 @Override
|
|
112 public Pair<TreeNode, NodePath> next() {
|
|
113 Pair<TreeNode, NodePath> currentPair = matchPair;
|
|
114 matchPair = nextmatch(itNode);
|
|
115 return currentPair;
|
|
116 }
|
|
117
|
|
118 @Override
|
|
119 public void remove() {
|
|
120 // TODO Auto-generated method stub
|
|
121
|
|
122 }
|
|
123
|
|
124 };
|
|
125
|
|
126 }
|
124
|
127
|
101
|
128
|
137
|
129 public Iterator<Pair<TreeNode, NodePath>> find(Query query, String key,
|
|
130 String searchValue) {
|
|
131
|
|
132 if (index.get(key).isSome()) {
|
136
|
133
|
137
|
134 TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = this.index
|
|
135 .get(key).some();
|
|
136 Option<List<Pair<TreeNode, NodePath>>> opList = innerIndex
|
|
137 .get(searchValue);
|
136
|
138
|
137
|
139 if (opList.isNone())
|
|
140 return null;// 空のIteratorを返す
|
|
141
|
|
142 final List<Pair<TreeNode, NodePath>> list = opList.some();
|
|
143 return list.iterator();
|
136
|
144
|
137
|
145 } else {
|
136
|
146
|
137
|
147 final PathNodeIterator itNode = new PathNodeIterator(node);
|
|
148 return new Iterator<Pair<TreeNode, NodePath>>() {
|
|
149
|
|
150 private Pair<TreeNode, NodePath> matchPair = nextmatch(itNode);
|
136
|
151
|
137
|
152 private Pair<TreeNode, NodePath> nextmatch(
|
|
153 PathNodeIterator itNode) {
|
136
|
154
|
137
|
155 for (; itNode.hasNext();) {
|
|
156 Pair<TreeNode, NodePath> pathNode = itNode.next();
|
|
157 String value = pathNode.left().getAttributes()
|
|
158 .getString(key);
|
|
159 Option<TreeMap<String, List<Pair<TreeNode, NodePath>>>> innerIndexOp = index
|
|
160 .get(key);
|
|
161
|
|
162 if (value != null) {
|
|
163 if (innerIndexOp.isNone()) {
|
136
|
164
|
137
|
165 TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = TreeMap
|
|
166 .empty(Ord.stringOrd);
|
|
167 List<Pair<TreeNode, NodePath>> list = List
|
|
168 .nil();
|
|
169 list = list.cons(pathNode);
|
|
170 innerIndex = innerIndex.set(value, list);
|
|
171 index = index.set(key, innerIndex);
|
124
|
172
|
137
|
173 } else {
|
99
|
174
|
137
|
175 TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = innerIndexOp
|
|
176 .some();
|
|
177 Option<List<Pair<TreeNode, NodePath>>> opList = innerIndex
|
|
178 .get(value);
|
124
|
179
|
137
|
180 if (opList.isNone()) {
|
124
|
181
|
137
|
182 List<Pair<TreeNode, NodePath>> list = List
|
|
183 .nil();
|
|
184 list = list.cons(pathNode);
|
|
185 innerIndex = innerIndex.set(value, list);
|
124
|
186
|
137
|
187 } else {
|
124
|
188
|
137
|
189 List<Pair<TreeNode, NodePath>> list = opList
|
|
190 .some();
|
|
191 list = list.cons(pathNode);
|
|
192 innerIndex = innerIndex.set(value, list);
|
124
|
193
|
137
|
194 }
|
|
195 index = index.set(key, innerIndex);
|
99
|
196
|
137
|
197 }
|
|
198 }
|
124
|
199
|
137
|
200 if (query.condition(pathNode.left()))
|
|
201 return pathNode;
|
|
202 }
|
|
203 return null;
|
|
204 }
|
124
|
205
|
137
|
206 @Override
|
|
207 public boolean hasNext() {
|
|
208 if (matchPair == null) {
|
|
209 // index = itNode.getIndex();
|
|
210 return false;
|
|
211 }
|
|
212 return true;
|
|
213 }
|
99
|
214
|
137
|
215 @Override
|
|
216 public Pair<TreeNode, NodePath> next() {
|
|
217 Pair<TreeNode, NodePath> currentPair = matchPair;
|
|
218 matchPair = nextmatch(itNode);
|
|
219 return currentPair;
|
|
220 }
|
99
|
221
|
137
|
222 @Override
|
|
223 public void remove() {
|
|
224 // TODO Auto-generated method stub
|
101
|
225
|
137
|
226 }
|
99
|
227
|
137
|
228 };
|
|
229 }
|
|
230 }
|
97
|
231 }
|