8
|
1 package jp.ac.u_ryukyu.ie.cr.bbs.network.bbs;
|
2
|
2
|
|
3
|
8
|
4 import jp.ac.u_ryukyu.ie.cr.bbs.local.bbs.BoardMessage;
|
|
5 import jp.ac.u_ryukyu.ie.cr.bbs.local.bbs.GetAttributeImp;
|
|
6 import jp.ac.u_ryukyu.ie.cr.bbs.local.bbs.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.LoggingNode;
|
|
12 import jp.ac.u_ryukyu.ie.cr.jungle.store.logger.OperationLog;
|
|
13 import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.DefaultNodePath;
|
|
14 import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.NodePath;
|
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.Error.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 import junit.framework.Assert;
|
|
32
|
|
33 import java.io.File;
|
|
34 import java.io.IOException;
|
|
35 import java.nio.ByteBuffer;
|
|
36 import java.util.concurrent.atomic.AtomicInteger;
|
|
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();
|
|
151 NodePath root = new DefaultNodePath();
|
2
|
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
|
|
183 NodeEditor e = new NodeEditor() {
|
|
184 ByteBuffer tBuffer2 = ByteBuffer.allocate(16);
|
|
185
|
8
|
186 Either<Error, LoggingNode> _edit(LoggingNode logNode) {
|
2
|
187 logNode = logNode.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b();
|
|
188 logNode = logNode.getAttributes().put("mes", ByteBuffer.wrap(_initMessage.getBytes())).b();
|
|
189 logNode = logNode.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b();
|
|
190 tBuffer2.putLong(timestamp);
|
|
191 logNode = logNode.getAttributes().put("timestamp", tBuffer2).b();
|
|
192 return DefaultEither.newB(logNode);
|
|
193 }
|
|
194
|
|
195 @Override
|
8
|
196 public Either<Error, LoggingNode> edit(TreeNode _e) {
|
|
197 LoggingNode logNode = wrap(_e);
|
|
198 return _edit(logNode);
|
2
|
199 }
|
8
|
200
|
|
201 private LoggingNode wrap(TreeNode node) {
|
|
202 return new LoggingNode(node);
|
|
203 }
|
|
204
|
|
205 @Override
|
|
206 public LoggingNode wrap(TreeNode newRoot, TreeNode editedNode, OperationLog operationLog) {
|
|
207 return new LoggingNode(newRoot, editedNode, operationLog);
|
|
208 }
|
|
209
|
2
|
210 };
|
|
211
|
|
212 either = editor.edit(root.add(0), e);
|
|
213 if (either.isA()) {
|
|
214 throw new IllegalStateException();
|
|
215 }
|
|
216 either.b().success();
|
|
217
|
|
218 }
|
|
219
|
8
|
220 public void createFolder(final String _board, final String _author, final String _message, final String _editKey, String _path) {
|
2
|
221 JungleTree tree = jungle.getTreeByName(_board);
|
|
222 if (tree == null) {
|
|
223 throw new IllegalStateException();
|
|
224 }
|
|
225
|
|
226 DefaultNodePath path = new DefaultNodePath();
|
|
227 String[] nums = _path.split(",");
|
|
228 for (String num : nums) {
|
|
229 if (!num.equals("-1"))
|
|
230 path = path.add(Integer.parseInt(num));
|
|
231 }
|
|
232
|
|
233 requestCounter.incrementAndGet();
|
|
234 Either<Error, JungleTreeEditor> either;
|
|
235 final long timestamp = System.currentTimeMillis();
|
|
236 final ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
237 tBuffer.putLong(timestamp);
|
|
238
|
|
239 do {
|
|
240 TreeNode node = tree.getRootNode();
|
|
241 DefaultTraverser traverser = new DefaultTraverser();
|
|
242 DefaultEvaluator evaluator = new DefaultEvaluator(path);
|
|
243 Either<Error, Traversal> ret = traverser.traverse(node, evaluator);
|
|
244 if (ret.isA()) {
|
|
245 Assert.fail();
|
|
246 }
|
|
247
|
|
248 Traversal traversal = ret.b();
|
|
249 TreeNode target = traversal.destination();
|
|
250 int size = target.getChildren().size();
|
8
|
251 JungleTreeEditor editor = tree.getJungleTreeEditor();
|
2
|
252 either = editor.addNewChildAt(path, size);
|
|
253 if (either.isA()) {
|
|
254 throw new IllegalStateException();
|
|
255 }
|
|
256 editor = either.b();
|
|
257
|
|
258 NodeEditor e = new NodeEditor() {
|
|
259
|
8
|
260 Either<Error, LoggingNode> _edit(LoggingNode logNode) {
|
2
|
261 logNode = logNode.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b();
|
|
262 logNode = logNode.getAttributes().put("timestamp", tBuffer).b();
|
|
263 return DefaultEither.newB(logNode);
|
|
264 }
|
|
265
|
|
266 @Override
|
8
|
267 public Either<Error, LoggingNode> edit(TreeNode _e) {
|
|
268 LoggingNode logNode = wrap(_e);
|
|
269 return _edit(logNode);
|
|
270 }
|
|
271
|
|
272 private LoggingNode wrap(TreeNode node) {
|
|
273 return new LoggingNode(node);
|
|
274 }
|
|
275
|
|
276 @Override
|
|
277 public LoggingNode wrap(TreeNode newRoot, TreeNode editedNode, OperationLog operationLog) {
|
|
278 return new LoggingNode(newRoot, editedNode, operationLog);
|
2
|
279 }
|
|
280
|
|
281 };
|
|
282 path = path.add(size);
|
|
283 either = editor.edit(path, e);
|
|
284 if (either.isA()) {
|
|
285 throw new IllegalStateException();
|
|
286 }
|
|
287 editor = either.b();
|
|
288 either = editor.success();
|
|
289 } while (either.isA());
|
|
290
|
|
291 }
|
|
292
|
|
293 public void createBoardMessage(final String _board, final String _author, final String _message, final String _editKey) {
|
|
294 requestCounter.incrementAndGet();
|
|
295 JungleTree tree = jungle.getTreeByName(_board);
|
|
296 if (tree == null) {
|
|
297 throw new IllegalStateException();
|
|
298 }
|
|
299
|
|
300 Either<Error, JungleTreeEditor> either;
|
|
301 final long timestamp = System.currentTimeMillis();
|
|
302 final ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
303 tBuffer.putLong(timestamp);
|
|
304 do {
|
|
305
|
|
306 TreeNode node = tree.getRootNode();
|
|
307 int size = node.getChildren().size();
|
|
308 DefaultNodePath path = new DefaultNodePath();
|
|
309
|
8
|
310 JungleTreeEditor editor = tree.getJungleTreeEditor();
|
2
|
311 either = editor.addNewChildAt(path, size);
|
|
312 if (either.isA()) {
|
|
313 throw new IllegalStateException();
|
|
314 }
|
|
315 editor = either.b();
|
|
316
|
|
317 NodeEditor e = new NodeEditor() {
|
8
|
318 Either<Error, LoggingNode> _edit(LoggingNode logNode) {
|
2
|
319 logNode = logNode.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b();
|
|
320 logNode = logNode.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b();
|
|
321 logNode = logNode.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b();
|
|
322 logNode = logNode.getAttributes().put("timestamp", tBuffer).b();
|
|
323 return DefaultEither.newB(logNode);
|
|
324 }
|
|
325
|
|
326 @Override
|
8
|
327 public Either<Error, LoggingNode> edit(TreeNode _e) {
|
|
328 LoggingNode logNode = wrap(_e);
|
|
329 return _edit(logNode);
|
|
330 }
|
|
331
|
|
332 private LoggingNode wrap(TreeNode node) {
|
|
333 return new LoggingNode(node);
|
|
334 }
|
|
335
|
|
336 @Override
|
|
337 public LoggingNode wrap(TreeNode newRoot, TreeNode editedNode, OperationLog operationLog) {
|
|
338 return new LoggingNode(newRoot, editedNode, operationLog);
|
2
|
339 }
|
|
340 };
|
|
341 path = path.add(size);
|
|
342 either = editor.edit(path, e);
|
|
343 if (either.isA()) {
|
|
344 throw new IllegalStateException();
|
|
345 }
|
|
346 editor = either.b();
|
|
347 either = editor.success();
|
|
348 } while (either.isA());
|
|
349
|
|
350 }
|
|
351
|
8
|
352 public void editMessage(String _board, String _path, final String _author, final String _message, final String _editKey) {
|
2
|
353 requestCounter.incrementAndGet();
|
|
354 final long timestamp = System.currentTimeMillis();
|
|
355 final ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
356 tBuffer.putLong(timestamp);
|
|
357 JungleTree tree = jungle.getTreeByName(_board);
|
|
358 Either<Error, JungleTreeEditor> either = null;
|
|
359 DefaultNodePath path = new DefaultNodePath();
|
|
360 String[] nums = _path.split(",");
|
|
361 for (String num : nums) {
|
|
362 if (!num.equals("-1"))
|
|
363 path = path.add(Integer.parseInt(num));
|
|
364 }
|
|
365 do {
|
|
366
|
8
|
367 JungleTreeEditor editor = tree.getJungleTreeEditor();
|
2
|
368 NodeEditor e = new NodeEditor() {
|
8
|
369 Either<Error, LoggingNode> _edit(LoggingNode logNode) {
|
2
|
370 logNode = logNode.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b();
|
|
371 logNode = logNode.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b();
|
|
372 logNode = logNode.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b();
|
|
373 logNode = logNode.getAttributes().put("timestamp", tBuffer).b();
|
|
374 return DefaultEither.newB(logNode);
|
|
375 }
|
|
376
|
|
377 @Override
|
8
|
378 public Either<Error, LoggingNode> edit(TreeNode _e) {
|
|
379 LoggingNode logNode = wrap(_e);
|
|
380 return _edit(logNode);
|
2
|
381 }
|
8
|
382
|
|
383 private LoggingNode wrap(TreeNode node) {
|
|
384 return new LoggingNode(node);
|
|
385 }
|
|
386
|
|
387 @Override
|
|
388 public LoggingNode wrap(TreeNode newRoot, TreeNode editedNode, OperationLog operationLog) {
|
|
389 return new LoggingNode(newRoot, editedNode, operationLog);
|
|
390 }
|
|
391
|
2
|
392 };
|
|
393 either = editor.edit(path, e);
|
|
394 if (either.isA()) {
|
|
395 throw new IllegalStateException();
|
|
396 }
|
|
397 editor = either.b();
|
|
398 either = editor.success();
|
|
399 } while (either.isA());
|
|
400 renewTime = timestamp;
|
|
401 }
|
|
402
|
8
|
403 public void createAttribute(String _board, String _path, final String _author, final String _message, final String _editKey) {
|
2
|
404 requestCounter.incrementAndGet();
|
|
405 final long timestamp = System.currentTimeMillis();
|
|
406 final ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
407 tBuffer.putLong(timestamp);
|
|
408 JungleTree tree = jungle.getTreeByName(_board);
|
|
409 Either<Error, JungleTreeEditor> either = null;
|
|
410 DefaultNodePath path = new DefaultNodePath();
|
|
411 String[] nums = _path.split(",");
|
|
412 for (String num : nums) {
|
|
413 if (!num.equals("-1"))
|
|
414 path = path.add(Integer.parseInt(num));
|
|
415 }
|
|
416
|
|
417 do {
|
8
|
418 JungleTreeEditor editor = tree.getJungleTreeEditor();
|
2
|
419 NodeEditor e = new NodeEditor() {
|
|
420 String str;
|
|
421
|
8
|
422 Either<Error, LoggingNode> _edit(LoggingNode logNode) { str = "0";
|
2
|
423 int count = 0;
|
|
424 for (; logNode.getAttributes().get("mes" + String.valueOf(count)) != null; count++) {
|
|
425 }
|
|
426 str = String.valueOf(count);
|
|
427 logNode = logNode.getAttributes().put("mes" + str, ByteBuffer.wrap(_message.getBytes())).b();
|
|
428 logNode = logNode.getAttributes().put("timestamp" + str, tBuffer).b();
|
|
429 return DefaultEither.newB(logNode);
|
|
430 }
|
|
431
|
|
432 @Override
|
8
|
433 public Either<Error, LoggingNode> edit(TreeNode _e) {
|
|
434 LoggingNode logNode = wrap(_e);
|
|
435 return _edit(logNode);
|
2
|
436 }
|
8
|
437
|
|
438 private LoggingNode wrap(TreeNode node) {
|
|
439 return new LoggingNode(node);
|
|
440 }
|
|
441
|
|
442 @Override
|
|
443 public LoggingNode wrap(TreeNode newRoot, TreeNode editedNode, OperationLog operationLog) {
|
|
444 return new LoggingNode(newRoot, editedNode, operationLog);
|
|
445 }
|
|
446
|
2
|
447 };
|
|
448 either = editor.edit(path, e);
|
|
449 if (either.isA()) {
|
|
450 throw new IllegalStateException();
|
|
451 }
|
|
452 editor = either.b();
|
|
453 either = editor.success();
|
|
454 } while (either.isA());
|
|
455 }
|
|
456
|
|
457 public void editAttribute(String _bname, String _path, final String id, final String _message) {
|
|
458 requestCounter.incrementAndGet();
|
|
459 final long timestamp = System.currentTimeMillis();
|
|
460 final ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
461 tBuffer.putLong(timestamp);
|
|
462 JungleTree tree = jungle.getTreeByName(_bname);
|
|
463 Either<Error, JungleTreeEditor> either = null;
|
|
464 DefaultNodePath path = new DefaultNodePath();
|
|
465 String[] nums = _path.split(",");
|
|
466 for (String num : nums) {
|
|
467 if (!num.equals("-1"))
|
|
468 path = path.add(Integer.parseInt(num));
|
|
469 }
|
|
470
|
|
471 do {
|
8
|
472 JungleTreeEditor editor = tree.getJungleTreeEditor();
|
2
|
473 NodeEditor e = new NodeEditor() {
|
8
|
474 Either<Error, LoggingNode> _edit(LoggingNode logNode) {
|
2
|
475 logNode = logNode.getAttributes().put("mes" + id, ByteBuffer.wrap(_message.getBytes())).b();
|
|
476 logNode = logNode.getAttributes().put("timestamp" + id, tBuffer).b();
|
|
477 return DefaultEither.newB(logNode);
|
|
478 }
|
|
479
|
|
480 @Override
|
8
|
481 public Either<Error, LoggingNode> edit(TreeNode _e) {
|
|
482 LoggingNode logNode = wrap(_e);
|
|
483 return _edit(logNode);
|
2
|
484 }
|
8
|
485
|
|
486 private LoggingNode wrap(TreeNode node) {
|
|
487 return new LoggingNode(node);
|
|
488 }
|
|
489
|
|
490 @Override
|
|
491 public LoggingNode wrap(TreeNode newRoot, TreeNode editedNode, OperationLog operationLog) {
|
|
492 return new LoggingNode(newRoot, editedNode, operationLog);
|
|
493 }
|
|
494
|
2
|
495 };
|
|
496 either = editor.edit(path, e);
|
|
497 if (either.isA()) {
|
|
498 throw new IllegalStateException();
|
|
499 }
|
|
500 editor = either.b();
|
|
501 either = editor.success();
|
|
502 } while (either.isA());
|
|
503 }
|
|
504
|
|
505 public void deleteNode(String _board, String _path, String _id) {
|
|
506 requestCounter.incrementAndGet();
|
|
507 int id = Integer.parseInt(_id);
|
|
508 final long timestamp = System.currentTimeMillis();
|
|
509 final ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
510 tBuffer.putLong(timestamp);
|
|
511 JungleTree tree = jungle.getTreeByName(_board);
|
|
512 Either<Error, JungleTreeEditor> either = null;
|
|
513 DefaultNodePath path = new DefaultNodePath();
|
|
514 String[] nums = _path.split(",");
|
|
515 for (String num : nums) {
|
|
516 if (!num.equals("-1"))
|
|
517 path = path.add(Integer.parseInt(num));
|
|
518 }
|
|
519
|
|
520 do {
|
8
|
521 JungleTreeEditor editor = tree.getJungleTreeEditor();
|
2
|
522
|
|
523 either = editor.deleteChildAt(path, id);
|
|
524 if (either.isA()) {
|
|
525 throw new IllegalStateException();
|
|
526 }
|
|
527 editor = either.b();
|
|
528 either = editor.success();
|
|
529 } while (either.isA());
|
|
530
|
|
531 }
|
|
532
|
|
533 public void deleteAttribute(String _board, String _path, final String id) {
|
|
534 requestCounter.incrementAndGet();
|
|
535 final long timestamp = System.currentTimeMillis();
|
|
536 final ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
537 tBuffer.putLong(timestamp);
|
|
538 JungleTree tree = jungle.getTreeByName(_board);
|
|
539 Either<Error, JungleTreeEditor> either = null;
|
|
540 DefaultNodePath path = new DefaultNodePath();
|
|
541 String[] nums = _path.split(",");
|
|
542 for (String num : nums) {
|
|
543 if (!num.equals("-1"))
|
|
544 path = path.add(Integer.parseInt(num));
|
|
545 }
|
|
546
|
|
547 do {
|
8
|
548 JungleTreeEditor editor = tree.getJungleTreeEditor();
|
2
|
549 NodeEditor e = new NodeEditor() {
|
8
|
550 Either<Error, LoggingNode> _edit(LoggingNode logNode) {
|
|
551 TreeNode node = logNode.getWrap();
|
2
|
552 logNode = logNode.getAttributes().delete("mes" + id).b();
|
|
553 logNode = logNode.getAttributes().delete("timestamp" + id).b();
|
|
554 int count = Integer.parseInt(id);
|
|
555 for (; logNode.getAttributes().get("mes" + String.valueOf(count + 1)) != null; ) {
|
8
|
556 logNode = logNode.getAttributes().put("mes" + count, node.getAttributes().get("mes" + String.valueOf(count + 1))).b();
|
2
|
557 logNode = logNode.getAttributes().put("timestamp" + count, tBuffer).b();
|
|
558 count++;
|
|
559 }
|
|
560 if (count != Integer.parseInt(id)) {
|
|
561 logNode = logNode.getAttributes().delete("timestamp" + count).b();
|
|
562 logNode = logNode.getAttributes().delete("mes" + count).b();
|
|
563 }
|
|
564
|
|
565 return DefaultEither.newB(logNode);
|
|
566 }
|
|
567
|
|
568 @Override
|
8
|
569 public Either<Error, LoggingNode> edit(TreeNode _e) {
|
|
570 LoggingNode logNode = wrap(_e);
|
|
571 return _edit(logNode);
|
2
|
572 }
|
8
|
573
|
|
574 private LoggingNode wrap(TreeNode node) {
|
|
575 return new LoggingNode(node);
|
|
576 }
|
|
577
|
|
578 @Override
|
|
579 public LoggingNode wrap(TreeNode newRoot, TreeNode editedNode, OperationLog operationLog) {
|
|
580 return new LoggingNode(newRoot, editedNode, operationLog);
|
|
581 }
|
|
582
|
2
|
583 };
|
|
584 either = editor.edit(path, e);
|
|
585 if (either.isA()) {
|
|
586 throw new IllegalStateException();
|
|
587 }
|
|
588 editor = either.b();
|
|
589 either = editor.success();
|
|
590 } while (either.isA());
|
|
591 }
|
|
592
|
8
|
593 public void editMatrixMessage(String _board, String _uuid, final String _author, final String _message, final String _editKey) {
|
2
|
594 requestCounter.incrementAndGet();
|
|
595 final long timestamp = System.currentTimeMillis();
|
|
596 final ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
597 tBuffer.putLong(timestamp);
|
|
598 JungleTree tree = jungle.getTreeByName(_board);
|
|
599 Either<Error, JungleTreeEditor> either = null;
|
|
600 do {
|
|
601 DefaultNodePath path = new DefaultNodePath();
|
|
602 path = path.add(Integer.parseInt(_uuid));
|
|
603
|
8
|
604 JungleTreeEditor editor = tree.getJungleTreeEditor();
|
2
|
605 NodeEditor e = new NodeEditor() {
|
8
|
606 Either<Error, LoggingNode> _edit(LoggingNode logNode) {
|
2
|
607 logNode = logNode.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b();
|
|
608 logNode = logNode.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b();
|
|
609 logNode = logNode.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b();
|
|
610 logNode = logNode.getAttributes().put("timestamp", tBuffer).b();
|
|
611 return DefaultEither.newB(logNode);
|
|
612 }
|
|
613
|
|
614 @Override
|
8
|
615 public Either<Error, LoggingNode> edit(TreeNode _e) {
|
|
616 LoggingNode logNode = wrap(_e);
|
|
617 return _edit(logNode);
|
2
|
618 }
|
8
|
619
|
|
620 private LoggingNode wrap(TreeNode node) {
|
|
621 return new LoggingNode(node);
|
|
622 }
|
|
623
|
|
624 @Override
|
|
625 public LoggingNode wrap(TreeNode newRoot, TreeNode editedNode, OperationLog operationLog) {
|
|
626 return new LoggingNode(newRoot, editedNode, operationLog);
|
|
627 }
|
|
628
|
2
|
629 };
|
|
630 either = editor.edit(path, e);
|
|
631 if (either.isA()) {
|
|
632 throw new IllegalStateException();
|
|
633 }
|
|
634 editor = either.b();
|
|
635 either = editor.success();
|
|
636 } while (either.isA());
|
|
637 renewTime = timestamp;
|
|
638 }
|
|
639
|
|
640 public Iterable<BoardMessage> getFolder(String _boardName, String _nodeNum) {
|
|
641 DefaultNodePath path = new DefaultNodePath();
|
|
642 System.out.println(_nodeNum.substring(0, 1));
|
|
643 String[] nums = _nodeNum.split(",");
|
|
644 for (String num : nums) {
|
|
645 if (!num.equals("-1"))
|
|
646 path = path.add(Integer.parseInt(num));
|
|
647 }
|
|
648 JungleTree tree = jungle.getTreeByName(_boardName);
|
|
649 TreeNode node = tree.getRootNode();
|
|
650 requestCounter.incrementAndGet();
|
|
651
|
|
652 DefaultTraverser traverser = new DefaultTraverser();
|
|
653 DefaultEvaluator evaluator = new DefaultEvaluator(path);
|
|
654 Either<Error, Traversal> ret = traverser.traverse(node, evaluator);
|
|
655 if (ret.isA()) {
|
|
656 Assert.fail();
|
|
657 }
|
|
658
|
|
659 Traversal traversal = ret.b();
|
|
660 TreeNode target = traversal.destination();
|
|
661 Children chs = target.getChildren();
|
|
662
|
|
663 final AtomicInteger counter = new AtomicInteger(0);
|
|
664 IterableConverter.Converter<BoardMessage, TreeNode> converter = new IterableConverter.Converter<BoardMessage, TreeNode>() {
|
|
665 public BoardMessage conv(TreeNode _b) {
|
|
666 String uuid = Integer.toString(counter.get());
|
|
667 String message = new String(_b.getAttributes().get("mes").array());
|
|
668 counter.incrementAndGet();
|
|
669 return new BoardMessageImpl(null, message, uuid);
|
|
670 }
|
|
671 };
|
|
672 return new IterableConverter<BoardMessage, TreeNode>(chs, converter);
|
|
673 }
|
|
674
|
|
675 public boolean compare(TreeNode compareNode, String compareAttribute) {
|
|
676 String labName = compareNode.getAttributes().getString("mes");
|
|
677 if (labName.equals(compareAttribute))
|
|
678 return true;
|
|
679
|
|
680 for (int loopCount = 0; compareNode.getAttributes().getString("mes" + loopCount) != null; loopCount++) {
|
|
681 labName = compareNode.getAttributes().getString("mes" + loopCount);
|
|
682 if (labName.equals(compareAttribute))
|
|
683 return true;
|
|
684 }
|
|
685
|
|
686 return false;
|
|
687 }
|
|
688
|
|
689 public int getRequestNum() {
|
|
690 return requestCounter.get();
|
|
691 }
|
|
692
|
|
693 private static class BoardMessageImpl implements BoardMessage {
|
|
694 private final String author;
|
|
695 private final String message;
|
|
696 private final String uuid;
|
|
697
|
|
698 public BoardMessageImpl(String _author, String _message, String _uuid) {
|
|
699 author = _author;
|
|
700 message = _message;
|
|
701 uuid = _uuid;
|
|
702 }
|
|
703
|
|
704 public String getAuthor() {
|
|
705 return author;
|
|
706 }
|
|
707
|
|
708 public String getMessage() {
|
|
709 return message;
|
|
710 }
|
|
711
|
|
712 public String getUUID() {
|
|
713 return uuid;
|
|
714 }
|
|
715
|
|
716 }
|
|
717
|
|
718 public String sanitize(String str) {
|
|
719 if (str == null) {
|
|
720 return str;
|
|
721 }
|
|
722 str = str.replaceAll("&", "&");
|
|
723 str = str.replaceAll("<", "<");
|
|
724 str = str.replaceAll(">", ">");
|
|
725 str = str.replaceAll("\"", """);
|
|
726 str = str.replaceAll("'", "'");
|
|
727 return str;
|
|
728 }
|
|
729
|
8
|
730 public GetAttributeImp getAttribute(String _bname, String nodePath, String revisionStr) {
|
4
|
731 DefaultNodePath path = createNodePath(nodePath);
|
2
|
732 JungleTree tree = jungle.getTreeByName(_bname);
|
4
|
733 Either<Error, TreeNode> either = tree.getNodeOfPath(path);
|
|
734 if (either.isA())
|
|
735 return new GetAttributeImp(new DefaultTreeNode());
|
|
736 TreeNode node = either.b();
|
|
737 return new GetAttributeImp(node);
|
|
738 }
|
2
|
739
|
4
|
740 private DefaultNodePath createNodePath(String nodePath) {
|
|
741 DefaultNodePath path = new DefaultNodePath();
|
8
|
742 String[] nums = nodePath.split(",");
|
|
743 for (String num : nums) {
|
|
744 if (num.equals("-1"))
|
|
745 continue;
|
|
746 path = path.add(Integer.parseInt(num));
|
2
|
747 }
|
4
|
748 return path;
|
|
749 }
|
2
|
750
|
|
751 }
|