17
|
1 package jungle.test.codesegment.practice;
|
|
2
|
18
|
3 import java.io.IOException;
|
|
4 import java.nio.ByteBuffer;
|
|
5
|
|
6 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle;
|
|
7 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
|
|
8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
|
|
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.Command;
|
|
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
|
|
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TreeEditor;
|
|
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
|
|
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog;
|
|
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.DefaultTreeOperation;
|
|
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation;
|
|
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.PutAttributeOperation;
|
|
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation;
|
|
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
|
|
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
|
|
20 import jungle.test.datasegment.store.operations.DefaultTreeOperationContainer;
|
|
21 import jungle.test.datasegment.store.operations.DefaultTreeOperationLogContainer;
|
|
22 import jungle.test.transaction.NetworkDefaultJungleTreeEditor;
|
|
23
|
17
|
24 import org.msgpack.type.Value;
|
|
25
|
|
26 import alice.codesegment.CodeSegment;
|
|
27 import alice.datasegment.CommandType;
|
|
28 import alice.datasegment.Receiver;
|
|
29
|
|
30 public class TestPutAttributeCodeSegment extends CodeSegment {
|
|
31
|
|
32 Receiver arg1 = ids.create(CommandType.TAKE);
|
|
33
|
|
34 public TestPutAttributeCodeSegment() {
|
|
35 arg1.setKey("log");
|
|
36 }
|
|
37
|
|
38
|
|
39 public void run() {
|
19
|
40 System.out.println("--TestPutAttributeCodeSegment--");
|
17
|
41 System.out.println("type = " + arg1.type);
|
|
42 System.out.println("index = " + arg1.index);
|
|
43 System.out.println("data = " + arg1.getVal());
|
|
44 System.out.println(((Value)arg1.getVal()).getType());
|
19
|
45
|
|
46 if (arg1.index >= 10) {
|
|
47 System.exit(0);
|
|
48 }
|
17
|
49
|
18
|
50 DefaultTreeOperationContainer convertedOpContainer = arg1.asClass(DefaultTreeOperationContainer.class);
|
|
51 TreeOperation convertedOp = null;
|
|
52 try {
|
|
53 convertedOp = convertedOpContainer.convert();
|
|
54 } catch (IOException e) {
|
|
55 e.printStackTrace();
|
|
56 }
|
|
57 Jungle jungle = new JungleManager().getJungle();
|
|
58 JungleTree tree = jungle.getTreeByName("tree");
|
|
59 NetworkDefaultJungleTreeEditor editor = (NetworkDefaultJungleTreeEditor) tree.getTreeEditor();
|
|
60 edit(editor, convertedOp);
|
|
61 editor.success();
|
17
|
62
|
19
|
63 TestPutAttributeCodeSegment cs = new TestPutAttributeCodeSegment();
|
|
64 DefaultTreeOperation treeOp = cs.getSampleOperation("message"+arg1.index);
|
|
65 DefaultTreeOperationContainer treeOperationContainer = new DefaultTreeOperationContainer();
|
|
66 try {
|
|
67 treeOperationContainer.unconvert(treeOp);
|
|
68 } catch (IOException e) {
|
|
69 e.printStackTrace();
|
|
70 }
|
|
71 cs.ods.update("local", "log", treeOperationContainer);
|
|
72
|
17
|
73 }
|
18
|
74
|
19
|
75 public DefaultTreeOperation getSampleOperation(String message) {
|
18
|
76 /* Create TreeOperation */
|
|
77 String key = "hoge";
|
19
|
78 ByteBuffer b = ByteBuffer.wrap(message.getBytes());
|
18
|
79 PutAttributeOperation op = new PutAttributeOperation(key, b);
|
|
80 DefaultNodePath p = new DefaultNodePath();
|
19
|
81 p = p.add(0);
|
18
|
82 DefaultTreeOperation treeOp = new DefaultTreeOperation(p, op);
|
|
83 return treeOp;
|
|
84 }
|
|
85
|
|
86 public void edit(JungleTreeEditor editor, TreeOperation op) {
|
|
87 NodePath path = op.getNodePath();
|
|
88 NodeOperation nodeOp = op.getNodeOperation();
|
|
89 Command c = nodeOp.getCommand();
|
|
90 String str = "";
|
|
91 String key = "";
|
|
92 switch (c) {
|
|
93 case PUT_ATTRIBUTE:
|
|
94 key = nodeOp.getKey();
|
|
95 ByteBuffer value = nodeOp.getValue();
|
|
96 if (value.limit() < 100) {
|
|
97 str = String.format("key:%s,value:%s", key,
|
|
98 new String(value.array()));
|
|
99 } else {
|
|
100 str = String.format("key:%s,value:%d", key, value.limit());
|
|
101 }
|
|
102 editor.putAttribute(path, key, value);
|
|
103 break;
|
|
104 case DELETE_ATTRIBUTE:
|
|
105 key = nodeOp.getKey();
|
|
106 str = String.format("key:%s", nodeOp.getKey());
|
|
107 editor.deleteAttribute(path, key);
|
|
108 break;
|
|
109 case APPEND_CHILD:
|
|
110 str = String.format("pos:%d", nodeOp.getPosition());
|
|
111 editor.addNewChildAt(path, 0);
|
|
112 break;
|
|
113 case DELETE_CHILD:
|
|
114 str = String.format("pos:%d", nodeOp.getPosition());
|
|
115 editor.deleteChildAt(path, 0);
|
|
116 break;
|
|
117 }
|
|
118 System.out.println(String.format("[%s:%s]", c, str));
|
|
119 System.out.println("path:");
|
|
120 for (int i: path ) {
|
|
121 System.out.println(i);
|
|
122 }
|
|
123 }
|
17
|
124 }
|