comparison src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/index/IndexFormTest.java @ 134:f46a6e0e4594

add deleteIndexEditor
author one
date Tue, 21 Oct 2014 19:47:25 +0900
parents
children e26462a38ce0
comparison
equal deleted inserted replaced
133:c4846688f635 134:f46a6e0e4594
1 package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.index;
2
3 import java.nio.ByteBuffer;
4
5 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
6 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
7 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNodeAttributes;
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.DefaultTreeNode;
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair;
13
14 import org.junit.Assert;
15 import org.junit.Test;
16
17 import fj.Ord;
18 import fj.data.List;
19 import fj.data.Option;
20 import fj.data.TreeMap;
21
22 public class IndexFormTest {
23
24 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index = TreeMap
25 .empty(Ord.stringOrd);
26
27 @Test
28 public void IndexFormTest() {
29
30 NodePath path = new DefaultNodePath();
31 TreeNode node = createTree(3, 0, 3, path);
32 TreeMap<String, List<Pair<TreeNode, NodePath>>> map = index.get(key).some();
33 Option<List<Pair<TreeNode, NodePath>>> opList = map.get("<-1,0,0,2>");
34 List<Pair<TreeNode, NodePath>> list = opList.some();
35 Pair<TreeNode, NodePath> Pair = list.head();
36 NodePath newPath = Pair.right();
37 String pathStr = newPath.toString();
38 Assert.assertEquals(pathStr,"<-1,0,0,2>");
39 System.out.println("aaa");
40 }
41
42 public static String key = "KEY";
43 public static ByteBuffer value = ByteBuffer.wrap(key.getBytes());
44 public static DefaultTreeNode factory = new DefaultTreeNode();
45
46 public TreeNode createTree(int _curX, int _curY, int _maxHeight,
47 NodePath _address) {
48 TreeNode parent = factory.createNewNode();
49 Either<Error, TreeNode> either = parent.getAttributes().put(key,ByteBuffer.wrap(_address.toString().getBytes()));
50 if (either.isA()) {
51 Assert.fail();
52 }
53 editIndex(parent, _address, _address.toString(), key);
54 parent = either.b();
55
56 if (_curY == _maxHeight) {
57 return parent;
58 }
59
60 for (int i = 0; i < _curY + 1; i++) {
61 TreeNode ch = createTree(i, _curY + 1, _maxHeight, _address.add(i));
62 either = parent.getChildren().addNewChildAt(i, ch);
63 if (either.isA()) {
64 Assert.fail();
65 }
66
67 parent = either.b();
68 }
69
70 return parent;
71 }
72
73
74 public void editIndex(TreeNode node, NodePath path, String attribute,String key) {
75
76 Pair<TreeNode, NodePath> pair = new Pair<TreeNode, NodePath>(node, path);
77 Option<TreeMap<String, List<Pair<TreeNode, NodePath>>>> opAttributeList = index.get(key);
78
79 if (opAttributeList.isNone()) {
80 TreeMap<String, List<Pair<TreeNode, NodePath>>> nodeIndex = TreeMap.empty(Ord.stringOrd);
81 List<Pair<TreeNode, NodePath>> list = List.nil();
82 list = list.cons(pair);
83 nodeIndex = nodeIndex.set(attribute, list);
84 index = index.set(key, nodeIndex);
85 } else {
86 TreeMap<String, List<Pair<TreeNode, NodePath>>> indexMap = opAttributeList.some();
87 Option<List<Pair<TreeNode, NodePath>>> opNodeIndex = indexMap.get(attribute);
88
89 if (opNodeIndex.isNone()) {
90 List<Pair<TreeNode, NodePath>> pairList = List.nil();
91 pairList = pairList.cons(pair);
92 indexMap = indexMap.set(attribute, pairList);
93
94 } else {
95 List<Pair<TreeNode, NodePath>> pairList = opNodeIndex.some();
96 pairList = pairList.cons(pair);
97 indexMap = indexMap.set(attribute, pairList);
98 }
99 index = index.set(key, indexMap);
100 }
101 System.out.println(attribute);
102
103 }
104
105 }