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