1 21 22 package org.apache.derby.impl.store.access.btree; 23 24 import org.apache.derby.iapi.reference.Property; 25 import org.apache.derby.iapi.reference.SQLState; 26 27 import org.apache.derby.iapi.services.sanity.SanityManager; 28 29 import org.apache.derby.iapi.error.StandardException; 30 31 import org.apache.derby.iapi.store.access.conglomerate.Conglomerate; 32 import org.apache.derby.iapi.store.access.conglomerate.LogicalUndo; 33 import org.apache.derby.iapi.store.access.conglomerate.TransactionManager; 34 35 import org.apache.derby.iapi.store.access.DynamicCompiledOpenConglomInfo; 36 import org.apache.derby.iapi.store.access.RowUtil; 37 import org.apache.derby.iapi.store.access.ScanController; 38 import org.apache.derby.iapi.store.access.StoreCostController; 39 import org.apache.derby.iapi.store.access.StoreCostResult; 40 import org.apache.derby.iapi.store.access.TransactionController; 41 42 import org.apache.derby.iapi.store.raw.ContainerHandle; 43 import org.apache.derby.iapi.store.raw.LockingPolicy; 44 import org.apache.derby.iapi.store.raw.RawStoreFactory; 45 import org.apache.derby.iapi.store.raw.Transaction; 46 47 import org.apache.derby.iapi.types.DataValueDescriptor; 48 49 import org.apache.derby.iapi.types.RowLocation; 50 51 52 import org.apache.derby.iapi.services.io.FormatableBitSet; 53 import java.util.Properties ; 54 55 80 81 public class BTreeCostController extends OpenBTree 82 implements StoreCostController 83 { 84 85 115 144 145 150 private static final double 151 BTREE_CACHED_FETCH_BY_KEY_PER_LEVEL = (0.541 / 2); 152 153 private static final double 154 BTREE_SORTMERGE_FETCH_BY_KEY_PER_LEVEL = (1.432 / 2); 155 156 private static final double 157 BTREE_UNCACHED_FETCH_BY_KEY_PER_LEVEL = (3.143 / 2); 158 159 TransactionManager init_xact_manager; 161 Transaction init_rawtran; 162 Conglomerate init_conglomerate; 163 164 167 long num_pages; 168 long num_rows; 169 long page_size; 170 int tree_height; 171 172 173 174 public BTreeCostController() 175 { 176 } 177 178 179 180 191 public void init( 192 TransactionManager xact_manager, 193 BTree conglomerate, 194 Transaction rawtran) 195 throws StandardException 196 { 197 super.init( 198 xact_manager, 199 xact_manager, 200 (ContainerHandle) null, rawtran, 202 false, 203 ContainerHandle.MODE_READONLY, 204 TransactionManager.MODE_NONE, 205 (BTreeLockingPolicy) null, conglomerate, 209 (LogicalUndo) null, (DynamicCompiledOpenConglomInfo) null); 211 212 num_pages = this.container.getEstimatedPageCount( 0); 216 217 num_rows = 220 this.container.getEstimatedRowCount( 0) - num_pages; 221 222 Properties prop = new Properties (); 223 prop.put(Property.PAGE_SIZE_PARAMETER, ""); 224 this.container.getContainerProperties(prop); 225 page_size = 226 Integer.parseInt(prop.getProperty(Property.PAGE_SIZE_PARAMETER)); 227 228 tree_height = getHeight(); 229 230 return; 231 } 232 233 234 235 244 public void close() 245 throws StandardException 246 { 247 super.close(); 248 } 249 250 294 public double getFetchFromRowLocationCost( 295 FormatableBitSet validColumns, 296 int access_type) 297 throws StandardException 298 { 299 throw StandardException.newException( 300 SQLState.BTREE_UNIMPLEMENTED_FEATURE); 301 } 302 303 350 public double getFetchFromFullKeyCost( 351 FormatableBitSet validColumns, 352 int access_type) 353 throws StandardException 354 { 355 double ret_cost; 356 357 if ((access_type & StoreCostController.STORECOST_CLUSTERED) == 0) 358 { 359 ret_cost = BTREE_UNCACHED_FETCH_BY_KEY_PER_LEVEL; 361 } 362 else 363 { 364 ret_cost = BTREE_SORTMERGE_FETCH_BY_KEY_PER_LEVEL; 365 } 366 ret_cost *= tree_height; 367 368 return(ret_cost); 369 } 370 371 372 507 public void getScanCost( 508 int scan_type, 509 long row_count, 510 int group_size, 511 boolean forUpdate, 512 FormatableBitSet scanColumnList, 513 DataValueDescriptor[] template, 514 DataValueDescriptor[] startKeyValue, 515 int startSearchOperator, 516 DataValueDescriptor[] stopKeyValue, 517 int stopSearchOperator, 518 boolean reopen_scan, 519 int access_type, 520 StoreCostResult cost_result) 521 throws StandardException 522 { 523 float left_of_start; 524 float left_of_stop; 525 ControlRow control_row = null; 526 long input_row_count = (row_count < 0 ? num_rows : row_count); 527 528 try 529 { 530 if (startKeyValue == null) 532 { 533 left_of_start = 0; 534 } 535 else 536 { 537 539 SearchParameters sp = new SearchParameters( 540 startKeyValue, 541 ((startSearchOperator == ScanController.GE) ? 542 SearchParameters.POSITION_LEFT_OF_PARTIAL_KEY_MATCH : 543 SearchParameters.POSITION_RIGHT_OF_PARTIAL_KEY_MATCH), 544 template, this, true); 545 546 control_row = 547 ControlRow.Get(this, BTree.ROOTPAGEID).search(sp); 548 549 control_row.release(); 550 control_row = null; 551 552 left_of_start = sp.left_fraction; 553 } 554 555 if (stopKeyValue == null) 556 { 557 left_of_stop = 1; 558 } 559 else 560 { 561 563 SearchParameters sp = 564 new SearchParameters( 565 stopKeyValue, 566 ((stopSearchOperator == ScanController.GE) ? 567 SearchParameters.POSITION_LEFT_OF_PARTIAL_KEY_MATCH : 568 SearchParameters.POSITION_RIGHT_OF_PARTIAL_KEY_MATCH), 569 template, this, true); 570 571 control_row = 572 ControlRow.Get(this, BTree.ROOTPAGEID).search(sp); 573 574 control_row.release(); 575 control_row = null; 576 577 left_of_stop = sp.left_fraction; 578 } 579 580 584 586 float ret_fraction = left_of_stop - left_of_start; 587 588 if (ret_fraction < 0) 591 ret_fraction = 0; 592 593 if (ret_fraction > 1) 597 ret_fraction = 1; 598 599 float estimated_row_count = input_row_count * ret_fraction; 600 601 double cost = 603 getFetchFromFullKeyCost(scanColumnList, access_type); 604 605 cost += 609 (num_pages * ret_fraction) * BASE_UNCACHED_ROW_FETCH_COST; 610 611 616 long cached_row_count = ((long) estimated_row_count) - num_pages; 621 if (cached_row_count < 0) 622 cached_row_count = 0; 623 624 if (scan_type == StoreCostController.STORECOST_SCAN_NORMAL) 625 cost += cached_row_count * BASE_GROUPSCAN_ROW_COST; 626 else 627 cost += cached_row_count * BASE_HASHSCAN_ROW_FETCH_COST; 628 629 long row_size = 631 (input_row_count == 0) ? 632 4 : (num_pages * page_size) / input_row_count; 633 634 cost += 635 (estimated_row_count * row_size) * BASE_ROW_PER_BYTECOST; 636 637 if (SanityManager.DEBUG) 638 { 639 if (cost < 0) 640 SanityManager.THROWASSERT("cost " + cost); 641 642 if (estimated_row_count < 0) 643 SanityManager.THROWASSERT( 644 "estimated_row_count = " + estimated_row_count); 645 } 646 647 cost_result.setEstimatedCost(cost); 649 650 cost_result.setEstimatedRowCount(Math.round(estimated_row_count)); 652 } 653 finally 654 { 655 if (control_row != null) 656 control_row.release(); 657 } 658 659 663 return; 664 } 665 666 674 public RowLocation newRowLocationTemplate() 675 throws StandardException 676 { 677 throw StandardException.newException( 678 SQLState.BTREE_UNIMPLEMENTED_FEATURE); 679 } 680 } 681 | Popular Tags |