comparison src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/AddNewChildrenIndexEditor.java @ 135:6e9a8d26e0cf

refactor
author one
date Fri, 24 Oct 2014 18:49:48 +0900
parents
children ec166c8ff079
comparison
equal deleted inserted replaced
134:f46a6e0e4594 135:6e9a8d26e0cf
1 package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index;
2
3 import java.util.Iterator;
4
5 import fj.F;
6 import fj.Ord;
7 import fj.P2;
8 import fj.data.List;
9 import fj.data.Option;
10 import fj.data.TreeMap;
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TreeEditor;
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog;
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.IndexJungleTreeEditor;
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.TransactionManager;
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither;
20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
22 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair;
23
24 public class AddNewChildrenIndexEditor implements IndexEditor {
25
26 NodePath editNodePath;
27
28 public AddNewChildrenIndexEditor(int pos, NodePath path) {
29 this.editNodePath = path.add(pos);
30 }
31
32 @Override
33 public Either<Error, IndexJungleTreeEditor> edit(
34 TreeNode root,
35 TransactionManager txManager,
36 TreeEditor editor,
37 TreeOperationLog log,
38 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) {
39
40 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = editIndex(index);
41 IndexJungleTreeEditor newEditor = new IndexJungleTreeEditor(root, txManager, editor, newIndex);
42 return DefaultEither.newB(newEditor);
43 }
44
45 public TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> editIndex(
46 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) {
47
48 if (!index.isEmpty()) {
49 List<String> keyList = index.keys();
50 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = TreeMap.empty(Ord.stringOrd);
51 TreeMap<String, List<Pair<TreeNode, NodePath>>> newInnerIndex = TreeMap.empty(Ord.stringOrd);
52
53 for (String indexKey : keyList) {
54 TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = index.get(indexKey).some();
55 List<String> innerIndexKeyList = innerIndex.keys();
56
57 for (String innerIndexKey : innerIndexKeyList) {
58 List<Pair<TreeNode, NodePath>> pairList = innerIndex.get(innerIndexKey).some();
59 List<Pair<TreeNode, NodePath>> list = checkPath(pairList);
60 if (!list.isEmpty()){
61 System.out.println(new String(list.head().left().getAttributes().get("KEY").array()));
62 newInnerIndex = newInnerIndex.set(innerIndexKey, list);
63 }
64 }
65 newIndex = newIndex.set(indexKey, newInnerIndex);
66 }
67 return newIndex;
68 } else {
69 return index;
70 }
71 }
72
73 public List<Pair<TreeNode, NodePath>> checkPath(List<Pair<TreeNode, NodePath>> pairList){
74
75 List<Pair<TreeNode, NodePath>> list = List.nil();
76 for (Pair<TreeNode, NodePath> pair : pairList) {
77
78 NodePath path = pair.right();
79 System.out.println("oldPath = " + path.toString());
80 NodePath newPath = new DefaultNodePath();
81
82
83 if (editNodePath.size() > path.size()) {
84 list = list.cons(pair);
85 continue;
86 }
87
88 Pair<Integer, NodePath> editNodePathCopy = editNodePath.pop();
89 int loopCount = 0;
90
91 for (Integer pathInt : path) {
92 loopCount++;
93
94 if (pathInt == -1)
95 continue;
96
97 if (editNodePathCopy.right().size() > 0) {
98 editNodePathCopy = editNodePathCopy.right().pop();
99
100 if (loopCount == editNodePath.size() && editNodePathCopy.left() <= pathInt) {
101 newPath = newPath.add(pathInt + 1);
102 continue;
103 }
104
105 if (!(editNodePathCopy.left() == pathInt)) {
106 newPath = path;
107 break;
108 }
109 }
110
111 newPath = newPath.add(pathInt);
112
113 }
114
115 System.out.println("newPath = " + newPath.toString());
116 Pair<TreeNode, NodePath> newPair = new Pair<TreeNode, NodePath>(pair.left(), newPath);
117 list = list.cons(newPair);
118 }
119
120 return list;
121 }
122
123 }