38
|
1 package jungle.test.bbs;
|
|
2
|
46
|
3 import java.io.IOException;
|
38
|
4 import java.nio.ByteBuffer;
|
56
|
5 import java.util.Date;
|
38
|
6 import java.util.concurrent.atomic.AtomicInteger;
|
39
|
7
|
52
|
8 import alice.jungle.codesegment.HashLogUpdateCodeSegment;
|
39
|
9 import alice.jungle.core.NetworkDefaultJungle;
|
46
|
10 import alice.jungle.datasegment.store.operations.DefaultTreeOperationLogContainer;
|
|
11 import alice.jungle.transaction.NetworkDefaultJungleTreeEditor;
|
38
|
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle;
|
|
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle;
|
|
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
|
|
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
|
|
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Children;
|
|
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node;
|
|
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
|
|
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor;
|
46
|
20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation;
|
38
|
21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableNode;
|
|
22 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor;
|
|
23 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser;
|
|
24 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither;
|
|
25 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
|
|
26 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
|
|
27 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter;
|
46
|
28 import jungle.test.bbs.codesegment.NullCodeSegmentForUpdate;
|
38
|
29
|
39
|
30 public class NetworkJungleBulletinBoard implements BulletinBoard
|
38
|
31 {
|
|
32 private final Jungle jungle;
|
|
33
|
43
|
34 public NetworkJungleBulletinBoard(String _serverName)
|
38
|
35 {
|
51
|
36 jungle = new NetworkDefaultJungle(null,"hoge",new DefaultTreeEditor(new DefaultTraverser()), _serverName);
|
38
|
37 jungle.createNewTree("boards");
|
51
|
38 JungleManager.setJungle(jungle);
|
38
|
39 }
|
|
40
|
|
41 public Iterable<String> getBoards()
|
|
42 {
|
|
43 JungleTree tree = jungle.getTreeByName("boards");
|
|
44 Node node = tree.getRootNode();
|
|
45 Children<Node> chs = node.getChildren();
|
|
46
|
|
47 IterableConverter.Converter<String,Node> converter = new IterableConverter.Converter<String,Node>(){
|
|
48 public String conv(Node _b) {
|
|
49 ByteBuffer e = _b.getAttributes().get("name");
|
|
50 return new String(e.array());
|
|
51 }
|
|
52 };
|
|
53
|
|
54 return new IterableConverter<String,Node>(chs,converter);
|
|
55 }
|
|
56
|
|
57 public void createBoards(final String _name,final String _author,final String _initMessage,final String _editKey)
|
|
58 {
|
|
59 if(null == jungle.createNewTree(_name)){
|
|
60 throw new IllegalStateException();
|
|
61 }
|
|
62
|
|
63 JungleTree tree = jungle.getTreeByName("boards");
|
|
64 JungleTreeEditor editor = tree.getTreeEditor();
|
|
65 DefaultNodePath root = new DefaultNodePath();
|
|
66 Either<Error,JungleTreeEditor> either = editor.addNewChildAt(root,0);
|
|
67 if(either.isA()){
|
|
68 throw new IllegalStateException();
|
|
69 }
|
|
70 editor = either.b();
|
|
71
|
|
72 either = editor.putAttribute(root.add(0),"name",ByteBuffer.wrap(_name.getBytes()));
|
|
73 if(either.isA()){
|
|
74 throw new IllegalStateException();
|
|
75 }
|
|
76 editor = either.b();
|
|
77 Either<Error,JungleTreeEditor> result = editor.success();
|
|
78 if(result.isA()){
|
|
79 throw new IllegalStateException();
|
|
80 }
|
56
|
81 final long timestamp = new Date().getTime();
|
46
|
82 /* Put DataSegment */
|
|
83 try {
|
57
|
84 putTreeOperationLog((NetworkDefaultJungleTreeEditor)editor, 0, timestamp);
|
46
|
85 } catch (IOException e1) {
|
|
86 e1.printStackTrace();
|
|
87 }
|
38
|
88
|
|
89 tree = jungle.getTreeByName(_name);
|
|
90 editor = tree.getTreeEditor();
|
|
91 either = editor.addNewChildAt(root,0);
|
|
92 if(either.isA()){
|
|
93 throw new IllegalStateException();
|
|
94 }
|
|
95 editor = either.b();
|
|
96
|
|
97 NodeEditor e = new NodeEditor(){
|
|
98 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){
|
|
99 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b();
|
|
100 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_initMessage.getBytes())).b();
|
|
101 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b();
|
56
|
102 ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
103 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b();
|
38
|
104 return DefaultEither.newB(_e);
|
|
105 }
|
|
106 };
|
|
107
|
|
108 either = editor.edit(root.add(0),e);
|
|
109 if(either.isA()){
|
|
110 throw new IllegalStateException();
|
|
111 }
|
|
112 editor = either.b();
|
|
113 editor.success();
|
46
|
114 /* Put DataSegment */
|
|
115 try {
|
57
|
116 putTreeOperationLog((NetworkDefaultJungleTreeEditor)editor, 0, timestamp);
|
46
|
117 } catch (IOException e1) {
|
|
118 e1.printStackTrace();
|
|
119 }
|
|
120
|
38
|
121 }
|
|
122
|
|
123 public void createBoardMessage(final String _board,final String _author,final String _message,final String _editKey)
|
|
124 {
|
|
125 JungleTree tree = jungle.getTreeByName(_board);
|
|
126 if(tree == null){
|
|
127 throw new IllegalStateException();
|
|
128 }
|
|
129
|
|
130 JungleTreeEditor editor;
|
46
|
131 Either<Error, JungleTreeEditor> either;
|
38
|
132 do{
|
|
133 Node node = tree.getRootNode();
|
|
134 int size = node.getChildren().size();
|
|
135 DefaultNodePath path = new DefaultNodePath();
|
|
136
|
|
137 editor = tree.getTreeEditor();
|
46
|
138 either = editor.addNewChildAt(path,size);
|
38
|
139 if(either.isA()){
|
|
140 throw new IllegalStateException();
|
|
141 }
|
|
142 editor = either.b();
|
56
|
143 final long timestamp = new Date().getTime();
|
38
|
144 NodeEditor e = new NodeEditor(){
|
|
145 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){
|
|
146 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b();
|
|
147 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b();
|
|
148 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b();
|
56
|
149 ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
150 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b();
|
38
|
151 return DefaultEither.newB(_e);
|
|
152 }
|
|
153 };
|
|
154 path = path.add(size);
|
|
155 either = editor.edit(path,e);
|
|
156 if(either.isA()){
|
|
157 throw new IllegalStateException();
|
|
158 }
|
|
159 editor = either.b();
|
46
|
160 either = editor.success();
|
|
161 try {
|
57
|
162 putTreeOperationLog((NetworkDefaultJungleTreeEditor)editor, size, timestamp);
|
46
|
163 } catch (IOException e1) {
|
|
164 e1.printStackTrace();
|
|
165 }
|
|
166 }while(either.isA());
|
51
|
167 /* Put DataSegment */
|
38
|
168 }
|
|
169
|
|
170 public void editMessage(String _board,String _uuid,final String _author,final String _message,final String _editKey)
|
|
171 {
|
|
172 JungleTreeEditor editor = null;
|
51
|
173 Either<Error,JungleTreeEditor> either = null;
|
38
|
174 do{
|
|
175 DefaultNodePath path = new DefaultNodePath();
|
|
176 path = path.add(Integer.parseInt(_uuid));
|
|
177
|
|
178 JungleTree tree = jungle.getTreeByName(_board);
|
|
179 editor = tree.getTreeEditor();
|
56
|
180 final long timestamp = new Date().getTime();
|
38
|
181 NodeEditor e = new NodeEditor(){
|
|
182 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){
|
|
183 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b();
|
|
184 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b();
|
|
185 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b();
|
56
|
186 ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
187 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b();
|
38
|
188 return DefaultEither.newB(_e);
|
|
189 }
|
|
190 };
|
|
191
|
46
|
192 either = editor.edit(path,e);
|
38
|
193 if(either.isA()){
|
|
194 throw new IllegalStateException();
|
|
195 }
|
|
196 editor = either.b();
|
46
|
197 either = editor.success();
|
|
198 try {
|
57
|
199 putTreeOperationLog((NetworkDefaultJungleTreeEditor)editor, Integer.parseInt(_uuid), timestamp);
|
46
|
200 } catch (IOException e1) {
|
|
201 e1.printStackTrace();
|
|
202 }
|
|
203 }while(either.isA());
|
38
|
204 }
|
|
205
|
|
206 public Iterable<BoardMessage> getMessages(String _boardName)
|
|
207 {
|
|
208 JungleTree tree = jungle.getTreeByName(_boardName);
|
|
209 Node node = tree.getRootNode();
|
|
210 Children<Node> chs = node.getChildren();
|
|
211
|
|
212 final AtomicInteger counter = new AtomicInteger(0);
|
|
213 IterableConverter.Converter<BoardMessage,Node> converter = new IterableConverter.Converter<BoardMessage,Node>(){
|
|
214 public BoardMessage conv(Node _b) {
|
|
215 String uuid = Integer.toString(counter.get());
|
|
216 String author = new String(_b.getAttributes().get("author").array());
|
|
217 String message = new String(_b.getAttributes().get("mes").array());
|
|
218 counter.incrementAndGet();
|
|
219 return new BoardMessageImpl(author,message,uuid);
|
|
220 }
|
|
221 };
|
|
222
|
|
223 return new IterableConverter<BoardMessage,Node>(chs,converter);
|
|
224 }
|
|
225
|
57
|
226 private void putTreeOperationLog(NetworkDefaultJungleTreeEditor editor, int pos, long timestamp) throws IOException {
|
46
|
227 String uuid = editor.getID();
|
|
228 String treeName = editor.getTreeName();
|
49
|
229 String updaterName = editor.getUpdaterName();
|
52
|
230 String revision = editor.getRevision();
|
46
|
231 Iterable<TreeOperation> log = editor.getTreeOperationLog();
|
57
|
232 putDataSegment(uuid, treeName, updaterName, log, revision, pos,timestamp);
|
46
|
233 }
|
|
234
|
57
|
235 private void putDataSegment(String _uuid, String _treeName, String _updaterName, Iterable<TreeOperation> _log, String nextRevision, int pos, long timestamp) throws IOException {
|
46
|
236 DefaultTreeOperationLogContainer container = new DefaultTreeOperationLogContainer();
|
|
237 container.setTreeName(_treeName);
|
|
238 container.setUUID(_uuid);
|
49
|
239 container.setUpdaterName(_updaterName);
|
46
|
240 container.setRevision(nextRevision);
|
57
|
241 container.setPosition(pos);
|
46
|
242 container.unconvert(_log);
|
56
|
243 container.setTimeStamp(timestamp);
|
|
244 HashLogUpdateCodeSegment cs = new HashLogUpdateCodeSegment();
|
|
245 container.getHashLogString();
|
47
|
246 cs.ods.put("log", container);
|
56
|
247 cs.ods.put("logString", container.getHashLogString());
|
|
248 System.out.println("putDataSegment : "+container.getHashLogString());
|
52
|
249 /* If this node is not Root node, push log to parent node's DS */
|
50
|
250 if(!_updaterName.equals("node0")) {
|
51
|
251 cs.ods.put("parent", "childLog", container);
|
50
|
252 }
|
46
|
253 }
|
|
254
|
|
255
|
38
|
256 private static class BoardMessageImpl implements BoardMessage
|
|
257 {
|
|
258 private final String author;
|
|
259 private final String message;
|
|
260 private final String uuid;
|
|
261
|
|
262 public BoardMessageImpl(String _author,String _message,String _uuid)
|
|
263 {
|
|
264 author = _author;
|
|
265 message = _message;
|
|
266 uuid = _uuid;
|
|
267 }
|
|
268
|
|
269 public String getAuthor()
|
|
270 {
|
|
271 return author;
|
|
272 }
|
|
273
|
|
274 public String getMessage()
|
|
275 {
|
|
276 return message;
|
|
277 }
|
|
278
|
|
279 public String getUUID()
|
|
280 {
|
|
281 return uuid;
|
|
282 }
|
|
283 }
|
|
284 }
|