1 6 7 package com.jofti.btree; 8 9 10 import java.util.ArrayList ; 11 import java.util.Collection ; 12 import java.util.LinkedHashMap ; 13 import java.util.List ; 14 import java.util.Map ; 15 16 import com.jofti.exception.JoftiException; 17 import com.jofti.locking.LockManager; 18 import com.jofti.util.OpenHashMap; 19 20 33 public class BTOperations { 34 35 private static NodeFactory factory = NodeFactory.getInstance(); 36 37 38 private BTOperations() { 39 } 40 41 42 private static int avRangeResults = 100; 43 44 57 public static void insertValue(BTree tree, Comparable key, 58 Comparable object, int dimension) throws JoftiException { 59 60 LeafNodeEntry entry = factory.createLeafNodeEntry(key, new ValueObject(dimension, object)); 61 tree.insert(entry); 62 } 63 64 65 78 public static void insertKeyValue(BTree tree, Comparable key, 79 Map attributes, int dimension) throws JoftiException { 80 81 LeafNodeEntry entry = factory.createLeafNodeEntry(key, new KeyValueObject(dimension, key, attributes)); 82 83 tree.insert(entry); 84 } 85 86 97 public static void removeValue(BTree tree, Object uniqueId, 98 Comparable object, int dimension) throws JoftiException { 99 removeValueObject(tree, uniqueId, new ValueObject(dimension, object)); 100 } 101 102 113 114 public static void removeValueObject(BTree tree, Object uniqueId, 115 Comparable object) throws JoftiException { 116 117 LeafNodeEntry entry = factory.createLeafNodeEntry(uniqueId,object); 118 tree.removeEntry(entry); 119 } 120 121 132 public static Map match(BTree tree, Comparable obj, int dimension) throws JoftiException{ 133 return tree.matchDirect(new ValueObject(dimension, obj), null, null); 134 } 135 136 149 public static Map match(BTree tree, Comparable obj, int dimension, Object valueReturn) throws JoftiException{ 150 return tree.matchDirect(new ValueObject(dimension, obj), null, valueReturn); 151 } 152 153 164 public static Map match(BTree tree, Comparable [] obj, int dimension) throws JoftiException{ 165 return match(tree, obj, dimension,null); 166 167 } 168 169 181 public static Map match(BTree tree, Comparable [] obj, int dimension, Object alias) throws JoftiException{ 182 183 OpenHashMap map = null; 184 for (int i =obj.length-1;i>=0;i--){ 185 map = tree.matchDirect(new ValueObject(dimension, obj[i]), map,alias); 186 } 187 return map; 188 } 189 190 198 public static Collection getKeyAttributes(BTree tree, Comparable obj, 199 int dimension) throws JoftiException { 200 201 return tree.getAttributesDirect(new ValueObject(dimension, obj)); 202 } 203 204 205 206 216 public static boolean contains(BTree tree, Comparable obj, int dimension) 217 throws JoftiException { 218 return tree.containsDirect(new ValueObject(dimension, obj)); 219 220 } 221 222 223 224 private static INode getContainingNode(BTree tree, Comparable obj, 225 int dimension) throws JoftiException { 226 227 ValueObject temp = new ValueObject(dimension, obj); 229 INode result = (INode) tree.search(temp); 230 if (result == null) { 231 return null; 232 } 233 LeafNodeEntry val = ((IResultNode) result).getEntry(temp); 235 if (val == null) { 236 return null; 237 } else { 238 return ((ResultNode) result).getDelegate(); 239 } 240 } 241 242 254 public static Collection getAllValuesForKey(BTree tree, Comparable obj, 255 int startDimension) throws JoftiException { 256 INode result = getContainingNode(tree, obj, startDimension); 258 if (result != null) { 259 Collection col = getAllValuesInTree(tree, result, obj, 261 startDimension); 262 return col; 263 264 } else { 265 return new ArrayList (); 267 } 268 } 269 270 283 284 private static Collection getAllValuesInTree(BTree tree, INode realNode, 285 Comparable obj, int startDimension) throws JoftiException { 286 287 List resultList = new ArrayList (); 288 290 if (realNode != null) { 291 292 try { 294 boolean foundInDimension = false; 295 boolean dimensionEnd = false; 296 LockManager.acquireLock((Node) realNode, LockManager.READ_LOCK); 297 boolean notEnd = true; 298 299 while (notEnd) { 300 Object [] entries = realNode.getEntries(); 301 for (int i = 0; i < entries.length; i++) { 302 LeafNodeEntry entry = (LeafNodeEntry) entries[i]; 303 304 if (entry != BTree.MAX_LEAF_ENTRY) { 305 if (entry.getUniqueIdSet().contains(obj) 306 && ((ValueObject) entry.getValue()) 307 .getDimension() == startDimension) { 308 309 resultList.add(entry.getValue()); 310 foundInDimension = true; 311 if (entries[entries.length - 1] != BTree.MAX_LEAF_ENTRY) { 312 break; 313 } 314 } else if (((ValueObject) entry.getValue()) 315 .getDimension() > startDimension) { 316 317 dimensionEnd = true; 318 break; 319 } 320 321 } else { 322 notEnd = false; 323 324 } 325 } 326 if (notEnd) { 327 INode nextNode = null; 328 if (foundInDimension || dimensionEnd) { 329 330 nextNode = getLowestNodeForDimension(tree, 331 ++startDimension); 332 333 foundInDimension = false; 334 dimensionEnd = false; 335 } else { 336 nextNode = realNode.getLinkNode().getNode(); 337 338 } 339 340 LockManager.releaseLock((Node) realNode, 341 LockManager.READ_LOCK); 342 LockManager.acquireLock((Node) nextNode, 343 LockManager.READ_LOCK); 344 345 realNode = nextNode; 346 347 } 348 } 349 350 } finally { 351 LockManager.releaseLock((Node) realNode, LockManager.READ_LOCK); 352 353 } 354 } 355 356 return (Collection ) resultList; 357 358 } 359 360 368 public static Map getAllResultsForDimension(BTree tree, int dimension) 369 throws JoftiException { 370 371 return range(tree, ValueObject.MIN_COMAPARBLE_VALUE, 372 ValueObject.MAX_COMAPARBLE_VALUE, dimension, true); 373 374 } 375 376 384 public static INode getLowestNodeForDimension(BTree tree, int dimension) 385 throws JoftiException { 386 ValueObject temp = new ValueObject(dimension, 387 ValueObject.MIN_COMAPARBLE_VALUE); 388 389 IResultNode result = (IResultNode) tree.search(temp); 390 391 return ((ResultNode) result).getDelegate(); 392 } 393 394 402 public static INode getHighestNodeForDimension(BTree tree, int dimension) 403 throws JoftiException { 404 ValueObject temp = new ValueObject(dimension, 405 ValueObject.MAX_COMAPARBLE_VALUE); 406 407 IResultNode result = (IResultNode) tree.search(temp); 408 409 return ((ResultNode) result).getDelegate(); 410 } 411 412 425 public static Map range(BTree tree, Comparable startObj, Comparable endObj, 426 int dimension, boolean inclusive) throws JoftiException { 427 428 return rangeArray(tree, startObj,endObj,dimension,dimension, inclusive, null); 429 } 430 431 445 public static Map range(BTree tree, Comparable startObj, Comparable endObj, 446 int dimension, boolean inclusive, Object alias) throws JoftiException { 447 448 return rangeArray(tree, startObj,endObj,dimension,dimension, inclusive, alias); 449 } 450 451 452 453 454 private static Map rangeArray(BTree tree, Comparable startObj, 455 Comparable endObj, int dimension, int endDimension, 456 boolean inclusive, Object valueReturn) throws JoftiException { 457 458 final OpenHashMap map = new OpenHashMap(avRangeResults * 2, 0.00f, 0.5f); 461 462 if (startObj == null) { 463 startObj = ValueObject.MIN_COMAPARBLE_VALUE; 464 } 465 if (endObj == null) { 466 endObj = ValueObject.MAX_COMAPARBLE_VALUE; 467 } 468 469 ValueObject startRange = new ValueObject(dimension, startObj); 470 ValueObject finishRange = new ValueObject(endDimension, endObj); 471 472 IResultNode startNode = (IResultNode) tree.search(startRange); 474 475 boolean end = false; 476 477 while (!end) { 478 Object [] entries = startNode.getEntries(); 479 480 int tempLength = entries.length; 481 482 if (entries[tempLength - 1] != BTree.MAX_LEAF_ENTRY 484 && ((LeafNodeEntry) entries[0]).value.compareTo(startRange) > 0 485 && ((LeafNodeEntry) entries[tempLength - 1]).value 486 .compareTo(finishRange) < 0) { 487 for (int i = 0; i < tempLength; i++) { 488 LeafNodeEntry entry = (LeafNodeEntry) entries[i]; 489 490 Object [] tempSet = entry.uniqueIdSet.toArray(); 491 final int length = tempSet.length; 492 map.ensureCapacity(map.size() + length); 493 494 for (int j = length - 1; j >= 0; j--) { 496 map.put(tempSet[j], valueReturn); 497 } 498 } 499 } else { 500 for (int i = 0; i < tempLength; i++) { 503 LeafNodeEntry entry = (LeafNodeEntry) entries[i]; 504 if (entry != BTree.MAX_LEAF_ENTRY) { 505 506 if (inclusive && entry.value.compareTo(startRange) >= 0 507 && entry.value.compareTo(finishRange) <= 0) { 508 509 Object [] tempSet = entry.uniqueIdSet.toArray(); 510 final int length = tempSet.length; 511 map.ensureCapacity(map.size() + length); 512 513 for (int j = length - 1; j >= 0; j--) { 515 map.put(tempSet[j], valueReturn); 516 } 517 518 } else if ((!inclusive) 519 && entry.value.compareTo(startRange) > 0 520 && entry.value.compareTo(finishRange) < 0) { 521 522 Object [] tempSet = entry.uniqueIdSet.toArray(); 523 final int length = tempSet.length; 524 525 map.ensureCapacity(map.size() + length); 526 527 for (int j = length - 1; j >= 0; j--) { 529 map.put(tempSet[j], valueReturn); 530 } 531 532 } else if (entry.value.compareTo(finishRange) > 0) { 533 535 end = true; 536 break; 537 538 } 539 } else { 540 end = true; 541 break; 542 } 543 544 } 545 } 546 if (!end) { 547 548 INode tempNode = ((ResultNode) startNode).getDelegate(); 549 550 LockManager.acquireLock((Node) tempNode, LockManager.READ_LOCK); 553 554 try { 556 startNode = (IResultNode) startNode.getLinkNode().getNode(); 557 558 } finally { 559 560 LockManager.releaseLock((Node) tempNode, 563 LockManager.READ_LOCK); 564 } 565 566 } 567 } 568 569 avRangeResults = (avRangeResults + map.size()) / 2; 571 return map; 572 573 } 574 575 576 577 591 public static Map getSubTreeKeyValues(BTree tree, Comparable startObj, Comparable endObj, 592 int dimension, int endDimension, boolean inclusive) throws JoftiException { 593 Map map = new LinkedHashMap (); 594 595 if (startObj == null) { 596 startObj = ValueObject.MIN_COMAPARBLE_VALUE; 597 } 598 if (endObj == null) { 599 endObj = ValueObject.MAX_COMAPARBLE_VALUE; 600 } 601 602 ValueObject startRange = new ValueObject(dimension, startObj); 603 ValueObject finishRange = new ValueObject(endDimension, endObj); 604 605 IResultNode startNode = (IResultNode) tree.search(startRange); 606 607 608 boolean end = false; 609 610 while (!end) { 611 Object [] entries = startNode.getEntries(); 612 for (int i = 0; i < entries.length; i++) { 614 LeafNodeEntry entry = (LeafNodeEntry) entries[i]; 615 if (entry != BTree.MAX_LEAF_ENTRY) { 616 617 if (inclusive && entry.value.compareTo(startRange) >= 0 618 && entry.value.compareTo(finishRange) <= 0) { 619 620 Object [] tempSet = entry.getUniqueIdSet().toArray(); 621 for (int j=0;j<tempSet.length;j++) { 623 if (entry.getValue() instanceof ValueObject){ 624 Map tmpMap = getMapForDimension(map, new Integer (((ValueObject)entry.getValue()).dimension)); 625 tmpMap.put(tempSet[j], entry.value); 626 } 627 } 628 629 } else if ((!inclusive) 630 && entry.value.compareTo(startRange) > 0 631 && entry.value.compareTo(finishRange) < 0) { 632 633 Object [] tempSet = entry.getUniqueIdSet().toArray(); 634 for (int j=0;j<tempSet.length;j++) { 636 if (entry.getValue() instanceof ValueObject){ 637 Map tmpMap = getMapForDimension(map, new Integer (((ValueObject)entry.getValue()).dimension)); 638 639 tmpMap.put(tempSet[j], entry.value); 640 } 641 } 642 643 } else if (entry.value.compareTo(finishRange) > 0) { 644 646 end = true; 647 break; 648 649 } 650 } else { 651 end = true; 652 break; 653 } 654 655 } 656 if (!end) { 657 658 INode tempNode = ((ResultNode)startNode).getDelegate(); 659 LockManager.acquireLock((Node) tempNode, LockManager.READ_LOCK); 660 661 try{ 662 startNode = (IResultNode)startNode.getLinkNode().getNode(); 663 664 }finally{ 665 666 LockManager.releaseLock((Node) tempNode, 668 LockManager.READ_LOCK); 669 } 670 671 } 672 } 673 674 return map; 675 676 } 677 678 private static Map getMapForDimension(Map resultMap, Integer dimension){ 679 680 Map tempMap = (Map )resultMap.get(dimension); 681 if (tempMap == null){ 682 tempMap = new LinkedHashMap (); 683 resultMap.put(dimension, tempMap); 684 } 685 return tempMap; 686 687 } 688 689 690 691 700 public static Map notEqual(BTree tree, Comparable obj, int dimension, Object valueReturn) 701 throws JoftiException { 702 OpenHashMap map = new OpenHashMap(avRangeResults*2,0.00f,0.5f); 703 704 Comparable start = ValueObject.MIN_COMAPARBLE_VALUE; 705 706 Comparable endObj = ValueObject.MAX_COMAPARBLE_VALUE; 707 708 ValueObject startRange = new ValueObject(dimension, start); 709 ValueObject finishRange = new ValueObject(dimension, endObj); 710 711 ValueObject actualValue = new ValueObject(dimension, obj); 712 713 IResultNode startNode = (IResultNode) tree.search(startRange); 714 INode realNode = ((ResultNode) startNode).getDelegate(); 715 716 try { 717 LockManager.acquireLock((Node) realNode, LockManager.READ_LOCK); 718 719 boolean end = false; 720 721 while (!end) { 722 Object [] entries = realNode.getEntries(); 723 724 for (int i = 0; i < entries.length; i++) { 725 LeafNodeEntry entry = (LeafNodeEntry) entries[i]; 726 if (entry != BTree.MAX_LEAF_ENTRY) { 727 Comparable tempObj = entry.value; 728 if (tempObj.compareTo(startRange) >= 0 729 && tempObj.compareTo(finishRange) <= 0) { 730 731 if (tempObj.compareTo(actualValue) != 0) { 732 733 734 Object [] tempEntrySet = entry.getUniqueIdSet().toArray(); 735 736 map.ensureCapacity(map.size() + tempEntrySet.length); 737 Object retVal = valueReturn != null ?valueReturn : null ; 738 739 740 for (int j=0;j<tempEntrySet.length;j++) { 741 map.put(tempEntrySet[j], retVal); 742 } 743 } 744 745 } else if (tempObj.compareTo(finishRange) > 0) { 746 748 end = true; 749 break; 750 751 } 752 753 } else { 754 end = true; 755 break; 756 } 757 758 } 759 if (!end) { 760 761 INode nextNode = realNode.getLinkNode().getNode(); 762 763 LockManager.releaseLock((Node) realNode, 765 LockManager.READ_LOCK); 766 LockManager.acquireLock((Node) nextNode, 767 LockManager.READ_LOCK); 768 769 realNode = nextNode; 770 } 771 } 772 } finally { 773 LockManager.releaseLock((Node) realNode, LockManager.READ_LOCK); 774 } 775 return map; 776 777 } 778 779 } | Popular Tags |