0
|
1 package jp.ac.u_ryukyu.ie.cr.jungle.transaction;
|
|
2
|
|
3 import jp.ac.u_ryukyu.ie.cr.jungle.JungleTreeEditor;
|
|
4 import jp.ac.u_ryukyu.ie.cr.jungle.store.NodePath;
|
|
5 import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeEditor;
|
|
6 import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultNodePath;
|
|
7 import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNode;
|
|
8 import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.logger.DefaultTreeOperationLog;
|
|
9 import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.logger.LoggingNode;
|
|
10 import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.logger.OperationLog;
|
|
11 import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.logger.TreeOperationLog;
|
|
12 import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.DefaultTreeOperation;
|
|
13 import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.NodeOperation;
|
|
14 import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.TreeOperation;
|
|
15 import jp.ac.u_ryukyu.ie.cr.jungle.store.trasnformer.*;
|
|
16 import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither;
|
|
17 import jp.ac.u_ryukyu.ie.cr.jungle.util.Either;
|
|
18 import jp.ac.u_ryukyu.ie.cr.jungle.util.Error;
|
|
19 import jp.ac.u_ryukyu.ie.cr.jungle.util.IterableConverter;
|
|
20
|
|
21 import java.nio.ByteBuffer;
|
|
22
|
|
23 public class DefaultJungleTreeEditor implements JungleTreeEditor
|
|
24 {
|
|
25 private final TransactionManager txManager;
|
|
26 private final TreeNode root;
|
|
27 private final TreeEditor editor;
|
|
28 private final TreeOperationLog log;
|
|
29
|
|
30
|
|
31 public DefaultJungleTreeEditor(TreeNode _root,TransactionManager _txManager,TreeEditor _editor)
|
|
32 {
|
|
33 this(_root,_txManager,_editor,new DefaultTreeOperationLog());
|
|
34 }
|
|
35
|
|
36
|
|
37
|
|
38 public DefaultJungleTreeEditor(TreeNode newNode,TransactionManager _txManager,TreeEditor _editor,TreeOperationLog _log)
|
|
39 {
|
|
40 this.root = newNode;
|
|
41 this.txManager = _txManager;
|
|
42 this.editor = _editor;
|
|
43 this.log = _log;
|
|
44 }
|
|
45
|
|
46 private Either<Error,JungleTreeEditor> _edit(final NodePath _path,NodeEditor _e)
|
|
47 {
|
|
48 Either<jp.ac.u_ryukyu.ie.cr.jungle.util.Error,LoggingNode> either = editor.edit(root,_path,_e);
|
|
49 if(either.isA()){
|
|
50 return DefaultEither.newA(either.a());
|
|
51 }
|
|
52
|
|
53 LoggingNode newLogging = either.b();
|
|
54 OperationLog newLog = newLogging.getOperationLog();
|
|
55 TreeNode newNode = newLogging.getWrap();
|
|
56
|
|
57 IterableConverter.Converter<TreeOperation,NodeOperation> converter = new IterableConverter.Converter<TreeOperation,NodeOperation>(){
|
|
58 @Override
|
|
59 public TreeOperation conv(NodeOperation _b){
|
|
60 return new DefaultTreeOperation(_path,_b);
|
|
61 }
|
|
62 };
|
|
63
|
|
64 Iterable<TreeOperation> iterable = new IterableConverter<>(newLog,converter);
|
|
65 DefaultTreeOperationLog treeOperationLog = new DefaultTreeOperationLog(iterable,newLog.length());
|
|
66 TreeOperationLog newTreeOpLog = log.append(treeOperationLog);
|
|
67
|
|
68 JungleTreeEditor newEditor = new DefaultJungleTreeEditor(newNode,txManager,editor,newTreeOpLog);
|
|
69 return DefaultEither.newB(newEditor);
|
|
70 }
|
|
71
|
|
72 @Override
|
|
73 public Either<Error,JungleTreeEditor> replaceNewRootNode()
|
|
74 {
|
|
75 replaceRootNodeAt appendChildAt = new replaceRootNodeAt();
|
|
76 return _edit(new DefaultNodePath(),appendChildAt);
|
|
77 }
|
|
78
|
|
79 @Override
|
|
80 public Either<Error,JungleTreeEditor> addNewChildAt(NodePath _path, int _pos)
|
|
81 {
|
|
82 AppendChildAt appendChildAt = new AppendChildAt(_pos);
|
|
83 return _edit(_path,appendChildAt);
|
|
84 }
|
|
85
|
|
86 @Override
|
|
87 public Either<Error,JungleTreeEditor> deleteChildAt(NodePath _path, int _pos)
|
|
88 {
|
|
89 DeleteChildAt deleteChildAt = new DeleteChildAt(_pos);
|
|
90 return _edit(_path,deleteChildAt);
|
|
91 }
|
|
92
|
|
93 @Override
|
|
94 public Either<Error,JungleTreeEditor> putAttribute(NodePath _path,String _key,ByteBuffer _value)
|
|
95 {
|
|
96 PutAttribute putAttribute = new PutAttribute(_key,_value);
|
|
97 return _edit(_path,putAttribute);
|
|
98 }
|
|
99
|
|
100 @Override
|
|
101 public Either<Error,JungleTreeEditor> deleteAttribute(NodePath _path, String _key)
|
|
102 {
|
|
103 DeleteAttribute deleteAttribute = new DeleteAttribute(_key);
|
|
104 return _edit(_path,deleteAttribute);
|
|
105 }
|
|
106
|
|
107 @Override
|
|
108 public Either<Error,JungleTreeEditor> edit(NodePath _path,NodeEditor _editor)
|
|
109 {
|
|
110 return _edit(_path,_editor);
|
|
111 }
|
|
112
|
|
113 @Override
|
|
114 public Either<Error,JungleTreeEditor> success()
|
|
115 {
|
|
116 Either<Error,TransactionManager> either = txManager.commit(root,log);
|
|
117 if(either.isA()){
|
|
118 return DefaultEither.newA(either.a());
|
|
119 }
|
|
120
|
|
121 TransactionManager newTxManager = either.b();
|
|
122 JungleTreeEditor newTreeEditor = new DefaultJungleTreeEditor(root,newTxManager,editor);
|
|
123
|
|
124 return DefaultEither.newB(newTreeEditor);
|
|
125 }
|
|
126
|
|
127 @Override
|
|
128 public Either<Error, JungleTreeEditor> flushSuccess() {
|
|
129 return success();
|
|
130 }
|
|
131
|
|
132
|
|
133 }
|