28
|
1 package jungle.test.core.practice;
|
|
2
|
|
3 import java.io.IOException;
|
29
|
4 import java.nio.ByteBuffer;
|
28
|
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;
|
29
|
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.Command;
|
|
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
|
28
|
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.DefaultTreeOperationLog;
|
30
|
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog;
|
29
|
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;
|
28
|
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
|
|
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
|
39
|
17 import jungle.test.bbs.JungleManager;
|
28
|
18 import alice.codesegment.CodeSegment;
|
|
19 import alice.datasegment.CommandType;
|
|
20 import alice.datasegment.Receiver;
|
31
|
21 import alice.jungle.datasegment.store.operations.DefaultTreeOperationLogContainer;
|
28
|
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 }
|
30
|
41 Jungle jungle = new JungleManager().getJungle();
|
28
|
42 JungleTree tree = jungle.getTreeByName("tree");
|
29
|
43 JungleTreeEditor editor = tree.getTreeEditor();
|
30
|
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;
|
29
|
56 Either<Error, JungleTreeEditor> either = null;
|
30
|
57 for (TreeOperation op : _log) {
|
|
58 either = _edit(editor, op);
|
29
|
59 if(either.isA()) {
|
30
|
60 return either;
|
29
|
61 }
|
|
62 editor = either.b();
|
28
|
63 }
|
30
|
64 return either;
|
28
|
65 }
|
|
66
|
30
|
67 private Either<Error, JungleTreeEditor> _edit(JungleTreeEditor editor,
|
29
|
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 }
|
28
|
98 }
|