14
|
1 package jungle.test.codesegment.log.practice;
|
2
|
2
|
9
|
3 import java.io.IOException;
|
|
4 import java.nio.ByteBuffer;
|
|
5
|
11
|
6 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle;
|
|
7 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle;
|
|
8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
|
|
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
|
|
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node;
|
9
|
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.Command;
|
|
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
|
10
|
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
|
11
|
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor;
|
13
|
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.DefaultTreeOperationLog;
|
9
|
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog;
|
|
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation;
|
|
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation;
|
11
|
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser;
|
|
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 jungle.test.core.NetworkDefaultJungle;
|
9
|
23 import jungle.test.datasegment.store.operations.DefaultTreeOperationLogContainer;
|
11
|
24 import jungle.test.transaction.NetworkDefaultJungleTreeEditor;
|
2
|
25 import alice.codesegment.CodeSegment;
|
|
26 import alice.datasegment.CommandType;
|
|
27 import alice.datasegment.Receiver;
|
13
|
28 import alice.test.topology.aquarium.FishPoint;
|
9
|
29
|
|
30 import org.msgpack.MessagePack;
|
2
|
31 import org.msgpack.type.Value;
|
|
32
|
|
33 public class TestCodeSegment extends CodeSegment {
|
|
34
|
11
|
35 static Jungle jungle = new NetworkDefaultJungle(null,"hoge",new DefaultTreeEditor(new DefaultTraverser()));
|
|
36
|
2
|
37 // create input datasegment arg1
|
|
38 Receiver arg1 = ids.create(CommandType.PEEK);
|
|
39
|
11
|
40 public TestCodeSegment() {
|
13
|
41 arg1.setKey("log");
|
11
|
42 jungle.createNewTree("tree");
|
|
43 }
|
|
44
|
2
|
45 @Override
|
|
46 public void run() {
|
|
47 System.out.println("type = " + arg1.type);
|
|
48 System.out.println("index = " + arg1.index);
|
|
49 System.out.println("data = " + arg1.getVal());
|
14
|
50 System.out.println(((Value)arg1.getVal()).getType());
|
2
|
51
|
13
|
52 DefaultTreeOperationLogContainer convertedLogContainer = arg1.asClass(DefaultTreeOperationLogContainer.class);
|
9
|
53 TreeOperationLog convertedLog = null;
|
|
54 try {
|
|
55 convertedLog = convertedLogContainer.convert();
|
|
56 } catch (IOException e) {
|
|
57 e.printStackTrace();
|
2
|
58 }
|
11
|
59 printLog(convertedLog);
|
|
60
|
|
61 JungleTree tree = jungle.getTreeByName("tree");
|
|
62 NetworkDefaultJungleTreeEditor editor = (NetworkDefaultJungleTreeEditor) tree.getTreeEditor();
|
|
63 JungleTreeEditor tEditor = editor.setNewLogAndGetEditor(convertedLog);
|
|
64 Either<Error,JungleTreeEditor> either = tEditor.success();
|
|
65 if (either.isA()) {
|
|
66 throw new IllegalStateException();
|
|
67 }
|
|
68 System.exit(0);
|
|
69 }
|
|
70
|
|
71 public void printLog(TreeOperationLog log) {
|
|
72 for (TreeOperation treeOp : log) {
|
9
|
73 NodePath path = treeOp.getNodePath();
|
|
74 NodeOperation nodeOp = treeOp.getNodeOperation();
|
|
75 Command c = nodeOp.getCommand();
|
|
76 String str = "";
|
|
77 switch (c) {
|
|
78 case PUT_ATTRIBUTE:
|
|
79 String k = nodeOp.getKey();
|
|
80 ByteBuffer value = nodeOp.getValue();
|
|
81 if (value.limit() < 100) {
|
|
82 str = String.format("key:%s,value:%s", k,
|
|
83 new String(value.array()));
|
|
84 } else {
|
|
85 str = String.format("key:%s,value:%d", k, value.limit());
|
|
86 }
|
|
87 break;
|
|
88 case DELETE_ATTRIBUTE:
|
|
89 str = String.format("key:%s", nodeOp.getKey());
|
|
90 break;
|
|
91 case APPEND_CHILD:
|
|
92 str = String.format("pos:%d", nodeOp.getPosition());
|
|
93 break;
|
|
94 case DELETE_CHILD:
|
|
95 str = String.format("pos:%d", nodeOp.getPosition());
|
|
96 break;
|
|
97 }
|
|
98 System.out.println(String.format("[%s:%s]", c, str));
|
|
99 System.out.println("path:");
|
|
100 for (int i: path ) {
|
|
101 System.out.println(i);
|
|
102 }
|
11
|
103 }
|
2
|
104 }
|
|
105
|
|
106 }
|