1 8 9 package com.sleepycat.je; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 14 import junit.framework.TestCase; 15 16 import com.sleepycat.je.config.EnvironmentParams; 17 import com.sleepycat.je.dbi.INList; 18 import com.sleepycat.je.dbi.MemoryBudget; 19 import com.sleepycat.je.junit.JUnitThread; 20 import com.sleepycat.je.log.LogUtils; 21 import com.sleepycat.je.tree.Key; 22 import com.sleepycat.je.util.TestUtils; 23 24 27 public class DatabaseTest extends TestCase { 28 private static final boolean DEBUG = false; 29 private static final int NUM_RECS = 257; 30 private static final int NUM_DUPS = 10; 31 32 private File envHome; 33 private Environment env; 34 35 public DatabaseTest() { 36 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 37 } 38 39 public void setUp() 40 throws IOException { 41 42 TestUtils.removeLogFiles("Setup", envHome, false); 43 } 44 45 public void tearDown() 46 throws Exception { 47 48 try { 49 50 if (env != null) { 51 env.close(); 52 } 53 } catch (DatabaseException e) { 54 55 } 56 env = null; TestUtils.removeLogFiles("TearDown", envHome, false); 58 } 59 60 64 public void testCursor() 65 throws Exception { 66 67 Environment txnalEnv = null; 68 Database nonTxnalDb = null; 69 Cursor txnalCursor = null; 70 Transaction txn = null; 71 72 try { 73 74 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 75 envConfig.setTransactional(true); 76 envConfig.setAllowCreate(true); 77 txnalEnv = new Environment(envHome, envConfig); 78 79 DatabaseConfig dbConfig = new DatabaseConfig(); 81 dbConfig.setAllowCreate(true); 82 dbConfig.setTransactional(false); 83 nonTxnalDb = txnalEnv.openDatabase(null, "testDB", dbConfig); 84 85 txn = txnalEnv.beginTransaction(null, null); 87 try { 88 txnalCursor = nonTxnalDb.openCursor(txn, null); 89 fail("Openin a txnal cursor on a nontxnal db is invalid."); 90 } catch (DatabaseException e) { 91 } 93 } finally { 94 if (txn != null) { 95 txn.abort(); 96 } 97 if (txnalCursor != null) { 98 txnalCursor.close(); 99 } 100 if (nonTxnalDb != null) { 101 nonTxnalDb.close(); 102 } 103 if (txnalEnv != null) { 104 txnalEnv.close(); 105 } 106 107 } 108 } 109 110 public void testPutExisting() 111 throws Throwable { 112 113 try { 114 Database myDb = initEnvAndDb(true, false, true, false, null); 115 DatabaseEntry key = new DatabaseEntry(); 116 DatabaseEntry data = new DatabaseEntry(); 117 DatabaseEntry getData = new DatabaseEntry(); 118 Transaction txn = env.beginTransaction(null, null); 119 for (int i = NUM_RECS; i > 0; i--) { 120 key.setData(TestUtils.getTestArray(i)); 121 data.setData(TestUtils.getTestArray(i)); 122 assertEquals(OperationStatus.SUCCESS, 123 myDb.put(txn, key, data)); 124 assertEquals(OperationStatus.SUCCESS, 125 myDb.get(txn, key, getData, LockMode.DEFAULT)); 126 assertEquals(0, Key.compareKeys(data.getData(), 127 getData.getData(), null)); 128 assertEquals(OperationStatus.SUCCESS, 129 myDb.put(txn, key, data)); 130 assertEquals(OperationStatus.SUCCESS, myDb.getSearchBoth 131 (txn, key, getData, LockMode.DEFAULT)); 132 assertEquals(0, Key.compareKeys(data.getData(), 133 getData.getData(), null)); 134 } 135 txn.commit(); 136 myDb.close(); 137 env.close(); 138 } catch (Throwable t) { 139 t.printStackTrace(); 140 throw t; 141 } 142 } 143 144 147 public void testZeroLengthData() 148 throws Throwable { 149 150 try { 151 Database myDb = initEnvAndDb(true, false, true, false, null); 152 DatabaseEntry key = new DatabaseEntry(); 153 DatabaseEntry data = new DatabaseEntry(); 154 DatabaseEntry getData = new DatabaseEntry(); 155 Transaction txn = env.beginTransaction(null, null); 156 byte[] appZLBA = new byte[0]; 157 for (int i = NUM_RECS; i > 0; i--) { 158 key.setData(TestUtils.getTestArray(i)); 159 data.setData(appZLBA); 160 assertEquals(OperationStatus.SUCCESS, 161 myDb.put(txn, key, data)); 162 assertEquals(OperationStatus.SUCCESS, 163 myDb.get(txn, key, getData, LockMode.DEFAULT)); 164 assertFalse(getData.getData() == appZLBA); 165 assertTrue(getData.getData() == 166 LogUtils.ZERO_LENGTH_BYTE_ARRAY); 167 assertEquals(0, Key.compareKeys(data.getData(), 168 getData.getData(), null)); 169 } 170 txn.commit(); 171 myDb.close(); 172 env.close(); 173 174 177 178 myDb = initEnvAndDb(true, false, true, false, null); 179 key = new DatabaseEntry(); 180 data = new DatabaseEntry(); 181 getData = new DatabaseEntry(); 182 txn = env.beginTransaction(null, null); 183 for (int i = NUM_RECS; i > 0; i--) { 184 key.setData(TestUtils.getTestArray(i)); 185 assertEquals(OperationStatus.SUCCESS, 186 myDb.get(txn, key, getData, LockMode.DEFAULT)); 187 assertTrue(getData.getData() == 188 LogUtils.ZERO_LENGTH_BYTE_ARRAY); 189 } 190 txn.commit(); 191 myDb.close(); 192 env.close(); 193 } catch (Throwable t) { 194 t.printStackTrace(); 195 throw t; 196 } 197 } 198 199 public void testDeleteNonDup() 200 throws Throwable { 201 202 try { 203 Database myDb = initEnvAndDb(true, false, true, false, null); 204 DatabaseEntry key = new DatabaseEntry(); 205 DatabaseEntry data = new DatabaseEntry(); 206 DatabaseEntry getData = new DatabaseEntry(); 207 Transaction txn = env.beginTransaction(null, null); 208 for (int i = NUM_RECS; i > 0; i--) { 209 key.setData(TestUtils.getTestArray(i)); 210 data.setData(TestUtils.getTestArray(i)); 211 assertEquals(OperationStatus.SUCCESS, 212 myDb.put(txn, key, data)); 213 } 214 215 for (int i = NUM_RECS; i > 0; i--) { 216 key.setData(TestUtils.getTestArray(i)); 217 data.setData(TestUtils.getTestArray(i)); 218 assertEquals(OperationStatus.SUCCESS, 219 myDb.delete(txn, key)); 220 OperationStatus status = 221 myDb.get(txn, key, getData, LockMode.DEFAULT); 222 if (status != OperationStatus.KEYEMPTY && 223 status != OperationStatus.NOTFOUND) { 224 fail("invalid Database.get return: " + status); 225 } 226 assertEquals(OperationStatus.NOTFOUND, 227 myDb.delete(txn, key)); 228 } 229 txn.commit(); 230 myDb.close(); 231 env.close(); 232 } catch (Throwable t) { 233 t.printStackTrace(); 234 throw t; 235 } 236 } 237 238 public void testDeleteDup() 239 throws Throwable { 240 241 try { 242 Database myDb = initEnvAndDb(true, true, true, false, null); 243 DatabaseEntry key = new DatabaseEntry(); 244 DatabaseEntry data = new DatabaseEntry(); 245 DatabaseEntry getData = new DatabaseEntry(); 246 Transaction txn = env.beginTransaction(null, null); 247 for (int i = NUM_RECS; i > 0; i--) { 248 key.setData(TestUtils.getTestArray(i)); 249 data.setData(TestUtils.getTestArray(i)); 250 assertEquals(OperationStatus.SUCCESS, 251 myDb.put(txn, key, data)); 252 for (int j = 0; j < NUM_DUPS; j++) { 253 data.setData(TestUtils.getTestArray(i + j)); 254 assertEquals(OperationStatus.SUCCESS, 255 myDb.put(txn, key, data)); 256 } 257 } 258 txn.commit(); 259 260 txn = env.beginTransaction(null, null); 261 for (int i = NUM_RECS; i > 0; i--) { 262 key.setData(TestUtils.getTestArray(i)); 263 data.setData(TestUtils.getTestArray(i)); 264 assertEquals(OperationStatus.SUCCESS, 265 myDb.delete(txn, key)); 266 OperationStatus status = 267 myDb.get(txn, key, getData, LockMode.DEFAULT); 268 if (status != OperationStatus.KEYEMPTY && 269 status != OperationStatus.NOTFOUND) { 270 fail("invalid Database.get return"); 271 } 272 assertEquals(OperationStatus.NOTFOUND, 273 myDb.delete(txn, key)); 274 } 275 txn.commit(); 276 myDb.close(); 277 env.close(); 278 279 } catch (Throwable t) { 280 t.printStackTrace(); 281 throw t; 282 } 283 } 284 285 377 378 public void testPutDuplicate() 379 throws Throwable { 380 381 try { 382 Database myDb = initEnvAndDb(true, true, true, false, null); 383 DatabaseEntry key = new DatabaseEntry(); 384 DatabaseEntry data = new DatabaseEntry(); 385 Transaction txn = env.beginTransaction(null, null); 386 for (int i = NUM_RECS; i > 0; i--) { 387 key.setData(TestUtils.getTestArray(i)); 388 data.setData(TestUtils.getTestArray(i)); 389 assertEquals(OperationStatus.SUCCESS, 390 myDb.put(txn, key, data)); 391 data.setData(TestUtils.getTestArray(i * 2)); 392 assertEquals(OperationStatus.SUCCESS, 393 myDb.put(txn, key, data)); 394 } 395 txn.commit(); 396 myDb.close(); 397 env.close(); 398 } catch (Throwable t) { 399 t.printStackTrace(); 400 throw t; 401 } 402 } 403 404 public void testPutNoDupData() 405 throws Throwable { 406 try { 407 Database myDb = initEnvAndDb(true, true, true, false, null); 408 DatabaseEntry key = new DatabaseEntry(); 409 DatabaseEntry data = new DatabaseEntry(); 410 Transaction txn = env.beginTransaction(null, null); 411 for (int i = NUM_RECS; i > 0; i--) { 412 key.setData(TestUtils.getTestArray(i)); 413 data.setData(TestUtils.getTestArray(i)); 414 assertEquals(OperationStatus.SUCCESS, 415 myDb.putNoDupData(txn, key, data)); 416 assertEquals(OperationStatus.KEYEXIST, 417 myDb.putNoDupData(txn, key, data)); 418 data.setData(TestUtils.getTestArray(i+1)); 419 assertEquals(OperationStatus.SUCCESS, 420 myDb.putNoDupData(txn, key, data)); 421 } 422 txn.commit(); 423 myDb.close(); 424 env.close(); 425 } catch (Throwable t) { 426 t.printStackTrace(); 427 throw t; 428 } 429 } 430 431 public void testPutNoOverwriteInANoDupDb() 432 throws Throwable { 433 434 try { 435 Database myDb = initEnvAndDb(true, false, true, false, null); 436 DatabaseEntry key = new DatabaseEntry(); 437 DatabaseEntry data = new DatabaseEntry(); 438 Transaction txn = env.beginTransaction(null, null); 439 for (int i = NUM_RECS; i > 0; i--) { 440 key.setData(TestUtils.getTestArray(i)); 441 data.setData(TestUtils.getTestArray(i)); 442 assertEquals(OperationStatus.SUCCESS, 443 myDb.putNoOverwrite(txn, key, data)); 444 assertEquals(OperationStatus.KEYEXIST, 445 myDb.putNoOverwrite(txn, key, data)); 446 } 447 txn.commit(); 448 myDb.close(); 449 env.close(); 450 } catch (Throwable t) { 451 t.printStackTrace(); 452 throw t; 453 } 454 } 455 456 public void testPutNoOverwriteInADupDbTxn() 457 throws Throwable { 458 459 try { 460 Database myDb = initEnvAndDb(true, true, true, false, null); 461 DatabaseEntry key = new DatabaseEntry(); 462 DatabaseEntry data = new DatabaseEntry(); 463 for (int i = NUM_RECS; i > 0; i--) { 464 Transaction txn1 = env.beginTransaction(null, null); 465 key.setData(TestUtils.getTestArray(i)); 466 data.setData(TestUtils.getTestArray(i)); 467 assertEquals(OperationStatus.SUCCESS, 468 myDb.putNoOverwrite(txn1, key, data)); 469 assertEquals(OperationStatus.KEYEXIST, 470 myDb.putNoOverwrite(txn1, key, data)); 471 data.setData(TestUtils.getTestArray(i << 1)); 472 assertEquals(OperationStatus.SUCCESS, 473 myDb.put(txn1, key, data)); 474 data.setData(TestUtils.getTestArray(i << 2)); 475 assertEquals(OperationStatus.KEYEXIST, 476 myDb.putNoOverwrite(txn1, key, data)); 477 assertEquals(OperationStatus.SUCCESS, 478 myDb.delete(txn1, key)); 479 assertEquals(OperationStatus.SUCCESS, 480 myDb.putNoOverwrite(txn1, key, data)); 481 txn1.commit(); 482 } 483 myDb.close(); 484 env.close(); 485 } catch (Throwable t) { 486 t.printStackTrace(); 487 throw t; 488 } 489 } 490 491 public void testPutNoOverwriteInADupDbNoTxn() 492 throws Throwable { 493 494 try { 495 Database myDb = initEnvAndDb(true, true, false, false, null); 496 DatabaseEntry key = new DatabaseEntry(); 497 DatabaseEntry data = new DatabaseEntry(); 498 for (int i = NUM_RECS; i > 0; i--) { 499 key.setData(TestUtils.getTestArray(i)); 500 data.setData(TestUtils.getTestArray(i)); 501 assertEquals(OperationStatus.SUCCESS, 502 myDb.putNoOverwrite(null, key, data)); 503 assertEquals(OperationStatus.KEYEXIST, 504 myDb.putNoOverwrite(null, key, data)); 505 data.setData(TestUtils.getTestArray(i << 1)); 506 assertEquals(OperationStatus.SUCCESS, 507 myDb.put(null, key, data)); 508 data.setData(TestUtils.getTestArray(i << 2)); 509 assertEquals(OperationStatus.KEYEXIST, 510 myDb.putNoOverwrite(null, key, data)); 511 assertEquals(OperationStatus.SUCCESS, 512 myDb.delete(null, key)); 513 assertEquals(OperationStatus.SUCCESS, 514 myDb.putNoOverwrite(null, key, data)); 515 } 516 myDb.close(); 517 env.close(); 518 } catch (Throwable t) { 519 t.printStackTrace(); 520 throw t; 521 } 522 } 523 524 public void testDatabaseCount() 525 throws Throwable { 526 527 try { 528 Database myDb = initEnvAndDb(true, false, true, false, null); 529 DatabaseEntry key = new DatabaseEntry(); 530 DatabaseEntry data = new DatabaseEntry(); 531 Transaction txn = env.beginTransaction(null, null); 532 for (int i = NUM_RECS; i > 0; i--) { 533 key.setData(TestUtils.getTestArray(i)); 534 data.setData(TestUtils.getTestArray(i)); 535 assertEquals(OperationStatus.SUCCESS, 536 myDb.put(txn, key, data)); 537 } 538 539 long count = myDb.count(); 540 assertEquals(NUM_RECS, count); 541 542 txn.commit(); 543 myDb.close(); 544 env.close(); 545 } catch (Throwable t) { 546 t.printStackTrace(); 547 throw t; 548 } 549 } 550 551 public void testDeferredWriteDatabaseCount() 552 throws Throwable { 553 554 try { 555 Database myDb = initEnvAndDb(true, false, true, true, null); 556 DatabaseEntry key = new DatabaseEntry(); 557 DatabaseEntry data = new DatabaseEntry(); 558 for (int i = NUM_RECS; i > 0; i--) { 559 key.setData(TestUtils.getTestArray(i)); 560 data.setData(TestUtils.getTestArray(i)); 561 assertEquals(OperationStatus.SUCCESS, 562 myDb.put(null, key, data)); 563 } 564 565 long count = myDb.count(); 566 assertEquals(NUM_RECS, count); 567 568 myDb.close(); 569 env.close(); 570 } catch (Throwable t) { 571 t.printStackTrace(); 572 throw t; 573 } 574 } 575 576 public void testStat() 577 throws Throwable { 578 579 try { 580 Database myDb = initEnvAndDb(true, false, true, false, null); 581 DatabaseEntry key = new DatabaseEntry(); 582 DatabaseEntry data = new DatabaseEntry(); 583 Transaction txn = env.beginTransaction(null, null); 584 for (int i = NUM_RECS; i > 0; i--) { 585 key.setData(TestUtils.getTestArray(i)); 586 data.setData(TestUtils.getTestArray(i)); 587 assertEquals(OperationStatus.SUCCESS, 588 myDb.put(txn, key, data)); 589 } 590 591 BtreeStats stat = (BtreeStats) 592 myDb.getStats(TestUtils.FAST_STATS); 593 594 assertEquals(0, stat.getInternalNodeCount()); 595 assertEquals(0, stat.getDuplicateInternalNodeCount()); 596 assertEquals(0, stat.getBottomInternalNodeCount()); 597 assertEquals(0, stat.getDuplicateBottomInternalNodeCount()); 598 assertEquals(0, stat.getLeafNodeCount()); 599 assertEquals(0, stat.getDeletedLeafNodeCount()); 600 assertEquals(0, stat.getDupCountLeafNodeCount()); 601 assertEquals(0, stat.getMainTreeMaxDepth()); 602 assertEquals(0, stat.getDuplicateTreeMaxDepth()); 603 604 stat = (BtreeStats) myDb.getStats(null); 605 606 assertEquals(15, stat.getInternalNodeCount()); 607 assertEquals(0, stat.getDuplicateInternalNodeCount()); 608 assertEquals(52, stat.getBottomInternalNodeCount()); 609 assertEquals(0, stat.getDuplicateBottomInternalNodeCount()); 610 assertEquals(NUM_RECS, stat.getLeafNodeCount()); 611 assertEquals(0, stat.getDeletedLeafNodeCount()); 612 assertEquals(0, stat.getDupCountLeafNodeCount()); 613 assertEquals(4, stat.getMainTreeMaxDepth()); 614 assertEquals(0, stat.getDuplicateTreeMaxDepth()); 615 616 stat = (BtreeStats) myDb.getStats(TestUtils.FAST_STATS); 617 618 assertEquals(15, stat.getInternalNodeCount()); 619 assertEquals(52, stat.getBottomInternalNodeCount()); 620 assertEquals(NUM_RECS, stat.getLeafNodeCount()); 621 assertEquals(0, stat.getDeletedLeafNodeCount()); 622 assertEquals(0, stat.getDupCountLeafNodeCount()); 623 assertEquals(4, stat.getMainTreeMaxDepth()); 624 assertEquals(0, stat.getDuplicateTreeMaxDepth()); 625 626 txn.commit(); 627 myDb.close(); 628 env.close(); 629 } catch (Throwable t) { 630 t.printStackTrace(); 631 throw t; 632 } 633 } 634 635 public void testDatabaseCountEmptyDB() 636 throws Throwable { 637 638 try { 639 Database myDb = initEnvAndDb(true, false, true, false, null); 640 DatabaseEntry key = new DatabaseEntry(); 641 DatabaseEntry data = new DatabaseEntry(); 642 643 long count = myDb.count(); 644 assertEquals(0, count); 645 646 myDb.close(); 647 env.close(); 648 } catch (Throwable t) { 649 t.printStackTrace(); 650 throw t; 651 } 652 } 653 654 public void testDatabaseCountWithDeletedEntries() 655 throws Throwable { 656 657 try { 658 Database myDb = initEnvAndDb(true, false, true, false, null); 659 DatabaseEntry key = new DatabaseEntry(); 660 DatabaseEntry data = new DatabaseEntry(); 661 Transaction txn = env.beginTransaction(null, null); 662 int deletedCount = 0; 663 for (int i = NUM_RECS; i > 0; i--) { 664 key.setData(TestUtils.getTestArray(i)); 665 data.setData(TestUtils.getTestArray(i)); 666 assertEquals(OperationStatus.SUCCESS, 667 myDb.put(txn, key, data)); 668 if ((i % 5) == 0) { 669 myDb.delete(txn, key); 670 deletedCount++; 671 } 672 } 673 674 long count = myDb.count(); 675 assertEquals(NUM_RECS - deletedCount, count); 676 677 txn.commit(); 678 myDb.close(); 679 env.close(); 680 } catch (Throwable t) { 681 t.printStackTrace(); 682 throw t; 683 } 684 } 685 686 public void testStatDups() 687 throws Throwable { 688 689 try { 690 Database myDb = initEnvAndDb(true, true, true, false, null); 691 DatabaseEntry key = new DatabaseEntry(); 692 DatabaseEntry data = new DatabaseEntry(); 693 Transaction txn = env.beginTransaction(null, null); 694 for (int i = NUM_RECS; i > 0; i--) { 695 key.setData(TestUtils.getTestArray(i)); 696 data.setData(TestUtils.getTestArray(i)); 697 assertEquals(OperationStatus.SUCCESS, 698 myDb.put(txn, key, data)); 699 for (int j = 0; j < 10; j++) { 700 data.setData(TestUtils.getTestArray(i + j)); 701 assertEquals(OperationStatus.SUCCESS, 702 myDb.put(txn, key, data)); 703 } 704 } 705 706 BtreeStats stat = (BtreeStats) 707 myDb.getStats(TestUtils.FAST_STATS); 708 709 assertEquals(0, stat.getInternalNodeCount()); 710 assertEquals(0, stat.getDuplicateInternalNodeCount()); 711 assertEquals(0, stat.getBottomInternalNodeCount()); 712 assertEquals(0, stat.getDuplicateBottomInternalNodeCount()); 713 assertEquals(0, stat.getLeafNodeCount()); 714 assertEquals(0, stat.getDeletedLeafNodeCount()); 715 assertEquals(0, stat.getDupCountLeafNodeCount()); 716 assertEquals(0, stat.getMainTreeMaxDepth()); 717 assertEquals(0, stat.getDuplicateTreeMaxDepth()); 718 719 stat = (BtreeStats) myDb.getStats(null); 720 721 assertEquals(23, stat.getInternalNodeCount()); 722 assertEquals(NUM_RECS, stat.getDuplicateInternalNodeCount()); 723 assertEquals(85, stat.getBottomInternalNodeCount()); 724 assertEquals(771, stat.getDuplicateBottomInternalNodeCount()); 725 assertEquals(2570, stat.getLeafNodeCount()); 726 assertEquals(0, stat.getDeletedLeafNodeCount()); 727 assertEquals(NUM_RECS, stat.getDupCountLeafNodeCount()); 728 assertEquals(4, stat.getMainTreeMaxDepth()); 729 assertEquals(2, stat.getDuplicateTreeMaxDepth()); 730 731 stat = (BtreeStats) myDb.getStats(TestUtils.FAST_STATS); 732 733 assertEquals(23, stat.getInternalNodeCount()); 734 assertEquals(NUM_RECS, stat.getDuplicateInternalNodeCount()); 735 assertEquals(85, stat.getBottomInternalNodeCount()); 736 assertEquals(771, stat.getDuplicateBottomInternalNodeCount()); 737 assertEquals(2570, stat.getLeafNodeCount()); 738 assertEquals(0, stat.getDeletedLeafNodeCount()); 739 assertEquals(NUM_RECS, stat.getDupCountLeafNodeCount()); 740 assertEquals(4, stat.getMainTreeMaxDepth()); 741 assertEquals(2, stat.getDuplicateTreeMaxDepth()); 742 743 txn.commit(); 744 myDb.close(); 745 env.close(); 746 } catch (Throwable t) { 747 t.printStackTrace(); 748 throw t; 749 } 750 } 751 752 public void testDatabaseCountDups() 753 throws Throwable { 754 755 try { 756 Database myDb = initEnvAndDb(true, true, true, false, null); 757 DatabaseEntry key = new DatabaseEntry(); 758 DatabaseEntry data = new DatabaseEntry(); 759 Transaction txn = env.beginTransaction(null, null); 760 for (int i = NUM_RECS; i > 0; i--) { 761 key.setData(TestUtils.getTestArray(i)); 762 data.setData(TestUtils.getTestArray(i)); 763 assertEquals(OperationStatus.SUCCESS, 764 myDb.put(txn, key, data)); 765 for (int j = 0; j < 10; j++) { 766 data.setData(TestUtils.getTestArray(i + j)); 767 assertEquals(OperationStatus.SUCCESS, 768 myDb.put(txn, key, data)); 769 } 770 } 771 772 long count = myDb.count(); 773 774 assertEquals(2570, count); 775 776 txn.commit(); 777 myDb.close(); 778 env.close(); 779 } catch (Throwable t) { 780 t.printStackTrace(); 781 throw t; 782 } 783 } 784 785 public void testDeferredWriteDatabaseCountDups() 786 throws Throwable { 787 788 try { 789 Database myDb = initEnvAndDb(true, true, true, true, null); 790 DatabaseEntry key = new DatabaseEntry(); 791 DatabaseEntry data = new DatabaseEntry(); 792 for (int i = NUM_RECS; i > 0; i--) { 793 key.setData(TestUtils.getTestArray(i)); 794 data.setData(TestUtils.getTestArray(i)); 795 assertEquals(OperationStatus.SUCCESS, 796 myDb.put(null, key, data)); 797 for (int j = 0; j < 10; j++) { 798 data.setData(TestUtils.getTestArray(i + j)); 799 assertEquals(OperationStatus.SUCCESS, 800 myDb.put(null, key, data)); 801 } 802 } 803 804 long count = myDb.count(); 805 806 assertEquals(2570, count); 807 808 myDb.close(); 809 env.close(); 810 } catch (Throwable t) { 811 t.printStackTrace(); 812 throw t; 813 } 814 } 815 816 public void testStatDeletes() 817 throws Throwable { 818 819 deleteTestInternal(1, 2, 0, 2); 820 deleteTestInternal(2, 2, 2, 2); 821 deleteTestInternal(10, 2, 10, 10); 822 deleteTestInternal(11, 2, 10, 12); 823 } 824 825 private void deleteTestInternal(int numRecs, 826 int numDupRecs, 827 int expectedLNs, 828 int expectedDeletedLNs) 829 throws Throwable { 830 831 try { 832 TestUtils.removeLogFiles("Setup", envHome, false); 833 Database myDb = initEnvAndDb(true, true, true, false, null); 834 DatabaseEntry key = new DatabaseEntry(); 835 DatabaseEntry data = new DatabaseEntry(); 836 Transaction txn = env.beginTransaction(null, null); 837 for (int i = numRecs; i > 0; i--) { 838 key.setData(TestUtils.getTestArray(i)); 839 for (int j = 0; j < numDupRecs; j++) { 840 data.setData(TestUtils.getTestArray(i + j)); 841 assertEquals(OperationStatus.SUCCESS, 842 myDb.put(txn, key, data)); 843 } 844 } 845 846 for (int i = numRecs; i > 0; i -= 2) { 847 key.setData(TestUtils.getTestArray(i)); 848 assertEquals(OperationStatus.SUCCESS, 849 myDb.delete(txn, key)); 850 } 851 852 BtreeStats stat = (BtreeStats) myDb.getStats(null); 853 854 assertEquals(expectedLNs, stat.getLeafNodeCount()); 855 assertEquals(expectedDeletedLNs, stat.getDeletedLeafNodeCount()); 856 assertEquals(numRecs, stat.getDupCountLeafNodeCount()); 857 858 txn.commit(); 859 myDb.close(); 860 env.close(); 861 } catch (Throwable t) { 862 t.printStackTrace(); 863 throw t; 864 } 865 } 866 867 870 public void testPreloadByteLimit() 871 throws Throwable { 872 873 874 Database myDb = initEnvAndDb(false, false, true, false, null); 875 DatabaseEntry key = new DatabaseEntry(); 876 DatabaseEntry data = new DatabaseEntry(); 877 Transaction txn = env.beginTransaction(null, null); 878 for (int i = 2500; i > 0; i--) { 879 key.setData(TestUtils.getTestArray(i)); 880 data.setData(TestUtils.getTestArray(i)); 881 assertEquals(OperationStatus.SUCCESS, 882 myDb.put(txn, key, data)); 883 } 884 885 886 long postCreateMemUsage = env.getMemoryUsage(); 887 INList inlist = env.getEnvironmentImpl().getInMemoryINs(); 888 long postCreateResidentNodes = inlist.getSize(); 889 txn.commit(); 890 myDb.close(); 891 env.close(); 892 myDb = initEnvAndDb 893 (true, false, true, false, 894 MemoryBudget.MIN_MAX_MEMORY_SIZE_STRING); 895 896 901 env.evictMemory(); env.evictMemory(); 904 long postEvictMemUsage = env.getMemoryUsage(); 905 inlist = env.getEnvironmentImpl().getInMemoryINs(); long postEvictResidentNodes = inlist.getSize(); 907 908 909 PreloadConfig conf = new PreloadConfig(); 910 conf.setMaxBytes(92000); 911 PreloadStats stats = 912 myDb.preload(conf); 913 914 assertEquals(PreloadStatus.FILLED_CACHE, stats.status); 915 916 long postPreloadMemUsage = env.getMemoryUsage(); 917 long postPreloadResidentNodes = inlist.getSize(); 918 919 920 Cursor cursor = myDb.openCursor(null, null); 921 int count = 0; 922 OperationStatus status = cursor.getFirst(key, data, LockMode.DEFAULT); 923 while (status == OperationStatus.SUCCESS) { 924 count++; 925 status = cursor.getNext(key, data, LockMode.DEFAULT); 926 } 927 cursor.close(); 928 929 long postIterationMemUsage = env.getMemoryUsage(); 930 long postIterationResidentNodes = inlist.getSize(); 931 932 if (DEBUG) { 933 System.out.println("postCreateMemUsage: " + postCreateMemUsage); 934 System.out.println("postEvictMemUsage: " + postEvictMemUsage); 935 System.out.println("postPreloadMemUsage: " + postPreloadMemUsage); 936 System.out.println("postIterationMemUsage: " + 937 postIterationMemUsage); 938 System.out.println("postEvictResidentNodes: " + 939 postEvictResidentNodes); 940 System.out.println("postPreloadResidentNodes: " + 941 postPreloadResidentNodes); 942 System.out.println("postIterationResidentNodes: " + 943 postIterationResidentNodes); 944 System.out.println("postCreateResidentNodes: " + 945 postCreateResidentNodes); 946 } 947 948 assertTrue(postEvictMemUsage < postCreateMemUsage); 949 assertTrue(postEvictMemUsage < postPreloadMemUsage); 950 assertTrue("postPreloadMemUsage=" + postPreloadMemUsage + 951 " postIterationMemUsage=" + postIterationMemUsage, 952 postPreloadMemUsage < postIterationMemUsage); 953 assertTrue(postIterationMemUsage <= postCreateMemUsage); 954 assertTrue(postEvictResidentNodes < postPreloadResidentNodes); 955 assertTrue(postCreateResidentNodes >= postIterationResidentNodes); 957 958 myDb.close(); 959 env.close(); 960 } 961 962 public void testPreloadTimeLimit() 963 throws Throwable { 964 965 966 Database myDb = initEnvAndDb(false, false, true, false, null); 967 DatabaseEntry key = new DatabaseEntry(); 968 DatabaseEntry data = new DatabaseEntry(); 969 Transaction txn = env.beginTransaction(null, null); 970 for (int i = 25000; i > 0; i--) { 971 key.setData(TestUtils.getTestArray(i)); 972 data.setData(new byte[1]); 973 assertEquals(OperationStatus.SUCCESS, 974 myDb.put(txn, key, data)); 975 } 976 977 978 long postCreateMemUsage = env.getMemoryUsage(); 979 INList inlist = env.getEnvironmentImpl().getInMemoryINs(); 980 long postCreateResidentNodes = inlist.getSize(); 981 txn.commit(); 982 myDb.close(); 983 env.close(); 984 myDb = initEnvAndDb(true, false, true, false, null); 985 986 991 env.evictMemory(); env.evictMemory(); 994 long postEvictMemUsage = env.getMemoryUsage(); 995 inlist = env.getEnvironmentImpl().getInMemoryINs(); long postEvictResidentNodes = inlist.getSize(); 997 998 999 PreloadConfig conf = new PreloadConfig(); 1000 conf.setMaxMillisecs(50); 1001 PreloadStats stats = myDb.preload(conf); 1002 assertEquals(PreloadStatus.EXCEEDED_TIME, stats.status); 1003 1004 long postPreloadMemUsage = env.getMemoryUsage(); 1005 long postPreloadResidentNodes = inlist.getSize(); 1006 1007 1008 Cursor cursor = myDb.openCursor(null, null); 1009 int count = 0; 1010 OperationStatus status = cursor.getFirst(key, data, LockMode.DEFAULT); 1011 while (status == OperationStatus.SUCCESS) { 1012 count++; 1013 status = cursor.getNext(key, data, LockMode.DEFAULT); 1014 } 1015 cursor.close(); 1016 1017 long postIterationMemUsage = env.getMemoryUsage(); 1018 long postIterationResidentNodes = inlist.getSize(); 1019 1020 if (DEBUG) { 1021 System.out.println("postCreateMemUsage: " + postCreateMemUsage); 1022 System.out.println("postEvictMemUsage: " + postEvictMemUsage); 1023 System.out.println("postPreloadMemUsage: " + postPreloadMemUsage); 1024 System.out.println("postIterationMemUsage: " + 1025 postIterationMemUsage); 1026 System.out.println("postEvictResidentNodes: " + 1027 postEvictResidentNodes); 1028 System.out.println("postPreloadResidentNodes: " + 1029 postPreloadResidentNodes); 1030 System.out.println("postIterationResidentNodes: " + 1031 postIterationResidentNodes); 1032 System.out.println("postCreateResidentNodes: " + 1033 postCreateResidentNodes); 1034 } 1035 1036 assertTrue(postEvictMemUsage < postCreateMemUsage); 1037 assertTrue(postEvictMemUsage < postPreloadMemUsage); 1038 assertTrue("postPreloadMemUsage=" + postPreloadMemUsage + 1039 " postIterationMemUsage=" + postIterationMemUsage, 1040 postPreloadMemUsage < postIterationMemUsage); 1041 assertTrue(postIterationMemUsage <= postCreateMemUsage); 1042 assertTrue(postEvictResidentNodes < postPreloadResidentNodes); 1043 assertTrue(postCreateResidentNodes >= postIterationResidentNodes); 1045 1046 myDb.close(); 1047 env.close(); 1048 } 1049 1050 1053 public void testPreloadEntireDatabase() 1054 throws Throwable { 1055 1056 1057 Database myDb = initEnvAndDb(false, false, false, false, null); 1058 DatabaseEntry key = new DatabaseEntry(); 1059 DatabaseEntry data = new DatabaseEntry(); 1060 key.setData(TestUtils.getTestArray(0)); 1061 data.setData(TestUtils.getTestArray(0)); 1062 assertEquals(OperationStatus.SUCCESS, myDb.put(null, key, data)); 1063 1064 1065 myDb.close(); 1066 env.close(); 1067 myDb = initEnvAndDb(false, false, false, false, null); 1068 1069 1073 PreloadConfig conf = new PreloadConfig(); 1074 conf.setMaxBytes(100000); 1075 myDb.preload(conf); 1076 1077 myDb.close(); 1078 env.close(); 1079 } 1080 1081 1084 public void testPreloadBytesExceedsCache() 1085 throws Throwable { 1086 1087 1088 Database myDb = initEnvAndDb(false, false, false, false, "100000"); 1089 DatabaseEntry key = new DatabaseEntry(); 1090 DatabaseEntry data = new DatabaseEntry(); 1091 key.setData(TestUtils.getTestArray(0)); 1092 data.setData(TestUtils.getTestArray(0)); 1093 assertEquals(OperationStatus.SUCCESS, myDb.put(null, key, data)); 1094 1095 1096 myDb.close(); 1097 env.close(); 1098 myDb = initEnvAndDb(false, false, false, false, "100000"); 1099 1100 1101 try { 1102 PreloadConfig conf = new PreloadConfig(); 1103 conf.setMaxBytes(100001); 1104 myDb.preload(conf); 1105 fail("should have thrown IAE"); 1106 } catch (IllegalArgumentException IAE) { 1107 } 1108 1109 myDb.close(); 1110 env.close(); 1111 } 1112 1113 public void testDbClose() 1114 throws Throwable { 1115 1116 1117 Database myDb = initEnvAndDb(false, false, true, false, null); 1118 DatabaseEntry key = new DatabaseEntry(); 1119 DatabaseEntry data = new DatabaseEntry(); 1120 Transaction txn = env.beginTransaction(null, null); 1121 for (int i = 2500; i > 0; i--) { 1122 key.setData(TestUtils.getTestArray(i)); 1123 data.setData(TestUtils.getTestArray(i)); 1124 assertEquals(OperationStatus.SUCCESS, 1125 myDb.put(txn, key, data)); 1126 } 1127 1128 1129 Cursor cursor = myDb.openCursor(txn, null); 1130 assertEquals(OperationStatus.SUCCESS, 1131 cursor.getFirst(key, data, LockMode.DEFAULT)); 1132 1133 try { 1134 myDb.close(); 1135 fail("didn't throw DatabaseException for unclosed cursor"); 1136 } catch (DatabaseException DBE) { 1137 } 1138 txn.commit(); 1139 env.close(); 1140 } 1141 1142 public void testDbCloseUnopenedDb() 1143 throws DatabaseException { 1144 1145 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 1146 envConfig.setTransactional(true); 1147 envConfig.setAllowCreate(true); 1148 env = new Environment(envHome, envConfig); 1149 Database myDb = new Database(env); 1150 try { 1151 myDb.close(); 1152 } catch (DatabaseException DBE) { 1153 fail("shouldn't catch DatabaseException for closing unopened db"); 1154 } 1155 env.close(); 1156 } 1157 1158 1161 public void testOpenCursor() 1162 throws DatabaseException { 1163 Database db = initEnvAndDb(true, false, true, false, null); 1164 Cursor cursor = db.openCursor(null, null); 1165 cursor.close(); 1166 db.close(); 1167 try { 1168 db.openCursor(null, null); 1169 fail("Should throw exception because databse is closed"); 1170 } catch (DatabaseException e) { 1171 } 1172 } 1173 1174 public void testBufferOverflowingPut() 1175 throws Throwable { 1176 1177 try { 1178 1179 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 1180 envConfig.setTransactional(true); 1181 envConfig.setAllowCreate(true); 1183 env = new Environment(envHome, envConfig); 1184 1185 DatabaseConfig dbConfig = new DatabaseConfig(); 1186 dbConfig.setSortedDuplicates(true); 1187 dbConfig.setAllowCreate(true); 1188 dbConfig.setTransactional(true); 1189 Database myDb = env.openDatabase(null, "testDB", dbConfig); 1190 1191 DatabaseEntry key = new DatabaseEntry(); 1192 DatabaseEntry data = new DatabaseEntry(new byte[10000000]); 1193 try { 1194 key.setData(TestUtils.getTestArray(10)); 1195 myDb.put(null, key, data); 1196 } catch (DatabaseException DE) { 1197 fail("unexpected DatabaseException"); 1198 } 1199 myDb.close(); 1200 env.close(); 1201 env = null; 1202 } catch (Throwable t) { 1203 t.printStackTrace(); 1204 throw t; 1205 } 1206 } 1207 1208 1211 private Database initEnvAndDb(boolean dontRunEvictor, 1212 boolean allowDuplicates, 1213 boolean transactional, 1214 boolean deferredWrite, 1215 String memSize) 1216 throws DatabaseException { 1217 1218 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 1219 envConfig.setTransactional(transactional); 1220 envConfig.setConfigParam 1221 (EnvironmentParams.ENV_CHECK_LEAKS.getName(), "false"); 1222 envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "6"); 1223 envConfig.setConfigParam(EnvironmentParams.NODE_MAX_DUPTREE.getName(), 1224 "6"); 1225 envConfig.setTxnNoSync(Boolean.getBoolean(TestUtils.NO_SYNC)); 1226 if (dontRunEvictor) { 1227 envConfig.setConfigParam(EnvironmentParams. 1228 ENV_RUN_EVICTOR.getName(), 1229 "false"); 1230 1231 1235 envConfig.setConfigParam(EnvironmentParams. 1236 EVICTOR_CRITICAL_PERCENTAGE.getName(), 1237 "500"); 1238 } 1239 1240 if (memSize != null) { 1241 envConfig.setConfigParam(EnvironmentParams. 1242 MAX_MEMORY.getName(), 1243 memSize); 1244 } 1245 1246 envConfig.setAllowCreate(true); 1247 env = new Environment(envHome, envConfig); 1248 1249 1250 DatabaseConfig dbConfig = new DatabaseConfig(); 1251 dbConfig.setSortedDuplicates(allowDuplicates); 1252 dbConfig.setAllowCreate(true); 1253 if (!deferredWrite) { 1254 dbConfig.setTransactional(transactional); 1255 } 1256 dbConfig.setDeferredWrite(deferredWrite); 1257 Database myDb = env.openDatabase(null, "testDB", dbConfig); 1258 return myDb; 1259 } 1260 1261 1265 private Database pNOCDb; 1266 1267 public void xxtestPutNoOverwriteConcurrently() 1268 throws Throwable { 1269 1270 pNOCDb = initEnvAndDb(true, true, true, false, null); 1271 JUnitThread tester1 = 1272 new JUnitThread("testNonBlocking1") { 1273 public void testBody() { 1274 try { 1275 Transaction txn1 = env.beginTransaction(null, null); 1276 DatabaseEntry key = new DatabaseEntry(); 1277 DatabaseEntry data = new DatabaseEntry(); 1278 key.setData(TestUtils.getTestArray(1)); 1279 data.setData(TestUtils.getTestArray(1)); 1280 OperationStatus status = 1281 pNOCDb.putNoOverwrite(txn1, key, data); 1282 txn1.commit(); 1283 System.out.println("thread1: " + status); 1284 } catch (DatabaseException DBE) { 1285 DBE.printStackTrace(); 1286 fail("caught DatabaseException " + DBE); 1287 } 1288 } 1289 }; 1290 1291 JUnitThread tester2 = 1292 new JUnitThread("testNonBlocking2") { 1293 public void testBody() { 1294 try { 1295 Transaction txn2 = env.beginTransaction(null, null); 1296 DatabaseEntry key = new DatabaseEntry(); 1297 DatabaseEntry data = new DatabaseEntry(); 1298 key.setData(TestUtils.getTestArray(1)); 1299 data.setData(TestUtils.getTestArray(2)); 1300 OperationStatus status = 1301 pNOCDb.putNoOverwrite(txn2, key, data); 1302 txn2.commit(); 1303 System.out.println("thread2: " + status); 1304 } catch (DatabaseException DBE) { 1305 DBE.printStackTrace(); 1306 fail("caught DatabaseException " + DBE); 1307 } 1308 } 1309 }; 1310 1311 tester1.start(); 1312 tester2.start(); 1313 tester1.finishTest(); 1314 tester2.finishTest(); 1315 } 1316} 1317 | Popular Tags |