118
|
1 package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index;
|
|
2
|
|
3
|
128
|
4
|
|
5 import fj.Ord;
|
|
6 import fj.data.List;
|
|
7 import fj.data.Option;
|
|
8 import fj.data.TreeMap;
|
|
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
|
|
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TreeEditor;
|
145
|
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.IndexTreeEditor;
|
128
|
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
|
|
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog;
|
118
|
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.IndexJungleTreeEditor;
|
128
|
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.TransactionManager;
|
|
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither;
|
118
|
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
|
|
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
|
128
|
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair;
|
|
20
|
|
21 import java.nio.ByteBuffer;
|
118
|
22
|
|
23 public class PutIndexEditor implements IndexEditor {
|
|
24
|
128
|
25 String key;
|
|
26 String attribute;
|
|
27 NodePath path;
|
|
28 TransactionManager txManager;
|
|
29 TreeNode root;
|
|
30 TreeEditor editor;
|
|
31 TreeOperationLog log;
|
|
32
|
|
33 public PutIndexEditor(String key, ByteBuffer attribute,NodePath path) {
|
137
|
34 this.key = key;
|
|
35 this.attribute = new String(attribute.array());
|
|
36 this.path = path;
|
128
|
37 }
|
|
38
|
118
|
39 @Override
|
145
|
40 public Either<Error, IndexJungleTreeEditor>edit(TreeNode root,TransactionManager txManager, IndexTreeEditor editor,TreeOperationLog log,TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index, TreeMap<TreeNode,TreeNode> parentIndex) {
|
130
|
41 NodePath newPath = path.pop().right();
|
|
42 TreeNode target = getTarget(root, newPath);
|
128
|
43 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = editIndex(target, path, key, attribute,index);
|
145
|
44 IndexJungleTreeEditor newEditor = new IndexJungleTreeEditor(root,txManager,editor,log, newIndex, parentIndex);
|
130
|
45 Either<Error, IndexJungleTreeEditor> either = DefaultEither.newB(newEditor);
|
128
|
46 return either;
|
118
|
47 }
|
128
|
48
|
|
49 public TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> editIndex(TreeNode target, NodePath path, String key, String attribute,TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index){
|
|
50
|
|
51 Option<TreeMap<String, List<Pair<TreeNode, NodePath>>>> innerIndexOp = index.get(key);
|
|
52 Pair<TreeNode, NodePath> pathNode = new Pair<TreeNode, NodePath>(target,path);
|
|
53
|
|
54 if (attribute != null) {
|
|
55 if (innerIndexOp.isNone()) {
|
|
56
|
137
|
57 TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = TreeMap.empty(Ord.stringOrd);
|
|
58 List<Pair<TreeNode, NodePath>> list = List.nil();
|
|
59 list = list.cons(pathNode);
|
|
60 innerIndex = innerIndex.set(attribute, list);
|
|
61 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = index.set(key, innerIndex);
|
139
|
62 return newIndex;
|
128
|
63 } else {
|
|
64
|
|
65 TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = innerIndexOp.some();
|
140
|
66
|
128
|
67 Option<List<Pair<TreeNode, NodePath>>> opList = innerIndex.get(attribute);
|
|
68
|
|
69 if (opList.isNone()) {
|
|
70
|
|
71 List<Pair<TreeNode, NodePath>> list = List.nil();
|
|
72 list = list.cons(pathNode);
|
|
73 innerIndex = innerIndex.set(attribute, list);
|
|
74
|
|
75 } else {
|
|
76
|
|
77 List<Pair<TreeNode, NodePath>> list = opList.some();
|
|
78 list = list.cons(pathNode);
|
|
79 innerIndex = innerIndex.set(attribute, list);
|
|
80
|
|
81 }
|
|
82 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = index.set(key, innerIndex);
|
|
83 return newIndex;
|
|
84 }
|
|
85 }
|
|
86 return index;
|
|
87 }
|
|
88
|
|
89 public TreeNode getTarget(TreeNode node , NodePath path){
|
|
90 Pair<Integer, NodePath> pathNode = path.pop();
|
|
91 Either<Error, TreeNode> either = node.getChildren().at(pathNode.left());
|
|
92 if (either.isA())
|
|
93 return node;
|
130
|
94
|
128
|
95 TreeNode child = either.b();
|
130
|
96 if (pathNode.right().size() == 0)
|
|
97 return child;
|
|
98
|
128
|
99 TreeNode target = getTarget(child,pathNode.right());
|
|
100 return target;
|
|
101 }
|
|
102
|
118
|
103 }
|