Mercurial > hg > Members > nobuyasu > jungle-network
comparison src/main/java/alice/jungle/transaction/NetworkDefaultJungleTreeEditor.java @ 105:f9e29a52efd3
Move some files
author | one |
---|---|
date | Tue, 26 Nov 2013 06:43:10 +0900 |
parents | src/alice/jungle/transaction/NetworkDefaultJungleTreeEditor.java@6e2cec364e5a |
children | f42f2e6b31de |
comparison
equal
deleted
inserted
replaced
104:03bf62bb699e | 105:f9e29a52efd3 |
---|---|
1 package alice.jungle.transaction; | |
2 | |
3 | |
4 import java.io.IOException; | |
5 import java.nio.ByteBuffer; | |
6 | |
7 import alice.jungle.operations.NetworkTreeOperationLog; | |
8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor; | |
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; | |
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; | |
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TreeEditor; | |
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode; | |
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.DefaultTreeOperationLog; | |
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.LoggingNodeHook; | |
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.OperationLog; | |
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog; | |
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.DefaultTreeOperation; | |
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation; | |
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation; | |
20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.AppendChildAt; | |
21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.DeleteAttribute; | |
22 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.DeleteChildAt; | |
23 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor; | |
24 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.PutAttribute; | |
25 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.TransactionManager; | |
26 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; | |
27 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; | |
28 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; | |
29 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter; | |
30 import jungle.app.bbs.codesegment.NullCodeSegment; | |
31 | |
32 public class NetworkDefaultJungleTreeEditor<T extends TreeNode<T>> implements JungleTreeEditor { | |
33 | |
34 private final TransactionManager<T> txManager; | |
35 private final T root; | |
36 private final TreeEditor editor; | |
37 private final String treeName; | |
38 private final TreeOperationLog log; | |
39 private boolean exportLog; | |
40 | |
41 public NetworkDefaultJungleTreeEditor(String _treeName, T _root,TransactionManager<T> _txManager,TreeEditor _editor) | |
42 { | |
43 this(_treeName, _root,_txManager,_editor,new DefaultTreeOperationLog()); | |
44 } | |
45 | |
46 public NetworkDefaultJungleTreeEditor(String _treeName, T _root,TransactionManager<T> _txManager,TreeEditor _editor, TreeOperationLog _log) | |
47 { | |
48 treeName = _treeName; | |
49 root = _root; | |
50 txManager = _txManager; | |
51 editor = _editor; | |
52 log = _log; | |
53 exportLog = true; | |
54 } | |
55 | |
56 public static <T1 extends TreeNode<T1>> NetworkDefaultJungleTreeEditor<T1> NewLocalJungleTreeEditor(String _treeName, T1 _root,TransactionManager<T1> _txManager,TreeEditor _editor) { | |
57 NetworkDefaultJungleTreeEditor<T1> treeEditor = new NetworkDefaultJungleTreeEditor<T1>(_treeName, _root,_txManager,_editor,new DefaultTreeOperationLog()); | |
58 treeEditor.exportLog = false; | |
59 return treeEditor; | |
60 } | |
61 | |
62 public static <T1 extends TreeNode<T1>> NetworkDefaultJungleTreeEditor<T1> NewLocalJungleTreeEditor(String _treeName, T1 _root,TransactionManager<T1> _txManager,TreeEditor _editor, TreeOperationLog _log) { | |
63 NetworkDefaultJungleTreeEditor<T1> treeEditor = new NetworkDefaultJungleTreeEditor<T1>(_treeName, _root,_txManager,_editor,_log); | |
64 treeEditor.exportLog = false; | |
65 return treeEditor; | |
66 } | |
67 | |
68 private Either<Error,JungleTreeEditor> _edit(final NodePath _path,NodeEditor _e) | |
69 { | |
70 LoggingNodeHook hook = new LoggingNodeHook(_e); | |
71 Either<Error,T> either = editor.edit(root,_path,hook); | |
72 if(either.isA()){ | |
73 return DefaultEither.newA(either.a()); | |
74 } | |
75 | |
76 T newNode = either.b(); | |
77 OperationLog newLog = hook.getLog(); | |
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 JungleTreeEditor newEditor; | |
91 if(exportLog) { | |
92 newEditor = new NetworkDefaultJungleTreeEditor<T>(treeName, newNode,txManager,editor,newTreeOpLog); | |
93 } else { | |
94 newEditor = NetworkDefaultJungleTreeEditor.NewLocalJungleTreeEditor(treeName, newNode, txManager, editor, newTreeOpLog); | |
95 } | |
96 return DefaultEither.newB(newEditor); | |
97 } | |
98 | |
99 @Override | |
100 public Either<Error,JungleTreeEditor> addNewChildAt(NodePath _path, int _pos) | |
101 { | |
102 AppendChildAt appendChildAt = new AppendChildAt(_pos); | |
103 return _edit(_path,appendChildAt); | |
104 } | |
105 | |
106 @Override | |
107 public Either<Error,JungleTreeEditor> deleteChildAt(NodePath _path, int _pos) | |
108 { | |
109 DeleteChildAt deleteChildAt = new DeleteChildAt(_pos); | |
110 return _edit(_path,deleteChildAt); | |
111 } | |
112 | |
113 @Override | |
114 public Either<Error,JungleTreeEditor> putAttribute(NodePath _path,String _key,ByteBuffer _value) | |
115 { | |
116 PutAttribute putAttribute = new PutAttribute(_key,_value); | |
117 return _edit(_path,putAttribute); | |
118 } | |
119 | |
120 @Override | |
121 public Either<Error,JungleTreeEditor> deleteAttribute(NodePath _path, String _key) | |
122 { | |
123 DeleteAttribute deleteAttribute = new DeleteAttribute(_key); | |
124 return _edit(_path,deleteAttribute); | |
125 } | |
126 | |
127 @Override | |
128 public Either<Error,JungleTreeEditor> edit(NodePath _path,NodeEditor _editor) | |
129 { | |
130 return _edit(_path,_editor); | |
131 } | |
132 | |
133 @Override | |
134 public Either<Error,JungleTreeEditor> success() | |
135 { | |
136 Either<Error,TransactionManager<T>> either = txManager.commit(root,log); | |
137 if(either.isA()){ | |
138 return DefaultEither.newA(either.a()); | |
139 } | |
140 if(exportLog) { | |
141 try { | |
142 putTreeOperationLog(log); | |
143 } catch (IOException e) { | |
144 return DefaultEither.newA(either.a()); | |
145 } | |
146 } | |
147 | |
148 TransactionManager<T> newTxManager = either.b(); | |
149 JungleTreeEditor newTreeEditor = new NetworkDefaultJungleTreeEditor<T>(treeName, root,newTxManager,editor); | |
150 | |
151 return DefaultEither.newB(newTreeEditor); | |
152 } | |
153 | |
154 @Override | |
155 public String getID() | |
156 { | |
157 return txManager.getUUID(); | |
158 } | |
159 | |
160 @Override | |
161 public String getRevision() | |
162 { | |
163 return Long.toString(txManager.getRevision()); | |
164 } | |
165 | |
166 @Override | |
167 public Node getRoot() | |
168 { | |
169 return root.getAsNode(); | |
170 } | |
171 | |
172 public String getTreeName() { | |
173 return treeName; | |
174 } | |
175 | |
176 public TreeOperationLog getTreeOperationLog() { | |
177 return log; | |
178 } | |
179 | |
180 public void putTreeOperationLog(Iterable<TreeOperation> newLog) throws IOException { | |
181 String uuid = getID(); | |
182 String treeName = getTreeName(); | |
183 String updaterName = getID(); | |
184 String revision = getRevision(); | |
185 putDataSegment(uuid, treeName, updaterName, newLog, revision); | |
186 } | |
187 | |
188 public void putDataSegment(String _uuid, String _treeName, String _updaterName, Iterable<TreeOperation> newLog, String nextRevision) throws IOException { | |
189 NetworkTreeOperationLog netLog = new NetworkTreeOperationLog(_uuid, _treeName,newLog); | |
190 NullCodeSegment cs = new NullCodeSegment(); | |
191 cs.ods.put("log", netLog); | |
192 cs.execute(); /* Do nothing CodeSegment */ | |
193 } | |
194 } |