2
|
1 package jp.ac.u_ryukyu.ie.cr.bbs.network;
|
|
2
|
|
3
|
3
|
4 import jp.ac.u_ryukyu.ie.cr.bbs.local.BoardMessage;
|
|
5 import jp.ac.u_ryukyu.ie.cr.bbs.local.GetAttributeImp;
|
|
6 import jp.ac.u_ryukyu.ie.cr.bbs.local.IterableConverter;
|
2
|
7 import jp.ac.u_ryukyu.ie.cr.jungle.Jungle;
|
|
8 import jp.ac.u_ryukyu.ie.cr.jungle.core.Children;
|
|
9 import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeList;
|
|
10 import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeListReader;
|
8
|
11 import jp.ac.u_ryukyu.ie.cr.jungle.store.logger.DefaultOperationLog;
|
|
12 import jp.ac.u_ryukyu.ie.cr.jungle.store.logger.LoggingNode;
|
|
13 import jp.ac.u_ryukyu.ie.cr.jungle.store.logger.OperationLog;
|
|
14 import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.DefaultNodePath;
|
2
|
15 import jp.ac.u_ryukyu.ie.cr.jungle.store.trasnformer.NodeEditor;
|
8
|
16 import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.jungleTreeEditor.JungleTreeEditor;
|
|
17 import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.Default.DefaultTreeNode;
|
|
18 import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode;
|
2
|
19 import jp.ac.u_ryukyu.ie.cr.jungle.traverser.DefaultEvaluator;
|
|
20 import jp.ac.u_ryukyu.ie.cr.jungle.traverser.DefaultTraverser;
|
|
21 import jp.ac.u_ryukyu.ie.cr.jungle.traverser.Traversal;
|
8
|
22 import jp.ac.u_ryukyu.ie.cr.jungle.tree.JungleTree;
|
2
|
23 import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither;
|
|
24 import jp.ac.u_ryukyu.ie.cr.jungle.util.Either;
|
8
|
25 import jp.ac.u_ryukyu.ie.cr.jungle.util.jungleError.Error;
|
2
|
26 import jp.ac.u_ryukyu.ie.cr.jungleNetwork.core.NetworkDefaultJungle;
|
|
27 import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.AliceJournal;
|
|
28 import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.NetworkJournal;
|
|
29 import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJournal;
|
|
30 import jp.ac.u_ryukyu.ie.cr.jungleNetwork.transaction.JungleUpdater;
|
|
31
|
|
32 import java.io.File;
|
|
33 import java.io.IOException;
|
|
34 import java.nio.ByteBuffer;
|
|
35 import java.util.concurrent.atomic.AtomicInteger;
|
10
|
36 import static org.junit.Assert.*;
|
2
|
37
|
|
38 public class NetworkJungleBulletinBoard implements NetworkBulletinBoard {
|
|
39 protected final Jungle jungle;
|
|
40 private final NetworkJournal journal;
|
|
41 private final String LOG_DIR;
|
|
42 private Boolean persistentFlag;
|
|
43 private AtomicInteger requestCounter;
|
|
44 private long renewTime;
|
|
45
|
|
46 private NetworkJungleBulletinBoard(String _uuid, NetworkJournal _journal) {
|
|
47 journal = _journal;
|
8
|
48 jungle = new NetworkDefaultJungle(journal, _uuid);
|
2
|
49 BulletinBoardJungleManager.setJungle(jungle);
|
|
50 persistentFlag = false;
|
|
51 requestCounter = BulletinBoardJungleManager.getRequestCounter();
|
|
52 LOG_DIR = "./log";
|
|
53 renewTime = 0;
|
|
54 }
|
|
55
|
|
56 public NetworkJungleBulletinBoard(String _uuid) {
|
|
57 this(_uuid, new AliceJournal());
|
|
58 jungle.createNewTree("boards");
|
|
59 }
|
|
60
|
|
61 public static NetworkBulletinBoard NewPersistentJungle(String _uuid) {
|
|
62 NetworkJungleBulletinBoard board = new NetworkJungleBulletinBoard(_uuid, new PersistentJournal());
|
|
63 board.persistentFlag = true;
|
|
64 return board;
|
|
65 }
|
|
66
|
|
67 public void init() {
|
|
68 if (!persistentFlag) {
|
|
69 return;
|
|
70 }
|
|
71 checkAndCreateLogDirectory();
|
|
72 try {
|
|
73 commitLogRecover();
|
|
74 } catch (IOException e) {
|
|
75 e.printStackTrace();
|
|
76 }
|
|
77 }
|
|
78
|
|
79 public void checkAndCreateLogDirectory() {
|
|
80 File logFile = new File(LOG_DIR);
|
|
81 if (!logFile.exists()) {
|
|
82 logFile.mkdir();
|
|
83 return;
|
|
84 }
|
|
85 if (logFile.isFile()) {
|
|
86 logFile.delete();
|
|
87 logFile.mkdir();
|
|
88 }
|
|
89 }
|
|
90
|
|
91 public void commitLogRecover() throws IOException {
|
|
92 File[] logFiles = new File(LOG_DIR).listFiles();
|
|
93 for (File logFile : logFiles) {
|
|
94 commitLogRecover(logFile);
|
|
95 logFile.delete();
|
|
96 }
|
|
97 if (jungle.getTreeByName("boards") == null) {
|
|
98 jungle.createNewTree("boards");
|
|
99 }
|
|
100 }
|
|
101
|
|
102 private void commitLogRecover(File logFile) throws IOException {
|
|
103 journal.setInputFile(logFile);
|
|
104 ChangeListReader reader = journal.getReader();
|
|
105 if (reader == null)
|
|
106 return;
|
|
107 for (ChangeList chList : reader) {
|
|
108 String treeName = chList.getTreeName();
|
|
109 JungleTree tree = jungle.getTreeByName(treeName);
|
|
110 if (tree == null) {
|
|
111 tree = jungle.createNewTree(treeName);
|
|
112 }
|
8
|
113 JungleTreeEditor editor = tree.getLocalJungleTreeEditor();
|
2
|
114 Either<Error, JungleTreeEditor> either = JungleUpdater.edit(editor, chList);
|
|
115 editor = either.b();
|
|
116 if (either.isA()) {
|
|
117 throw new IOException("Failed commit log recovery");
|
|
118 }
|
|
119 editor.success();
|
|
120 }
|
|
121 }
|
|
122
|
|
123 public Iterable<String> getBoards() {
|
|
124 JungleTree tree = jungle.getTreeByName("boards");
|
|
125 TreeNode node = tree.getRootNode();
|
|
126 Children chs = node.getChildren();
|
|
127
|
|
128 IterableConverter.Converter<String, TreeNode> converter = new IterableConverter.Converter<String, TreeNode>() {
|
|
129 public String conv(TreeNode _b) {
|
|
130 ByteBuffer e = _b.getAttributes().get("name");
|
|
131 System.out.println(new String(e.array()));
|
|
132 return new String(e.array());
|
|
133 }
|
|
134 };
|
|
135
|
|
136 return new IterableConverter<String, TreeNode>(chs, converter);
|
|
137 }
|
|
138
|
|
139 public long getRenewTime(String _boardName) {
|
|
140 return renewTime;
|
|
141 }
|
|
142
|
|
143 public void createBoards(final String _name, final String _author, final String _initMessage, final String _editKey) {
|
|
144 requestCounter.incrementAndGet();
|
|
145 if (null == jungle.createNewTree(_name)) {
|
|
146 throw new IllegalStateException();
|
|
147 }
|
|
148
|
|
149 JungleTree tree = jungle.getTreeByName("boards");
|
8
|
150 JungleTreeEditor editor = tree.getJungleTreeEditor();
|
2
|
151 DefaultNodePath root = new DefaultNodePath();
|
|
152 Either<Error, JungleTreeEditor> either = editor.addNewChildAt(root, 0);
|
|
153 if (either.isA()) {
|
|
154 throw new IllegalStateException();
|
|
155 }
|
|
156 editor = either.b();
|
|
157
|
|
158 either = editor.putAttribute(root.add(0), "name", ByteBuffer.wrap(_name.getBytes()));
|
|
159 if (either.isA()) {
|
|
160 throw new IllegalStateException();
|
|
161 }
|
|
162 editor = either.b();
|
|
163 final long timestamp = System.currentTimeMillis();
|
|
164 ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
165 tBuffer.putLong(timestamp);
|
|
166 either = editor.putAttribute(root.add(0), "timestamp", tBuffer);
|
|
167 if (either.isA()) {
|
|
168 throw new IllegalStateException();
|
|
169 }
|
|
170 either = either.b().success();
|
|
171 if (either.isA()) {
|
|
172 throw new IllegalStateException();
|
|
173 }
|
|
174
|
|
175 tree = jungle.getTreeByName(_name);
|
8
|
176 editor = tree.getJungleTreeEditor();
|
2
|
177 either = editor.addNewChildAt(root, 0);
|
|
178 if (either.isA()) {
|
|
179 throw new IllegalStateException();
|
|
180 }
|
|
181 editor = either.b();
|
|
182
|
9
|
183 jungleTransaction(editor, root,0, timestamp, logNode -> {
|
|
184 logNode = logNode.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b();
|
|
185 logNode = logNode.getAttributes().put("mes", ByteBuffer.wrap(_initMessage.getBytes())).b();
|
|
186 logNode = logNode.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b();
|
|
187 return logNode;
|
|
188 });
|
|
189 }
|
|
190
|
|
191 private Either<Error, JungleTreeEditor> jungleTransaction(JungleTreeEditor editor, DefaultNodePath path, int size, final long timestamp, JungleTransaction getLoggingNode) {
|
|
192 Either<Error, JungleTreeEditor> either;
|
2
|
193 NodeEditor e = new NodeEditor() {
|
|
194
|
|
195 public Either<Error, LoggingNode> edit(TreeNode node) {
|
8
|
196 LoggingNode logNode = wrap(node,null, new DefaultOperationLog());
|
9
|
197 ByteBuffer tBuffer2 = ByteBuffer.allocate(16);
|
|
198 logNode = getLoggingNode.transaction(logNode);
|
2
|
199 tBuffer2.putLong(timestamp);
|
|
200 logNode = logNode.getAttributes().put("timestamp", tBuffer2).b();
|
|
201 return DefaultEither.newB(logNode);
|
|
202 }
|
|
203 @Override
|
8
|
204 public LoggingNode wrap(TreeNode treeNode, TreeNode treeNode1, OperationLog operationLog) {
|
|
205 return new LoggingNode(treeNode, operationLog);
|
2
|
206 }
|
|
207 };
|
|
208
|
9
|
209 either = editor.edit(path.add(size), e);
|
2
|
210 if (either.isA()) {
|
|
211 throw new IllegalStateException();
|
|
212 }
|
|
213 either.b().success();
|
9
|
214 return either;
|
2
|
215 }
|
|
216
|
|
217 public void createFolder(final String _board, final String _author, final String _message, final String _editKey,
|
|
218 String _path) {
|
|
219 JungleTree tree = jungle.getTreeByName(_board);
|
|
220 if (tree == null) {
|
|
221 throw new IllegalStateException();
|
|
222 }
|
|
223
|
|
224 DefaultNodePath path = new DefaultNodePath();
|
|
225 String[] nums = _path.split(",");
|
|
226 for (String num : nums) {
|
|
227 if (!num.equals("-1"))
|
|
228 path = path.add(Integer.parseInt(num));
|
|
229 }
|
|
230
|
|
231 requestCounter.incrementAndGet();
|
|
232 Either<Error, JungleTreeEditor> either;
|
|
233 final long timestamp = System.currentTimeMillis();
|
|
234 final ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
235 tBuffer.putLong(timestamp);
|
|
236
|
|
237 do {
|
|
238 TreeNode node = tree.getRootNode();
|
|
239 DefaultTraverser traverser = new DefaultTraverser();
|
|
240 DefaultEvaluator evaluator = new DefaultEvaluator(path);
|
|
241 Either<Error, Traversal> ret = traverser.traverse(node, evaluator);
|
|
242 if (ret.isA()) {
|
10
|
243 assertEquals(1,0);
|
2
|
244 }
|
|
245
|
|
246 Traversal traversal = ret.b();
|
|
247 TreeNode target = traversal.destination();
|
|
248 int size = target.getChildren().size();
|
8
|
249 JungleTreeEditor editor = tree.getJungleTreeEditor();
|
2
|
250 either = editor.addNewChildAt(path, size);
|
|
251 if (either.isA()) {
|
|
252 throw new IllegalStateException();
|
|
253 }
|
|
254 editor = either.b();
|
|
255
|
9
|
256 either = jungleTransaction(editor, path,size, timestamp, logNode -> {
|
|
257 logNode = logNode.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b();
|
|
258 logNode = logNode.getAttributes().put("timestamp", tBuffer).b();
|
|
259 return logNode;
|
|
260 });
|
2
|
261 } while (either.isA());
|
|
262
|
|
263 }
|
|
264
|
|
265 public void createBoardMessage(final String _board, final String _author, final String _message, final String _editKey) {
|
|
266 requestCounter.incrementAndGet();
|
|
267 JungleTree tree = jungle.getTreeByName(_board);
|
|
268 if (tree == null) {
|
|
269 throw new IllegalStateException();
|
|
270 }
|
|
271
|
|
272 Either<Error, JungleTreeEditor> either;
|
|
273 final long timestamp = System.currentTimeMillis();
|
|
274 final ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
275 tBuffer.putLong(timestamp);
|
|
276 do {
|
|
277
|
|
278 TreeNode node = tree.getRootNode();
|
|
279 int size = node.getChildren().size();
|
|
280 DefaultNodePath path = new DefaultNodePath();
|
|
281
|
8
|
282 JungleTreeEditor editor = tree.getJungleTreeEditor();
|
2
|
283 either = editor.addNewChildAt(path, size);
|
|
284 if (either.isA()) {
|
|
285 throw new IllegalStateException();
|
|
286 }
|
|
287 editor = either.b();
|
|
288
|
|
289 NodeEditor e = new NodeEditor() {
|
|
290 public Either<Error, LoggingNode> edit(TreeNode node) {
|
8
|
291 LoggingNode logNode = wrap(node, null, new DefaultOperationLog());
|
2
|
292 logNode = logNode.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b();
|
|
293 logNode = logNode.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b();
|
|
294 logNode = logNode.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b();
|
|
295 logNode = logNode.getAttributes().put("timestamp", tBuffer).b();
|
|
296 return DefaultEither.newB(logNode);
|
|
297 }
|
|
298
|
|
299 @Override
|
8
|
300 public LoggingNode wrap(TreeNode node,TreeNode node1, OperationLog op) {
|
2
|
301 return new LoggingNode(node, op);
|
|
302 }
|
|
303 };
|
|
304 path = path.add(size);
|
|
305 either = editor.edit(path, e);
|
|
306 if (either.isA()) {
|
|
307 throw new IllegalStateException();
|
|
308 }
|
|
309 editor = either.b();
|
|
310 either = editor.success();
|
|
311 } while (either.isA());
|
|
312
|
|
313 }
|
|
314
|
|
315 public void editMessage(String _board, String _path, final String _author, final String _message,
|
|
316 final String _editKey) {
|
|
317 requestCounter.incrementAndGet();
|
|
318 final long timestamp = System.currentTimeMillis();
|
|
319 final ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
320 tBuffer.putLong(timestamp);
|
|
321 JungleTree tree = jungle.getTreeByName(_board);
|
|
322 Either<Error, JungleTreeEditor> either = null;
|
|
323 DefaultNodePath path = new DefaultNodePath();
|
|
324 String[] nums = _path.split(",");
|
|
325 for (String num : nums) {
|
|
326 if (!num.equals("-1"))
|
|
327 path = path.add(Integer.parseInt(num));
|
|
328 }
|
|
329 do {
|
|
330
|
8
|
331 JungleTreeEditor editor = tree.getJungleTreeEditor();
|
2
|
332 NodeEditor e = new NodeEditor() {
|
|
333 public Either<Error, LoggingNode> edit(TreeNode node) {
|
8
|
334 LoggingNode logNode = wrap(node,null,new DefaultOperationLog());
|
2
|
335 System.out.println(new String(node.getAttributes().get("mes").array()));
|
|
336 logNode = logNode.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b();
|
|
337 logNode = logNode.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b();
|
|
338 logNode = logNode.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b();
|
|
339 logNode = logNode.getAttributes().put("timestamp", tBuffer).b();
|
|
340 System.out.println(new String(node.getAttributes().get("mes").array()));
|
|
341 return DefaultEither.newB(logNode);
|
|
342 }
|
|
343
|
|
344 @Override
|
8
|
345 public LoggingNode wrap(TreeNode node,TreeNode node1, OperationLog op) {
|
2
|
346 return new LoggingNode(node, op);
|
|
347 }
|
|
348 };
|
|
349 either = editor.edit(path, e);
|
|
350 if (either.isA()) {
|
|
351 throw new IllegalStateException();
|
|
352 }
|
|
353 editor = either.b();
|
|
354 either = editor.success();
|
|
355 } while (either.isA());
|
|
356 renewTime = timestamp;
|
|
357 }
|
|
358
|
|
359 public void createAttribute(String _board, String _path, final String _author, final String _message,
|
|
360 final String _editKey) {
|
|
361 requestCounter.incrementAndGet();
|
|
362 final long timestamp = System.currentTimeMillis();
|
|
363 final ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
364 tBuffer.putLong(timestamp);
|
|
365 JungleTree tree = jungle.getTreeByName(_board);
|
|
366 Either<Error, JungleTreeEditor> either = null;
|
|
367 DefaultNodePath path = new DefaultNodePath();
|
|
368 String[] nums = _path.split(",");
|
|
369 for (String num : nums) {
|
|
370 if (!num.equals("-1"))
|
|
371 path = path.add(Integer.parseInt(num));
|
|
372 }
|
|
373
|
|
374 do {
|
8
|
375 JungleTreeEditor editor = tree.getJungleTreeEditor();
|
2
|
376 NodeEditor e = new NodeEditor() {
|
|
377 String str;
|
|
378
|
|
379 public Either<Error, LoggingNode> edit(TreeNode node) {
|
8
|
380 LoggingNode logNode = wrap(node, null,new DefaultOperationLog());
|
2
|
381 str = "0";
|
|
382 int count = 0;
|
|
383 for (; logNode.getAttributes().get("mes" + String.valueOf(count)) != null; count++) {
|
|
384 }
|
|
385 str = String.valueOf(count);
|
|
386 logNode = logNode.getAttributes().put("mes" + str, ByteBuffer.wrap(_message.getBytes())).b();
|
|
387 logNode = logNode.getAttributes().put("timestamp" + str, tBuffer).b();
|
|
388 return DefaultEither.newB(logNode);
|
|
389 }
|
|
390
|
|
391 @Override
|
8
|
392 public LoggingNode wrap(TreeNode node, TreeNode node1,OperationLog op) {
|
2
|
393 return new LoggingNode(node, op);
|
|
394 }
|
|
395 };
|
|
396 either = editor.edit(path, e);
|
|
397 if (either.isA()) {
|
|
398 throw new IllegalStateException();
|
|
399 }
|
|
400 editor = either.b();
|
|
401 either = editor.success();
|
|
402 } while (either.isA());
|
|
403 }
|
|
404
|
|
405 public void editAttribute(String _bname, String _path, final String id, final String _message) {
|
|
406 requestCounter.incrementAndGet();
|
|
407 final long timestamp = System.currentTimeMillis();
|
|
408 final ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
409 tBuffer.putLong(timestamp);
|
|
410 JungleTree tree = jungle.getTreeByName(_bname);
|
|
411 Either<Error, JungleTreeEditor> either = null;
|
|
412 DefaultNodePath path = new DefaultNodePath();
|
|
413 String[] nums = _path.split(",");
|
|
414 for (String num : nums) {
|
|
415 if (!num.equals("-1"))
|
|
416 path = path.add(Integer.parseInt(num));
|
|
417 }
|
|
418
|
|
419 do {
|
8
|
420 JungleTreeEditor editor = tree.getJungleTreeEditor();
|
2
|
421 NodeEditor e = new NodeEditor() {
|
|
422 public Either<Error, LoggingNode> edit(TreeNode node) {
|
8
|
423 LoggingNode logNode = wrap(node, null,new DefaultOperationLog());
|
2
|
424 logNode = logNode.getAttributes().put("mes" + id, ByteBuffer.wrap(_message.getBytes())).b();
|
|
425 logNode = logNode.getAttributes().put("timestamp" + id, tBuffer).b();
|
|
426 return DefaultEither.newB(logNode);
|
|
427 }
|
|
428
|
|
429 @Override
|
8
|
430 public LoggingNode wrap(TreeNode node,TreeNode node1, OperationLog op) {
|
2
|
431 return new LoggingNode(node, op);
|
|
432 }
|
|
433 };
|
|
434 either = editor.edit(path, e);
|
|
435 if (either.isA()) {
|
|
436 throw new IllegalStateException();
|
|
437 }
|
|
438 editor = either.b();
|
|
439 either = editor.success();
|
|
440 } while (either.isA());
|
|
441 }
|
|
442
|
|
443 public void deleteNode(String _board, String _path, String _id) {
|
|
444 requestCounter.incrementAndGet();
|
|
445 int id = Integer.parseInt(_id);
|
|
446 final long timestamp = System.currentTimeMillis();
|
|
447 final ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
448 tBuffer.putLong(timestamp);
|
|
449 JungleTree tree = jungle.getTreeByName(_board);
|
|
450 Either<Error, JungleTreeEditor> either = null;
|
|
451 DefaultNodePath path = new DefaultNodePath();
|
|
452 String[] nums = _path.split(",");
|
|
453 for (String num : nums) {
|
|
454 if (!num.equals("-1"))
|
|
455 path = path.add(Integer.parseInt(num));
|
|
456 }
|
|
457
|
|
458 do {
|
8
|
459 JungleTreeEditor editor = tree.getJungleTreeEditor();
|
2
|
460
|
|
461 either = editor.deleteChildAt(path, id);
|
|
462 if (either.isA()) {
|
|
463 throw new IllegalStateException();
|
|
464 }
|
|
465 editor = either.b();
|
|
466 either = editor.success();
|
|
467 } while (either.isA());
|
|
468
|
|
469 }
|
|
470
|
|
471 public void deleteAttribute(String _board, String _path, final String id) {
|
|
472 requestCounter.incrementAndGet();
|
|
473 final long timestamp = System.currentTimeMillis();
|
|
474 final ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
475 tBuffer.putLong(timestamp);
|
|
476 JungleTree tree = jungle.getTreeByName(_board);
|
|
477 Either<Error, JungleTreeEditor> either = null;
|
|
478 DefaultNodePath path = new DefaultNodePath();
|
|
479 String[] nums = _path.split(",");
|
|
480 for (String num : nums) {
|
|
481 if (!num.equals("-1"))
|
|
482 path = path.add(Integer.parseInt(num));
|
|
483 }
|
|
484
|
|
485 do {
|
8
|
486 JungleTreeEditor editor = tree.getJungleTreeEditor();
|
2
|
487 NodeEditor e = new NodeEditor() {
|
|
488 public Either<Error, LoggingNode> edit(TreeNode node) {
|
8
|
489 LoggingNode logNode = wrap(node,null, new DefaultOperationLog());
|
2
|
490 logNode = logNode.getAttributes().delete("mes" + id).b();
|
|
491 logNode = logNode.getAttributes().delete("timestamp" + id).b();
|
|
492 int count = Integer.parseInt(id);
|
|
493 for (; logNode.getAttributes().get("mes" + String.valueOf(count + 1)) != null; ) {
|
|
494 logNode = logNode.getAttributes()
|
|
495 .put("mes" + count, node.getAttributes().get("mes" + String.valueOf(count + 1))).b();
|
|
496 logNode = logNode.getAttributes().put("timestamp" + count, tBuffer).b();
|
|
497 count++;
|
|
498 }
|
|
499 if (count != Integer.parseInt(id)) {
|
|
500 logNode = logNode.getAttributes().delete("timestamp" + count).b();
|
|
501 logNode = logNode.getAttributes().delete("mes" + count).b();
|
|
502 }
|
|
503
|
|
504 return DefaultEither.newB(logNode);
|
|
505 }
|
|
506
|
|
507 @Override
|
8
|
508 public LoggingNode wrap(TreeNode node, TreeNode node1,OperationLog op) {
|
2
|
509 return new LoggingNode(node, op);
|
|
510 }
|
|
511 };
|
|
512 either = editor.edit(path, e);
|
|
513 if (either.isA()) {
|
|
514 throw new IllegalStateException();
|
|
515 }
|
|
516 editor = either.b();
|
|
517 either = editor.success();
|
|
518 } while (either.isA());
|
|
519 }
|
|
520
|
|
521 public void editMatrixMessage(String _board, String _uuid, final String _author, final String _message,
|
|
522 final String _editKey) {
|
|
523 requestCounter.incrementAndGet();
|
|
524 final long timestamp = System.currentTimeMillis();
|
|
525 final ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
526 tBuffer.putLong(timestamp);
|
|
527 JungleTree tree = jungle.getTreeByName(_board);
|
|
528 Either<Error, JungleTreeEditor> either = null;
|
|
529 do {
|
|
530 DefaultNodePath path = new DefaultNodePath();
|
|
531 path = path.add(Integer.parseInt(_uuid));
|
|
532
|
8
|
533 JungleTreeEditor editor = tree.getJungleTreeEditor();
|
2
|
534 NodeEditor e = new NodeEditor() {
|
|
535 public Either<Error, LoggingNode> edit(TreeNode node) {
|
8
|
536 LoggingNode logNode = wrap(node, null,new DefaultOperationLog());
|
2
|
537 logNode = logNode.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b();
|
|
538 logNode = logNode.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b();
|
|
539 logNode = logNode.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b();
|
|
540 logNode = logNode.getAttributes().put("timestamp", tBuffer).b();
|
|
541 return DefaultEither.newB(logNode);
|
|
542 }
|
|
543
|
|
544 @Override
|
8
|
545 public LoggingNode wrap(TreeNode node, TreeNode node1,OperationLog op) {
|
2
|
546 return new LoggingNode(node, op);
|
|
547 }
|
|
548 };
|
|
549 either = editor.edit(path, e);
|
|
550 if (either.isA()) {
|
|
551 throw new IllegalStateException();
|
|
552 }
|
|
553 editor = either.b();
|
|
554 either = editor.success();
|
|
555 } while (either.isA());
|
|
556 renewTime = timestamp;
|
|
557 }
|
|
558
|
|
559 public Iterable<BoardMessage> getFolder(String _boardName, String _nodeNum) {
|
|
560 DefaultNodePath path = new DefaultNodePath();
|
|
561 System.out.println(_nodeNum.substring(0, 1));
|
|
562 String[] nums = _nodeNum.split(",");
|
|
563 for (String num : nums) {
|
|
564 if (!num.equals("-1"))
|
|
565 path = path.add(Integer.parseInt(num));
|
|
566 }
|
|
567 JungleTree tree = jungle.getTreeByName(_boardName);
|
|
568 TreeNode node = tree.getRootNode();
|
|
569 requestCounter.incrementAndGet();
|
|
570
|
|
571 DefaultTraverser traverser = new DefaultTraverser();
|
|
572 DefaultEvaluator evaluator = new DefaultEvaluator(path);
|
|
573 Either<Error, Traversal> ret = traverser.traverse(node, evaluator);
|
|
574 if (ret.isA()) {
|
10
|
575 assertEquals(1,0);
|
2
|
576 }
|
|
577
|
|
578 Traversal traversal = ret.b();
|
|
579 TreeNode target = traversal.destination();
|
|
580 Children chs = target.getChildren();
|
|
581
|
|
582 final AtomicInteger counter = new AtomicInteger(0);
|
|
583 IterableConverter.Converter<BoardMessage, TreeNode> converter = new IterableConverter.Converter<BoardMessage, TreeNode>() {
|
|
584 public BoardMessage conv(TreeNode _b) {
|
|
585 String uuid = Integer.toString(counter.get());
|
|
586 String message = new String(_b.getAttributes().get("mes").array());
|
|
587 counter.incrementAndGet();
|
|
588 return new BoardMessageImpl(null, message, uuid);
|
|
589 }
|
|
590 };
|
|
591 return new IterableConverter<BoardMessage, TreeNode>(chs, converter);
|
|
592 }
|
|
593
|
|
594 public boolean compare(TreeNode compareNode, String compareAttribute) {
|
|
595 String labName = compareNode.getAttributes().getString("mes");
|
|
596 if (labName.equals(compareAttribute))
|
|
597 return true;
|
|
598
|
|
599 for (int loopCount = 0; compareNode.getAttributes().getString("mes" + loopCount) != null; loopCount++) {
|
|
600 labName = compareNode.getAttributes().getString("mes" + loopCount);
|
|
601 if (labName.equals(compareAttribute))
|
|
602 return true;
|
|
603 }
|
|
604
|
|
605 return false;
|
|
606 }
|
|
607
|
|
608 public int getRequestNum() {
|
|
609 return requestCounter.get();
|
|
610 }
|
|
611
|
|
612 private static class BoardMessageImpl implements BoardMessage {
|
|
613 private final String author;
|
|
614 private final String message;
|
|
615 private final String uuid;
|
|
616
|
|
617 public BoardMessageImpl(String _author, String _message, String _uuid) {
|
|
618 author = _author;
|
|
619 message = _message;
|
|
620 uuid = _uuid;
|
|
621 }
|
|
622
|
|
623 public String getAuthor() {
|
|
624 return author;
|
|
625 }
|
|
626
|
|
627 public String getMessage() {
|
|
628 return message;
|
|
629 }
|
|
630
|
|
631 public String getUUID() {
|
|
632 return uuid;
|
|
633 }
|
|
634
|
|
635 }
|
|
636
|
|
637 public String sanitize(String str) {
|
|
638 if (str == null) {
|
|
639 return str;
|
|
640 }
|
|
641 str = str.replaceAll("&", "&");
|
|
642 str = str.replaceAll("<", "<");
|
|
643 str = str.replaceAll(">", ">");
|
|
644 str = str.replaceAll("\"", """);
|
|
645 str = str.replaceAll("'", "'");
|
|
646 return str;
|
|
647 }
|
|
648
|
4
|
649 public GetAttributeImp getAttribute(String _bname, String nodePath,String revisionStr) {
|
|
650 Long revision = Long.parseLong(revisionStr);
|
|
651 DefaultNodePath path = createNodePath(nodePath);
|
2
|
652 JungleTree tree = jungle.getTreeByName(_bname);
|
4
|
653 JungleTree oldTree = tree.getOldTree(revision).b();
|
|
654 Either<Error, TreeNode> either = tree.getNodeOfPath(path);
|
|
655 if (either.isA())
|
|
656 return new GetAttributeImp(new DefaultTreeNode());
|
|
657 TreeNode node = either.b();
|
|
658 return new GetAttributeImp(node);
|
|
659 }
|
2
|
660
|
4
|
661 private DefaultNodePath createNodePath(String nodePath) {
|
|
662 DefaultNodePath path = new DefaultNodePath();
|
|
663 try {
|
|
664 for (int count = 0; nodePath.substring(count, count + 1) != null; count++) {
|
|
665 if (!nodePath.substring(count, count + 1).equals("/"))
|
|
666 path = path.add(Integer.parseInt(nodePath.substring(count, count + 1)));
|
|
667 }
|
|
668 } catch (Exception _e) {
|
2
|
669 }
|
4
|
670 return path;
|
|
671 }
|
2
|
672
|
|
673 }
|