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