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