151
|
1 package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index;
|
|
2
|
|
3 import java.util.Iterator;
|
|
4
|
153
|
5 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NulIterator;
|
151
|
6 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
|
|
7 import fj.Ord;
|
|
8 import fj.P2;
|
|
9 import fj.data.List;
|
|
10 import fj.data.Option;
|
|
11 import fj.data.TreeMap;
|
|
12
|
|
13 public class Index {
|
|
14
|
|
15 TreeMap<String, TreeMap<String, List<TreeNode>>> indexList;
|
|
16
|
|
17 public Index() {
|
|
18 this.indexList = TreeMap.empty(Ord.stringOrd);
|
|
19 }
|
|
20
|
|
21 public Index(TreeMap<String, TreeMap<String, List<TreeNode>>> indexList) {
|
|
22 this.indexList = indexList;
|
|
23 }
|
|
24
|
|
25 public Index(Index indexList) {
|
|
26 this.indexList = indexList.getIndex();
|
|
27 }
|
|
28
|
|
29 public Index set(String key, String value, TreeNode node) {
|
|
30 Option<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key);
|
153
|
31 if (indexOp.isNone()) {
|
|
32 TreeMap<String, List<TreeNode>> index = TreeMap.empty(Ord.stringOrd);
|
|
33 List<TreeNode> nodeList = List.nil();
|
|
34 List<TreeNode> newNodeList = nodeList.cons(node);
|
|
35 TreeMap<String, List<TreeNode>> newIndex = index.set(value, newNodeList);
|
|
36 TreeMap<String, TreeMap<String, List<TreeNode>>> newIndexList = indexList.set(key, newIndex);
|
|
37
|
|
38
|
|
39 return new Index(newIndexList);
|
|
40 }
|
151
|
41
|
|
42 TreeMap<String, List<TreeNode>> index = indexOp.some();
|
|
43 Option<List<TreeNode>> nodeListOp = index.get(value);
|
|
44
|
|
45 List<TreeNode> newNodeList;
|
|
46 if (nodeListOp.isSome()) {
|
|
47 List<TreeNode> nodeList = nodeListOp.some();
|
|
48 newNodeList = nodeList.cons(node);
|
|
49 } else {
|
|
50 List<TreeNode> nodeList = List.nil();
|
|
51 newNodeList = nodeList.cons(node);
|
|
52 }
|
|
53 TreeMap<String, List<TreeNode>> newIndex = index.set(value, newNodeList);
|
|
54 TreeMap<String, TreeMap<String, List<TreeNode>>> newIndexList = indexList.set(key, newIndex);
|
|
55
|
|
56 return new Index(newIndexList);
|
|
57 }
|
153
|
58
|
|
59
|
|
60 // public Index delete(String key, String value, TreeNode node) {
|
|
61 // Option<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key);
|
|
62 // if (indexOp.isNone())
|
|
63 // return this;
|
|
64 //
|
|
65 // TreeMap<String, List<TreeNode>> index = indexOp.some();
|
|
66 // TreeMap<String, List<TreeNode>> newIndex = index;
|
|
67 // Option<List<TreeNode>> nodeListOp = index.get(value);
|
|
68 // if (nodeListOp.isSome()) {
|
|
69 // List<TreeNode> nodeList = nodeListOp.some();
|
|
70 // List<TreeNode> newNodeList = List.nil();
|
|
71 // for (TreeNode indexingNode : nodeList) {
|
|
72 // if (indexingNode.equals(node))
|
|
73 // newNodeList = newNodeList.cons(indexingNode);
|
|
74 // }
|
|
75 //
|
|
76 // newIndex = index.set(value, newNodeList);
|
|
77 // } else {
|
|
78 // return this;
|
|
79 // }
|
|
80 // TreeMap<String, TreeMap<String, List<TreeNode>>> newIndexList = indexList.set(key, newIndex);
|
|
81 // return new Index(newIndexList);
|
|
82 // }
|
151
|
83
|
|
84
|
|
85
|
|
86 public List<TreeNode> get(String key, String value) {
|
153
|
87
|
151
|
88 Option<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key);
|
|
89 if (indexOp.isNone())
|
153
|
90 return null;
|
151
|
91
|
|
92 TreeMap<String, List<TreeNode>> index = indexOp.some();
|
|
93 Option<List<TreeNode>> nodeListOp = index.get(value);
|
|
94
|
|
95 if (nodeListOp.isNone())
|
|
96 return List.nil();
|
|
97
|
|
98 return nodeListOp.some();
|
|
99 }
|
|
100
|
153
|
101 public Iterator<TreeNode> getAll(String key){
|
|
102
|
|
103 Option<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key);
|
|
104 if (indexOp.isNone())
|
|
105 return null;
|
|
106
|
|
107 TreeMap<String, List<TreeNode>> index = indexOp.some();
|
|
108 if (!index.isEmpty())
|
|
109 return new NulIterator<TreeNode>();
|
|
110
|
|
111
|
|
112 return new Iterator<TreeNode>(){
|
|
113
|
|
114 Iterator<P2<String, List<TreeNode>>> treeMapIterator = index.iterator();
|
|
115 List<TreeNode> nodeList = List.nil();
|
|
116 TreeNode node;
|
|
117
|
|
118 @Override
|
|
119 public boolean hasNext() {
|
|
120
|
|
121 if (nodeList.isNotEmpty()) {
|
|
122 node = nodeList.head();
|
|
123 nodeList = nodeList.tail();
|
|
124 return true;
|
|
125 }
|
|
126
|
|
127 for (;treeMapIterator.hasNext();) {
|
|
128 nodeList = treeMapIterator.next()._2();
|
|
129 node = nodeList.head();
|
|
130 nodeList = nodeList.tail();
|
|
131 return true;
|
|
132 }
|
|
133 return false;
|
|
134 }
|
|
135
|
|
136 @Override
|
|
137 public TreeNode next() {
|
|
138 return node;
|
|
139 }
|
|
140
|
|
141 };
|
|
142
|
|
143 }
|
151
|
144
|
|
145 private TreeMap<String, TreeMap<String, List<TreeNode>>> getIndex() {
|
|
146 return indexList;
|
|
147 }
|
|
148
|
|
149 }
|