223
|
1 package jp.ac.u_ryukyu.ie.cr.jungleNetwork.transaction;
|
|
2
|
|
3
|
263
|
4 import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.jungleTreeEditor.JungleTreeEditor;
|
223
|
5 import jp.ac.u_ryukyu.ie.cr.jungle.store.Command;
|
263
|
6 import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.NodePath;
|
223
|
7 import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.NodeOperation;
|
|
8 import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.TreeOperation;
|
|
9 import jp.ac.u_ryukyu.ie.cr.jungle.util.Either;
|
|
10 import jp.ac.u_ryukyu.ie.cr.jungle.util.Error;
|
|
11
|
|
12 import java.nio.ByteBuffer;
|
|
13
|
|
14 public class JungleUpdater {
|
|
15
|
235
|
16 public JungleUpdater() {
|
|
17
|
|
18 }
|
|
19
|
|
20 public static Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor, Iterable<TreeOperation> _log) {
|
|
21 JungleTreeEditor editor = _editor;
|
|
22 Either<Error, JungleTreeEditor> either = null;
|
|
23 for (TreeOperation op : _log) {
|
|
24 NodePath path = op.getNodePath();
|
|
25 NodeOperation nodeOp = op.getNodeOperation();
|
|
26 either = _edit(editor, path, nodeOp, nodeOp.getPosition());
|
|
27 if (either.isA()) {
|
|
28 return either;
|
|
29 }
|
|
30 editor = either.b();
|
|
31 }
|
|
32 return either;
|
|
33 }
|
|
34
|
|
35 /*
|
|
36 * This method decide node position by param.
|
|
37 */
|
|
38 public static Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor, Iterable<TreeOperation> _log, int pos) {
|
|
39 JungleTreeEditor editor = _editor;
|
|
40 Either<Error, JungleTreeEditor> either = null;
|
|
41 for (TreeOperation op : _log) {
|
|
42 NodePath path = op.getNodePath();
|
|
43 NodeOperation nodeOp = op.getNodeOperation();
|
|
44 either = _edit(editor, path, nodeOp, pos);
|
|
45 if (either.isA()) {
|
|
46 return either;
|
|
47 }
|
|
48 editor = either.b();
|
|
49 }
|
|
50 return either;
|
|
51 }
|
|
52
|
|
53
|
|
54 private static Either<Error, JungleTreeEditor> _edit(JungleTreeEditor editor, NodePath path, NodeOperation nodeOp, int pos) {//posはmergeの時に使う?
|
|
55 String key = "";
|
|
56 Command c = nodeOp.getCommand();
|
|
57 int num;
|
|
58 switch (c) {
|
|
59 case PUT_ATTRIBUTE:
|
|
60 key = nodeOp.getKey();
|
|
61 ByteBuffer value = nodeOp.getValue();
|
|
62 return editor.putAttribute(path, key, value);
|
|
63 case DELETE_ATTRIBUTE:
|
|
64 key = nodeOp.getKey();
|
|
65 return editor.deleteAttribute(path, key);
|
|
66 case APPEND_CHILD:
|
|
67 num = nodeOp.getPosition();
|
|
68 return editor.addNewChildAt(path, num);
|
|
69 case DELETE_CHILD:
|
|
70 num = nodeOp.getPosition();
|
|
71 return editor.deleteChildAt(path, num);
|
|
72 }
|
|
73 return null;
|
|
74 }
|
223
|
75
|
|
76 }
|