1 21 22 package org.apache.derbyTesting.unitTests.store; 23 24 import org.apache.derby.iapi.store.raw.*; 25 26 import org.apache.derby.iapi.services.io.FormatableBitSet; 27 28 import org.apache.derby.iapi.reference.Property; 29 30 import org.apache.derbyTesting.unitTests.harness.T_MultiThreadedIterations; 32 import org.apache.derbyTesting.unitTests.harness.T_Fail; 33 34 import org.apache.derby.iapi.services.context.ContextService; 35 import org.apache.derby.iapi.services.context.ContextManager; 36 import org.apache.derby.iapi.services.locks.*; 37 import org.apache.derby.iapi.services.sanity.SanityManager; 38 39 import org.apache.derby.iapi.error.StandardException; 40 41 import org.apache.derby.iapi.store.access.AccessFactoryGlobals; 42 import org.apache.derby.iapi.store.access.Qualifier; 43 44 import org.apache.derby.iapi.store.access.conglomerate.LogicalUndo; 45 46 import org.apache.derby.iapi.types.DataValueDescriptor; 47 48 49 import org.apache.derby.iapi.reference.Attribute; 50 import org.apache.derby.iapi.services.property.PropertyUtil; 51 import org.apache.derby.iapi.error.ExceptionSeverity; 52 import java.io.*; 53 import java.util.Properties ; 54 import org.apache.derby.iapi.types.SQLChar; 55 56 57 66 public class T_Util 67 { 68 69 RawStoreFactory rsFactory; 70 LockFactory lFactory; 71 ContextService csFactory; 72 73 private int openContainerMode; 75 public T_Util(RawStoreFactory rsf, LockFactory lf, 76 ContextService csf) 77 { 78 rsFactory = rsf; 79 lFactory = lf; 80 csFactory = csf; 81 82 openContainerMode = 0; } 84 85 public void setOpenMode(int newMode) { 86 openContainerMode = newMode; 87 } 88 89 93 94 97 public void t_checkNullLockCount(Transaction t) throws T_Fail { 98 if (lFactory.areLocksHeld(t)) 99 throw T_Fail.testFailMsg("Previous action did not clean up all locks."); 100 } 101 102 105 public static void t_checkPageNumber(Page page, long pageNumber) throws T_Fail { 106 if (page.getPageNumber() != pageNumber) 107 throw T_Fail.testFailMsg("page number expected to be " + pageNumber + ", is " + 108 page.getPageNumber()); 109 } 110 111 117 public static void t_checkRecordCount(Page page, int count, int nonDeleted) throws T_Fail, StandardException { 118 if (page.recordCount() != count) 119 throw T_Fail.testFailMsg("recordCount() expected to be " + count + ", is " + page.recordCount()); 120 121 if (page.nonDeletedRecordCount() != nonDeleted) 122 throw T_Fail.testFailMsg("nonDeletedRecordCount() expected to be " + nonDeleted + ", is " + page.nonDeletedRecordCount()); 123 } 124 125 128 public static void t_checkFieldCount(Page page, int slot, int count) throws T_Fail, StandardException { 129 if (page.fetchNumFieldsAtSlot(slot) != count) 130 throw T_Fail.testFailMsg("number of fields at slot " + slot + " expected to be " + count 131 + ", is " + page.fetchNumFieldsAtSlot(slot)); 132 } 133 134 152 public static void t_checkFetch(Page page, RecordHandle rh, String data, int stringLen) 153 throws T_Fail, StandardException { 154 155 t_checkFetch(page, rh, T_Util.getStringFromData(data, stringLen)); 156 } 157 158 public static void t_checkFetch(Page page, RecordHandle rh, String data) 159 throws T_Fail, StandardException { 160 161 if (!page.recordExists(rh, false)) 162 throw T_Fail.testFailMsg("Record does not exist"); 163 164 T_RawStoreRow readRow = new T_RawStoreRow((String ) null); 165 166 int slot = page.getSlotNumber(rh); 167 168 RecordHandle rhf = 169 page.fetchFromSlot( 170 rh, slot, readRow.getRow(), 171 (FetchDescriptor) null, 172 false); 173 174 if (rhf == null) 175 throw T_Fail.testFailMsg("Failed to read record"); 176 177 if ((data == null) || readRow.getStorableColumn(0).isNull()) { 178 179 if ((data == null) && readRow.getStorableColumn(0).isNull()) 180 return; 181 182 throw T_Fail.testFailMsg("Record's value incorrect"); 183 } 184 185 if (!readRow.toString().equals(data)) 186 throw T_Fail.testFailMsg("Record's value incorrect, expected :" + data + ": - got :" + readRow.toString()); 187 } 188 189 200 public void t_checkFetch(ContainerHandle c, RecordHandle rh, String data) 201 throws T_Fail, StandardException { 202 203 Page page = t_getPage(c, rh.getPageNumber()); 204 205 try 206 { 207 t_checkFetch(page, rh, data); 208 } 209 finally 210 { 211 page.unlatch(); 212 } 213 } 214 215 221 public void t_checkFetchFail(ContainerHandle c, RecordHandle rh) 222 throws T_Fail, StandardException 223 { 224 Page page = t_getPage(c, rh.getPageNumber()); 225 226 try 227 { 228 if (page.recordExists(rh, true)) 229 throw T_Fail.testFailMsg("Record Exists"); 230 } 231 finally 232 { 233 page.unlatch(); 234 } 235 } 236 237 246 public void t_checkFetchDeleted(ContainerHandle c, RecordHandle rh, 247 String data) 248 throws T_Fail, StandardException 249 { 250 Page p = t_getPage(c, rh.getPageNumber()); 251 if (p == null) 252 throw T_Fail.testFailMsg("Page not found " + rh); 253 254 T_RawStoreRow readRow = new T_RawStoreRow((String ) null); 255 256 try 257 { 258 int slot = p.getSlotNumber(rh); 259 if (p.fetchFromSlot( 260 rh, slot, readRow.getRow(), 261 (FetchDescriptor) null, 262 false) != null) 263 { 264 throw T_Fail.testFailMsg( 265 "Record at slot " + slot + " not deleted"); 266 } 267 } 268 finally 269 { 270 p.unlatch(); 271 } 272 } 273 274 275 282 public static void t_checkFetch(Page page, RecordHandle rh, T_RawStoreRow row) 283 throws T_Fail, StandardException 284 { 285 if (!page.recordExists(rh, false)) 286 throw T_Fail.testFailMsg("Record does not exist"); 287 288 int ncol = row.nColumns(); 290 T_RawStoreRow readRow = new T_RawStoreRow(ncol); 291 for (int i = 0; i < ncol; i++) 292 readRow.setColumn(i, (String ) null); 293 294 RecordHandle rhf = page.fetch(rh, readRow.getRow(), (FormatableBitSet) null, false); 295 if (rhf == null) 296 throw T_Fail.testFailMsg("Failed to read record"); 297 if (!readRow.toString().equals(row.toString())) 298 throw T_Fail.testFailMsg("Record's value incorrect, expected :" + 299 row.toString() + ": - got :" + readRow.toString()); 300 } 301 302 303 309 public static void t_checkFetchCol(Page page, RecordHandle rh, int colNum, 310 int numCols, String data) 311 throws T_Fail, StandardException 312 { 313 if (!page.recordExists(rh, false)) 314 throw T_Fail.testFailMsg("Record does not exist"); 315 316 T_RawStoreRow readRow = new T_RawStoreRow(numCols); 317 for (int i = 0; i < numCols; i++) 318 readRow.setColumn(i, (String ) null); 319 FormatableBitSet colList = new FormatableBitSet(numCols); 320 colList.set(colNum); 321 322 RecordHandle rhf = page.fetch(rh, readRow.getRow(), colList, false); 323 if (rhf == null) 324 throw T_Fail.testFailMsg("Failed to read record"); 325 String col = readRow.getStorableColumn(colNum).toString(); 326 if (!col.equals(data)) 327 throw T_Fail.testFailMsg("Record's value for column " + colNum + 328 " incorrect, expected :" + data + 329 ": - got :" + readRow.toString()); 330 } 331 332 333 340 341 345 public static RecordHandle t_checkFetchFirst(Page page, String data) 346 throws T_Fail, StandardException { 347 T_RawStoreRow readRow = new T_RawStoreRow((String ) null); 348 349 int slot = 0; 350 while (page.isDeletedAtSlot(slot)) 351 { 352 slot++; 353 } 354 355 RecordHandle rhf = 356 page.fetchFromSlot( 357 (RecordHandle) null, slot, 358 readRow.getRow(), 359 (FetchDescriptor) null, 360 false); 361 362 if (rhf == null) 363 throw T_Fail.testFailMsg("Failed to read record"); 364 if (!readRow.toString().equals(data)) 365 throw T_Fail.testFailMsg("Record's value incorrect, expected :" + data + ": - got :" + readRow.toString()); 366 367 return rhf; 368 } 369 370 374 public static RecordHandle t_checkFetchNext(Page page, RecordHandle rh, String data) 375 throws T_Fail, StandardException { 376 377 if (!page.recordExists(rh, false)) 378 throw T_Fail.testFailMsg("Record does not exist"); 379 380 T_RawStoreRow readRow = new T_RawStoreRow((String ) null); 381 382 int slot = page.getSlotNumber(rh) + 1; 383 while (page.isDeletedAtSlot(slot)) 384 { 385 slot++; 386 } 387 388 RecordHandle rhf = 389 page.fetchFromSlot( 390 (RecordHandle) null, 391 slot, 392 readRow.getRow(), 393 (FetchDescriptor) null, 394 false); 395 396 if (rhf == null) 397 throw T_Fail.testFailMsg("Failed to read record"); 398 if (!readRow.toString().equals(data)) 399 throw T_Fail.testFailMsg("Record's value incorrect, expected :" + data + ": - got :" + readRow.toString()); 400 401 return rhf; 402 } 403 404 408 public static RecordHandle t_checkFetchPrevious(Page page, RecordHandle rh, String data) 409 throws T_Fail, StandardException { 410 411 if (!page.recordExists(rh, false)) 412 throw T_Fail.testFailMsg("Record does not exist"); 413 414 T_RawStoreRow readRow = new T_RawStoreRow((String ) null); 415 416 int slot = page.getSlotNumber(rh) - 1; 417 418 while (page.isDeletedAtSlot(slot) && slot >= 0) 419 { 420 slot--; 421 } 422 423 if (slot == -1) 424 return(null); 425 426 427 RecordHandle rhf = 428 page.fetchFromSlot( 429 (RecordHandle) null, 430 slot, 431 readRow.getRow(), 432 (FetchDescriptor) null, 433 false); 434 435 if (rhf == null) 436 throw T_Fail.testFailMsg("Failed to read record"); 437 if (!readRow.toString().equals(data)) 438 throw T_Fail.testFailMsg("Record's value incorrect, expected :" + data + ": - got :" + readRow.toString()); 439 440 return rhf; 441 } 442 443 447 public static RecordHandle t_checkFetchLast(Page page, String data) 448 throws T_Fail, StandardException { 449 T_RawStoreRow readRow = new T_RawStoreRow((String ) null); 450 451 int slot = page.recordCount() - 1; 452 while (page.isDeletedAtSlot(slot) && slot >= 0) 453 { 454 slot--; 455 } 456 457 if (slot == -1) 458 return(null); 459 460 RecordHandle rhf = 461 page.fetchFromSlot( 462 (RecordHandle) null, 463 slot, 464 readRow.getRow(), 465 (FetchDescriptor) null, 466 false); 467 468 if (rhf == null) 469 throw T_Fail.testFailMsg("Failed to read record"); 470 if (!readRow.toString().equals(data)) 471 throw T_Fail.testFailMsg("Record's value incorrect, expected :" + data + ": - got :" + readRow.toString()); 472 473 return rhf; 474 } 475 476 477 495 public static void t_checkFetchBySlot(Page page, int slot, 496 String data, boolean deleted, 497 boolean forUpdate) 498 throws T_Fail, StandardException 499 { 500 T_RawStoreRow readRow = new T_RawStoreRow((String ) null); 501 RecordHandle rh = 502 page.fetchFromSlot( 503 (RecordHandle) null, slot, 504 readRow.getRow(), 505 (FetchDescriptor) null, 506 true); 507 508 if (rh == null) 509 throw T_Fail.testFailMsg("Failed to read record"); 510 if (!readRow.toString().equals(data)) 511 throw T_Fail.testFailMsg("Record's value incorrect, expected :" + data + ": - got :" + readRow.toString()); 512 513 if (page.isDeletedAtSlot(slot) != deleted) 514 throw T_Fail.testFailMsg("Record at slot " + slot + " deleted=" + 515 page.isDeletedAtSlot(slot) + ", expect " + deleted); 516 517 } 519 520 538 public static void t_checkFetchColFromSlot(Page page, 539 int slot, 540 int fieldId, 541 DataValueDescriptor column, 542 boolean forUpdate, 543 String data, 544 int stringLen) 545 throws StandardException, T_Fail 546 { 547 t_checkFetchColFromSlot(page, slot, fieldId, column, forUpdate, T_Util.getStringFromData(data, stringLen)); 548 } 549 550 public static void t_checkFetchColFromSlot( 551 Page page, 552 int slot, 553 int fieldId, 554 DataValueDescriptor column, 555 boolean forUpdate, 556 String data) 557 throws StandardException, T_Fail 558 { 559 DataValueDescriptor[] fetch_row = new DataValueDescriptor[fieldId + 1]; 560 fetch_row[fieldId] = column; 561 FormatableBitSet validCols = new FormatableBitSet(fieldId + 1); 562 validCols.set(fieldId); 563 564 RecordHandle rh = 565 page.fetchFromSlot( 566 null, slot, fetch_row, 567 new FetchDescriptor( 568 fetch_row.length, validCols, (Qualifier[][]) null), 569 true); 570 571 if (rh == null) 572 throw T_Fail.testFailMsg("Failed to fetch record: slot " 573 + slot + " field " + fieldId); 574 575 577 if (data == null) 578 { 579 if (!column.isNull()) 580 throw T_Fail.testFailMsg("Failed to fetch null column: slot " 581 + slot + " field " + fieldId + " column is " + column); 582 } 583 else 584 { 585 if (column.isNull()) 586 throw T_Fail.testFailMsg("expect non null column, got null: slot " 587 + slot + " field " + fieldId); 588 if (!column.toString().equals(data)) 589 throw T_Fail.testFailMsg("expect " + data + " got " + column.toString() 590 + ": slot " + slot + " field " + fieldId); 591 } 592 } 593 594 595 601 public static void t_checkEmptyPage(Page page) throws T_Fail, StandardException { 602 603 t_checkRecordCount(page, 0, 0); 605 606 try 607 { 608 page.fetchFromSlot( 609 (RecordHandle) null, 0, null, 610 (FetchDescriptor) null, 611 false); 612 613 throw T_Fail.testFailMsg( 614 "fetchFromSlot() must throw exception on fetch from slot 0 on an empty page"); 615 } 616 catch (StandardException se) 617 { 618 } 620 621 if (page.getRecordHandle(0) != null) 626 throw T_Fail.testFailMsg("obtained a RecordHandle for an empty page"); 627 628 if (page.getRecordHandle(RecordHandle.FIRST_RECORD_ID) != null) 629 throw T_Fail.testFailMsg("obtained a RecordHandle for an empty page"); 630 631 if (page.getAuxObject() != null) 633 throw T_Fail.testFailMsg("empty page has an aux object"); 634 635 t_readOnlySlotOutOfRange(page, Page.FIRST_SLOT_NUMBER); 636 637 if (!page.spaceForInsert()) 638 throw T_Fail.testFailMsg("spaceForInsert() returned false on an empty page"); 639 } 640 641 645 public static void t_readOnlySlotOutOfRange(Page page, int slot) throws T_Fail, StandardException { 646 647 try { 648 page.fetchFromSlot( 649 (RecordHandle) null, slot, 650 new DataValueDescriptor[0], 651 (FetchDescriptor) null, 652 true); 653 654 throw T_Fail.testFailMsg("fetchFromSlot succeeded on out of range slot " + slot); 655 } catch (StandardException se0) { 656 if (se0.getSeverity() > ExceptionSeverity.STATEMENT_SEVERITY) 658 throw se0; 659 } 660 try { 661 page.isDeletedAtSlot(slot); 662 throw T_Fail.testFailMsg("isDeletedAtSlot succeeded on out of range slot " + slot); 663 } catch (StandardException se2) { 664 if (se2.getSeverity() > ExceptionSeverity.STATEMENT_SEVERITY) 666 throw se2; 667 } 668 } 669 670 674 public static void t_updateSlotOutOfRange(Page page, int slot) throws T_Fail, StandardException { 675 676 try { 677 page.deleteAtSlot(slot, false, (LogicalUndo)null); 678 throw T_Fail.testFailMsg("deleteAtSlot succeeded on out of range slot " + slot); 679 } catch (StandardException se0) { 680 if (se0.getSeverity() > ExceptionSeverity.STATEMENT_SEVERITY) 682 throw se0; 683 } 684 try { 685 page.deleteAtSlot(slot, true, (LogicalUndo)null); 686 throw T_Fail.testFailMsg("deleteAtSlot succeeded on out of range slot " + slot); 687 } catch (StandardException se0) { 688 if (se0.getSeverity() > ExceptionSeverity.STATEMENT_SEVERITY) 690 throw se0; 691 } 692 693 T_RawStoreRow row = new T_RawStoreRow((String ) null); 694 695 if (page.recordCount() != slot) { 697 try { 698 page.insertAtSlot(slot, row.getRow(), (FormatableBitSet) null, (LogicalUndo)null, 699 Page.INSERT_DEFAULT, 100); 700 throw T_Fail.testFailMsg("insertAtSlot succeeded, on out of range slot " + slot); 701 } catch (StandardException se0) { 702 if (se0.getSeverity() > ExceptionSeverity.STATEMENT_SEVERITY) 704 throw se0; 705 } 706 } 707 708 try { 709 page.updateAtSlot(slot, row.getRow(), (FormatableBitSet) null); 710 throw T_Fail.testFailMsg("updateAtSlot succeeded on out of range slot " + slot); 711 } catch (StandardException se0) { 712 if (se0.getSeverity() > ExceptionSeverity.STATEMENT_SEVERITY) 714 throw se0; 715 } 716 } 717 718 719 722 723 729 public static void t_checkInvalidSavePoint(Transaction t, String name) 730 throws T_Fail, StandardException { 731 732 try { 734 t.rollbackToSavePoint(name, null); 735 736 throw T_Fail.testFailMsg("non existent save point did not cause exception on rollbackToSavePoint"); 737 } catch (StandardException se) { 738 } 740 try { 741 t.releaseSavePoint(name, null); 742 throw T_Fail.testFailMsg("non existent save point did not cause exception on releaseSavePoint"); 743 744 } catch (StandardException se) { 745 } 747 } 748 749 753 public void t_checkInvalidSavePoint(T_TWC ctx, String name) 754 throws T_Fail, StandardException { 755 csFactory.setCurrentContextManager(ctx.cm); 756 try { 757 t_checkInvalidSavePoint(ctx.tran, name); 758 } finally { 759 csFactory.resetCurrentContextManager(ctx.cm); 760 } 761 } 762 763 764 768 769 770 774 public Transaction t_startTransaction() 775 throws StandardException, T_Fail { 776 777 Transaction t1 = 778 rsFactory.startTransaction( 779 csFactory.getCurrentContextManager(), 780 AccessFactoryGlobals.USER_TRANS_NAME); 781 782 if (t1 == null) 783 throw T_Fail.testFailMsg("Start a transaction"); 784 t_checkNullLockCount(t1); 785 return t1; 786 } 787 788 792 public Transaction t_startGlobalTransaction( 793 int format_id, 794 byte[] global_id, 795 byte[] branch_id) 796 throws StandardException, T_Fail { 797 798 Transaction t1 = 799 rsFactory.startGlobalTransaction( 800 csFactory.getCurrentContextManager(), 801 format_id, global_id, branch_id); 802 803 if (t1 == null) 804 throw T_Fail.testFailMsg("Start a transaction"); 805 t_checkNullLockCount(t1); 806 return t1; 807 } 808 809 812 public T_TWC t_startTransactionWithContext() 813 throws StandardException, T_Fail 814 { 815 T_TWC ctx = new T_TWC(csFactory, lFactory, rsFactory); 816 ctx.startUserTransaction(); 817 return ctx; 818 } 819 820 823 public Transaction t_startInternalTransaction() 824 throws StandardException, T_Fail { 825 826 Transaction t1 = rsFactory.startInternalTransaction(csFactory.getCurrentContextManager()); 827 828 if (t1 == null) 829 throw T_Fail.testFailMsg("Failed to start an internal transaction"); 830 t_checkNullLockCount(t1); 831 return t1; 832 } 833 834 837 public void t_commit(Transaction t) 838 throws StandardException, T_Fail { 839 t.commit(); 840 t_checkNullLockCount(t); 841 } 842 843 846 public void t_commit(T_TWC ctx) 847 throws StandardException, T_Fail 848 { 849 csFactory.setCurrentContextManager(ctx.cm); 850 try { 851 t_commit(ctx.tran); 852 } finally { 853 csFactory.resetCurrentContextManager(ctx.cm); 854 } 855 } 856 857 860 public void t_close(T_TWC ctx) 861 throws StandardException, T_Fail 862 { 863 ctx.tran.close(); 864 ctx.tran = null; 865 ctx.cm = null; } 867 868 871 public void t_abort(Transaction t) 872 throws StandardException, T_Fail { 873 t.abort(); 874 t_checkNullLockCount(t); 875 } 876 877 880 public void t_abort(T_TWC ctx) 881 throws StandardException, T_Fail 882 { 883 csFactory.setCurrentContextManager(ctx.cm); 884 try { 885 t_abort(ctx.tran); 886 } finally { 887 csFactory.resetCurrentContextManager(ctx.cm); 888 } 889 } 890 891 897 public long t_addContainer(Transaction t, long segmentId) 898 throws StandardException, T_Fail { 899 900 long cid = 901 t.addContainer( 902 segmentId, ContainerHandle.DEFAULT_ASSIGN_ID, 903 ContainerHandle.MODE_DEFAULT, (Properties ) null, 0); 904 905 if (cid < 0) 906 throw T_Fail.testFailMsg("add container"); 907 908 return cid; 909 } 910 911 public long t_addContainer(T_TWC ctx, long segmentId) 912 throws StandardException, T_Fail 913 { 914 csFactory.setCurrentContextManager(ctx.cm); 915 try { 916 return t_addContainer(ctx.tran, segmentId); 917 } finally { 918 csFactory.resetCurrentContextManager(ctx.cm); 919 } 920 } 921 922 929 public long t_addContainer(Transaction t, long segmentId, int pageSize) 930 throws StandardException, T_Fail { 931 932 Properties tableProperties = new Properties (); 933 tableProperties.put(Property.PAGE_SIZE_PARAMETER, Integer.toString(pageSize)); 934 935 long cid = 936 t.addContainer( 937 segmentId, ContainerHandle.DEFAULT_ASSIGN_ID, 938 ContainerHandle.MODE_DEFAULT, tableProperties, 0); 939 940 if (cid < 0) 941 throw T_Fail.testFailMsg("add container"); 942 943 return cid; 944 } 945 946 public long t_addContainer(T_TWC ctx, long segmentId, int pageSize) 947 throws StandardException, T_Fail { 948 949 csFactory.setCurrentContextManager(ctx.cm); 950 try { 951 return t_addContainer(ctx.tran, segmentId, pageSize); 952 } finally { 953 csFactory.resetCurrentContextManager(ctx.cm); 954 } 955 } 956 957 public long t_addContainer(Transaction t, long segmentId, Properties tableProperties) 958 throws StandardException, T_Fail { 959 960 long cid = 961 t.addContainer( 962 segmentId, ContainerHandle.DEFAULT_ASSIGN_ID, 963 ContainerHandle.MODE_DEFAULT, tableProperties, 0); 964 965 if (cid < 0) 966 throw T_Fail.testFailMsg("add container"); 967 968 return cid; 969 } 970 971 972 980 public long t_addContainer(Transaction t, long segmentId, int pageSize, int spareSpace, 981 int minimumRecordSize, boolean reusableRecordId) 982 throws StandardException, T_Fail { 983 984 Properties tableProperties = new Properties (); 985 tableProperties.put(Property.PAGE_SIZE_PARAMETER, Integer.toString(pageSize)); 986 tableProperties.put(RawStoreFactory.PAGE_RESERVED_SPACE_PARAMETER, Integer.toString(spareSpace)); 987 tableProperties.put(RawStoreFactory.MINIMUM_RECORD_SIZE_PARAMETER, Integer.toString(minimumRecordSize)); 988 989 if (reusableRecordId) { 990 tableProperties.put(RawStoreFactory.PAGE_REUSABLE_RECORD_ID, "true"); 991 } 992 993 long cid = 994 t.addContainer( 995 segmentId, ContainerHandle.DEFAULT_ASSIGN_ID, 996 ContainerHandle.MODE_DEFAULT, tableProperties, 0); 997 998 if (cid < 0) 999 throw T_Fail.testFailMsg("add container"); 1000 1001 return cid; 1002 } 1003 1004 public long t_addContainer(T_TWC ctx, long segmentId, int pageSize, int spareSpace, int minimumRecordSize) 1005 throws StandardException, T_Fail { 1006 csFactory.setCurrentContextManager(ctx.cm); 1007 try { 1008 return t_addContainer(ctx.tran, segmentId, pageSize, spareSpace, minimumRecordSize, false); 1009 } finally { 1010 csFactory.resetCurrentContextManager(ctx.cm); 1011 } 1012 } 1013 1014 1020 1021 public ContainerHandle t_openContainer(Transaction t, long segmentId, long containerId, boolean forUpdate) 1022 throws StandardException, T_Fail 1023 { 1024 ContainerKey id = new ContainerKey(segmentId, containerId); 1025 ContainerHandle c = t.openContainer(id, 1026 forUpdate ? (ContainerHandle.MODE_FORUPDATE | openContainerMode) : ContainerHandle.MODE_READONLY); 1027 if (c == null) 1028 throw T_Fail.testFailMsg("ContainerHandle failed to open: (" + 1029 segmentId + "," + containerId + ")"); 1030 1031 return c; 1032 } 1033 public ContainerHandle t_openContainer(T_TWC ctx, long segmentId, long containerId, boolean forUpdate) 1034 throws StandardException, T_Fail 1035 { 1036 csFactory.setCurrentContextManager(ctx.cm); 1037 try { 1038 return t_openContainer(ctx.tran, segmentId, containerId, forUpdate); 1039 } finally { 1040 csFactory.resetCurrentContextManager(ctx.cm); 1041 } 1042 } 1043 1044 1050 public void t_dropContainer(Transaction t, long segmentId, long containerId) 1051 throws StandardException, T_Fail 1052 { 1053 t.dropContainer(new ContainerKey(segmentId, containerId)); 1054 } 1055 1056 1063 public Page t_getLastPage(ContainerHandle c) throws T_Fail, StandardException { 1064 1065 Page page = c.getFirstPage(); 1066 if (page != null) 1067 { 1068 Page nextPage; 1069 while((nextPage = c.getNextPage(page.getPageNumber())) != null) 1070 { 1071 page.unlatch(); 1072 page = nextPage; 1073 } 1074 } 1075 1076 return page; 1077 } 1078 1079 1080 1087 public Page t_getPage(ContainerHandle c, long pageNumber) throws T_Fail, StandardException { 1088 1089 Page page = c.getPage(pageNumber); 1090 if (page == null) 1091 throw T_Fail.testFailMsg("fail to get page " + pageNumber + " from container " + c); 1092 1093 if (page.getPageNumber() != pageNumber) 1094 throw T_Fail.testFailMsg("page expected to have page number " + 1095 pageNumber + ", has " + page.getPageNumber() + " Container " + c); 1096 1097 return page; 1098 } 1099 1100 1106 public Page t_addPage(ContainerHandle c) throws T_Fail, StandardException { 1107 1108 Page page = c.addPage(); 1109 1110 if (page == null) 1111 throw T_Fail.testFailMsg("addPage() returned null"); 1112 1113 return page; 1114 } 1115 1116 1122 public void t_removePage(ContainerHandle c, Page p) throws T_Fail, StandardException 1123 { 1124 long pnum = p.getPageNumber(); 1125 c.removePage(p); 1126 1127 Page badp = c.getPage(pnum); 1129 if (badp != null) 1130 throw T_Fail.testFailMsg("got a deallcated page back"); 1131 } 1132 1133 1134 1143 public static RecordHandle t_insert(Page page, T_RawStoreRow row) 1144 throws T_Fail, StandardException { 1145 1146 RecordHandle rh = page.insert(row.getRow(), (FormatableBitSet) null, Page.INSERT_DEFAULT, 100); 1147 1148 return rh; 1149 } 1150 1151 1160 public static RecordHandle t_insertAtSlot(Page page, int slot, T_RawStoreRow row) 1161 throws T_Fail, StandardException { 1162 1163 RecordHandle rh = page.insertAtSlot(slot, row.getRow(), (FormatableBitSet) null, 1164 (LogicalUndo) null, Page.INSERT_DEFAULT, 100); 1165 1166 return rh; 1167 } 1168 1169 1178 public static RecordHandle t_insertAtSlot(Page page, int slot, T_RawStoreRow row, byte insertFlag) 1179 throws T_Fail, StandardException { 1180 1181 RecordHandle rh = page.insertAtSlot(slot, row.getRow(), (FormatableBitSet) null, 1182 (LogicalUndo) null, insertFlag, 100); 1183 1184 return rh; 1185 } 1186 1187 1196 public static RecordHandle t_insertAtSlot(Page page, int slot, T_RawStoreRow row, byte insertFlag, 1197 int overflowThreshold) throws T_Fail, StandardException { 1198 1199 RecordHandle rh = page.insertAtSlot(slot, row.getRow(), (FormatableBitSet) null, 1200 (LogicalUndo) null, insertFlag, overflowThreshold); 1201 1202 return rh; 1203 } 1204 1205 1214 public RecordHandle t_insert(ContainerHandle c, T_RawStoreRow row) 1215 throws T_Fail, StandardException { 1216 1217 Page page = c.getPageForInsert(0); 1218 boolean addedPage = false; 1219 1220 if (page == null) 1221 { 1222 page = t_addPage(c); 1223 addedPage = true; 1224 } 1225 else if (!page.spaceForInsert(row.getRow(), (FormatableBitSet) null, 100)) { 1226 page.unlatch(); 1227 page = t_addPage(c); 1228 addedPage = true; 1229 } 1230 1231 RecordHandle rh = t_insert(page, row); 1232 page.unlatch(); 1233 1234 if (rh == null) { 1235 if (addedPage) 1236 throw T_Fail.testFailMsg("insert returned null on an empty page"); 1237 1238 page = t_addPage(c); 1239 rh = t_insert(page, row); 1240 page.unlatch(); 1241 } 1242 return rh; 1243 } 1244 1245 1253 public void t_update(ContainerHandle c, RecordHandle rh, T_RawStoreRow row) 1254 throws T_Fail, StandardException 1255 { 1256 Page page = t_getPage(c, rh.getPageNumber()); 1257 try 1258 { 1259 if (!page.update(rh, row.getRow(), (FormatableBitSet)null)) 1260 throw T_Fail.testFailMsg("update failed"); 1261 1262 t_checkFetch(page, rh, row); 1263 } 1264 finally 1265 { 1266 page.unlatch(); 1267 } 1268 } 1269 1270 1271 1272 1281 public void t_checkUpdateCol(Page page, RecordHandle rh, int colNum, int 1282 numCols, String data) 1283 throws T_Fail, StandardException 1284 { 1285 if (!page.recordExists(rh, false)) 1286 throw T_Fail.testFailMsg("Record does not exist"); 1287 1288 T_RawStoreRow writeRow = new T_RawStoreRow(numCols); 1289 for (int i = 0; i < numCols; i++) 1290 writeRow.setColumn(i, (String ) null); 1291 writeRow.setColumn(colNum, data); 1292 FormatableBitSet colList = new FormatableBitSet(numCols); 1293 colList.set(colNum); 1294 1295 if (!page.update(rh, writeRow.getRow(), colList)) 1296 throw T_Fail.testFailMsg("update failed"); 1297 1298 t_checkFetchCol(page, rh, colNum, numCols, data); 1299 } 1300 1301 1302 1303 1311 public void t_delete(ContainerHandle c, RecordHandle rh) 1312 throws T_Fail, StandardException { 1313 1314 Page page = t_getPage(c, rh.getPageNumber()); 1315 1316 try 1317 { 1318 if (!page.recordExists(rh, false)) 1319 throw T_Fail.testFailMsg("record does not exist"); 1320 1321 if (!page.delete(rh, (LogicalUndo)null)) 1322 throw T_Fail.testFailMsg("delete failed"); 1323 1324 if (page.recordExists(rh, false)) 1325 throw T_Fail.testFailMsg("recordExists() returns true after a delete"); 1326 } 1327 finally 1328 { 1329 page.unlatch(); 1330 } 1331 } 1332 1333 1340 public void t_checkStringLengthFetch(Page page, int slot, int expectedLength) throws T_Fail, StandardException { 1341 1342 T_RawStoreRow rr = new T_RawStoreRow((String ) null); 1343 1344 page.fetchFromSlot( 1345 (RecordHandle) null, slot, rr.getRow(), 1346 (FetchDescriptor) null, 1347 true); 1348 1349 String s = ((SQLChar) (rr.getStorableColumn(0))).getString(); 1350 1351 1352 if ((s == null) && (expectedLength < 0)) 1353 return; 1354 1355 if ((s != null) && (expectedLength < 0)) 1356 throw T_Fail.testFailMsg("Expected null string, fetched one of length " + s.length()); 1357 1358 if (s == null) 1359 throw T_Fail.testFailMsg("Expected string length " + expectedLength + " got null string"); 1360 1361 if (s.length() != expectedLength) 1362 throw T_Fail.testFailMsg("fetch string length incorrect expected " + expectedLength + " got " + s.length()); 1363 } 1364 1365 1371 public void t_genRandomFile(String fileName, String mode, int size) throws T_Fail { 1372 1373 RandomAccessFile iFile = null; 1374 try { 1375 iFile = new RandomAccessFile(fileName, mode); 1376 for (int i = 0; i < size; i++){ 1377 byte b = (byte) (i & 0xff); 1378 b = (byte) (((b >= ' ') && (b <= '~')) ? b : ' '); 1379 iFile.write(b); 1380 } 1381 iFile.close(); 1382 } catch (FileNotFoundException fnfe) { 1383 throw T_Fail.testFailMsg("cannot create new file"); 1384 } catch (IOException ioe) { 1385 throw T_Fail.testFailMsg("io error, test failed"); 1386 } 1387 1388 } 1389 1390 1394 public static String getStringFromData(String data, int stringLen) { 1395 char[] ca = new char[stringLen]; 1396 1397 char[] sd = data.toCharArray(); 1398 1399 System.arraycopy(sd, 0, ca, 0, sd.length); 1400 1401 return new String (ca); 1402 } 1403 1404 1407 public static void t_wait(int milliSecond) 1408 { 1409 Thread.currentThread().yield(); 1410 try 1411 { 1412 Thread.currentThread().sleep(milliSecond); 1413 } 1414 catch (InterruptedException ie) 1415 { 1416 } 1417 } 1418 1419 1423 public static Properties setEncryptionParam(Properties startParams) 1424 { 1425 String encryptionPassword = 1427 PropertyUtil.getSystemProperty("testDataEncryption"); 1428 String encryptionProvider = 1430 PropertyUtil.getSystemProperty("testEncryptionProvider"); 1431 if (encryptionPassword != null) 1432 { 1433 if (startParams == null) 1434 startParams = new Properties (); 1435 1436 startParams.put(Attribute.DATA_ENCRYPTION, "true"); 1437 startParams.put(Attribute.BOOT_PASSWORD, encryptionPassword); 1438 if (encryptionProvider != null) { 1439 startParams.put(Attribute.CRYPTO_PROVIDER, encryptionProvider); 1440 } 1441 1442 1444 } 1445 1446 return startParams; 1447 } 1448 1449} 1450 | Popular Tags |