1 8 9 package com.sleepycat.je.dbi; 10 11 import java.util.Comparator ; 12 import java.util.Hashtable ; 13 14 import com.sleepycat.je.DbInternal; 15 import com.sleepycat.je.Cursor; 16 import com.sleepycat.je.DatabaseException; 17 import com.sleepycat.je.LockMode; 18 import com.sleepycat.je.OperationStatus; 19 import com.sleepycat.je.util.StringDbt; 20 import com.sleepycat.je.util.TestUtils; 21 22 25 public class DbCursorTest extends DbCursorTestBase { 26 27 public DbCursorTest() 28 throws DatabaseException { 29 30 super(); 31 } 32 33 37 public void testSimpleGetPut() 38 throws Throwable { 39 40 try { 41 initEnv(false); 42 doSimpleCursorPuts(); 43 44 DataWalker dw = new DataWalker(simpleDataMap) { 45 void perData(String foundKey, String foundData) { 46 assertTrue(foundKey.compareTo(prevKey) >= 0); 47 prevKey = foundKey; 48 } 49 }; 50 dw.walkData(); 51 assertTrue(dw.nEntries == simpleKeyStrings.length); 52 } catch (Throwable t) { 53 t.printStackTrace(); 54 throw t; 55 } 56 } 57 58 61 public void testCursorAdvance() 62 throws Throwable { 63 64 try { 65 initEnv(false); 66 doSimpleCursorPuts(); 67 68 StringDbt foundKey = new StringDbt(); 69 StringDbt foundData = new StringDbt(); 70 String prevKey = ""; 71 72 OperationStatus status = cursor.getFirst(foundKey, foundData, 73 LockMode.DEFAULT); 74 75 79 DbInternal.advanceCursor(cursor, foundKey, foundData); 80 DbInternal.retrieveNext 81 (cursor, foundKey, foundData, LockMode.DEFAULT, GetMode.PREV); 82 int nEntries = 0; 83 while (status == OperationStatus.SUCCESS) { 84 String foundKeyString = foundKey.getString(); 85 String foundDataString = foundData.getString(); 86 87 assertTrue(foundKeyString.compareTo(prevKey) >= 0); 88 prevKey = foundKeyString; 89 nEntries++; 90 91 status = cursor.getNext(foundKey, foundData, LockMode.DEFAULT); 92 } 93 94 assertTrue(nEntries == simpleKeyStrings.length); 95 } catch (Throwable t) { 96 t.printStackTrace(); 97 throw t; 98 } 99 } 100 101 105 public void testSimpleGetPutBackwards() 106 throws Throwable { 107 108 try { 109 initEnv(false); 110 doSimpleCursorPuts(); 111 112 DataWalker dw = new BackwardsDataWalker(simpleDataMap) { 113 void perData(String foundKey, String foundData) { 114 if (!prevKey.equals("")) { 115 assertTrue(foundKey.compareTo(prevKey) <= 0); 116 } 117 prevKey = foundKey; 118 } 119 120 OperationStatus getFirst(StringDbt foundKey, 121 StringDbt foundData) 122 throws DatabaseException { 123 124 return cursor.getLast(foundKey, foundData, 125 LockMode.DEFAULT); 126 } 127 128 OperationStatus getData(StringDbt foundKey, 129 StringDbt foundData) 130 throws DatabaseException { 131 132 return cursor.getPrev(foundKey, foundData, 133 LockMode.DEFAULT); 134 } 135 }; 136 dw.walkData(); 137 assertTrue(dw.nEntries == simpleKeyStrings.length); 138 } catch (Throwable t) { 139 t.printStackTrace(); 140 throw t; 141 } 142 } 143 144 150 public void testSimpleGetPut2() 151 throws Throwable { 152 153 try { 154 initEnv(false); 155 doSimpleGetPut2("quux", "fub"); 156 } catch (Throwable t) { 157 t.printStackTrace(); 158 throw t; 159 } 160 } 161 162 public void doSimpleGetPut2(String whenFoundDoInsert, 163 String newKey) 164 throws DatabaseException { 165 166 doSimpleCursorPuts(); 167 168 DataWalker dw = 169 new BackwardsDataWalker(whenFoundDoInsert, newKey, simpleDataMap) { 170 void perData(String foundKey, String foundData) 171 throws DatabaseException { 172 173 if (foundKey.equals(whenFoundDoInsert)) { 174 putAndVerifyCursor(cursor2, new StringDbt(newKey), 175 new StringDbt("ten"), true); 176 simpleDataMap.put(newKey, "ten"); 177 } 178 } 179 180 OperationStatus getFirst(StringDbt foundKey, 181 StringDbt foundData) 182 throws DatabaseException { 183 184 return cursor.getLast(foundKey, foundData, 185 LockMode.DEFAULT); 186 } 187 188 OperationStatus getData(StringDbt foundKey, 189 StringDbt foundData) 190 throws DatabaseException { 191 192 return cursor.getPrev(foundKey, foundData, 193 LockMode.DEFAULT); 194 } 195 }; 196 dw.walkData(); 197 assertTrue(dw.nEntries == simpleKeyStrings.length + 1); 198 } 199 200 207 public void testSimpleGetPutNextKeyForwardTraverse() 208 throws Throwable { 209 210 try { 211 tearDown(); 212 for (int i = 0; i < simpleKeyStrings.length; i++) { 213 setUp(); 214 initEnv(false); 215 doSimpleGetPut(true, 216 simpleKeyStrings[i], 217 nextKey(simpleKeyStrings[i]), 218 1); 219 tearDown(); 220 } 221 } catch (Throwable t) { 222 t.printStackTrace(); 223 throw t; 224 } 225 } 226 227 234 public void testSimpleGetPutPrevKeyForwardTraverse() 235 throws Throwable { 236 237 try { 238 tearDown(); 239 for (int i = 0; i < simpleKeyStrings.length; i++) { 240 setUp(); 241 initEnv(false); 242 doSimpleGetPut(true, simpleKeyStrings[i], 243 prevKey(simpleKeyStrings[i]), 0); 244 tearDown(); 245 } 246 } catch (Throwable t) { 247 t.printStackTrace(); 248 throw t; 249 } 250 } 251 252 259 public void testSimpleGetPutPrevKeyBackwardsTraverse() 260 throws Throwable { 261 262 try { 263 tearDown(); 264 for (int i = 0; i < simpleKeyStrings.length; i++) { 265 setUp(); 266 initEnv(false); 267 doSimpleGetPut(false, simpleKeyStrings[i], 268 prevKey(simpleKeyStrings[i]), 1); 269 tearDown(); 270 } 271 } catch (Throwable t) { 272 t.printStackTrace(); 273 } 274 } 275 276 284 public void testSimpleGetPutNextKeyBackwardsTraverse() 285 throws Throwable { 286 287 try { 288 tearDown(); 289 for (int i = 0; i < simpleKeyStrings.length; i++) { 290 setUp(); 291 initEnv(false); 292 doSimpleGetPut(true, simpleKeyStrings[i], 293 prevKey(simpleKeyStrings[i]), 0); 294 tearDown(); 295 } 296 } catch (Throwable t) { 297 t.printStackTrace(); 298 throw t; 299 } 300 } 301 302 305 private void doSimpleGetPut(boolean forward, 306 String whenFoundDoInsert, 307 String newKey, 308 int additionalEntries) 309 throws DatabaseException { 310 311 doSimpleCursorPuts(); 312 313 DataWalker dw; 314 if (forward) { 315 dw = new DataWalker(whenFoundDoInsert, newKey, simpleDataMap) { 316 void perData(String foundKey, String foundData) 317 throws DatabaseException { 318 319 if (foundKey.equals(whenFoundDoInsert)) { 320 putAndVerifyCursor(cursor2, new StringDbt(newKey), 321 new StringDbt("ten"), true); 322 simpleDataMap.put(newKey, "ten"); 323 } 324 } 325 }; 326 } else { 327 dw = new BackwardsDataWalker(whenFoundDoInsert, 328 newKey, 329 simpleDataMap) { 330 void perData(String foundKey, String foundData) 331 throws DatabaseException { 332 333 if (foundKey.equals(whenFoundDoInsert)) { 334 putAndVerifyCursor(cursor2, new StringDbt(newKey), 335 new StringDbt("ten"), true); 336 simpleDataMap.put(newKey, "ten"); 337 } 338 } 339 340 OperationStatus getFirst(StringDbt foundKey, 341 StringDbt foundData) 342 throws DatabaseException { 343 344 return cursor.getLast(foundKey, foundData, 345 LockMode.DEFAULT); 346 } 347 348 OperationStatus getData(StringDbt foundKey, 349 StringDbt foundData) 350 throws DatabaseException { 351 352 return cursor.getPrev(foundKey, foundData, 353 LockMode.DEFAULT); 354 } 355 }; 356 } 357 dw.walkData(); 358 assertEquals(simpleKeyStrings.length + additionalEntries, dw.nEntries); 359 } 360 361 367 public void testSimpleReplace() 368 throws Throwable { 369 370 try { 371 initEnv(false); 372 doSimpleReplace(); 373 } catch (Throwable t) { 374 t.printStackTrace(); 375 throw t; 376 } 377 } 378 379 public void doSimpleReplace() 380 throws DatabaseException { 381 382 doSimpleCursorPuts(); 383 384 DataWalker dw = 385 new DataWalker(simpleDataMap) { 386 void perData(String foundKey, String foundData) 387 throws DatabaseException { 388 389 String newData = foundData + "x"; 390 cursor.putCurrent(new StringDbt(newData)); 391 simpleDataMap.put(foundKey, newData); 392 } 393 }; 394 dw.walkData(); 395 dw = new DataWalker(simpleDataMap) { 396 void perData(String foundKey, String foundData) 397 throws DatabaseException { 398 399 assertTrue(foundData.equals(simpleDataMap.get(foundKey))); 400 } 401 }; 402 dw.walkData(); 403 } 404 405 412 public void testLargeGetPutPrevKeyForwardTraverse() 413 throws Throwable { 414 415 try { 416 initEnv(false); 417 doLargeGetPutPrevKeyForwardTraverse(); 418 } catch (Throwable t) { 419 t.printStackTrace(); 420 throw t; 421 } 422 } 423 424 427 private void doLargeGetPutPrevKeyForwardTraverse() 428 throws DatabaseException { 429 430 Hashtable dataMap = new Hashtable (); 431 doLargePut(dataMap, N_KEYS); 432 433 DataWalker dw = new DataWalker(dataMap) { 434 void perData(String foundKey, String foundData) 435 throws DatabaseException { 436 437 assertTrue(foundKey.compareTo(prevKey) >= 0); 438 putAndVerifyCursor(cursor2, 439 new StringDbt(prevKey(foundKey)), 440 new StringDbt 441 (Integer.toString(dataMap.size() + 442 nEntries)), 443 true); 444 prevKey = foundKey; 445 assertTrue(dataMap.get(foundKey) != null); 446 dataMap.remove(foundKey); 447 } 448 }; 449 dw.walkData(); 450 if (dataMap.size() > 0) { 451 fail("dataMap still has entries"); 452 } 453 assertTrue(dw.nEntries == N_KEYS); 454 } 455 456 461 public void testLargeCount() 462 throws Throwable { 463 464 try { 465 initEnv(false); 466 doLargeCount(); 467 } catch (Throwable t) { 468 t.printStackTrace(); 469 throw t; 470 } 471 } 472 473 476 private void doLargeCount() 477 throws DatabaseException { 478 479 Hashtable dataMap = new Hashtable (); 480 doLargePut(dataMap, N_KEYS); 481 482 DataWalker dw = new DataWalker(dataMap) { 483 void perData(String foundKey, String foundData) 484 throws DatabaseException { 485 486 assertTrue(cursor.count() == 1); 487 assertTrue(foundKey.compareTo(prevKey) >= 0); 488 prevKey = foundKey; 489 assertTrue(dataMap.get(foundKey) != null); 490 dataMap.remove(foundKey); 491 } 492 }; 493 dw.walkData(); 494 if (dataMap.size() > 0) { 495 fail("dataMap still has entries"); 496 } 497 assertTrue(dw.nEntries == N_KEYS); 498 } 499 500 public void xxtestGetPerf() 501 throws Throwable { 502 503 try { 504 initEnv(false); 505 final int N = 50000; 506 int count = 0; 507 doLargePutPerf(N); 508 509 StringDbt foundKey = new StringDbt(); 510 StringDbt foundData = new StringDbt(); 511 OperationStatus status; 512 status = cursor.getFirst(foundKey, foundData, LockMode.DEFAULT); 513 514 while (status == OperationStatus.SUCCESS) { 515 status = cursor.getNext(foundKey, foundData, LockMode.DEFAULT); 516 count++; 517 } 518 519 assertTrue(count == N); 520 } catch (Throwable t) { 521 t.printStackTrace(); 522 throw t; 523 } 524 } 525 526 531 public void testLargeReplace() 532 throws Throwable { 533 534 try { 535 initEnv(false); 536 Hashtable dataMap = new Hashtable (); 537 doLargePut(dataMap, N_KEYS); 538 539 DataWalker dw = new DataWalker(dataMap) { 540 void perData(String foundKey, String foundData) 541 throws DatabaseException { 542 543 String newData = foundData + "x"; 544 cursor.putCurrent(new StringDbt(newData)); 545 dataMap.put(foundKey, newData); 546 } 547 }; 548 dw.walkData(); 549 dw = new DataWalker(dataMap) { 550 void perData(String foundKey, String foundData) 551 throws DatabaseException { 552 553 assertTrue(foundData.equals(dataMap.get(foundKey))); 554 dataMap.remove(foundKey); 555 } 556 }; 557 dw.walkData(); 558 assertTrue(dw.nEntries == N_KEYS); 559 assertTrue(dataMap.size() == 0); 560 } catch (Throwable t) { 561 t.printStackTrace(); 562 throw t; 563 } 564 } 565 566 573 public void testLargeGetPutNextKeyBackwardsTraverse() 574 throws Throwable { 575 576 try { 577 tearDown(); 578 for (int i = 0; i < N_ITERS; i++) { 579 setUp(); 580 initEnv(false); 581 doLargeGetPutNextKeyBackwardsTraverse(); 582 tearDown(); 583 } 584 } catch (Throwable t) { 585 t.printStackTrace(); 586 throw t; 587 } 588 } 589 590 593 private void doLargeGetPutNextKeyBackwardsTraverse() 594 throws DatabaseException { 595 596 Hashtable dataMap = new Hashtable (); 597 doLargePut(dataMap, N_KEYS); 598 599 DataWalker dw = new BackwardsDataWalker(dataMap) { 600 void perData(String foundKey, String foundData) 601 throws DatabaseException { 602 603 if (!prevKey.equals("")) { 604 assertTrue(foundKey.compareTo(prevKey) <= 0); 605 } 606 putAndVerifyCursor(cursor2, 607 new StringDbt(nextKey(foundKey)), 608 new StringDbt 609 (Integer.toString(dataMap.size() + 610 nEntries)), 611 true); 612 prevKey = foundKey; 613 assertTrue(dataMap.get(foundKey) != null); 614 dataMap.remove(foundKey); 615 } 616 617 OperationStatus getFirst(StringDbt foundKey, 618 StringDbt foundData) 619 throws DatabaseException { 620 621 return cursor.getLast(foundKey, foundData, 622 LockMode.DEFAULT); 623 } 624 625 OperationStatus getData(StringDbt foundKey, 626 StringDbt foundData) 627 throws DatabaseException { 628 629 return cursor.getPrev(foundKey, foundData, 630 LockMode.DEFAULT); 631 } 632 }; 633 dw.walkData(); 634 if (dataMap.size() > 0) { 635 fail("dataMap still has entries"); 636 } 637 assertTrue(dw.nEntries == N_KEYS); 638 } 639 640 647 public void testLargeGetPutNextKeyForwardTraverse() 648 throws Throwable { 649 650 try { 651 initEnv(false); 652 doLargeGetPutNextKeyForwardTraverse(N_KEYS); 653 } catch (Throwable t) { 654 t.printStackTrace(); 655 throw t; 656 } 657 } 658 659 662 private void doLargeGetPutNextKeyForwardTraverse(int nKeys) 663 throws DatabaseException { 664 665 Hashtable dataMap = new Hashtable (); 666 Hashtable addedDataMap = new Hashtable (); 667 doLargePut(dataMap, nKeys); 668 669 DataWalker dw = new DataWalker(dataMap, addedDataMap) { 670 void perData(String foundKey, String foundData) 671 throws DatabaseException { 672 673 assertTrue(foundKey.compareTo(prevKey) >= 0); 674 if (addedDataMap.get(foundKey) == null) { 675 String newKey = nextKey(foundKey); 676 String newData = 677 Integer.toString(dataMap.size() + nEntries); 678 putAndVerifyCursor(cursor2, 679 new StringDbt(newKey), 680 new StringDbt(newData), 681 true); 682 addedDataMap.put(newKey, newData); 683 prevKey = foundKey; 684 assertTrue(dataMap.get(foundKey) != null); 685 dataMap.remove(foundKey); 686 } else { 687 addedDataMap.remove(foundKey); 688 } 689 } 690 }; 691 dw.walkData(); 692 if (dataMap.size() > 0) { 693 fail("dataMap still has entries"); 694 } 695 if (addedDataMap.size() > 0) { 696 System.out.println(addedDataMap); 697 fail("addedDataMap still has entries"); 698 } 699 assertTrue(dw.nEntries == nKeys * 2); 700 } 701 702 709 public void testLargeGetPutPrevKeyBackwardsTraverse() 710 throws Throwable { 711 712 try { 713 initEnv(false); 714 doLargeGetPutPrevKeyBackwardsTraverse(N_KEYS); 715 } catch (Throwable t) { 716 t.printStackTrace(); 717 throw t; 718 } 719 } 720 721 724 private void doLargeGetPutPrevKeyBackwardsTraverse(int nKeys) 725 throws DatabaseException { 726 727 Hashtable dataMap = new Hashtable (); 728 Hashtable addedDataMap = new Hashtable (); 729 doLargePut(dataMap, nKeys); 730 731 DataWalker dw = new BackwardsDataWalker(dataMap, addedDataMap) { 732 void perData(String foundKey, String foundData) 733 throws DatabaseException { 734 735 if (!prevKey.equals("")) { 736 assertTrue(foundKey.compareTo(prevKey) <= 0); 737 } 738 if (addedDataMap.get(foundKey) == null) { 739 String newKey = prevKey(foundKey); 740 String newData = 741 Integer.toString(dataMap.size() + nEntries); 742 putAndVerifyCursor(cursor2, 743 new StringDbt(newKey), 744 new StringDbt(newData), 745 true); 746 addedDataMap.put(newKey, newData); 747 prevKey = foundKey; 748 assertTrue(dataMap.get(foundKey) != null); 749 dataMap.remove(foundKey); 750 } else { 751 addedDataMap.remove(foundKey); 752 } 753 } 754 755 OperationStatus getFirst(StringDbt foundKey, 756 StringDbt foundData) 757 throws DatabaseException { 758 759 return cursor.getLast(foundKey, foundData, 760 LockMode.DEFAULT); 761 } 762 763 OperationStatus getData(StringDbt foundKey, 764 StringDbt foundData) 765 throws DatabaseException { 766 767 return cursor.getPrev(foundKey, foundData, 768 LockMode.DEFAULT); 769 } 770 }; 771 dw.walkData(); 772 if (dataMap.size() > 0) { 773 fail("dataMap still has entries"); 774 } 775 if (addedDataMap.size() > 0) { 776 System.out.println(addedDataMap); 777 fail("addedDataMap still has entries"); 778 } 779 assertTrue(dw.nEntries == nKeys * 2); 780 } 781 782 789 public void testLargeGetPutBothKeyForwardTraverse() 790 throws Throwable { 791 792 try { 793 initEnv(false); 794 doLargeGetPutBothKeyForwardTraverse(N_KEYS); 795 } catch (Throwable t) { 796 t.printStackTrace(); 797 throw t; 798 } 799 } 800 801 804 private void doLargeGetPutBothKeyForwardTraverse(int nKeys) 805 throws DatabaseException { 806 807 Hashtable dataMap = new Hashtable (); 808 Hashtable addedDataMap = new Hashtable (); 809 doLargePut(dataMap, nKeys); 810 811 DataWalker dw = new DataWalker(dataMap, addedDataMap) { 812 void perData(String foundKey, String foundData) 813 throws DatabaseException { 814 815 assertTrue(foundKey.compareTo(prevKey) >= 0); 816 if (addedDataMap.get(foundKey) == null) { 817 String newKey = nextKey(foundKey); 818 String newData = 819 Integer.toString(dataMap.size() + nEntries); 820 putAndVerifyCursor(cursor2, 821 new StringDbt(newKey), 822 new StringDbt(newData), 823 true); 824 addedDataMap.put(newKey, newData); 825 newKey = prevKey(foundKey); 826 newData = Integer.toString(dataMap.size() + nEntries); 827 putAndVerifyCursor(cursor2, 828 new StringDbt(newKey), 829 new StringDbt(newData), 830 true); 831 prevKey = foundKey; 832 assertTrue(dataMap.get(foundKey) != null); 833 dataMap.remove(foundKey); 834 } else { 835 addedDataMap.remove(foundKey); 836 } 837 } 838 }; 839 dw.walkData(); 840 if (dataMap.size() > 0) { 841 fail("dataMap still has entries"); 842 } 843 if (addedDataMap.size() > 0) { 844 System.out.println(addedDataMap); 845 fail("addedDataMap still has entries"); 846 } 847 assertTrue(dw.nEntries == nKeys * 2); 848 } 849 850 857 public void testLargeGetPutBothKeyBackwardsTraverse() 858 throws Throwable { 859 860 try { 861 initEnv(false); 862 doLargeGetPutBothKeyBackwardsTraverse(N_KEYS); 863 } catch (Throwable t) { 864 t.printStackTrace(); 865 throw t; 866 } 867 } 868 869 872 private void doLargeGetPutBothKeyBackwardsTraverse(int nKeys) 873 throws DatabaseException { 874 875 Hashtable dataMap = new Hashtable (); 876 Hashtable addedDataMap = new Hashtable (); 877 doLargePut(dataMap, nKeys); 878 879 DataWalker dw = new BackwardsDataWalker(dataMap, addedDataMap) { 880 void perData(String foundKey, String foundData) 881 throws DatabaseException { 882 883 if (!prevKey.equals("")) { 884 assertTrue(foundKey.compareTo(prevKey) <= 0); 885 } 886 if (addedDataMap.get(foundKey) == null) { 887 String newKey = nextKey(foundKey); 888 String newData = 889 Integer.toString(dataMap.size() + nEntries); 890 putAndVerifyCursor(cursor2, 891 new StringDbt(newKey), 892 new StringDbt(newData), 893 true); 894 newKey = prevKey(foundKey); 895 newData = Integer.toString(dataMap.size() + nEntries); 896 putAndVerifyCursor(cursor2, 897 new StringDbt(newKey), 898 new StringDbt(newData), 899 true); 900 addedDataMap.put(newKey, newData); 901 prevKey = foundKey; 902 assertTrue(dataMap.get(foundKey) != null); 903 dataMap.remove(foundKey); 904 } else { 905 addedDataMap.remove(foundKey); 906 } 907 } 908 909 OperationStatus getFirst(StringDbt foundKey, 910 StringDbt foundData) 911 throws DatabaseException { 912 913 return cursor.getLast(foundKey, foundData, 914 LockMode.DEFAULT); 915 } 916 917 OperationStatus getData(StringDbt foundKey, 918 StringDbt foundData) 919 throws DatabaseException { 920 921 return cursor.getPrev(foundKey, foundData, 922 LockMode.DEFAULT); 923 } 924 }; 925 dw.walkData(); 926 if (dataMap.size() > 0) { 927 fail("dataMap still has entries"); 928 } 929 if (addedDataMap.size() > 0) { 930 System.out.println(addedDataMap); 931 fail("addedDataMap still has entries"); 932 } 933 assertTrue(dw.nEntries == nKeys * 2); 934 } 935 936 944 public void testLargeGetPutRandomKeyForwardTraverse() 945 throws Throwable { 946 947 try { 948 initEnv(false); 949 doLargeGetPutRandomKeyForwardTraverse(N_KEYS); 950 } catch (Throwable t) { 951 t.printStackTrace(); 952 throw t; 953 } 954 } 955 956 959 private void doLargeGetPutRandomKeyForwardTraverse(int nKeys) 960 throws DatabaseException { 961 962 Hashtable dataMap = new Hashtable (); 963 Hashtable addedDataMap = new Hashtable (); 964 doLargePut(dataMap, nKeys); 965 966 DataWalker dw = new DataWalker(dataMap, addedDataMap) { 967 void perData(String foundKey, String foundData) 968 throws DatabaseException { 969 970 assertTrue(foundKey.compareTo(prevKey) >= 0); 971 byte[] key = new byte[N_KEY_BYTES]; 972 TestUtils.generateRandomAlphaBytes(key); 973 String newKey = new String (key); 974 String newData = 975 Integer.toString(dataMap.size() + nEntries); 976 putAndVerifyCursor(cursor2, 977 new StringDbt(newKey), 978 new StringDbt(newData), 979 true); 980 if (newKey.compareTo(foundKey) > 0) { 981 addedDataMap.put(newKey, newData); 982 extraVisibleEntries++; 983 } 984 if (addedDataMap.get(foundKey) == null) { 985 prevKey = foundKey; 986 assertTrue(dataMap.get(foundKey) != null); 987 dataMap.remove(foundKey); 988 } else { 989 if (addedDataMap.remove(foundKey) == null) { 990 fail(foundKey + " not found in either datamap"); 991 } 992 } 993 } 994 }; 995 dw.walkData(); 996 if (dataMap.size() > 0) { 997 fail("dataMap still has entries"); 998 } 999 if (addedDataMap.size() > 0) { 1000 System.out.println(addedDataMap); 1001 fail("addedDataMap still has entries"); 1002 } 1003 assertTrue(dw.nEntries == nKeys + dw.extraVisibleEntries); 1004 } 1005 1006 1014 public void testLargeGetPutRandomKeyBackwardsTraverse() 1015 throws Throwable { 1016 1017 try { 1018 initEnv(false); 1019 doLargeGetPutRandomKeyBackwardsTraverse(N_KEYS); 1020 } catch (Throwable t) { 1021 t.printStackTrace(); 1022 throw t; 1023 } 1024 } 1025 1026 1029 private void doLargeGetPutRandomKeyBackwardsTraverse(int nKeys) 1030 throws DatabaseException { 1031 1032 Hashtable dataMap = new Hashtable (); 1033 Hashtable addedDataMap = new Hashtable (); 1034 doLargePut(dataMap, nKeys); 1035 1036 DataWalker dw = new BackwardsDataWalker(dataMap, addedDataMap) { 1037 void perData(String foundKey, String foundData) 1038 throws DatabaseException { 1039 1040 if (!prevKey.equals("")) { 1041 assertTrue(foundKey.compareTo(prevKey) <= 0); 1042 } 1043 byte[] key = new byte[N_KEY_BYTES]; 1044 TestUtils.generateRandomAlphaBytes(key); 1045 String newKey = new String (key); 1046 String newData = 1047 Integer.toString(dataMap.size() + nEntries); 1048 putAndVerifyCursor(cursor2, 1049 new StringDbt(newKey), 1050 new StringDbt(newData), 1051 true); 1052 if (newKey.compareTo(foundKey) < 0) { 1053 addedDataMap.put(newKey, newData); 1054 extraVisibleEntries++; 1055 } 1056 if (addedDataMap.get(foundKey) == null) { 1057 prevKey = foundKey; 1058 assertTrue(dataMap.get(foundKey) != null); 1059 dataMap.remove(foundKey); 1060 } else { 1061 if (addedDataMap.remove(foundKey) == null) { 1062 fail(foundKey + " not found in either datamap"); 1063 } 1064 } 1065 } 1066 1067 OperationStatus getFirst(StringDbt foundKey, 1068 StringDbt foundData) 1069 throws DatabaseException { 1070 1071 return cursor.getLast(foundKey, foundData, 1072 LockMode.DEFAULT); 1073 } 1074 1075 OperationStatus getData(StringDbt foundKey, 1076 StringDbt foundData) 1077 throws DatabaseException { 1078 1079 return cursor.getPrev(foundKey, foundData, 1080 LockMode.DEFAULT); 1081 } 1082 }; 1083 dw.walkData(); 1084 if (dataMap.size() > 0) { 1085 fail("dataMap still has entries"); 1086 } 1087 if (addedDataMap.size() > 0) { 1088 System.out.println(addedDataMap); 1089 fail("addedDataMap still has entries"); 1090 } 1091 assertTrue(dw.nEntries == nKeys + dw.extraVisibleEntries); 1092 } 1093 1094 1099 public void testLargeGetForwardTraverseWithNormalComparisonFunction() 1100 throws Throwable { 1101 1102 try { 1103 tearDown(); 1104 btreeComparisonFunction = btreeComparator; 1105 setUp(); 1106 initEnv(false); 1107 doLargeGetForwardTraverseWithNormalComparisonFunction(); 1108 } catch (Throwable t) { 1109 t.printStackTrace(); 1110 throw t; 1111 } 1112 } 1113 1114 1117 private void doLargeGetForwardTraverseWithNormalComparisonFunction() 1118 throws DatabaseException { 1119 1120 Hashtable dataMap = new Hashtable (); 1121 doLargePut(dataMap, N_KEYS); 1122 1123 DataWalker dw = new DataWalker(dataMap) { 1124 void perData(String foundKey, String foundData) 1125 throws DatabaseException { 1126 1127 assertTrue(foundKey.compareTo(prevKey) >= 0); 1128 prevKey = foundKey; 1129 assertTrue(dataMap.get(foundKey) != null); 1130 dataMap.remove(foundKey); 1131 } 1132 }; 1133 dw.walkData(); 1134 if (dataMap.size() > 0) { 1135 fail("dataMap still has entries"); 1136 } 1137 assertTrue(dw.nEntries == N_KEYS); 1138 } 1139 1140 1145 public void testLargeGetForwardTraverseWithReverseComparisonFunction() 1146 throws Throwable { 1147 1148 try { 1149 tearDown(); 1150 btreeComparisonFunction = reverseBtreeComparator; 1151 setUp(); 1152 initEnv(false); 1153 doLargeGetForwardTraverseWithReverseComparisonFunction(); 1154 } catch (Throwable t) { 1155 t.printStackTrace(); 1156 throw t; 1157 } 1158 } 1159 1160 1163 private void doLargeGetForwardTraverseWithReverseComparisonFunction() 1164 throws DatabaseException { 1165 1166 Hashtable dataMap = new Hashtable (); 1167 doLargePut(dataMap, N_KEYS); 1168 1169 DataWalker dw = new DataWalker(dataMap) { 1170 void perData(String foundKey, String foundData) 1171 throws DatabaseException { 1172 1173 if (prevKey.length() != 0) { 1174 assertTrue(foundKey.compareTo(prevKey) <= 0); 1175 } 1176 prevKey = foundKey; 1177 assertTrue(dataMap.get(foundKey) != null); 1178 dataMap.remove(foundKey); 1179 } 1180 }; 1181 dw.walkData(); 1182 if (dataMap.size() > 0) { 1183 fail("dataMap still has entries"); 1184 } 1185 assertTrue(dw.nEntries == N_KEYS); 1186 } 1187 1188 public void testIllegalArgs() 1189 throws Throwable { 1190 1191 try { 1192 initEnv(false); 1193 1194 doSimpleCursorPuts(); 1195 1196 DataWalker dw = new DataWalker(simpleDataMap) { 1197 void perData(String foundKey, String foundData) { 1198 1199 1200 try { 1201 cursor.getCurrent(new StringDbt(""), 1202 null, 1203 LockMode.DEFAULT); 1204 fail("didn't throw NullPointerException"); 1205 } catch (NullPointerException IAE) { 1206 } catch (DatabaseException DBE) { 1207 fail("threw DatabaseException not " + 1208 "NullPointerException"); 1209 } 1210 1211 try { 1212 cursor.getCurrent(null, 1213 new StringDbt(""), 1214 LockMode.DEFAULT); 1215 fail("didn't throw NullPointerException"); 1216 } catch (NullPointerException IAE) { 1217 } catch (DatabaseException DBE) { 1218 fail("threw DatabaseException not " + 1219 "NullPointerException"); 1220 } 1221 1222 1223 try { 1224 cursor.getFirst(new StringDbt(""), 1225 null, 1226 LockMode.DEFAULT); 1227 fail("didn't throw NullPointerException"); 1228 } catch (NullPointerException IAE) { 1229 } catch (DatabaseException DBE) { 1230 fail("threw DatabaseException not " + 1231 "NullPointerException"); 1232 } 1233 1234 try { 1235 cursor.getFirst(null, 1236 new StringDbt(""), 1237 LockMode.DEFAULT); 1238 fail("didn't throw NullPointerException"); 1239 } catch (NullPointerException IAE) { 1240 } catch (DatabaseException DBE) { 1241 fail("threw DatabaseException not " + 1242 "NullPointerException"); 1243 } 1244 1245 1247 try { 1248 cursor.getNext(new StringDbt(""), 1249 null, 1250 LockMode.DEFAULT); 1251 fail("didn't throw NullPointerException"); 1252 } catch (NullPointerException IAE) { 1253 } catch (DatabaseException DBE) { 1254 fail("threw DatabaseException not " + 1255 "NullPointerException"); 1256 } 1257 1258 try { 1259 cursor.getNext(null, 1260 new StringDbt(""), 1261 LockMode.DEFAULT); 1262 fail("didn't throw NullPointerException"); 1263 } catch (NullPointerException IAE) { 1264 } catch (DatabaseException DBE) { 1265 fail("threw DatabaseException not " + 1266 "NullPointerException"); 1267 } 1268 1269 1270 try { 1271 cursor.put(new StringDbt(""), null); 1272 fail("didn't throw NullPointerException"); 1273 } catch (NullPointerException IAE) { 1274 } catch (DatabaseException DBE) { 1275 fail("threw DatabaseException not " + 1276 "NullPointerException"); 1277 } 1278 1279 try { 1280 cursor.put(null, new StringDbt("")); 1281 fail("didn't throw NullPointerException"); 1282 } catch (NullPointerException IAE) { 1283 } catch (DatabaseException DBE) { 1284 fail("threw DatabaseException not " + 1285 "NullPointerException"); 1286 } 1287 } 1288 }; 1289 dw.walkData(); 1290 assertTrue(dw.nEntries == simpleKeyStrings.length); 1291 } catch (Throwable t) { 1292 t.printStackTrace(); 1293 throw t; 1294 } 1295 } 1296 1297 public void testCursorOutOfBoundsBackwards() 1298 throws Throwable { 1299 1300 try { 1301 initEnv(false); 1302 int count = 0; 1303 doSimpleCursorPuts(); 1304 1305 StringDbt foundKey = new StringDbt(); 1306 StringDbt foundData = new StringDbt(); 1307 OperationStatus status; 1308 status = cursor.getFirst(foundKey, foundData, LockMode.DEFAULT); 1309 1310 assertEquals(OperationStatus.SUCCESS, status); 1311 assertEquals("aaa", foundKey.getString()); 1312 assertEquals("four", foundData.getString()); 1313 1314 status = cursor.getPrev(foundKey, foundData, LockMode.DEFAULT); 1315 1316 assertEquals(OperationStatus.NOTFOUND, status); 1317 1318 status = cursor.getNext(foundKey, foundData, LockMode.DEFAULT); 1319 1320 assertEquals(OperationStatus.SUCCESS, status); 1321 assertEquals("bar", foundKey.getString()); 1322 assertEquals("two", foundData.getString()); 1323 } catch (Throwable t) { 1324 t.printStackTrace(); 1325 throw t; 1326 } 1327 } 1328 1329 public void testCursorOutOfBoundsForwards() 1330 throws Throwable { 1331 1332 try { 1333 initEnv(false); 1334 int count = 0; 1335 doSimpleCursorPuts(); 1336 1337 StringDbt foundKey = new StringDbt(); 1338 StringDbt foundData = new StringDbt(); 1339 OperationStatus status; 1340 status = cursor.getLast(foundKey, foundData, LockMode.DEFAULT); 1341 1342 assertEquals(OperationStatus.SUCCESS, status); 1343 assertEquals("quux", foundKey.getString()); 1344 assertEquals("seven", foundData.getString()); 1345 1346 status = cursor.getNext(foundKey, foundData, LockMode.DEFAULT); 1347 assertEquals(OperationStatus.NOTFOUND, status); 1348 1349 status = cursor.getPrev(foundKey, foundData, LockMode.DEFAULT); 1350 1351 assertEquals(OperationStatus.SUCCESS, status); 1352 assertEquals("mumble", foundKey.getString()); 1353 assertEquals("eight", foundData.getString()); 1354 } catch (Throwable t) { 1355 t.printStackTrace(); 1356 throw t; 1357 } 1358 } 1359 1360 public void testTwiceClosedCursor() 1361 throws Throwable { 1362 1363 try { 1364 initEnv(false); 1365 doSimpleCursorPuts(); 1366 Cursor cursor = exampleDb.openCursor(null, null); 1367 cursor.close(); 1368 try { 1369 cursor.close(); 1370 fail("didn't catch DatabaseException for twice closed cursor"); 1371 } catch (DatabaseException DBE) { 1372 } 1373 try { 1374 cursor.put 1375 (new StringDbt("bogus"), new StringDbt("thingy")); 1376 fail("didn't catch DatabaseException for re-use of cursor"); 1377 } catch (DatabaseException DBE) { 1378 } 1379 } catch (Throwable t) { 1380 t.printStackTrace(); 1381 throw t; 1382 } 1383 } 1384 1385 public void testTreeSplittingWithDeletedIdKey() 1386 throws Throwable { 1387 1388 treeSplittingWithDeletedIdKeyWorker(); 1389 } 1390 1391 public void testTreeSplittingWithDeletedIdKeyWithUserComparison() 1392 throws Throwable { 1393 1394 tearDown(); 1395 btreeComparisonFunction = btreeComparator; 1396 setUp(); 1397 treeSplittingWithDeletedIdKeyWorker(); 1398 } 1399 1400 static private Comparator btreeComparator = new BtreeComparator(); 1401 1402 static private Comparator reverseBtreeComparator = 1403 new ReverseBtreeComparator(); 1404 1405 private void treeSplittingWithDeletedIdKeyWorker() 1406 throws Throwable { 1407 1408 initEnv(false); 1409 StringDbt data = new StringDbt("data"); 1410 1411 Cursor cursor = exampleDb.openCursor(null, null); 1412 cursor.put(new StringDbt("AGPFX"), data); 1413 cursor.put(new StringDbt("AHHHH"), data); 1414 cursor.put(new StringDbt("AIIII"), data); 1415 cursor.put(new StringDbt("AAAAA"), data); 1416 cursor.put(new StringDbt("AABBB"), data); 1417 cursor.put(new StringDbt("AACCC"), data); 1418 cursor.close(); 1419 exampleDb.delete(null, new StringDbt("AGPFX")); 1420 exampleEnv.compress(); 1421 cursor = exampleDb.openCursor(null, null); 1422 cursor.put(new StringDbt("AAAAB"), data); 1423 cursor.put(new StringDbt("AAAAC"), data); 1424 cursor.close(); 1425 validateDatabase(); 1426 } 1427} 1428 | Popular Tags |