118
|
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.DeleteIndexEditor;
|
|
29 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index.IndexEditor;
|
|
30 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index.PutIndexEditor;
|
|
31
|
|
32 public class IndexJungleTreeEditor implements JungleTreeEditor
|
|
33 {
|
|
34 private final TransactionManager txManager;
|
|
35 private final TreeNode root;
|
|
36 private final TreeEditor editor;
|
|
37 private final TreeOperationLog log;
|
|
38 private TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index;
|
|
39
|
|
40 // public DefaultJungleTreeEditor(TreeNode root)
|
|
41 // {
|
|
42 // this(root,txManager,_editor,new DefaultTreeOperationLog());
|
|
43 // }
|
|
44
|
|
45 public TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> getIndex() {
|
|
46 return index;
|
|
47 }
|
|
48
|
|
49 public void setIndex(
|
|
50 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) {
|
|
51 this.index = index;
|
|
52 }
|
|
53
|
|
54 public IndexJungleTreeEditor(TreeNode _root,TransactionManager _txManager,TreeEditor _editor, TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index)
|
|
55 {
|
|
56 this(_root,_txManager,_editor,new DefaultTreeOperationLog(), index);
|
|
57 }
|
|
58
|
|
59 public IndexJungleTreeEditor(TreeNode newNode,TransactionManager _txManager,TreeEditor _editor,TreeOperationLog _log, TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index)
|
|
60 {
|
|
61 this.root = newNode;
|
|
62 this.txManager = _txManager;
|
|
63 this.editor = _editor;
|
|
64 this.log = _log;
|
|
65 this.index = index;
|
|
66 }
|
|
67
|
|
68 private Either<Error,IndexJungleTreeEditor> _edit(final NodePath _path,NodeEditor _e)
|
|
69 {
|
|
70 Either<Error,LoggingNode> either = editor.edit(root,_path,_e);
|
|
71 if(either.isA()){
|
|
72 return DefaultEither.newA(either.a());
|
|
73 }
|
|
74
|
|
75 LoggingNode newLogging = either.b();
|
|
76 OperationLog newLog = newLogging.getOperationLog();
|
|
77 TreeNode newNode = newLogging.getWrap();
|
|
78
|
|
79 IterableConverter.Converter<TreeOperation,NodeOperation> converter = new IterableConverter.Converter<TreeOperation,NodeOperation>(){
|
|
80 @Override
|
|
81 public TreeOperation conv(NodeOperation _b){
|
|
82 return new DefaultTreeOperation(_path,_b);
|
|
83 }
|
|
84 };
|
|
85
|
|
86 Iterable<TreeOperation> iterable = new IterableConverter<TreeOperation,NodeOperation>(newLog,converter);
|
|
87 DefaultTreeOperationLog treeOperationLog = new DefaultTreeOperationLog(iterable,newLog.length());
|
|
88 TreeOperationLog newTreeOpLog = log.append(treeOperationLog);
|
|
89
|
|
90 IndexJungleTreeEditor newEditor = new IndexJungleTreeEditor(newNode,txManager,editor,newTreeOpLog, index);
|
|
91 return DefaultEither.newB(newEditor);
|
|
92 }
|
|
93
|
|
94 @Override
|
|
95 public Either<Error,JungleTreeEditor> addNewChildAt(NodePath _path, int _pos)
|
|
96 {
|
|
97 AppendChildAt appendChildAt = new AppendChildAt(_pos);
|
|
98 Either<Error,IndexJungleTreeEditor> either = _edit(_path,appendChildAt);
|
|
99 JungleTreeEditor editor = either.b();
|
|
100 Either<Error,JungleTreeEditor> newEither = DefaultEither.newB(editor);
|
|
101 return newEither;
|
|
102 }
|
|
103
|
|
104 @Override
|
|
105 public Either<Error,JungleTreeEditor> deleteChildAt(NodePath _path, int _pos)
|
|
106 {
|
|
107 DeleteChildAt deleteChildAt = new DeleteChildAt(_pos);
|
|
108 Either<Error,IndexJungleTreeEditor> either = _edit(_path,deleteChildAt);
|
|
109 JungleTreeEditor editor = either.b();
|
|
110 Either<Error,JungleTreeEditor> newEither = DefaultEither.newB(editor);
|
|
111 return newEither;
|
|
112 }
|
|
113
|
|
114 @Override
|
|
115 public Either<Error,JungleTreeEditor> putAttribute(NodePath _path,String _key,ByteBuffer _value)
|
|
116 {
|
|
117 PutAttribute putAttribute = new PutAttribute(_key,_value);
|
|
118 Either<Error,IndexJungleTreeEditor> either = _edit(_path,putAttribute);
|
|
119 PutIndexEditor indexEditor = new PutIndexEditor();
|
|
120 Either<Error,JungleTreeEditor> newEither = indexEditor.edit(either.b());
|
|
121 return newEither;
|
|
122 }
|
|
123
|
|
124 @Override
|
|
125 public Either<Error,JungleTreeEditor> deleteAttribute(NodePath _path, String _key)
|
|
126 {
|
|
127 DeleteAttribute deleteAttribute = new DeleteAttribute(_key);
|
|
128 Either<Error,IndexJungleTreeEditor> either = _edit(_path,deleteAttribute);
|
|
129 DeleteIndexEditor indexEditor = new DeleteIndexEditor();
|
|
130 Either<Error,JungleTreeEditor> newEither = indexEditor.edit(either.b());
|
|
131 return newEither;
|
|
132 }
|
|
133
|
|
134 @Override
|
|
135 public Either<Error,JungleTreeEditor> edit(NodePath _path,NodeEditor _editor)
|
|
136 {
|
|
137 Either<Error,IndexJungleTreeEditor> either = _edit(_path,_editor);
|
|
138 JungleTreeEditor editor = either.b();
|
|
139 Either<Error,JungleTreeEditor> newEither = DefaultEither.newB(editor);
|
|
140 return newEither;
|
|
141 }
|
|
142
|
|
143 @Override
|
|
144 public Either<Error,JungleTreeEditor> success()
|
|
145 {
|
126
|
146 Either<Error,TransactionManager> either = txManager.commit(root,log,index);
|
118
|
147 if(either.isA()){
|
|
148 return DefaultEither.newA(either.a());
|
|
149 }
|
|
150
|
|
151 TransactionManager newTxManager = either.b();
|
|
152 JungleTreeEditor newTreeEditor = new DefaultJungleTreeEditor(root,newTxManager,editor);
|
|
153
|
|
154 return DefaultEither.newB(newTreeEditor);
|
|
155 }
|
|
156
|
|
157 @Override
|
|
158 public String getID()
|
|
159 {
|
|
160 return txManager.getUUID();
|
|
161 }
|
|
162
|
|
163 @Override
|
|
164 public String getRevision()
|
|
165 {
|
|
166 return Long.toString(txManager.getRevision());
|
|
167 }
|
|
168
|
|
169 @Override
|
|
170 public TreeNode getRoot()
|
|
171 {
|
|
172 return root;
|
|
173 }
|
127
|
174
|
118
|
175 } |