1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2002,2010 Oracle. All rights reserved.
*
* $Id: DatabaseConfigTest.java,v 1.30.2.2 2010/01/04 15:30:42 cwl Exp $
*/
package com.sleepycat.je;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Comparator;
import java.util.Set;
import junit.framework.TestCase;
import com.sleepycat.je.util.TestUtils;
/**
* Basic database configuration testing.
*/
public class DatabaseConfigTest extends TestCase {
private static final boolean DEBUG = false;
private File envHome;
private Environment env;
public DatabaseConfigTest() {
envHome = new File(System.getProperty(TestUtils.DEST_DIR));
}
public void setUp()
throws IOException {
TestUtils.removeLogFiles("Setup", envHome, false);
}
public void tearDown()
throws Exception {
try {
/* Close in case we hit an exception and didn't close. */
if (env != null) {
env.close();
}
} catch (DatabaseException e) {
/* Ok if already closed */
}
env = null; // for JUNIT, to reduce memory usage when run in a suite.
TestUtils.removeLogFiles("TearDown", envHome, false);
}
/**
* Test that we can retrieve a database configuration and that it clones
* its configuration appropriately.
*/
public void testConfig()
throws Throwable {
try {
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
envConfig.setAllowCreate(true);
env = new Environment(envHome, envConfig);
/*
* Make sure that the database keeps its own copy of the
* configuration object.
*/
DatabaseConfig dbConfigA = new DatabaseConfig();
dbConfigA.setAllowCreate(true);
Database dbA = env.openDatabase(null, "foo", dbConfigA);
/* Change the original dbConfig */
dbConfigA.setAllowCreate(false);
DatabaseConfig getConfig1 = dbA.getConfig();
assertEquals(true, getConfig1.getAllowCreate());
assertEquals(false, getConfig1.getSortedDuplicates());
/*
* Change the retrieved config, ought to have no effect on what the
* Database is storing.
*/
getConfig1.setSortedDuplicates(true);
DatabaseConfig getConfig2 = dbA.getConfig();
assertEquals(false, getConfig2.getSortedDuplicates());
dbA.close();
env.close();
} catch (Throwable t) {
t.printStackTrace();
throw t;
}
}
public void testConfigMatching()
throws Throwable {
try {
/* DatabaseConfig matching. */
DatabaseConfig confA = new DatabaseConfig();
DatabaseConfig confB = new DatabaseConfig();
try {
confA.validate(confB);
} catch (Exception E) {
fail("expected valid match");
}
try {
confB.validate(confA);
} catch (Exception E) {
fail("expected valid match");
}
try {
confA.validate(null); // uses the DEFAULT config
} catch (Exception E) {
fail("expected valid match");
}
confA.setReadOnly(true);
try {
confA.validate(confB);
fail("expected exception");
} catch (DatabaseException E) {
// ok
}
confA.setReadOnly(false);
confA.setSortedDuplicates(true);
try {
confA.validate(confB);
fail("expected exception");
} catch (DatabaseException E) {
// ok
}
confA.setSortedDuplicates(false);
confA.setOverrideBtreeComparator(true);
confA.setBtreeComparator(TestComparator.class);
confB.setOverrideBtreeComparator(true);
confB.setBtreeComparator(TestComparator2.class);
try {
confA.validate(confB);
fail("expected exception");
} catch (DatabaseException E) {
// ok
}
confA.setBtreeComparator((Class) null);
confA.setOverrideBtreeComparator(false);
confB.setBtreeComparator((Class) null);
confB.setOverrideBtreeComparator(false);
confA.setOverrideDuplicateComparator(true);
confA.setDuplicateComparator(TestComparator.class);
confB.setOverrideDuplicateComparator(true);
confB.setDuplicateComparator(TestComparator2.class);
try {
confA.validate(confB);
fail("expected exception");
} catch (DatabaseException E) {
// ok
}
/* Same tests as above but for serialized comparators. */
confA.setOverrideBtreeComparator(true);
confA.setBtreeComparator(new TestSerialComparator());
confB.setOverrideBtreeComparator(true);
confB.setBtreeComparator(new TestSerialComparator2());
try {
confA.validate(confB);
fail("expected exception");
} catch (DatabaseException E) {
// ok
}
confA.setBtreeComparator((Comparator) null);
confA.setOverrideBtreeComparator(false);
confB.setBtreeComparator((Comparator) null);
confB.setOverrideBtreeComparator(false);
confA.setOverrideDuplicateComparator(true);
confA.setDuplicateComparator(new TestSerialComparator());
confB.setOverrideDuplicateComparator(true);
confB.setDuplicateComparator(new TestSerialComparator2());
try {
confA.validate(confB);
fail("expected exception");
} catch (DatabaseException E) {
// ok
}
/* SecondaryConfig matching. */
SecondaryConfig confC = new SecondaryConfig();
SecondaryConfig confD = new SecondaryConfig();
confC.setKeyCreator(new SecKeyCreator1());
confD.setKeyCreator(new SecKeyCreator1());
try {
confC.validate(confD);
} catch (Exception E) {
E.printStackTrace();
fail("expected valid match");
}
try {
confD.validate(confC);
} catch (Exception E) {
fail("expected valid match");
}
try {
confC.validate(null);
fail("expected exception");
} catch (DatabaseException E) {
// ok
}
confD.setKeyCreator(new SecKeyCreator2());
try {
confC.validate(confD);
fail("expected exception");
} catch (DatabaseException E) {
// ok
}
confD.setKeyCreator(new SecKeyCreator1());
confD.setMultiKeyCreator(new SecMultiKeyCreator1());
try {
confC.validate(confD);
fail("expected exception");
} catch (DatabaseException E) {
// ok
}
confD.setMultiKeyCreator(null);
confC.setForeignKeyDeleteAction(ForeignKeyDeleteAction.NULLIFY);
try {
confC.validate(confD);
fail("expected exception");
} catch (DatabaseException E) {
// ok
}
confC.setForeignKeyDeleteAction(ForeignKeyDeleteAction.ABORT);
confC.setForeignKeyNullifier(new ForeignKeyNullifier1());
try {
confC.validate(confD);
fail("expected exception");
} catch (DatabaseException E) {
// ok
}
confC.setForeignKeyNullifier(null);
confC.setForeignMultiKeyNullifier(new ForeignMultiKeyNullifier1());
try {
confC.validate(confD);
fail("expected exception");
} catch (DatabaseException E) {
// ok
}
confC.setForeignMultiKeyNullifier(null);
confC.setImmutableSecondaryKey(true);
try {
confC.validate(confD);
fail("expected exception");
} catch (DatabaseException E) {
// ok
}
confC.setImmutableSecondaryKey(false);
} catch (Throwable t) {
t.printStackTrace();
throw t;
}
}
/**
* Make sure we can instantiate a comparator at the time it's set.
*/
public void testComparator()
throws Throwable {
try {
/* Can't be instantiated, a nested class */
try {
DatabaseConfig config = new DatabaseConfig();
config.setBtreeComparator(BadComparator1.class);
fail("Comparator shouldn't be instantiated");
} catch (IllegalArgumentException e) {
/* Expected. */
if (DEBUG) {
System.out.println(e);
}
}
/* No zero-parameter constructor */
try {
DatabaseConfig config = new DatabaseConfig();
config.setBtreeComparator(BadComparator2.class);
fail("Comparator shouldn't be instantiated");
} catch (IllegalArgumentException e) {
/* Expected. */
if (DEBUG) {
System.out.println(e);
}
}
/* Can't be serialized, not serializable */
try {
DatabaseConfig config = new DatabaseConfig();
config.setBtreeComparator(new BadSerialComparator1());
fail("Comparator shouldn't be instantiated");
} catch (IllegalArgumentException e) {
/* Expected. */
if (DEBUG) {
System.out.println(e);
}
}
/* Can't be serialized, contains non-serializable field */
try {
DatabaseConfig config = new DatabaseConfig();
config.setBtreeComparator(new BadSerialComparator2());
fail("Comparator shouldn't be instantiated");
} catch (IllegalArgumentException e) {
/* Expected. */
if (DEBUG) {
System.out.println(e);
}
}
/* Valid comparators */
DatabaseConfig config = new DatabaseConfig();
config.setBtreeComparator(TestComparator.class);
config.setBtreeComparator(TestComparator2.class);
config.setBtreeComparator(new TestSerialComparator());
config.setBtreeComparator(new TestSerialComparator2());
} catch (Throwable t) {
t.printStackTrace();
throw t;
}
}
/**
* Test that any conflicts between configuration object settings and the
* underlying impl object are detected.
*/
public void testConfigConfict()
throws Throwable {
try {
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
envConfig.setAllowCreate(true);
env = new Environment(envHome, envConfig);
/*
* Test conflicts of duplicate allowed configuration.
*/
/* 1a. Create allowing duplicates. */
DatabaseConfig firstConfig = new DatabaseConfig();
firstConfig.setAllowCreate(true);
firstConfig.setSortedDuplicates(true);
Database firstHandle = env.openDatabase(null, "fooDups",
firstConfig);
/* 1b. Try to open w/no duplicates. */
DatabaseConfig secondConfig = new DatabaseConfig();
secondConfig.setSortedDuplicates(false);
try {
env.openDatabase(null, "fooDups", secondConfig);
fail("Conflict in duplicates allowed should be detected.");
} catch (IllegalArgumentException expected) {
}
firstHandle.close();
env.removeDatabase(null, "fooDups");
/* 2a. Create dis-allowing duplicates. */
firstConfig.setSortedDuplicates(false);
firstHandle = env.openDatabase(null, "fooDups", firstConfig);
/* 2b. Try to open w/duplicates. */
secondConfig.setSortedDuplicates(true);
try {
env.openDatabase(null, "fooDups", secondConfig);
fail("Conflict in duplicates allowed should be detected.");
} catch (IllegalArgumentException expected) {
}
firstHandle.close();
/*
* Test conflicts of read only. If the environment is read/write
* we should be able to open handles in read only or read/write
* mode. If the environment is readonly, the database handles
* must also be read only.
*/
DatabaseConfig readOnlyConfig = new DatabaseConfig();
readOnlyConfig.setReadOnly(true);
Database roHandle = env.openDatabase(null, "fooDups",
readOnlyConfig);
roHandle.close();
/* Open the environment in read only mode. */
env.close();
envConfig = TestUtils.initEnvConfig();
envConfig.setReadOnly(true);
env = new Environment(envHome, envConfig);
/* Open a readOnly database handle, should succeed */
roHandle = env.openDatabase(null, "fooDups",
readOnlyConfig);
roHandle.close();
/* Open a read/write database handle, should not succeed. */
try {
env.openDatabase(null, "fooDups", null);
fail("Should not be able to open read/write");
} catch (IllegalArgumentException expected) {
}
env.close();
/*
* Check comparator changes.
*/
/* 1a. Open w/a null comparator */
env = new Environment(envHome, null);
firstConfig = new DatabaseConfig();
firstConfig.setAllowCreate(true);
firstHandle = env.openDatabase(null,
"fooComparator",
firstConfig);
DatabaseConfig firstRetrievedConfig = firstHandle.getConfig();
assertEquals(null, firstRetrievedConfig.getBtreeComparator());
assertEquals(null, firstRetrievedConfig.getDuplicateComparator());
/*
* 1b. Open a db w/a different comparator, shouldn't take effect
* because override is not set.
*/
secondConfig = new DatabaseConfig();
Comparator btreeComparator = new TestComparator();
Comparator dupComparator = new TestComparator();
secondConfig.setBtreeComparator
((Class<Comparator<byte[]>>)btreeComparator.getClass());
secondConfig.setDuplicateComparator
((Class<Comparator<byte[]>>)dupComparator.getClass());
Database secondHandle =
env.openDatabase(null, "fooComparator", secondConfig);
DatabaseConfig retrievedConfig = secondHandle.getConfig();
assertEquals(null, retrievedConfig.getBtreeComparator());
assertEquals(null, retrievedConfig.getDuplicateComparator());
secondHandle.close();
/* Same as above but with a serialized comparator. */
secondConfig = new DatabaseConfig();
btreeComparator = new TestSerialComparator();
dupComparator = new TestSerialComparator();
secondConfig.setBtreeComparator(btreeComparator);
secondConfig.setDuplicateComparator(dupComparator);
secondHandle =
env.openDatabase(null, "fooComparator", secondConfig);
retrievedConfig = secondHandle.getConfig();
assertEquals(null, retrievedConfig.getBtreeComparator());
assertEquals(null, retrievedConfig.getDuplicateComparator());
secondHandle.close();
/* 1c. Allow override */
secondConfig.setOverrideBtreeComparator(true);
secondConfig.setOverrideDuplicateComparator(true);
btreeComparator = new TestComparator();
dupComparator = new TestComparator();
secondConfig.setBtreeComparator
((Class<Comparator<byte[]>>)btreeComparator.getClass());
secondConfig.setDuplicateComparator
((Class<Comparator<byte[]>>)dupComparator.getClass());
secondHandle = env.openDatabase(null,
"fooComparator",
secondConfig);
retrievedConfig = secondHandle.getConfig();
assertEquals(btreeComparator.getClass(),
retrievedConfig.getBtreeComparator().getClass());
assertEquals(dupComparator.getClass(),
retrievedConfig.getDuplicateComparator().getClass());
secondHandle.close();
/* Same as above but with a serialized comparator. */
secondConfig.setOverrideBtreeComparator(true);
secondConfig.setOverrideDuplicateComparator(true);
btreeComparator = new TestSerialComparator();
dupComparator = new TestSerialComparator();
secondConfig.setBtreeComparator(btreeComparator);
secondConfig.setDuplicateComparator(dupComparator);
secondHandle = env.openDatabase(null,
"fooComparator",
secondConfig);
retrievedConfig = secondHandle.getConfig();
assertEquals(btreeComparator,
retrievedConfig.getBtreeComparator());
assertEquals(dupComparator,
retrievedConfig.getDuplicateComparator());
secondHandle.close();
firstHandle.close();
env.close();
} catch (Throwable t) {
t.printStackTrace();
env.close();
throw t;
}
}
public void testIsTransactional()
throws Throwable {
try {
/* Open environment in transactional mode.*/
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
envConfig.setTransactional(true);
envConfig.setAllowCreate(true);
env = new Environment(envHome, envConfig);
/* Create a db, open transactionally with implied auto-commit. */
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(true);
dbConfig.setTransactional(true);
Database myDb = env.openDatabase(null, "testDB", dbConfig);
assertTrue(myDb.isTransactional());
assertTrue(myDb.getConfig().getTransactional());
myDb.close();
/* Open an existing db, can open it non-transactionally. */
dbConfig.setTransactional(false);
myDb = env.openDatabase(null, "testDB", null);
assertFalse(myDb.isTransactional());
assertFalse(myDb.getConfig().getTransactional());
myDb.close();
/* Open another db, pass an explicit transaction. */
dbConfig.setTransactional(true);
Transaction txn = env.beginTransaction(null, null);
myDb = env.openDatabase(txn, "testDB2", dbConfig);
assertTrue(myDb.isTransactional());
assertTrue(myDb.getConfig().getTransactional());
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry data = new DatabaseEntry();
key.setData(TestUtils.getTestArray(0));
data.setData(TestUtils.getTestArray(0));
try {
myDb.put(null, key, data);
} catch (DatabaseException DBE) {
fail("didn't expect DatabaseException, implied autocommit");
}
key.setData(TestUtils.getTestArray(1));
data.setData(TestUtils.getTestArray(1));
try {
myDb.put(txn, key, data);
} catch (DatabaseException DBE) {
fail("didn't expect DatabaseException with txn passed");
}
try {
myDb.get(txn, key, data, LockMode.DEFAULT);
} catch (DatabaseException DBE) {
fail("didn't expect DatabaseException with txn passed");
}
txn.commit();
try {
myDb.get(null, key, data, LockMode.DEFAULT);
} catch (DatabaseException DBE) {
fail("didn't expect DatabaseException because no txn passed");
}
myDb.close();
env.close();
} catch (Throwable t) {
t.printStackTrace();
throw t;
}
}
public void testOpenReadOnly()
throws Throwable {
try {
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
envConfig.setTransactional(true);
envConfig.setAllowCreate(true);
env = new Environment(envHome, envConfig);
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry data = new DatabaseEntry();
Transaction txn = env.beginTransaction(null, null);
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setTransactional(true);
dbConfig.setAllowCreate(true);
Database myDb = env.openDatabase(txn, "testDB2", dbConfig);
key.setData(TestUtils.getTestArray(0));
data.setData(TestUtils.getTestArray(0));
try {
myDb.put(txn, key, data);
} catch (DatabaseException DBE) {
fail("unexpected DatabaseException during put");
}
txn.commit();
myDb.close();
dbConfig = new DatabaseConfig();
dbConfig.setTransactional(true);
dbConfig.setReadOnly(true);
txn = env.beginTransaction(null, null);
myDb = env.openDatabase(txn, "testDB2", dbConfig);
assertTrue(myDb.isTransactional());
assertTrue(myDb.getConfig().getTransactional());
key.setData(TestUtils.getTestArray(0));
data.setData(TestUtils.getTestArray(0));
try {
myDb.put(txn, key, data);
fail
("expected UnsupportedOperationException because open RDONLY");
} catch (UnsupportedOperationException expected) {
}
key.setData(TestUtils.getTestArray(0));
data.setData(TestUtils.getTestArray(0));
assertEquals(OperationStatus.SUCCESS,
myDb.get(txn, key, data, LockMode.DEFAULT));
Cursor cursor = myDb.openCursor(txn, null);
assertEquals(OperationStatus.SUCCESS,
cursor.getFirst(key, data, LockMode.DEFAULT));
try {
cursor.delete();
fail("expected Exception from delete on RD_ONLY db");
} catch (DatabaseException DBE) {
}
key.setData(TestUtils.getTestArray(1));
data.setData(TestUtils.getTestArray(1));
try {
myDb.put(txn, key, data);
fail
("expected UnsupportedOperationException because open RDONLY");
} catch (UnsupportedOperationException expected) {
}
cursor.close();
txn.commit();
myDb.close();
env.close();
} catch (Throwable t) {
t.printStackTrace();
throw t;
}
}
/**
* Test exclusive creation.
*/
public void testExclusive()
throws Throwable {
try {
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
/*
* Make sure that the database keeps its own copy of the
* configuration object.
*/
envConfig.setAllowCreate(true);
env = new Environment(envHome, envConfig);
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(true);
dbConfig.setExclusiveCreate(true);
/* Should succeed and create the database. */
Database dbA = env.openDatabase(null, "foo", dbConfig);
dbA.close();
/* Should not succeed, because the database exists. */
try {
env.openDatabase(null, "foo", dbConfig);
fail("Database already exists");
} catch (DatabaseException e) {
}
env.close();
} catch (Throwable t) {
t.printStackTrace();
throw t;
}
}
/*
* Test that changing the Btree comparator really writes it to disk.
*/
public void testConfigOverrideUpdateSR15743()
throws Throwable {
try {
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
envConfig.setAllowCreate(true);
env = new Environment(envHome, envConfig);
/*
* Make sure that the database keeps its own copy of the
* configuration object.
*/
DatabaseConfig dbConfigA = new DatabaseConfig();
dbConfigA.setOverrideBtreeComparator(false);
dbConfigA.setBtreeComparator(TestComparator.class);
dbConfigA.setAllowCreate(true);
Database dbA = env.openDatabase(null, "foo", dbConfigA);
/* Change the original dbConfig */
dbConfigA.setBtreeComparator(TestComparator2.class);
DatabaseConfig getConfig1 = dbA.getConfig();
assertEquals(TestComparator.class,
getConfig1.getBtreeComparator().getClass());
/*
* Change the retrieved config, ought to have no effect on what the
* Database is storing.
*/
getConfig1.setBtreeComparator(TestComparator2.class);
DatabaseConfig getConfig2 = dbA.getConfig();
assertEquals(TestComparator.class,
getConfig2.getBtreeComparator().getClass());
dbA.close();
env.close();
/* Ensure new comparator is written to disk. */
envConfig = TestUtils.initEnvConfig();
env = new Environment(envHome, envConfig);
dbConfigA = new DatabaseConfig();
/* Change the comparator. */
dbConfigA.setOverrideBtreeComparator(true);
dbConfigA.setBtreeComparator(TestComparator2.class);
dbA = env.openDatabase(null, "foo", dbConfigA);
getConfig2 = dbA.getConfig();
assertEquals(TestComparator2.class,
getConfig2.getBtreeComparator().getClass());
dbA.close();
env.close();
/* Read it back during recovery to ensure it was written. */
envConfig = TestUtils.initEnvConfig();
env = new Environment(envHome, envConfig);
dbConfigA = new DatabaseConfig();
dbA = env.openDatabase(null, "foo", dbConfigA);
getConfig2 = dbA.getConfig();
assertEquals(TestComparator2.class,
getConfig2.getBtreeComparator().getClass());
/* Create a root for the tree. */
dbA.put(null,
new DatabaseEntry(new byte[1]),
new DatabaseEntry(new byte[1]));
dbA.close();
env.close();
/* Change it to a third one when there is a root present. */
envConfig = TestUtils.initEnvConfig();
env = new Environment(envHome, envConfig);
dbConfigA = new DatabaseConfig();
/* Change the comparator. */
dbConfigA.setOverrideBtreeComparator(true);
dbConfigA.setBtreeComparator(TestComparator3.class);
dbA = env.openDatabase(null, "foo", dbConfigA);
getConfig2 = dbA.getConfig();
assertEquals(TestComparator3.class,
getConfig2.getBtreeComparator().getClass());
dbA.close();
env.close();
/* Read it back during recovery to ensure it was written. */
envConfig = TestUtils.initEnvConfig();
env = new Environment(envHome, envConfig);
dbConfigA = new DatabaseConfig();
dbA = env.openDatabase(null, "foo", dbConfigA);
getConfig2 = dbA.getConfig();
assertEquals(TestComparator3.class,
getConfig2.getBtreeComparator().getClass());
dbA.close();
env.close();
} catch (Throwable t) {
t.printStackTrace();
throw t;
}
}
/*
* This Comparator can't be instantiated because it's private and not
* static.
*/
private class BadComparator1 implements Comparator<byte[]> {
public BadComparator1(int foo) {
}
public int compare(byte[] o1, byte[] o2) {
return 0;
}
}
/*
* This Comparator can't be instantiated because it doesn't have zero
* parameter constructor.
*/
public static class BadComparator2 implements Comparator<byte[]> {
public BadComparator2(int i) {
}
public int compare(byte[] o1, byte[] o2) {
return 0;
}
}
/*
* OK comparator for setting comparators.
*/
public static class TestComparator implements Comparator<byte[]> {
public TestComparator() {
}
public int compare(byte[] o1, byte[] o2) {
return 0;
}
}
/*
* OK comparator for setting comparators.
*/
public static class TestComparator2 implements Comparator<byte[]> {
public TestComparator2() {
}
public int compare(byte[] o1, byte[] o2) {
return 0;
}
}
/*
* OK comparator for setting comparators.
*/
public static class TestComparator3 implements Comparator<byte[]> {
public TestComparator3() {
}
public int compare(byte[] o1, byte[] o2) {
return 0;
}
}
/*
* This Comparator can't be serialized because it's not serializable.
*/
public class BadSerialComparator1 implements Comparator<byte[]> {
public BadSerialComparator1() {
}
public int compare(byte[] o1, byte[] o2) {
return 0;
}
}
/*
* This Comparator can't be serialized because it contains a reference to
* an object that's not serializable.
*/
@SuppressWarnings("serial")
public class BadSerialComparator2 implements Comparator<byte[]>,
Serializable {
private BadSerialComparator1 o = new BadSerialComparator1();
public BadSerialComparator2() {
}
public int compare(byte[] o1, byte[] o2) {
return 0;
}
}
/*
* OK comparator for setting comparators -- private class, private
* constructor, and serializable fields are allowed.
*/
@SuppressWarnings("serial")
private static class TestSerialComparator
implements Comparator<byte[]>, Serializable {
private String s = "sss";
private TestSerialComparator() {
}
public int compare(byte[] o1, byte[] o2) {
return 0;
}
public boolean equals(Object other) {
TestSerialComparator o = (TestSerialComparator) other;
return s.equals(o.s);
}
}
/*
* OK comparator for setting comparators.
*/
@SuppressWarnings("serial")
public static class TestSerialComparator2
implements Comparator<byte[]>, Serializable {
public int compare(byte[] o1, byte[] o2) {
return 0;
}
}
public static class SecKeyCreator1 implements SecondaryKeyCreator {
public boolean createSecondaryKey(SecondaryDatabase secondary,
DatabaseEntry key,
DatabaseEntry data,
DatabaseEntry result)
throws DatabaseException {
return true;
}
public boolean equals(Object o) {
if (o == null) {
return false;
}
return (o.getClass() == getClass());
}
}
public static class SecKeyCreator2 implements SecondaryKeyCreator {
public boolean createSecondaryKey(SecondaryDatabase secondary,
DatabaseEntry key,
DatabaseEntry data,
DatabaseEntry result)
throws DatabaseException {
return true;
}
public boolean equals(Object o) {
if (o == null) {
return false;
}
return (o.getClass() == getClass());
}
}
public static class SecMultiKeyCreator1
implements SecondaryMultiKeyCreator {
public void createSecondaryKeys(SecondaryDatabase secondary,
DatabaseEntry key,
DatabaseEntry data,
Set results)
throws DatabaseException {
}
public boolean equals(Object o) {
if (o == null) {
return false;
}
return (o.getClass() == getClass());
}
}
public static class ForeignKeyNullifier1 implements ForeignKeyNullifier {
public boolean nullifyForeignKey(SecondaryDatabase secondary,
DatabaseEntry data)
throws DatabaseException {
return true;
}
}
public static class ForeignMultiKeyNullifier1
implements ForeignMultiKeyNullifier {
public boolean nullifyForeignKey(SecondaryDatabase secondary,
DatabaseEntry key,
DatabaseEntry data,
DatabaseEntry secKey)
throws DatabaseException {
return true;
}
}
}
|