comparison src/main/java/app/bbs/NetworkJungleBulletinBoard.java @ 179:d6944d788121

fit Junglecore
author tatsuki
date Thu, 04 Sep 2014 13:12:25 +0900
parents 6f104ab4eb81
children 2828205bdc3a
comparison
equal deleted inserted replaced
178:89db8388db0f 179:d6944d788121
123 } 123 }
124 124
125 public Iterable<String> getBoards() { 125 public Iterable<String> getBoards() {
126 JungleTree tree = jungle.getTreeByName("boards"); 126 JungleTree tree = jungle.getTreeByName("boards");
127 TreeNode node = tree.getRootNode(); 127 TreeNode node = tree.getRootNode();
128 Children<TreeNode> chs = node.getChildren(); 128 Children chs = node.getChildren();
129 129
130 IterableConverter.Converter<String, TreeNode> converter = new IterableConverter.Converter<String, TreeNode>() { 130 IterableConverter.Converter<String, TreeNode> converter = new IterableConverter.Converter<String, TreeNode>() {
131 public String conv(TreeNode _b) { 131 public String conv(TreeNode _b) {
132 ByteBuffer e = _b.getAttributes().get("name"); 132 ByteBuffer e = _b.getAttributes().get("name");
133 System.out.println(new String(e.array())); 133 System.out.println(new String(e.array()));
184 editor = either.b(); 184 editor = either.b();
185 185
186 NodeEditor e = new NodeEditor() { 186 NodeEditor e = new NodeEditor() {
187 ByteBuffer tBuffer2 = ByteBuffer.allocate(16); 187 ByteBuffer tBuffer2 = ByteBuffer.allocate(16);
188 188
189 public <T extends TreeNode> Either<Error, TreeNode> edit(T _e) { 189 public Either<Error, TreeNode> edit(TreeNode node) {
190 TreeNode node = _e;
191 node = node.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b(); 190 node = node.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b();
192 node = node.getAttributes().put("mes", ByteBuffer.wrap(_initMessage.getBytes())).b(); 191 node = node.getAttributes().put("mes", ByteBuffer.wrap(_initMessage.getBytes())).b();
193 node = node.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b(); 192 node = node.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b();
194 tBuffer2.putLong(timestamp); 193 tBuffer2.putLong(timestamp);
195 node = node.getAttributes().put("timestamp", tBuffer2).b(); 194 node = node.getAttributes().put("timestamp", tBuffer2).b();
196 return DefaultEither.newB(node); 195 return DefaultEither.newB(node);
197 } 196 }
198 197
199 @Override
200 public OperationLog getLog() {
201 OperationLog op = new DefaultOperationLog();
202 op = op.add(new PutAttributeOperation("author", ByteBuffer.wrap(_author.getBytes())));
203 op = op.add(new PutAttributeOperation("mes", ByteBuffer.wrap(_initMessage.getBytes())));
204 op = op.add(new PutAttributeOperation("key", ByteBuffer.wrap(_editKey.getBytes())));
205 op = op.add(new PutAttributeOperation("timestamp", tBuffer2));
206 return op;
207 }
208 }; 198 };
209 199
210 either = editor.edit(root.add(0), e); 200 either = editor.edit(root.add(0), e);
211 if (either.isA()) { 201 if (either.isA()) {
212 throw new IllegalStateException(); 202 throw new IllegalStateException();
240 TreeNode node = tree.getRootNode(); 230 TreeNode node = tree.getRootNode();
241 DefaultTraverser traverser = new DefaultTraverser(); 231 DefaultTraverser traverser = new DefaultTraverser();
242 // TraversableNodeWrapper<Node> traversable = new 232 // TraversableNodeWrapper<Node> traversable = new
243 // TraversableNodeWrapper<Node>(node); 233 // TraversableNodeWrapper<Node>(node);
244 DefaultEvaluator evaluator = new DefaultEvaluator(path); 234 DefaultEvaluator evaluator = new DefaultEvaluator(path);
245 Either<Error, Traversal<TreeNode>> ret = traverser.traverse(node, 235 Either<Error, Traversal> ret = traverser.traverse(node,
246 evaluator); 236 evaluator);
247 if (ret.isA()) { 237 if (ret.isA()) {
248 Assert.fail(); 238 Assert.fail();
249 } 239 }
250 240
251 Traversal<TreeNode> traversal = ret.b(); 241 Traversal traversal = ret.b();
252 TreeNode target = traversal.destination(); 242 TreeNode target = traversal.destination();
253 int size = target.getChildren().size(); 243 int size = target.getChildren().size();
254 JungleTreeEditor editor = tree.getTreeEditor(); 244 JungleTreeEditor editor = tree.getTreeEditor();
255 either = editor.addNewChildAt(path, size); 245 either = editor.addNewChildAt(path, size);
256 if (either.isA()) { 246 if (either.isA()) {
258 } 248 }
259 editor = either.b(); 249 editor = either.b();
260 250
261 NodeEditor e = new NodeEditor() { 251 NodeEditor e = new NodeEditor() {
262 252
263 public <T extends TreeNode> Either<Error, TreeNode> edit(T _e) { 253 public Either<Error, TreeNode> edit(TreeNode node) {
264 TreeNode node = _e;
265 node = node.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b(); 254 node = node.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b();
266 node = node.getAttributes().put("timestamp", tBuffer).b(); 255 node = node.getAttributes().put("timestamp", tBuffer).b();
267 return DefaultEither.newB(node); 256 return DefaultEither.newB(node);
268 } 257 }
269 258
270 @Override
271 public OperationLog getLog() {
272 OperationLog op = new DefaultOperationLog();
273 op = op.add(new PutAttributeOperation("mes", ByteBuffer.wrap(_message.getBytes())));
274 op = op.add(new PutAttributeOperation("timestamp", tBuffer));
275 return op;
276 }
277 }; 259 };
278 path = path.add(size); 260 path = path.add(size);
279 either = editor.edit(path, e); 261 either = editor.edit(path, e);
280 if (either.isA()) { 262 if (either.isA()) {
281 throw new IllegalStateException(); 263 throw new IllegalStateException();
309 throw new IllegalStateException(); 291 throw new IllegalStateException();
310 } 292 }
311 editor = either.b(); 293 editor = either.b();
312 294
313 NodeEditor e = new NodeEditor() { 295 NodeEditor e = new NodeEditor() {
314 public <T extends TreeNode> Either<Error, TreeNode> edit(T _e) { 296 public Either<Error, TreeNode> edit(TreeNode node) {
315 TreeNode node = _e;
316 node = node.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b(); 297 node = node.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b();
317 node = node.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b(); 298 node = node.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b();
318 node = node.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b(); 299 node = node.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b();
319 node = node.getAttributes().put("timestamp", tBuffer).b(); 300 node = node.getAttributes().put("timestamp", tBuffer).b();
320 return DefaultEither.newB(node); 301 return DefaultEither.newB(node);
321 } 302 }
322 303
323 @Override
324 public OperationLog getLog() {
325 OperationLog op = new DefaultOperationLog();
326 op = op.add(new PutAttributeOperation("author", ByteBuffer.wrap(_author.getBytes())));
327 op = op.add(new PutAttributeOperation("mes", ByteBuffer.wrap(_message.getBytes())));
328 op = op.add(new PutAttributeOperation("key", ByteBuffer.wrap(_editKey.getBytes())));
329 op = op.add(new PutAttributeOperation("timestamp", tBuffer));
330 return op;
331 }
332 }; 304 };
333 path = path.add(size); 305 path = path.add(size);
334 either = editor.edit(path, e); 306 either = editor.edit(path, e);
335 if (either.isA()) { 307 if (either.isA()) {
336 throw new IllegalStateException(); 308 throw new IllegalStateException();
358 } 330 }
359 } catch (Exception _e) { 331 } catch (Exception _e) {
360 } 332 }
361 JungleTreeEditor editor = tree.getTreeEditor(); 333 JungleTreeEditor editor = tree.getTreeEditor();
362 NodeEditor e = new NodeEditor() { 334 NodeEditor e = new NodeEditor() {
363 public <T extends TreeNode> Either<Error, TreeNode> edit(T _e) { 335 public Either<Error, TreeNode> edit(TreeNode node) {
364 TreeNode node = _e;
365 System.out.println(new String(node.getAttributes().get("mes").array())); 336 System.out.println(new String(node.getAttributes().get("mes").array()));
366 node = node.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b(); 337 node = node.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b();
367 node = node.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b(); 338 node = node.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b();
368 node = node.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b(); 339 node = node.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b();
369 node = node.getAttributes().put("timestamp", tBuffer).b(); 340 node = node.getAttributes().put("timestamp", tBuffer).b();
370 System.out.println(new String(node.getAttributes().get("mes").array())); 341 System.out.println(new String(node.getAttributes().get("mes").array()));
371 return DefaultEither.newB(node); 342 return DefaultEither.newB(node);
372 } 343 }
373 344
374 @Override
375 public OperationLog getLog() {
376 OperationLog op = new DefaultOperationLog();
377 op = op.add(new PutAttributeOperation("author", ByteBuffer.wrap(_author.getBytes())));
378 op = op.add(new PutAttributeOperation("mes", ByteBuffer.wrap(_message.getBytes())));
379 op = op.add(new PutAttributeOperation("key", ByteBuffer.wrap(_editKey.getBytes())));
380 op = op.add(new PutAttributeOperation("timestamp", tBuffer));
381 return op;
382 }
383 }; 345 };
384 either = editor.edit(path, e); 346 either = editor.edit(path, e);
385 if (either.isA()) { 347 if (either.isA()) {
386 throw new IllegalStateException(); 348 throw new IllegalStateException();
387 } 349 }
411 373
412 JungleTreeEditor editor = tree.getTreeEditor(); 374 JungleTreeEditor editor = tree.getTreeEditor();
413 NodeEditor e = new NodeEditor() { 375 NodeEditor e = new NodeEditor() {
414 String str; 376 String str;
415 377
416 public <T extends TreeNode> Either<Error, TreeNode> edit(T _e) { 378 public Either<Error, TreeNode> edit(TreeNode node) {
417 TreeNode node = _e;
418 str = "0"; 379 str = "0";
419 int count = 0; 380 int count = 0;
420 for (; _e.getAttributes().get("mes" + String.valueOf(count)) != null; count++) { 381 for (; node.getAttributes().get("mes" + String.valueOf(count)) != null; count++) {
421 } 382 }
422 str = String.valueOf(count); 383 str = String.valueOf(count);
423 node = node.getAttributes().put("mes" + str,ByteBuffer.wrap(_message.getBytes())).b(); 384 node = node.getAttributes().put("mes" + str,ByteBuffer.wrap(_message.getBytes())).b();
424 node = node.getAttributes().put("timestamp" + str, tBuffer).b(); 385 node = node.getAttributes().put("timestamp" + str, tBuffer).b();
425 return DefaultEither.newB(node); 386 return DefaultEither.newB(node);
426 } 387 }
427 388
428 @Override
429 public OperationLog getLog() {
430 OperationLog op = new DefaultOperationLog();
431 op = op.add(new PutAttributeOperation("mes" + str,ByteBuffer.wrap(_message.getBytes())));
432 op = op.add(new PutAttributeOperation("timestamp" + str,tBuffer));
433 return op;
434 }
435 }; 389 };
436 either = editor.edit(path, e); 390 either = editor.edit(path, e);
437 if (either.isA()) { 391 if (either.isA()) {
438 throw new IllegalStateException(); 392 throw new IllegalStateException();
439 } 393 }
460 } catch (Exception _e) { 414 } catch (Exception _e) {
461 } 415 }
462 416
463 JungleTreeEditor editor = tree.getTreeEditor(); 417 JungleTreeEditor editor = tree.getTreeEditor();
464 NodeEditor e = new NodeEditor() { 418 NodeEditor e = new NodeEditor() {
465 public <T extends TreeNode> Either<Error, TreeNode> edit(T _e) { 419 public Either<Error, TreeNode> edit(TreeNode node) {
466 TreeNode node = _e;
467 // EnableNodeWrapper<T> node = _e.getWrap(); 420 // EnableNodeWrapper<T> node = _e.getWrap();
468 node = node.getAttributes().put("mes" + id,ByteBuffer.wrap(_message.getBytes())).b(); 421 node = node.getAttributes().put("mes" + id,ByteBuffer.wrap(_message.getBytes())).b();
469 node = node.getAttributes().put("timestamp" + id, tBuffer).b(); 422 node = node.getAttributes().put("timestamp" + id, tBuffer).b();
470 return DefaultEither.newB(node); 423 return DefaultEither.newB(node);
471 } 424 }
472 425
473 @Override
474 public OperationLog getLog() {
475 OperationLog op = new DefaultOperationLog();
476 op = op.add(new PutAttributeOperation("mes"+ id, ByteBuffer.wrap(_message.getBytes())));
477 op = op.add(new PutAttributeOperation("timestamp"+ id, tBuffer));
478 return op;
479 }
480 }; 426 };
481 either = editor.edit(path, e); 427 either = editor.edit(path, e);
482 if (either.isA()) { 428 if (either.isA()) {
483 throw new IllegalStateException(); 429 throw new IllegalStateException();
484 } 430 }
536 System.out.println("屑"); 482 System.out.println("屑");
537 } 483 }
538 484
539 JungleTreeEditor editor = tree.getTreeEditor(); 485 JungleTreeEditor editor = tree.getTreeEditor();
540 NodeEditor e = new NodeEditor() { 486 NodeEditor e = new NodeEditor() {
541 public <T extends TreeNode> Either<Error, TreeNode> edit(T _e) { 487 public Either<Error, TreeNode> edit(TreeNode node) {
542 TreeNode node = _e;
543 node = node.getAttributes().delete("mes" + id).b(); 488 node = node.getAttributes().delete("mes" + id).b();
544 node = node.getAttributes().delete("timestamp" + id).b(); 489 node = node.getAttributes().delete("timestamp" + id).b();
545 int count = Integer.parseInt(id); 490 int count = Integer.parseInt(id);
546 for (; node.getAttributes().get("mes" + String.valueOf(count + 1)) != null;) { 491 for (; node.getAttributes().get("mes" + String.valueOf(count + 1)) != null;) {
547 node = node.getAttributes().put("mes" + count,_e.getAttributes().get("mes"+ String.valueOf(count + 1))).b(); 492 node = node.getAttributes().put("mes" + count,node.getAttributes().get("mes"+ String.valueOf(count + 1))).b();
548 node = node.getAttributes().put("timestamp" + count, tBuffer).b(); 493 node = node.getAttributes().put("timestamp" + count, tBuffer).b();
549 count++; 494 count++;
550 } 495 }
551 if (count != Integer.parseInt(id)) { 496 if (count != Integer.parseInt(id)) {
552 node = node.getAttributes().delete("timestamp" + count).b(); 497 node = node.getAttributes().delete("timestamp" + count).b();
554 } 499 }
555 500
556 return DefaultEither.newB(node); 501 return DefaultEither.newB(node);
557 } 502 }
558 503
559 @Override
560 public OperationLog getLog() {
561 return new DefaultOperationLog();
562 }
563 }; 504 };
564 either = editor.edit(path, e); 505 either = editor.edit(path, e);
565 if (either.isA()) { 506 if (either.isA()) {
566 throw new IllegalStateException(); 507 throw new IllegalStateException();
567 } 508 }
582 DefaultNodePath path = new DefaultNodePath(); 523 DefaultNodePath path = new DefaultNodePath();
583 path = path.add(Integer.parseInt(_uuid)); 524 path = path.add(Integer.parseInt(_uuid));
584 525
585 JungleTreeEditor editor = tree.getTreeEditor(); 526 JungleTreeEditor editor = tree.getTreeEditor();
586 NodeEditor e = new NodeEditor() { 527 NodeEditor e = new NodeEditor() {
587 public <T extends TreeNode> Either<Error, TreeNode> edit(T _e) { 528 public Either<Error, TreeNode> edit(TreeNode node) {
588 TreeNode node = _e;
589 node = node.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b(); 529 node = node.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b();
590 node = node.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b(); 530 node = node.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b();
591 node = node.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b(); 531 node = node.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b();
592 node = node.getAttributes().put("timestamp", tBuffer).b(); 532 node = node.getAttributes().put("timestamp", tBuffer).b();
593 return DefaultEither.newB(node); 533 return DefaultEither.newB(node);
594 } 534 }
595
596 @Override
597 public OperationLog getLog() {
598 OperationLog op = new DefaultOperationLog();
599 op = op.add(new PutAttributeOperation("author", ByteBuffer.wrap(_author.getBytes())));
600 op = op.add(new PutAttributeOperation("mes", ByteBuffer.wrap(_message.getBytes())));
601 op = op.add(new PutAttributeOperation("key", ByteBuffer.wrap(_editKey.getBytes())));
602 op = op.add(new PutAttributeOperation("timestamp", tBuffer));
603 return op;
604 }
605 }; 535 };
606 either = editor.edit(path, e); 536 either = editor.edit(path, e);
607 if (either.isA()) { 537 if (either.isA()) {
608 throw new IllegalStateException(); 538 throw new IllegalStateException();
609 } 539 }
615 545
616 public Iterable<BoardMessage> getMessages(String _boardName) { 546 public Iterable<BoardMessage> getMessages(String _boardName) {
617 requestCounter.incrementAndGet(); 547 requestCounter.incrementAndGet();
618 JungleTree tree = jungle.getTreeByName(_boardName); 548 JungleTree tree = jungle.getTreeByName(_boardName);
619 TreeNode node = tree.getRootNode(); 549 TreeNode node = tree.getRootNode();
620 Children<TreeNode> chs = node.getChildren(); 550 Children chs = node.getChildren();
621 final AtomicInteger counter = new AtomicInteger(0); 551 final AtomicInteger counter = new AtomicInteger(0);
622 IterableConverter.Converter<BoardMessage, TreeNode> converter = new IterableConverter.Converter<BoardMessage, TreeNode>() { 552 IterableConverter.Converter<BoardMessage, TreeNode> converter = new IterableConverter.Converter<BoardMessage, TreeNode>() {
623 public BoardMessage conv(TreeNode _b) { 553 public BoardMessage conv(TreeNode _b) {
624 String uuid = Integer.toString(counter.get()); 554 String uuid = Integer.toString(counter.get());
625 String author = new String(_b.getAttributes().get("author").array()); 555 String author = new String(_b.getAttributes().get("author").array());
647 577
648 DefaultTraverser traverser = new DefaultTraverser(); 578 DefaultTraverser traverser = new DefaultTraverser();
649 // TraversableNodeWrapper<Node> traversable = new 579 // TraversableNodeWrapper<Node> traversable = new
650 // TraversableNodeWrapper<Node>(node); 580 // TraversableNodeWrapper<Node>(node);
651 DefaultEvaluator evaluator = new DefaultEvaluator(path); 581 DefaultEvaluator evaluator = new DefaultEvaluator(path);
652 Either<Error, Traversal<TreeNode>> ret = traverser.traverse(node, 582 Either<Error, Traversal> ret = traverser.traverse(node,
653 evaluator); 583 evaluator);
654 if (ret.isA()) { 584 if (ret.isA()) {
655 Assert.fail(); 585 Assert.fail();
656 } 586 }
657 587
658 Traversal<TreeNode> traversal = ret.b(); 588 Traversal traversal = ret.b();
659 TreeNode target = traversal.destination(); 589 TreeNode target = traversal.destination();
660 Children<TreeNode> chs = target.getChildren(); 590 Children chs = target.getChildren();
661 591
662 final AtomicInteger counter = new AtomicInteger(0); 592 final AtomicInteger counter = new AtomicInteger(0);
663 IterableConverter.Converter<BoardMessage, TreeNode> converter = new IterableConverter.Converter<BoardMessage, TreeNode>() { 593 IterableConverter.Converter<BoardMessage, TreeNode> converter = new IterableConverter.Converter<BoardMessage, TreeNode>() {
664 public BoardMessage conv(TreeNode _b) { 594 public BoardMessage conv(TreeNode _b) {
665 String uuid = Integer.toString(counter.get()); 595 String uuid = Integer.toString(counter.get());
689 619
690 DefaultTraverser traverser = new DefaultTraverser(); 620 DefaultTraverser traverser = new DefaultTraverser();
691 // TraversableNodeWrapper<Node> traversable = new 621 // TraversableNodeWrapper<Node> traversable = new
692 // TraversableNodeWrapper<Node>(node); 622 // TraversableNodeWrapper<Node>(node);
693 DefaultEvaluator evaluator = new DefaultEvaluator(path); 623 DefaultEvaluator evaluator = new DefaultEvaluator(path);
694 Either<Error, Traversal<TreeNode>> ret = traverser.traverse(node, 624 Either<Error, Traversal> ret = traverser.traverse(node,
695 evaluator); 625 evaluator);
696 if (ret.isA()) { 626 if (ret.isA()) {
697 Assert.fail(); 627 Assert.fail();
698 } 628 }
699 629
700 Traversal<TreeNode> traversal = ret.b(); 630 Traversal traversal = ret.b();
701 TreeNode target = traversal.destination(); 631 TreeNode target = traversal.destination();
702 return new getAttributeImp(target); 632 return new getAttributeImp(target);
703 } 633 }
704 634
705 public int getRequestNum() { 635 public int getRequestNum() {