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