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