comparison src/jungle/test/codesegment/operation/TestPutAttributeCodeSegment.java @ 27:0493340583ab

move some files
author one
date Mon, 01 Jul 2013 04:27:11 +0900
parents src/jungle/test/codesegment/practice/TestPutAttributeCodeSegment.java@1d7f52c3b3d9
children 190f6a3bdab2
comparison
equal deleted inserted replaced
26:1d7f52c3b3d9 27:0493340583ab
1 package jungle.test.codesegment.operation;
2
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
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 public void run() {
39 System.out.println("--TestPutAttributeCodeSegment--");
40 DefaultTreeOperationContainer convertedOpContainer = arg1
41 .asClass(DefaultTreeOperationContainer.class);
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");
50 JungleTreeEditor editor = tree.getTreeEditor();
51 DefaultNodePath root = new DefaultNodePath();
52 Either<Error, JungleTreeEditor> either = editor.addNewChildAt(root, 0);
53 if (either.isA()) {
54 throw new IllegalStateException();
55 }
56 editor = either.b();
57 editor.success();
58 NetworkDefaultJungleTreeEditor nEditor = (NetworkDefaultJungleTreeEditor) tree
59 .getTreeEditor();
60 either = edit(nEditor, convertedOp);
61 if (either.isA()) {
62 throw new IllegalStateException();
63 }
64 editor = either.b();
65 editor.success();
66 if (arg1.index >= 10) {
67 new ShowAttribute();
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);
80 }
81
82 public DefaultTreeOperation getSampleOperation(String message) {
83 /* Create TreeOperation */
84 String key = "key1";
85 ByteBuffer b = ByteBuffer.wrap(message.getBytes());
86 PutAttributeOperation op = new PutAttributeOperation(key, b);
87 DefaultNodePath p = new DefaultNodePath();
88 p = p.add(0);
89 DefaultTreeOperation treeOp = new DefaultTreeOperation(p, op);
90 return treeOp;
91 }
92
93 public Either<Error, JungleTreeEditor> edit(JungleTreeEditor editor,
94 TreeOperation op) {
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 }
110 return editor.putAttribute(path, key, value);
111 case DELETE_ATTRIBUTE:
112 key = nodeOp.getKey();
113 str = String.format("key:%s", nodeOp.getKey());
114 return editor.deleteAttribute(path, key);
115 case APPEND_CHILD:
116 str = String.format("pos:%d", nodeOp.getPosition());
117 return editor.addNewChildAt(path, 0);
118 case DELETE_CHILD:
119 str = String.format("pos:%d", nodeOp.getPosition());
120 return editor.deleteChildAt(path, 0);
121 }
122 return null;
123 }
124 }