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