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