Mercurial > hg > Members > nobuyasu > jungle-network
annotate 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 |
rev | line source |
---|---|
82 | 1 package test.java.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.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; | |
66 | 20 import jungle.app.bbs.JungleManager; |
18 | 21 |
17 | 22 import org.msgpack.type.Value; |
23 | |
24 import alice.codesegment.CodeSegment; | |
25 import alice.datasegment.CommandType; | |
26 import alice.datasegment.Receiver; | |
75
87ec5dd0dc27
Rename from alice.jungle.datasegment.store.operation to alice.jungle.datasegment.store.container
one
parents:
66
diff
changeset
|
27 import alice.jungle.datasegment.store.container.DefaultTreeOperationContainer; |
87ec5dd0dc27
Rename from alice.jungle.datasegment.store.operation to alice.jungle.datasegment.store.container
one
parents:
66
diff
changeset
|
28 import alice.jungle.datasegment.store.container.DefaultTreeOperationLogContainer; |
31 | 29 import alice.jungle.transaction.NetworkDefaultJungleTreeEditor; |
17 | 30 |
31 public class TestPutAttributeCodeSegment extends CodeSegment { | |
32 | |
33 Receiver arg1 = ids.create(CommandType.TAKE); | |
27 | 34 |
17 | 35 public TestPutAttributeCodeSegment() { |
36 arg1.setKey("log"); | |
37 } | |
27 | 38 |
17 | 39 public void run() { |
19 | 40 System.out.println("--TestPutAttributeCodeSegment--"); |
27 | 41 DefaultTreeOperationContainer convertedOpContainer = arg1 |
42 .asClass(DefaultTreeOperationContainer.class); | |
18 | 43 TreeOperation convertedOp = null; |
44 try { | |
45 convertedOp = convertedOpContainer.convert(); | |
46 } catch (IOException e) { | |
47 e.printStackTrace(); | |
48 } | |
45 | 49 Jungle jungle = JungleManager.getJungle(); |
18 | 50 JungleTree tree = jungle.getTreeByName("tree"); |
26 | 51 JungleTreeEditor editor = tree.getTreeEditor(); |
25 | 52 DefaultNodePath root = new DefaultNodePath(); |
27 | 53 Either<Error, JungleTreeEditor> either = editor.addNewChildAt(root, 0); |
54 if (either.isA()) { | |
25 | 55 throw new IllegalStateException(); |
56 } | |
26 | 57 editor = either.b(); |
58 editor.success(); | |
27 | 59 NetworkDefaultJungleTreeEditor nEditor = (NetworkDefaultJungleTreeEditor) tree |
60 .getTreeEditor(); | |
26 | 61 either = edit(nEditor, convertedOp); |
27 | 62 if (either.isA()) { |
26 | 63 throw new IllegalStateException(); |
64 } | |
65 editor = either.b(); | |
18 | 66 editor.success(); |
20 | 67 if (arg1.index >= 10) { |
68 new ShowAttribute(); | |
27 | 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); | |
17 | 81 } |
27 | 82 |
19 | 83 public DefaultTreeOperation getSampleOperation(String message) { |
18 | 84 /* Create TreeOperation */ |
20 | 85 String key = "key1"; |
19 | 86 ByteBuffer b = ByteBuffer.wrap(message.getBytes()); |
18 | 87 PutAttributeOperation op = new PutAttributeOperation(key, b); |
88 DefaultNodePath p = new DefaultNodePath(); | |
19 | 89 p = p.add(0); |
18 | 90 DefaultTreeOperation treeOp = new DefaultTreeOperation(p, op); |
91 return treeOp; | |
92 } | |
27 | 93 |
94 public Either<Error, JungleTreeEditor> edit(JungleTreeEditor editor, | |
95 TreeOperation op) { | |
18 | 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 } | |
25 | 111 return editor.putAttribute(path, key, value); |
18 | 112 case DELETE_ATTRIBUTE: |
113 key = nodeOp.getKey(); | |
114 str = String.format("key:%s", nodeOp.getKey()); | |
25 | 115 return editor.deleteAttribute(path, key); |
18 | 116 case APPEND_CHILD: |
117 str = String.format("pos:%d", nodeOp.getPosition()); | |
25 | 118 return editor.addNewChildAt(path, 0); |
18 | 119 case DELETE_CHILD: |
120 str = String.format("pos:%d", nodeOp.getPosition()); | |
25 | 121 return editor.deleteChildAt(path, 0); |
18 | 122 } |
25 | 123 return null; |
18 | 124 } |
17 | 125 } |