1 21 22 package org.apache.derby.impl.store.access.btree; 23 24 25 import org.apache.derby.iapi.reference.SQLState; 26 27 import org.apache.derby.iapi.services.io.ArrayInputStream; 28 import org.apache.derby.iapi.services.io.FormatableBitSet; 29 30 import org.apache.derby.iapi.services.monitor.Monitor; 31 import org.apache.derby.iapi.services.sanity.SanityManager; 32 33 import org.apache.derby.iapi.services.io.FormatIdUtil; 34 import org.apache.derby.iapi.services.io.Storable; 35 36 import org.apache.derby.iapi.services.stream.InfoStreams; 37 38 39 import org.apache.derby.iapi.error.StandardException; 40 import org.apache.derby.iapi.store.access.conglomerate.Conglomerate; 41 import org.apache.derby.iapi.store.access.conglomerate.ScanManager; 42 import org.apache.derby.iapi.store.access.conglomerate.TransactionManager; 43 import org.apache.derby.iapi.store.access.ConglomerateController; 44 import org.apache.derby.iapi.store.access.DynamicCompiledOpenConglomInfo; 45 import org.apache.derby.iapi.store.access.Qualifier; 46 import org.apache.derby.iapi.store.access.RowLocationRetRowSource; 47 import org.apache.derby.iapi.store.access.RowUtil; 48 import org.apache.derby.iapi.store.access.ScanController; 49 import org.apache.derby.iapi.store.access.StaticCompiledOpenConglomInfo; 50 import org.apache.derby.iapi.store.access.TransactionController; 51 52 import org.apache.derby.iapi.store.raw.LockingPolicy; 53 import org.apache.derby.iapi.store.raw.Page; 54 import org.apache.derby.iapi.store.raw.RawStoreFactory; 55 import org.apache.derby.iapi.store.raw.RecordHandle; 56 import org.apache.derby.iapi.store.raw.ContainerHandle; 57 import org.apache.derby.iapi.store.raw.Transaction; 58 import org.apache.derby.iapi.store.raw.ContainerKey; 59 60 import org.apache.derby.iapi.types.DataValueDescriptor; 61 62 import org.apache.derby.iapi.types.RowLocation; 63 64 import org.apache.derby.impl.store.access.conglomerate.ConglomerateUtil; 65 import org.apache.derby.impl.store.access.conglomerate.GenericConglomerate; 66 import org.apache.derby.impl.store.access.conglomerate.OpenConglomerateScratchSpace; 67 import org.apache.derby.impl.store.access.conglomerate.TemplateRow; 68 69 70 import java.io.IOException ; 71 import java.io.ObjectOutput ; 72 import java.io.ObjectInput ; 73 74 import java.util.Properties ; 75 76 77 99 100 public abstract class BTree extends GenericConglomerate 101 { 102 106 107 113 public static final long ROOTPAGEID = ContainerHandle.FIRST_PAGE_NUMBER; 114 115 121 public static final String PROPERTY_MAX_ROWS_PER_PAGE_PARAMETER = 122 (SanityManager.DEBUG ? "derby.access.btreeMaxRowPerPage" : null); 123 124 125 public static final String PROPERTY_ALLOWDUPLICATES = "allowDuplicates"; 126 public static final String PROPERTY_NKEYFIELDS = "nKeyFields"; 127 public static final String PROPERTY_NUNIQUECOLUMNS = "nUniqueColumns"; 128 public static final String PROPERTY_PARENTLINKS = "maintainParentLinks"; 129 130 131 132 136 137 140 protected ContainerKey id; 141 142 145 protected int nKeyFields; 146 147 151 int nUniqueColumns; 152 153 156 boolean allowDuplicates; 157 158 163 boolean maintainParentLinks; 164 165 175 static int maxRowsPerPage = Integer.MAX_VALUE; 176 177 180 protected int conglom_format_id; 181 182 185 int[] format_ids; 186 187 protected boolean[] ascDescInfo; 190 191 194 195 198 199 200 211 212 217 abstract protected BTreeLockingPolicy getBtreeLockingPolicy( 218 Transaction rawtran, 219 int lock_level, 220 int mode, 221 int isolation_level, 222 ConglomerateController base_cc, 223 OpenBTree open_btree) 224 throws StandardException; 225 226 239 abstract public ConglomerateController lockTable( 240 TransactionManager xact_manager, 241 int open_mode, 242 int lock_level, 243 int isolation_level) 244 throws StandardException; 245 246 247 251 252 253 265 final DataValueDescriptor[] createBranchTemplate( 266 DataValueDescriptor page_ptr) 267 throws StandardException 268 { 269 return(TemplateRow.newBranchRow(format_ids, page_ptr)); 270 } 271 272 273 277 278 291 final public DataValueDescriptor[] createTemplate() 292 throws StandardException 293 { 294 if (SanityManager.DEBUG) 295 SanityManager.ASSERT(format_ids != null); 296 297 return(TemplateRow.newRow((FormatableBitSet) null, format_ids)); 298 } 299 300 303 final public boolean isUnique() 304 { 305 return(nKeyFields != nUniqueColumns); 306 } 307 308 312 313 325 public void addColumn( 326 TransactionManager xact_manager, 327 int column_id, 328 Storable template_column) 329 throws StandardException 330 { 331 throw StandardException.newException( 332 SQLState.BTREE_UNIMPLEMENTED_FEATURE); 333 } 334 335 344 public final ContainerKey getId() 345 { 346 return(id); 347 } 348 349 350 390 391 public void create( 392 Transaction rawtran, 393 int segmentId, 394 long input_containerid, 395 DataValueDescriptor[] template, 396 Properties properties, 397 int conglom_format_id, 398 int tmpFlag 399 ) 400 throws StandardException 401 { 402 String result_string; 403 404 if (properties == null) 405 { 406 throw( 407 StandardException.newException( 408 SQLState.BTREE_PROPERTY_NOT_FOUND, PROPERTY_NKEYFIELDS)); 409 } 410 411 allowDuplicates = (Boolean.valueOf( 413 properties.getProperty(PROPERTY_ALLOWDUPLICATES, "false"))).booleanValue(); 414 415 result_string = properties.getProperty(PROPERTY_NKEYFIELDS); 416 if (result_string == null) 417 { 418 throw( 419 StandardException.newException( 420 SQLState.BTREE_PROPERTY_NOT_FOUND, PROPERTY_NKEYFIELDS)); 421 } 422 else 423 { 424 nKeyFields = Integer.parseInt(result_string); 425 } 426 427 result_string = properties.getProperty(PROPERTY_NUNIQUECOLUMNS); 428 if (result_string == null) 429 { 430 throw(StandardException.newException( 431 SQLState.BTREE_PROPERTY_NOT_FOUND, PROPERTY_NUNIQUECOLUMNS)); 432 } 433 else 434 { 435 nUniqueColumns = Integer.parseInt(result_string); 436 } 437 438 439 if (SanityManager.DEBUG) 440 { 441 result_string = 442 properties.getProperty(PROPERTY_MAX_ROWS_PER_PAGE_PARAMETER); 443 444 if (result_string != null) 445 { 446 maxRowsPerPage = Integer.parseInt(result_string); 447 } 448 } 449 450 maintainParentLinks = (Boolean.valueOf( 451 properties.getProperty(PROPERTY_PARENTLINKS, "true"))).booleanValue(); 452 453 if (SanityManager.DEBUG) 456 { 457 if (template.length != nKeyFields) 458 { 459 SanityManager.THROWASSERT( 460 "template.length (" + template.length + 461 ") expected to equal nKeyFields (" + 462 nKeyFields + ")"); 463 } 464 SanityManager.ASSERT((nUniqueColumns == nKeyFields) || 465 (nUniqueColumns == (nKeyFields - 1))); 466 } 467 468 format_ids = ConglomerateUtil.createFormatIds(template); 471 472 this.conglom_format_id = conglom_format_id; 474 475 properties.put(RawStoreFactory.PAGE_RESERVED_SPACE_PARAMETER, "0"); 478 properties.put(RawStoreFactory.MINIMUM_RECORD_SIZE_PARAMETER, "1"); 479 properties.put(RawStoreFactory.PAGE_REUSABLE_RECORD_ID, "true"); 480 481 long containerid = 482 rawtran.addContainer( 483 segmentId, input_containerid, 484 ContainerHandle.MODE_DEFAULT, properties, tmpFlag); 485 486 if (containerid <= 0) 489 { 490 throw(StandardException.newException( 491 SQLState.BTREE_CANT_CREATE_CONTAINER)); 492 } 493 494 if (SanityManager.DEBUG) 495 { 496 if (input_containerid != ContainerHandle.DEFAULT_ASSIGN_ID) 497 SanityManager.ASSERT(containerid == input_containerid); 498 } 499 500 id = new ContainerKey(segmentId, containerid); 501 } 502 503 510 public abstract void drop(TransactionManager xact_manager) 511 throws StandardException; 512 513 519 public abstract long load( 520 TransactionManager xact_manager, 521 boolean createConglom, 522 RowLocationRetRowSource rowSource) 523 throws StandardException; 524 525 public long getContainerid() 526 { 527 return(this.id.getContainerId()); 528 } 529 530 548 public DynamicCompiledOpenConglomInfo getDynamicCompiledConglomInfo( 549 long conglomId) 550 throws StandardException 551 { 552 return(new OpenConglomerateScratchSpace(format_ids)); 553 } 554 555 556 562 public boolean isTemporary() 563 { 564 return (id.getSegmentId() == ContainerHandle.TEMPORARY_SEGMENT); 565 } 566 567 574 public abstract ConglomerateController open( 575 TransactionManager xact_manager, 576 Transaction rawtran, 577 boolean hold, 578 int open_mode, 579 int lock_level, 580 LockingPolicy locking_policy, 581 StaticCompiledOpenConglomInfo static_info, 582 DynamicCompiledOpenConglomInfo dynamic_info) 583 throws StandardException; 584 585 586 587 592 593 594 600 public boolean isNull() 601 { 602 return id == null; 603 } 604 605 612 public void restoreToNull() 613 { 614 id = null; 615 } 616 617 628 public void readExternal(ObjectInput in) 629 throws IOException , ClassNotFoundException 630 { 631 conglom_format_id = FormatIdUtil.readFormatIdInteger(in); 633 634 long containerid = in.readLong(); 636 int segmentid = in.readInt(); 637 nKeyFields = in.readInt(); 638 nUniqueColumns = in.readInt(); 639 allowDuplicates = in.readBoolean(); 640 maintainParentLinks = in.readBoolean(); 641 642 format_ids = ConglomerateUtil.readFormatIdArray(this.nKeyFields, in); 644 645 id = new ContainerKey(segmentid, containerid); 646 } 647 648 public void readExternalFromArray(ArrayInputStream in) 649 throws IOException , ClassNotFoundException 650 { 651 conglom_format_id = FormatIdUtil.readFormatIdInteger(in); 653 654 long containerid = in.readLong(); 656 int segmentid = in.readInt(); 657 nKeyFields = in.readInt(); 658 nUniqueColumns = in.readInt(); 659 allowDuplicates = in.readBoolean(); 660 maintainParentLinks = in.readBoolean(); 661 662 format_ids = ConglomerateUtil.readFormatIdArray(this.nKeyFields, in); 664 665 id = new ContainerKey(segmentid, containerid); 666 } 667 668 669 677 public void writeExternal(ObjectOutput out) 678 throws IOException 679 { 680 FormatIdUtil.writeFormatIdInteger(out, conglom_format_id); 681 682 out.writeLong(id.getContainerId()); 683 out.writeInt((int) id.getSegmentId()); 684 out.writeInt((nKeyFields)); 685 out.writeInt((nUniqueColumns)); 686 out.writeBoolean((allowDuplicates)); 687 out.writeBoolean((maintainParentLinks)); 688 689 ConglomerateUtil.writeFormatIdArray(format_ids, out); 690 } 691 692 696 697 public String toString() 698 { 699 if (SanityManager.DEBUG) 700 { 701 return ("BTREE: containerid = " + 702 (this.id == null ? "null" : this.id.toString()) + 703 ";nKeyFields = " + nKeyFields + 704 ";nUniqueColumns = " + nUniqueColumns + 705 ";allowDuplicates = " + allowDuplicates); 706 } 707 else 708 { 709 return(super.toString()); 710 } 711 } 712 } 713 | Popular Tags |