Mercurial > hg > Database > jungle-network
comparison src/test/java/alice/jungle/log/example/LogReadCodeSegment.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/core/practice/LogReadCodeSegment.java@87ec5dd0dc27 |
children | f9e29a52efd3 |
comparison
equal
deleted
inserted
replaced
81:b9dd8ec0e66e | 82:60d28fedcbf2 |
---|---|
1 package test.java.alice.jungle.log.example; | |
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.impl.logger.DefaultTreeOperationLog; | |
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog; | |
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.TreeOperation; | |
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; | |
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; | |
17 import jungle.app.bbs.JungleManager; | |
18 import alice.codesegment.CodeSegment; | |
19 import alice.datasegment.CommandType; | |
20 import alice.datasegment.Receiver; | |
21 import alice.jungle.datasegment.store.container.DefaultTreeOperationLogContainer; | |
22 | |
23 public class LogReadCodeSegment extends CodeSegment { | |
24 | |
25 Receiver arg1 = ids.create(CommandType.TAKE); | |
26 | |
27 public LogReadCodeSegment() { | |
28 arg1.setKey("log"); | |
29 } | |
30 | |
31 public void run() { | |
32 System.out.println("--LogReadCodeSegment--"); | |
33 | |
34 DefaultTreeOperationLogContainer container = arg1.asClass(DefaultTreeOperationLogContainer.class); | |
35 DefaultTreeOperationLog log = null; | |
36 try { | |
37 log = container.convert(); | |
38 } catch (IOException e) { | |
39 e.printStackTrace(); | |
40 } | |
41 Jungle jungle = JungleManager.getJungle(); | |
42 JungleTree tree = jungle.getTreeByName("tree"); | |
43 JungleTreeEditor editor = tree.getTreeEditor(); | |
44 Either<Error, JungleTreeEditor> either = edit(editor, log); | |
45 if (either.isA()) { | |
46 throw new IllegalStateException(); | |
47 } | |
48 editor = either.b(); | |
49 editor.success(); | |
50 new PrintChildrenAttribute("key1"); | |
51 ods.update("key1", "key1"); | |
52 } | |
53 | |
54 private Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor ,TreeOperationLog _log) { | |
55 JungleTreeEditor editor = _editor; | |
56 Either<Error, JungleTreeEditor> either = null; | |
57 for (TreeOperation op : _log) { | |
58 either = _edit(editor, op); | |
59 if(either.isA()) { | |
60 return either; | |
61 } | |
62 editor = either.b(); | |
63 } | |
64 return either; | |
65 } | |
66 | |
67 private Either<Error, JungleTreeEditor> _edit(JungleTreeEditor editor, | |
68 TreeOperation op) { | |
69 NodePath path = op.getNodePath(); | |
70 NodeOperation nodeOp = op.getNodeOperation(); | |
71 Command c = nodeOp.getCommand(); | |
72 String str = ""; | |
73 String key = ""; | |
74 switch (c) { | |
75 case PUT_ATTRIBUTE: | |
76 key = nodeOp.getKey(); | |
77 ByteBuffer value = nodeOp.getValue(); | |
78 if (value.limit() < 100) { | |
79 str = String.format("key:%s,value:%s", key, | |
80 new String(value.array())); | |
81 } else { | |
82 str = String.format("key:%s,value:%d", key, value.limit()); | |
83 } | |
84 return editor.putAttribute(path, key, value); | |
85 case DELETE_ATTRIBUTE: | |
86 key = nodeOp.getKey(); | |
87 str = String.format("key:%s", nodeOp.getKey()); | |
88 return editor.deleteAttribute(path, key); | |
89 case APPEND_CHILD: | |
90 str = String.format("pos:%d", nodeOp.getPosition()); | |
91 return editor.addNewChildAt(path, 0); | |
92 case DELETE_CHILD: | |
93 str = String.format("pos:%d", nodeOp.getPosition()); | |
94 return editor.deleteChildAt(path, 0); | |
95 } | |
96 return null; | |
97 } | |
98 } |