1 package com.daffodilwoods.daffodildb.server.datasystem.btree; 2 3 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem.*; 4 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*; 5 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.*; 6 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 7 import com.daffodilwoods.daffodildb.utils.*; 8 import com.daffodilwoods.daffodildb.utils.byteconverter.*; 9 import com.daffodilwoods.daffodildb.utils.comparator.*; 10 import com.daffodilwoods.database.resource.*; 11 import com.daffodilwoods.database.utility.*; 12 13 22 23 public class BTree 24 implements _Index { 25 26 29 30 private SuperComparator comparator; 31 32 35 36 private _IndexInformation indexInformation; 37 38 41 42 private boolean[] orderType; 43 44 47 48 public boolean DUPLICATEKEYSALLOWED = true; 49 50 53 54 private _BTreeCharacteristics btreeCharacteristics; 55 56 59 60 private _NodeManager nodeManager; 61 62 65 66 Object [] uniqueColumnReferences; 67 68 69 public BTree(SuperComparator comparator0, 70 _BTreeCharacteristics btreeCharacteristics0, 71 _NodeManager nodeManger0) throws DException { 72 comparator = comparator0; 73 btreeCharacteristics = btreeCharacteristics0; 74 nodeManager = nodeManger0; 75 nodeManager.setBTree(this); 76 77 78 } 79 80 public _IndexInformation getIndexInformation() throws DException { 81 return indexInformation; 82 } 83 84 89 90 public void setIndexInformation(_IndexInformation indexInformation0) throws 91 DException { 92 indexInformation = indexInformation0; 93 orderType = indexInformation.getOrderOfColumns(); 94 uniqueColumnReferences = new Object [2]; 95 uniqueColumnReferences[0] = indexInformation.getColumnIndexes(); 96 uniqueColumnReferences[1] = indexInformation.getColumns(); 97 } 98 99 133 134 public _IndexKey insert(_DatabaseUser user, Object key, Object value) throws 135 DException { 136 BTreeNode btreeNode = null; 137 BTreeNode rootNode = null; 138 BTreeKey btKey = null; 139 int insertPosition = -1; 140 if ( (rootNode = nodeManager.getRootNode(user)) == null) { 141 btreeNode = rootNode = nodeManager.getNewNode(user); 142 BTreeElement element = new BTreeElement(); 143 element.setCurrentNode(btreeNode); 144 rootNode.insertDummyElement(element); 145 btreeNode.setLevel( (short) 1); 146 btKey = new BTreeKey(btreeNode, 0); 147 } 148 else { 149 btKey = locateNode(user, key, value, rootNode); 150 btreeNode = btKey.getNode(); 151 } 152 BTreeNode newRootNode = rootNode; 153 BTreeNode newNode = null; 154 BTreeNode rightNode = null; 155 156 int posa = btreeNode.insert(user, key, value, btKey.getPosition()); 157 if (posa != -1) { 158 nodeManager.updateSizeAndBTreeInfo(user, true, rootNode); 159 BTreeKey btreeKey = new BTreeKey(); 160 btreeKey.setNodeAndPosition(btreeNode, posa); 161 return btreeKey; 162 } 163 BTreeKey temp = null; 164 boolean flag = true; 165 BTreeElement dummyElement, middleElement; 166 BTreeNode parentNode; 167 int splitPoint, cmp, pos; 168 int testElementCount ; 169 while (posa == -1) { 170 btreeNode.setIsNodeCanBeRemovedFromMap(false); 171 newNode = nodeManager.getNewNode(user); 172 newNode.setIsNodeCanBeRemovedFromMap(false); 173 newNode.setLevel(btreeNode.getLevel()); 174 175 dummyElement = new BTreeElement(); 176 dummyElement.setCurrentNode(btreeNode); 177 newNode.insertDummyElement(dummyElement); 178 splitPoint = getSplitPoint(btreeNode); 179 testElementCount = btreeNode.getElementCount() ; 180 middleElement = btreeNode.getElement(splitPoint, user); 181 182 shiftRightHalf(user, btreeNode, newNode, splitPoint); 183 cmp = comparator.compare(middleElement.getKey(), key); 184 pos = -1; 185 if (!flag) { 186 btreeNode.setLeafNode(false); 187 newNode.setLeafNode(false); 188 } 189 pos = cmp > 0 ? btreeNode.insert(user, key, value, -1) : 190 newNode.insert(user, key, value, -1); 191 if (!flag) { 192 BTreeNode nod = cmp > 0 ? btreeNode : newNode; 193 BTreeElement bt = nod.getElement(pos, user); 194 bt.updateChild(rightNode); 195 rightNode.setIsNodeCanBeRemovedFromMap(true); 196 } 197 198 parentNode = btreeNode.getParentNode(user); 199 boolean isParentNode = false; 200 if (parentNode == null) { 201 isParentNode = true; 202 newRootNode = parentNode = nodeManager.getNewNode(user); 203 newRootNode.setIsNodeCanBeRemovedFromMap(false); 204 parentNode.setLevel( (short) (btreeNode.getLevel() + 1)); 205 BTreeElement element = new BTreeElement(); 206 element.setCurrentNode(parentNode); 207 element.setChild(btreeNode); 208 parentNode.insertDummyElement(element); 209 parentNode.setLeafNode(false); 210 } 211 if (flag) { 212 flag = false; 213 temp = new BTreeKey(); 214 if (cmp > 0){ 215 if(pos < 0){ 216 ; } 218 219 temp.setNodeAndPosition(btreeNode, pos); } 221 else 222 temp.setNodeAndPosition(newNode, pos); } 224 if (DUPLICATEKEYSALLOWED && !isParentNode) { 225 insertPosition = parentNode.binarySearch(key); 226 } 227 key = middleElement.getKey(); 228 value = middleElement.getValue(); 229 btreeNode.setIsNodeCanBeRemovedFromMap(true); 230 btreeNode = parentNode; 231 posa = btreeNode.insert(user, key, value, 232 isParentNode ? 0 : insertPosition); 233 if (posa == -1) { 234 rightNode = newNode; 235 rightNode.setIsNodeCanBeRemovedFromMap(false); 236 } 237 else { 238 BTreeElement ele = btreeNode.getElement(posa, user); 239 ele.updateChild(newNode); 240 } 241 newNode.setIsNodeCanBeRemovedFromMap(true); 242 } 243 rootNode = newRootNode; 244 newRootNode.setIsNodeCanBeRemovedFromMap(true); 245 nodeManager.updateSizeAndBTreeInfo(user, true, rootNode); 246 return temp; 247 } 248 249 264 265 public void update(_DatabaseUser user, Object oldKey, Object newKey, 266 Object oldValue, Object newValue) throws DException { 267 int cmp = comparator.compare(oldKey, newKey); 268 if (cmp == 0) { 269 if (oldValue == null && newValue == null) 270 return; 271 if (oldValue != null && oldValue.equals(newValue)) 272 return; 273 } 274 BTreeNode rootNode = nodeManager.getRootNode(user); 275 BTreeKey obj = searchNode(rootNode, oldKey, oldValue); 276 BTreeKey updateKey = (BTreeKey) obj.clone(); 277 BTreeNode btreenode = obj.getNode(); 278 boolean flag = true; 279 if (next1(obj)) { 280 Object nextKey = obj.getKey(); 281 if (comparator.compare(nextKey, newKey) > 0 && 282 updateKey.getNode().hashCode() == obj.getNode().hashCode()) { 283 previous1(obj); 284 flag = true; 285 } 286 else 287 flag = false; 288 } 289 290 if (flag) { 291 if (previous1(obj)) { 292 Object previousKey = obj.getKey(); 293 if (comparator.compare(previousKey, newKey) < 0 && 294 updateKey.getNode().hashCode() == obj.getNode().hashCode()) { 295 update(user, btreenode, updateKey, oldKey, oldValue, newKey, newValue); 296 return; 297 } 298 } 299 else { 300 update(user, btreenode, updateKey, oldKey, oldValue, newKey, newValue); 301 return; 302 } 303 } 304 305 delete(user, updateKey, rootNode); 306 insert(user, newKey, newValue); 307 } 308 309 private void update(_DatabaseUser user, BTreeNode btreenode, BTreeKey obj, 310 Object oldKey, Object oldValue, Object newKey, 311 Object newValue) throws DException { 312 btreenode = getNode(user, btreenode.getNodeKey()); 313 try { 314 btreenode.update(null, obj, newKey, newValue); 315 } 316 catch (DException ex) { 317 if (ex.getDseCode().equalsIgnoreCase("DSE2001")) { 318 delete(user, oldKey, oldValue); 319 insert(user, newKey, newValue); 320 } 321 else 322 throw ex; 323 } 324 } 325 326 327 336 337 public _IndexKey delete(_DatabaseUser user, Object key, Object value) throws 338 DException { 339 BTreeNode rootNode = nodeManager.getRootNode(user); 340 BTreeKey obj = searchNode(rootNode, key, value); 341 if (obj == null) { 342 String we = ""; 343 Object key1 = null; 344 try { 345 CbCzufIboemfs[] handlers = nodeManager.getByteHandlers(); 346 key1 = getObject(key, handlers, nodeManager.getColumnTypes()); 347 we = P.print(key1); 348 } 349 catch (Exception E) { 350 we = "" + key1; 351 } 352 obj = searchNode(rootNode, key, value); 353 throw new DException("DSE2008", new Object [] {we, value}); 354 } 355 356 return delete(user, obj, rootNode); 357 } 358 359 365 366 public _IndexKey delete(_DatabaseUser user, _IndexKey key) throws DException { 367 368 return delete(user, (BTreeKey) key, nodeManager.getRootNode(user)); 369 } 370 371 389 390 393 394 private BTreeKey locateNode(_DatabaseUser user, Object key, Object value, 395 BTreeNode rootNode) throws DException { 396 BTreeNode node = rootNode; 397 BTreeNode locatedNode = null; 398 int pos = -1; 399 do { 400 if (node.getElementCount() < 2 && node.isLeaf()) { 401 return new BTreeKey(node, 0); 402 } 403 locatedNode = node; 404 pos = node.binarySearch(key); 405 if (pos < 0) { 406 pos = Math.abs(pos); 407 } 408 node = node.isLeaf() ? null : getNode(user, node.getChildNodeKey(pos)); 409 } 410 while (node != null); 411 return new BTreeKey(locatedNode, pos); 412 } 413 414 public void showBTree() throws DException { 415 BTreeNode rootNode = nodeManager.getRootNode(null); 416 if (rootNode == null || rootNode.getElementCount() == 1) { 417 ; return; 419 } 420 showBTree(rootNode, 0); 421 } 422 423 424 private void showBTree(BTreeNode rootNode, int j) throws DException { 425 if (rootNode == null) 426 return; 427 BTreeNode nod = rootNode.getParentNode(null); 428 try { 429 } 430 catch (NullPointerException ex) { 431 432 } 433 if (rootNode.getElementCount() == 0) 434 return; 435 439 for (int i = 0; i < rootNode.getElementCount(); i++) { 440 BTreeElement ele = rootNode.getElement(i, null); 441 if (ele != null) { 442 BTreeNode nodea = getNode(null, rootNode.getChildNodeKey(i)); 443 showBTree(nodea, j); 444 } 445 } 446 447 } 448 449 457 458 private void shiftRightHalf(_DatabaseUser user, BTreeNode oldNode, 459 BTreeNode newNode, int splitPoint) throws 460 DException { 461 int insertPosition = oldNode.isLeaf() ? splitPoint : splitPoint + 1; 462 int deletePosition = splitPoint - 1; 463 int endPosition = oldNode.getElementCount() - 1; 464 newNode.insertRange(oldNode.getElements(), insertPosition, endPosition); 465 if (!oldNode.isLeaf()) { 466 BTreeElement ele; 467 BTreeNode nd; 468 for (int i = insertPosition; i <= endPosition; i++) { 469 ele = oldNode.getElement(i, user); 470 nd = nodeManager.getNode(user, ele.getChildNodeKey()); 471 if (nd != null) { 472 nd.setParentNode(newNode); 473 } 474 } 475 ele = oldNode.getElement(splitPoint, user); 476 nd = nodeManager.getNode(user, ele.getChildNodeKey()); 477 if (nd != null) { 478 newNode.getElement(0, user).updateChild(nd); } 480 } 481 oldNode.deleteRange(0, deletePosition); 482 BTreeNode nextNode = oldNode.getNextNode(user); 483 if (nextNode != null) { 484 newNode.setNextNode(nextNode); 485 nextNode.setPreviousNode(newNode); 486 } 487 oldNode.setNextNode(newNode); 488 newNode.setPreviousNode(oldNode); 489 } 490 491 499 500 private int getSplitPoint(BTreeNode node) throws DException { 501 int splitPoint = node.getSplitPoint(); 502 int startPosition = splitPoint; 503 if (DUPLICATEKEYSALLOWED) { 504 int count = node.getElementCount(); 505 Object key = node.getKey(startPosition); 506 int a; 507 int b; 508 while (true) { 509 a = startPosition + 1; 510 511 if (a >= count) { 512 startPosition = splitPoint; 513 b = splitPoint - 1; 514 while (b > 0 && comparator.compare(key, node.getKey(b)) == 0) { 515 startPosition--; 516 b--; 517 } 518 if (startPosition == 1) { 519 return splitPoint; 520 } 521 return startPosition; 522 } 523 524 startPosition++; 525 526 if (comparator.compare(key, node.getKey(a)) != 0) { 527 if (a == splitPoint + 1 && 528 comparator.compare(key, node.getKey(splitPoint - 1)) != 0) { 529 return splitPoint; 530 } 531 return startPosition; 532 } 533 } 534 } 535 return startPosition; 536 } 537 538 541 542 public String [] getColumnNames() throws DException { 543 return indexInformation == null ? null : indexInformation.getColumns(); 544 } 545 546 public int getHighestLevelofBtree() throws DException { 547 return nodeManager.getRootNode(null).getLevel(); 548 } 549 550 553 554 public SuperComparator getComparator() { 555 return comparator; 556 } 557 558 564 565 public Object seekFromTopRelative(_IndexPredicate[] condition) throws 566 DException { 567 if (getSize() == 0) 568 return null; 569 572 BTreeNode currentRootNode = nodeManager.getRootNode(null); 573 return currentRootNode == null ? null : 574 searchNode(currentRootNode, condition, 575 new BTreeReader(btreeCharacteristics), true); 576 } 577 578 584 585 public Object seekFromBottomRelative(_IndexPredicate[] condition) throws 586 DException { 587 if (getSize() == 0) 588 return null; 589 BTreeNode currentRootNode = nodeManager.getRootNode(null); 590 591 if (currentRootNode == null) 592 return null; 593 594 return searchNode(currentRootNode, condition, 595 new BTreeReader(btreeCharacteristics), false); 596 } 597 598 private int search(BTreeNode cluster, _IndexPredicate[] condition, 599 _VariableValues reader, int low, int high, boolean flag) throws 600 DException { 601 if (condition == null) 602 return flag ? low : high; 603 int cmp = 0; 604 int position = -1; 605 int length = 0; 606 for (int i = 0; i < condition.length && condition[i] != null; i++, length++) 607 ; 608 Object object = null; 609 while (low <= high) { 610 int mid = (low + high) / 2; 611 object = cluster.getKey(mid); 612 ( (BTreeReader) reader).setValue(object); 613 cmp = evaluate1(condition, reader, length); 614 if (cmp > 0) 615 low = mid + 1; 616 else if (cmp < 0) 617 high = mid - 1; 618 else { 619 cmp = evaluate(condition, reader, length); 620 if (cmp == 0) 621 position = mid; 622 if (flag) 623 high = mid - 1; 624 else 625 low = mid + 1; 626 } 627 } 628 return position == -1 ? - (low - 1) : position; 629 630 } 631 632 637 638 public Object seek(Object indexKey) throws DException { 639 if (getSize() == 0) 640 return null; 641 boolean isPartial = false; 642 if (getColumnNames().length == 1) { 643 if (indexKey instanceof Object []) 644 indexKey = ( (Object []) indexKey)[0]; 645 } 646 else { 647 isPartial = ( (Object []) indexKey).length != getColumnNames().length; 648 } 649 BTreeNode currentRootNode = nodeManager.getRootNode(null); 650 if (currentRootNode == null || 651 currentRootNode.isLeaf() && currentRootNode.getElementCount() < 2) 652 return null; 653 return searchNode(currentRootNode, indexKey, false, true, isPartial); 654 } 655 656 private int search(BTreeNode cluster, Object keyToSeek, int low, int high, 657 boolean flag) throws DException { 658 int cmp = 0; 659 int position = -1; 660 Object object = null; 661 while (low <= high) { 662 int mid = (low + high) / 2; 663 object = cluster.getKey(mid); 664 cmp = comparator.compare(keyToSeek, object); 665 if (cmp > 0) 666 low = mid + 1; 667 else if (cmp < 0) 668 high = mid - 1; 669 else { 670 if (cmp == 0) 671 position = mid; 672 if (flag) 673 high = mid - 1; 674 else 675 low = mid + 1; 676 } 677 } 678 return position == -1 ? - (low - 1) : position; 679 } 680 681 691 692 public Object seekFromTopRelative(Object key, Object indexKey) throws 693 DException { 694 if (getSize() == 0) 695 return null; 696 indexKey = getColumnNames().length == 1 && indexKey instanceof Object [] ? 697 ( (Object []) indexKey)[0] : indexKey; 698 BTreeKey currentKey = (BTreeKey) key; 699 boolean flag = currentKey == null ? first(currentKey = new BTreeKey()) : 700 next(currentKey); 701 while (flag) { 702 if (comparator.compare(currentKey.getKey(), indexKey) >= 0) 703 return currentKey; 704 flag = next(currentKey); 705 } 706 return null; 707 } 708 709 718 719 public Object seekFromBottomRelative(Object key, Object indexKey) throws 720 DException { 721 if (getSize() == 0) 722 return null; 723 indexKey = getColumnNames().length == 1 && indexKey instanceof Object [] ? 724 ( (Object []) indexKey)[0] : indexKey; 725 BTreeKey currentKey = (BTreeKey) key; 726 boolean flag = currentKey == null ? last(currentKey = new BTreeKey()) : 727 previous(currentKey); 728 while (flag) { 729 comparator.compare(currentKey.getKey(), indexKey); 730 if (comparator.compare(currentKey.getKey(), indexKey) <= 0) { 731 return currentKey; 732 } 733 flag = previous(currentKey); 734 } 735 return null; 736 } 737 738 private int evaluate(_IndexPredicate[] conditions, _VariableValues reader, 739 int start) throws DException { 740 int cmp = 0; 741 for (int i = start, length = conditions.length; cmp == 0 && i < length; i++) { 742 cmp = conditions[i] == null ? 0 : conditions[i].run(reader).hashCode(); 743 cmp = (orderType == null || orderType[i]) ? cmp : -cmp; 744 } 745 return cmp; 746 } 747 748 private int evaluate1(_IndexPredicate[] conditions, _VariableValues reader, 749 int stopPosition) throws DException { 750 int cmp = 0; 751 for (int i = 0; cmp == 0 && i < stopPosition; i++) { 752 cmp = conditions[i].run(reader).hashCode(); 753 cmp = (orderType == null || orderType[i]) ? cmp : -cmp; 754 } 755 return cmp; 756 } 757 758 773 774 public _IndexKey locateKey(Object indexKey, boolean top) throws DException { 775 if (getSize() == 0) 776 return null; 777 String [] columnmNames = getColumnNames(); 778 boolean isPartial = false; 779 if (columnmNames.length == 1) { 780 if (indexKey instanceof Object []) 781 indexKey = ( (Object []) indexKey)[0]; 782 } 783 else { 784 isPartial = ( (Object []) indexKey).length != columnmNames.length; 785 } 786 BTreeKey entry = null; 787 BTreeNode currentRootNode = nodeManager.getRootNode(null); 788 789 if (currentRootNode == null || 790 currentRootNode.isLeaf() && currentRootNode.getElementCount() < 2) 791 return null; 792 entry = searchNode(currentRootNode, indexKey, true, top, isPartial); 793 if (entry.getPosition() <= 0) 794 return locateNode(entry, top); 795 return entry; 796 } 797 798 public _IndexKey insert(Object key, Object value) throws DException { 799 return this.insert(null, key, value); 800 } 801 802 public void update(Object oldKey, Object newKey, Object oldValue, 803 Object newValue) throws DException { 804 update(null, oldKey, newKey, oldValue, newValue); 805 } 806 807 public _IndexKey delete(Object key, Object value) throws DException { 808 return this.delete(null, key, value); 809 } 810 811 public Object seekAbsolute(Object key, Object indexKey) throws DException { 812 if (getSize() == 0) 813 return null; 814 indexKey = getColumnNames().length == 1 && indexKey instanceof Object [] ? 815 ( (Object []) indexKey)[0] : indexKey; 816 BTreeKey currentKey = (BTreeKey) key; 817 boolean flag = currentKey == null ? first(currentKey = new BTreeKey()) : 818 next(currentKey); 819 while (flag) { 820 int comp = comparator.compare(currentKey.getKey(), indexKey); 821 if (comp == 0) 822 return currentKey; 823 else flag = comp > 0 ? false : next(currentKey); 824 } 825 return null; 826 } 827 828 public int getSize() { 829 return nodeManager.getSize(); 830 } 831 832 841 842 public boolean first(_IndexKey key0) throws DException { 843 if (getSize() == 0) 844 return false; 845 BTreeKey key = (BTreeKey) key0; 846 BTreeNode node = nodeManager.getRootNode(null); 847 if (node == null) 848 return false; 849 while (!node.isLeaf()) { 850 node = getNode(null, node.getChildNodeKey(0)); } 852 try { 853 int position = node.getFirstValidPosition(); 854 key.setNodeAndPosition(node, position); 855 return true; 856 } 857 catch (DException ex) { 858 if (ex.getDseCode().equals("DSE2042") || 859 ex.getDseCode().equals("DSE2043")) { 860 node = node.getNextNode(null); 861 while (node != null) { 862 try { 863 key.setNodeAndPosition(node, node.getFirstValidPosition()); 864 return true; 865 } 866 catch (DException ex1) { 867 node = node.getNextNode(null); 868 } 869 } 870 return false; 871 } 872 throw ex; 873 } 874 } 875 876 884 885 public boolean next(_IndexKey btreeKey0) throws DException { 886 if (getSize() == 0) 887 return false; 888 BTreeKey btreeKey = (BTreeKey) btreeKey0; 889 btreeKey.checkValidity(); 890 if (btreeKey.getPosition() == 0) { 891 return next1(btreeKey); 892 } 893 Object oldKey = btreeKey.getOldkey(); 894 Object oldValue = btreeKey.getOldValue(); 895 BTreeElement element = null; 896 Object newValue = null; 897 try { 898 BTreeNode node = btreeKey.getNode(); 899 if (node.isNodeRemovedFromMap()) 900 node = nodeManager.getNode(null, node.getNodeKey()); 901 newValue = node.getValue(btreeKey.getPosition()); 902 } 903 catch (DException de) { 904 if (!de.getDseCode().equalsIgnoreCase("DSE2043")) 905 throw de; } 907 if (oldValue.equals(newValue)) { 908 return next1(btreeKey); 909 } 910 else { 911 912 BTreeKey btreeKeyAfterLocate = (BTreeKey) locateKey(oldKey, true); 913 914 915 if (btreeKeyAfterLocate == null) { 916 return false; 917 } 918 Object newKey = btreeKeyAfterLocate.getKey(); 919 newValue = btreeKeyAfterLocate.getValue(); 920 btreeKey.setNodeAndPosition(btreeKeyAfterLocate.getNode(), 921 btreeKeyAfterLocate.getPosition()); 922 if (comparator.compare(oldKey, newKey) != 0) 923 return true; 924 925 BTreeKey clonedKey = (BTreeKey) btreeKey.clone(); 926 boolean foundOldRecord = false; 927 while (comparator.compare(oldKey, newKey) == 0) { 928 if (oldValue.equals(newValue)) { 929 foundOldRecord = true; 930 break; 931 } 932 if (next1(btreeKey) == false) 933 break; 934 935 newValue = btreeKey.getValue(); 936 newKey = btreeKey.getKey(); 937 } 938 939 if (foundOldRecord) 940 return next1(btreeKey); 941 else { 942 btreeKey.setNodeAndPosition(clonedKey.getNode(), clonedKey.getPosition()); 943 return true; 944 } 945 946 } 947 } 948 949 private boolean next1(BTreeKey key) throws DException { 950 BTreeNode node = key.getNode(); 951 try { 952 if (node.isNodeRemovedFromMap()) 953 node = nodeManager.getNode(null, node.getNodeKey()); 954 int position = node.getNextValidPosition(key.getPosition()); 955 956 key.setNodeAndPosition(node, position); 957 return true; 958 } 959 catch (DException de) { 960 961 if (de.getDseCode().equals("DSE2042") || de.getDseCode().equals("DSE2043")) { 962 node = node.getNextNode(null); 963 while (node != null) { 964 try { 965 key.setNodeAndPosition(node, node.getFirstValidPosition()); 966 return true; 967 } 968 catch (DException ex) { 969 node = node.getNextNode(null); 970 } 971 } 972 return false; 973 } 974 throw de; 975 } 976 } 977 978 987 988 public boolean last(_IndexKey key0) throws DException { 989 if (getSize() == 0) 990 return false; 991 BTreeKey key = (BTreeKey) key0; 992 BTreeNode node = nodeManager.getRootNode(null); 993 if (node == null) 994 return false; 995 while (!node.isLeaf()) { 996 node = getNode(null, node.getChildNodeKey(node.getElementCount() - 1)); } 998 try { 999 int position = node.getLastValidPosition(); 1000 key.setNodeAndPosition(node, position); 1001 return true; 1002 } 1003 catch (DException ex) { 1004 if (ex.getDseCode().equals("DSE2042") || ex.getDseCode().equals("DSE2043")) { 1005 node = node.getPreviousNode(null); 1006 while (node != null) { 1007 try { 1008 key.setNodeAndPosition(node, node.getLastValidPosition()); 1009 return true; 1010 } 1011 catch (DException ex1) { 1012 node = node.getPreviousNode(null); 1013 } 1014 } 1015 return false; 1016 } 1017 throw ex; 1018 } 1019 } 1020 1021 1030 public boolean previous(_IndexKey btreeKey0) throws DException { 1031 if (getSize() == 0) 1032 return false; 1033 BTreeKey btreeKey = (BTreeKey) btreeKey0; 1034 Object oldKey = btreeKey.getOldkey(); 1035 Object oldValue = btreeKey.getOldValue(); 1036 Object newValue = null; 1037 try { 1038 BTreeNode node = btreeKey.getNode(); 1039 if (node.isNodeRemovedFromMap()) 1040 node = nodeManager.getNode(null, node.getNodeKey()); 1041 newValue = node.getValue(btreeKey.getPosition()); 1042 } 1043 catch (DException de) { 1044 if (!de.getDseCode().equalsIgnoreCase("DSE2043")) 1045 throw de; 1046 } 1047 if (oldValue.equals(newValue)) 1048 return previous1(btreeKey); 1049 else { 1050 BTreeKey btreeKeyAfterLocate = (BTreeKey) locateKey(oldKey, false); 1051 if (btreeKeyAfterLocate == null) 1052 return false; 1053 Object newKey = btreeKeyAfterLocate.getKey(); 1054 newValue = btreeKeyAfterLocate.getValue(); 1055 btreeKey.setNodeAndPosition(btreeKeyAfterLocate.getNode(), 1056 btreeKeyAfterLocate.getPosition()); 1057 if (comparator.compare(oldKey, newKey) != 0) 1058 return true; 1059 1060 BTreeKey clonedKey = (BTreeKey) btreeKey.clone(); 1061 boolean foundOldRecord = false; 1062 while (comparator.compare(oldKey, newKey) == 0) { 1063 if (oldValue.equals(newValue)) { 1064 foundOldRecord = true; 1065 break; 1066 } 1067 if (previous1(btreeKey) == false) 1068 break; 1069 1070 newValue = btreeKey.getValue(); 1071 newKey = btreeKey.getKey(); 1072 } 1073 1074 if (foundOldRecord) 1075 return previous1(btreeKey); 1076 else { 1077 btreeKey.setNodeAndPosition(clonedKey.getNode(), clonedKey.getPosition()); 1078 return true; 1079 1080 } 1081 } 1082 } 1083 1084 private boolean previous1(BTreeKey key) throws DException { 1085 BTreeNode node = key.getNode(); 1086 try { 1087 int position = 0; 1088 if (node.isNodeRemovedFromMap()) 1089 node = nodeManager.getNode(null, node.getNodeKey()); 1090 position = node.getPreviousValidPosition(key.getPosition()); 1091 key.setNodeAndPosition(node, position); 1092 return true; 1093 } 1094 catch (DException de) { 1095 if (de.getDseCode().equals("DSE2042") || de.getDseCode().equals("DSE2043")) { 1096 node = node.getPreviousNode(null); 1097 while (node != null) { 1098 try { 1099 key.setNodeAndPosition(node, node.getLastValidPosition()); 1100 return true; 1101 } 1102 catch (DException ex) { 1103 node = node.getPreviousNode(null); 1104 } 1105 } 1106 return false; 1107 } 1108 throw de; 1109 } 1110 } 1111 1112 public BTreeNode getRootNode() throws DException { 1113 return nodeManager.getRootNode(null); 1114 } 1115 1116 public _NodeManager getNodeManager() throws DException { 1117 return nodeManager; 1118 } 1119 1120 private BTreeKey getBTreeKey(BTreeNode node, int position) throws DException { 1121 BTreeKey bk = new BTreeKey(); 1122 bk.setNodeAndPosition(node, position); 1123 return bk; 1124 } 1125 1126 private static Object getObject(Object bb, CbCzufIboemfs[] handlers, 1127 int[] columnTypes) throws DException { 1128 if (bb instanceof BufferRange) { 1129 return ( (BufferRange) bb).getNull() ? null : 1130 handlers[0].getObject( (BufferRange) bb, columnTypes[0]); 1131 } 1132 BufferRange[] bytes = (BufferRange[]) bb; 1133 Object [] values = new Object [bytes.length]; 1134 for (int i = 0; i < bytes.length; i++) 1135 values[i] = bytes[i] == null ? null : bytes[i].getNull() ? null : 1136 handlers[i].getObject(bytes[i], columnTypes[i]).getObject(); 1137 return values; 1138 } 1139 1140 protected BTreeNode getNode(_DatabaseUser user, Object childNodeKey) throws 1141 DException { 1142 return childNodeKey == null ? null : nodeManager.getNode(user, childNodeKey); 1143 } 1144 1145 public Object [] getUniqueColumnReference() throws DException { 1146 return uniqueColumnReferences; 1147 } 1148 1149 public int getTotalNumberOfClusterLoadedInMemory() { 1150 return ( (FileNodeManager) nodeManager).getTotalSizeOfNodeMapAndWeakNodeMap(); 1151 } 1152 1153 public Object getObject(Object key) throws DException { 1154 CbCzufIboemfs[] handlers = nodeManager.getByteHandlers(); 1155 return getObject(key, handlers, nodeManager.getColumnTypes()); 1156 } 1157 1158 protected void finalize123() { 1159 } 1160 1161 1177 1178 private BTreeKey searchNode(BTreeNode rootNode, Object key, Object value) throws 1179 DException { 1180 BTreeNode node = rootNode; 1181 BTreeNode locatedNode = null; 1182 int pos = -1; 1183 int valToRet = -1; 1184 do { 1185 if (node.getElementCount() < 2 && node.isLeaf()) 1186 return null; 1187 locatedNode = node; 1188 pos = node.binarySearch(key); 1189 valToRet = pos; 1190 if (pos < 0) { 1191 pos = Math.abs(pos); 1192 } 1193 node = node.isLeaf() ? null : getNode(null, node.getChildNodeKey(pos)); 1194 } 1195 while (node != null); 1196 1197 if (valToRet < 0) return null; 1199 1200 BTreeKey btreeKey = getBTreeKey(locatedNode, valToRet); 1201 if (!DUPLICATEKEYSALLOWED) 1202 return btreeKey; 1203 if (value.equals(btreeKey.getValue())) 1204 return btreeKey; 1205 BTreeKey searchKey = (BTreeKey) btreeKey.clone(); 1206 while (next1(btreeKey)) { 1207 Object nextKey = btreeKey.getKey(); 1208 if ( (comparator.compare(nextKey, key) == 0)) { 1209 if (value.equals(btreeKey.getValue())) { 1210 return btreeKey; 1211 } 1212 } 1213 else 1214 break; 1215 } 1216 1217 btreeKey = (BTreeKey) searchKey.clone(); 1218 while (previous1(btreeKey)) { 1219 Object previousKey = btreeKey.getKey(); 1220 if (comparator.compare(previousKey, key) == 0) { 1221 if (value.equals(btreeKey.getValue())) { 1222 return btreeKey; 1223 } 1224 } 1225 else 1226 break; 1227 } 1228 return searchKey; 1229 } 1230 1231 1265 1266 private BTreeKey searchNode(BTreeNode rootNode, Object key, boolean locate, 1267 boolean top, boolean isPartial) throws DException { 1268 BTreeNode node = rootNode; 1269 BTreeNode locatedNode = null; 1270 int pos = -1; 1271 int valToRet = -1; 1272 do { 1273 locatedNode = node; 1274 pos = search(node, key, 1, node.getElementCount() - 1, top); 1275 valToRet = pos; 1276 if (pos < 0) { 1277 pos = Math.abs(pos); 1278 } 1279 node = node.isLeaf() ? null : getNode(null, node.getChildNodeKey(pos)); 1280 } 1281 while (node != null); 1282 1283 if (!isPartial && valToRet < 0) { return locate ? new BTreeKey(locatedNode, valToRet) : null; 1285 } 1286 else if (valToRet > 0) { 1287 BTreeKey btreeKey = getBTreeKey(locatedNode, valToRet); 1288 1289 BTreeKey searchKey = (BTreeKey) btreeKey.clone(); 1290 if (top) { 1291 while (previous1(btreeKey)) { 1292 Object previousKey = btreeKey.getKey(); 1293 if (! (comparator.compare(previousKey, key) == 0)) { 1294 next1(btreeKey); 1295 return btreeKey; 1296 } 1297 } 1298 return btreeKey; 1299 } 1300 else { 1301 btreeKey = (BTreeKey) searchKey.clone(); 1302 while (next1(btreeKey)) { 1303 Object nextKey = btreeKey.getKey(); 1304 if (! (comparator.compare(nextKey, key) == 0)) { 1305 previous1(btreeKey); 1306 return btreeKey; 1307 } 1308 } 1309 return btreeKey; 1310 } 1311 } 1312 else { 1313 BTreeKey btreeKey = getBTreeKey(locatedNode, Math.abs(valToRet)); 1314 BTreeKey searchKey = (BTreeKey) btreeKey.clone(); 1315 BTreeKey locatedKey = null; 1316 if (top) { 1317 while (previous1(btreeKey)) { 1318 Object previousKey = btreeKey.getKey(); 1319 if (comparator.compare(previousKey, key) != 0) 1320 break; 1321 else 1322 locatedKey = (BTreeKey) btreeKey.clone(); 1323 } 1324 if (locatedKey != null) 1325 return locatedKey; 1326 btreeKey = (BTreeKey) searchKey.clone(); 1327 1332 1333 if (next1(btreeKey)) { 1334 Object nextKey = btreeKey.getKey(); 1335 if (comparator.compare(nextKey, key) == 0) 1336 return btreeKey; 1337 } 1338 1339 return locate ? new BTreeKey(locatedNode, valToRet) : null; 1340 } 1341 else { 1342 while (next1(btreeKey)) { 1343 Object nextKey = btreeKey.getKey(); 1344 if (comparator.compare(nextKey, key) != 0) 1345 break; 1346 else 1347 locatedKey = (BTreeKey) btreeKey.clone(); 1348 } 1349 if (locatedKey != null) 1350 return locatedKey; 1351 btreeKey = (BTreeKey) searchKey.clone(); 1352 1357 1358 if (previous1(btreeKey)) { 1359 Object previousKey = btreeKey.getKey(); 1360 if (comparator.compare(previousKey, key) == 0) 1361 return btreeKey; 1362 } 1363 return locate ? new BTreeKey(locatedNode, valToRet) : null; 1364 } 1365 } 1366 1367 } 1368 1369 1397 1398 private BTreeKey searchNode(BTreeNode rootNode, _IndexPredicate[] condition, 1399 _VariableValues reader, boolean top) throws 1400 DException { 1401 1402 if (rootNode == null || (rootNode.getElementCount() < 2 && rootNode.isLeaf())) { 1403 return null; 1404 } 1405 BTreeNode node = rootNode; 1406 BTreeNode locatedNode = null; 1407 int pos = -1; 1408 int valToRet = -1; 1409 do { 1410 locatedNode = node; 1411 pos = search(node, condition, reader, 1, node.getElementCount() - 1, top); 1412 valToRet = pos; 1413 if (pos < 0) { 1414 pos = Math.abs(pos); 1415 } 1416 node = node.isLeaf() ? null : getNode(null, node.getChildNodeKey(pos)); 1417 } 1418 while (node != null); 1419 boolean isPartial = condition.length != getColumnNames().length; 1420 if (!isPartial && valToRet < 0) return null; 1422 else if (valToRet > 0) { 1423 BTreeKey btreeKey = getBTreeKey(locatedNode, valToRet); 1424 BTreeKey searchKey = (BTreeKey) btreeKey.clone(); 1425 if (top) { 1426 while (previous1(btreeKey)) { 1427 Object previousKey = btreeKey.getKey(); 1428 ( (BTreeReader) reader).setValue(previousKey); 1429 if (evaluate(condition, reader, 0) != 0) { 1430 next1(btreeKey); 1431 return btreeKey; 1432 } 1433 } 1434 return btreeKey; 1435 } 1436 else { 1437 btreeKey = (BTreeKey) searchKey.clone(); 1438 while (next1(btreeKey)) { 1439 Object nextKey = btreeKey.getKey(); 1440 ( (BTreeReader) reader).setValue(nextKey); 1441 if (evaluate(condition, reader, 0) != 0) { 1442 previous1(btreeKey); 1443 return btreeKey; 1444 } 1445 } 1446 return btreeKey; 1447 } 1448 } 1449 else { 1450 BTreeKey btreeKey = getBTreeKey(locatedNode, Math.abs(valToRet)); 1451 BTreeKey searchKey = (BTreeKey) btreeKey.clone(); 1452 BTreeKey locatedKey = null; 1453 if (top) { 1454 while (previous1(btreeKey)) { 1455 Object previousKey = btreeKey.getKey(); 1456 ( (BTreeReader) reader).setValue(previousKey); 1457 if (evaluate(condition, reader, 0) != 0) 1458 break; 1459 else { 1460 locatedKey = (BTreeKey) btreeKey.clone(); 1461 } 1462 } 1463 if (locatedKey != null) 1464 return locatedKey; 1465 btreeKey = (BTreeKey) searchKey.clone(); 1466 1471 if (next1(btreeKey)) { 1472 Object nextKey = btreeKey.getKey(); 1473 ( (BTreeReader) reader).setValue(nextKey); 1474 if (evaluate(condition, reader, 0) == 0) 1475 return btreeKey; 1476 } 1477 return null; 1478 } 1479 else { 1480 while (next1(btreeKey)) { 1481 Object nextKey = btreeKey.getKey(); 1482 ( (BTreeReader) reader).setValue(nextKey); 1483 if (evaluate(condition, reader, 0) != 0) 1484 break; 1485 else 1486 locatedKey = (BTreeKey) btreeKey.clone(); 1487 } 1488 if (locatedKey != null) 1489 return locatedKey; 1490 btreeKey = (BTreeKey) searchKey.clone(); 1491 1496 1497 if (previous1(btreeKey)) { 1498 Object previousKey = btreeKey.getKey(); 1499 ( (BTreeReader) reader).setValue(previousKey); 1500 if (evaluate(condition, reader, 0) == 0) 1501 return btreeKey; 1502 } 1503 return null; 1504 } 1505 } 1506 } 1507 1508 1518 private BTreeKey locateNode(BTreeKey entry, boolean top) throws DException { 1519 int position = entry.getPosition(); 1520 position = Math.abs(position); 1521 BTreeNode node = entry.getNode(); 1522 if (node.isNodeRemovedFromMap()) 1523 node = nodeManager.getNode(null, node.getNodeKey()); 1524 entry.setNodeAndPosition(node, position); 1525 if (top) 1526 return next1(entry) ? entry : null; 1527 else 1528 return entry.getPosition() == 0 ? previous1(entry) ? entry : null : entry; 1529 } 1530 1531 private BTreeKey delete(_DatabaseUser user, BTreeKey key, BTreeNode rootNode) throws 1532 DException { 1533 BTreeNode node = key.getNode(); 1534 node = getNode(user, node.getNodeKey()); 1535 node.delete(user, key.getPosition()); 1536 nodeManager.updateSizeAndBTreeInfo(user, false, rootNode); 1537 return key; 1538 1539 } 1540 1541 public void setDuplicateAllowed(boolean flag) { 1542 DUPLICATEKEYSALLOWED = flag; 1543 } 1544 1545 public void releaseResource(_DatabaseUser user, boolean releaseCompletely) throws 1546 DException { 1547 nodeManager.releaseResource(user, releaseCompletely); 1548 } 1549 1550 public boolean getDuplicateAllowed() { 1551 return DUPLICATEKEYSALLOWED; 1552 } 1553 1554 public _IndexKey keyInstance() { 1555 return new BTreeKey(); 1556 } 1557 1558 public BTreeNavigator getNavigator() { 1559 return new BTreeNavigatorCurrent(this); 1560 } 1561 1562 1563 1564 1565 1566 1567 1568 1569 public void setSize(int size0) { 1570 nodeManager.setSize(size0); 1571 } 1572 1573 1574 1575 1876} 1877 | Popular Tags |