169
|
1 package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index;
|
|
2
|
184
|
3 import java.util.Optional;
|
169
|
4 import java.util.Stack;
|
|
5
|
|
6 import fj.data.List;
|
184
|
7 import jp.ac.u_ryukyu.ie.cr.tatsuki.TreeMap.TreeMap;
|
169
|
8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
|
|
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNodeChildren;
|
|
10
|
|
11 public class IndexCreater {
|
|
12
|
183
|
13 TreeNode root;
|
|
14 TreeNode node;
|
|
15 int childNumber;
|
|
16 private TreeNodeChildren children;
|
|
17 private Stack<TreeNode> nodeStack = new Stack<TreeNode>();
|
|
18 private Stack<Integer> searchStack = new Stack<Integer>();
|
|
19 ParentIndex parentIndex = new ParentIndex();
|
184
|
20 TreeMap<String, TreeMap<String, List<TreeNode>>> indexList = new TreeMap();
|
169
|
21
|
183
|
22 public IndexCreater(TreeNode rootNode) {
|
|
23 this.root = rootNode;
|
|
24 this.node = rootNode;
|
|
25 while (node != null) {
|
|
26 TreeNode targetNode = node;
|
|
27 List<String> keys = targetNode.getAttributes().getKeys();
|
|
28 for (String key : keys) {
|
|
29 String value = targetNode.getAttributes().getString(key);
|
|
30 if (value != null)
|
|
31 indexList = set(key, value, targetNode);
|
|
32 }
|
|
33 if (node.getChildren().size() > 0) {
|
|
34 nodeStack.push(node);
|
|
35 TreeNode parent = node;
|
|
36 children = node.getChildren();
|
|
37 node = children.at(0).b();
|
|
38 parentIndex.set(parent, node);
|
|
39 childNumber = 1;
|
|
40 searchStack.push(childNumber);
|
|
41 } else if (node == root) {
|
|
42 node = null; // no more node
|
|
43 children = null;
|
|
44 return;
|
|
45 } else if (children != null && children.size() > childNumber) {
|
|
46 childNumber = searchStack.pop();
|
|
47 TreeNode parent = nodeStack.pop();
|
|
48 nodeStack.push(parent);
|
|
49 node = children.at(childNumber).b();
|
|
50 parentIndex.set(parent, node);
|
|
51 searchStack.push(++childNumber);
|
|
52 } else {
|
|
53 node = nodeStack.pop();
|
|
54 children = node.getChildren();
|
|
55 childNumber = searchStack.pop();
|
|
56 for (; children.size() == childNumber; ) {
|
|
57 if (node == root) {
|
|
58 node = null; // no more node
|
|
59 children = null;
|
|
60 return;
|
|
61 }
|
|
62 node = nodeStack.pop();
|
|
63 children = node.getChildren();
|
|
64 childNumber = searchStack.pop();
|
|
65 }
|
|
66 if (node != null && childNumber < children.size()) {
|
|
67 nodeStack.push(node);
|
|
68 TreeNode parent = node;
|
|
69 node = children.at(childNumber).b();
|
|
70 parentIndex.set(parent, node);
|
|
71 searchStack.push(++childNumber);
|
|
72 }
|
|
73 }
|
169
|
74 }
|
174
|
75 }
|
|
76
|
183
|
77 public TreeMap<String, TreeMap<String, List<TreeNode>>> set(String key, String value, TreeNode node) {
|
184
|
78 if (key == null )
|
|
79 System.out.println("");
|
186
|
80 Optional<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key);
|
184
|
81 if (!indexOp.isPresent()) {
|
|
82 TreeMap<String, List<TreeNode>> index = new TreeMap();
|
183
|
83 List<TreeNode> nodeList = List.nil();
|
|
84 nodeList = nodeList.cons(node);
|
184
|
85 TreeMap<String, List<TreeNode>> newIndex = index.put(value, nodeList);
|
|
86 indexList = indexList.put(key, newIndex);
|
183
|
87 return indexList;
|
|
88 }
|
174
|
89
|
184
|
90 TreeMap<String, List<TreeNode>> index = indexOp.get();
|
186
|
91 Optional<List<TreeNode>> nodeListOp = index.get(value);
|
183
|
92
|
|
93 List<TreeNode> newNodeList;
|
184
|
94
|
|
95 if (nodeListOp.isPresent()) {
|
|
96 newNodeList = nodeListOp.get().cons(node);
|
|
97
|
183
|
98 } else {
|
184
|
99 List<TreeNode> nodeList = List.nil();
|
183
|
100 newNodeList = nodeList.cons(node);
|
184
|
101
|
183
|
102 }
|
184
|
103 TreeMap<String, List<TreeNode>> newIndex = index.put(value, newNodeList);
|
|
104 indexList = indexList.put(key, newIndex);
|
183
|
105
|
|
106 return indexList;
|
174
|
107 }
|
|
108
|
183
|
109 public TreeMap<String, TreeMap<String, List<TreeNode>>> getIndex() {
|
|
110 return indexList;
|
|
111 }
|
174
|
112
|
183
|
113 public ParentIndex getParentIndex() {
|
|
114 return parentIndex;
|
|
115 }
|
169
|
116 }
|