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