1 21 22 package org.apache.derby.impl.store.access.sort; 23 24 import java.util.Properties ; 25 26 import org.apache.derby.iapi.services.monitor.ModuleControl; 27 import org.apache.derby.iapi.services.monitor.ModuleSupportable; 28 import org.apache.derby.iapi.services.monitor.Monitor; 29 import org.apache.derby.iapi.services.property.PropertyUtil; 30 import org.apache.derby.iapi.services.sanity.SanityManager; 31 32 import org.apache.derby.iapi.error.StandardException; 33 34 import org.apache.derby.iapi.store.access.conglomerate.MethodFactory; 35 import org.apache.derby.iapi.store.access.conglomerate.Sort; 36 import org.apache.derby.iapi.store.access.conglomerate.SortFactory; 37 38 import org.apache.derby.iapi.store.access.SortObserver; 39 import org.apache.derby.iapi.store.access.SortCostController; 40 import org.apache.derby.iapi.store.access.ColumnOrdering; 41 import org.apache.derby.iapi.store.access.TransactionController; 42 43 import org.apache.derby.iapi.types.DataValueDescriptor; 44 45 import org.apache.derby.iapi.services.uuid.UUIDFactory; 46 47 import org.apache.derby.catalog.UUID; 48 49 52 53 public class ExternalSortFactory implements 54 SortFactory, ModuleControl, ModuleSupportable, SortCostController 55 { 56 57 private boolean userSpecified; private int defaultSortBufferMax; 59 private int sortBufferMax; 60 61 private static final String IMPLEMENTATIONID = "sort external"; 62 private static final String FORMATUUIDSTRING = "D2976090-D9F5-11d0-B54D-00A024BF8879"; 63 private UUID formatUUID = null; 64 private static final int DEFAULT_SORTBUFFERMAX = 1024; 65 private static final int MINIMUM_SORTBUFFERMAX = 4; 66 67 protected static final int DEFAULT_MEM_USE = 1024*1024; protected static final int DEFAULT_MAX_MERGE_RUN = 512; 71 72 private static final int SORT_ROW_OVERHEAD = 8*4+12; 74 75 76 79 80 84 public Properties defaultProperties() 85 { 86 return new Properties (); 87 } 88 89 92 public boolean supportsImplementation(String implementationId) 93 { 94 return implementationId.equals(IMPLEMENTATIONID); 95 } 96 97 100 public String primaryImplementationType() 101 { 102 return IMPLEMENTATIONID; 103 } 104 105 108 public boolean supportsFormat(UUID formatid) 109 { 110 return formatid.equals(formatUUID); 111 } 112 113 116 public UUID primaryFormat() 117 { 118 return formatUUID; 119 } 120 121 124 125 132 public Sort createSort( 133 TransactionController tran, 134 int segment, 135 Properties implParameters, 136 DataValueDescriptor[] template, 137 ColumnOrdering columnOrdering[], 138 SortObserver sortObserver, 139 boolean alreadyInOrder, 140 long estimatedRows, 141 int estimatedRowSize) 142 throws StandardException 143 { 144 MergeSort sort = new MergeSort(); 145 146 156 if (!userSpecified) 157 { 158 162 if (estimatedRowSize > 0) 165 { 166 estimatedRowSize += SORT_ROW_OVERHEAD + 174 (template.length*(4+12)) + 8; 175 sortBufferMax = DEFAULT_MEM_USE/estimatedRowSize; 176 } 177 else 178 { 179 sortBufferMax = defaultSortBufferMax; 180 } 181 182 if (estimatedRows > sortBufferMax && 189 (estimatedRows*1.1) < sortBufferMax*2) 190 sortBufferMax = (int)(estimatedRows/2 + estimatedRows/10); 191 192 if (sortBufferMax < MINIMUM_SORTBUFFERMAX) 194 sortBufferMax = MINIMUM_SORTBUFFERMAX; 195 } 196 else 197 { 198 sortBufferMax = defaultSortBufferMax; 200 } 201 202 if (SanityManager.DEBUG) 203 { 204 if (SanityManager.DEBUG_ON("SortTuning")) 205 { 206 SanityManager.DEBUG("SortTuning", 207 "sortBufferMax = " + sortBufferMax + 208 " estimatedRows = " + estimatedRows + 209 " estimatedRowSize = " + estimatedRowSize + 210 " defaultSortBufferMax = " + defaultSortBufferMax); 211 } 212 } 213 214 sort.initialize( 215 template, columnOrdering, sortObserver, 216 alreadyInOrder, estimatedRows, sortBufferMax); 217 return sort; 218 } 219 220 233 public SortCostController openSortCostController() 234 throws StandardException 235 { 236 return(this); 237 } 238 239 242 243 public void close() 244 { 245 } 247 248 296 public double getSortCost( 297 DataValueDescriptor[] template, 298 ColumnOrdering columnOrdering[], 299 boolean alreadyInOrder, 300 long estimatedInputRows, 301 long estimatedExportRows, 302 int estimatedRowSize) 303 throws StandardException 304 { 305 306 if (estimatedInputRows == 0) 307 return 0.0; 308 309 312 if (SanityManager.DEBUG) 313 { 314 SanityManager.ASSERT(estimatedInputRows >= 0); 315 SanityManager.ASSERT(estimatedExportRows >= 0); 316 } 317 318 double ret_val = 319 1 + 320 ((0.32) * (estimatedInputRows) * Math.log(estimatedInputRows)); 321 322 return(ret_val); 323 } 324 325 328 329 public boolean canSupport(Properties startParams) { 330 331 if (startParams == null) 332 return false; 333 334 String impl = startParams.getProperty("derby.access.Conglomerate.type"); 335 if (impl == null) 336 return false; 337 338 return supportsImplementation(impl); 339 } 340 341 342 public void boot(boolean create, Properties startParams) 343 throws StandardException 344 { 345 UUIDFactory uuidFactory = Monitor.getMonitor().getUUIDFactory(); 347 348 formatUUID = uuidFactory.recreateUUID(FORMATUUIDSTRING); 350 351 defaultSortBufferMax = PropertyUtil.getSystemInt("derby.storage.sortBufferMax", 353 0, Integer.MAX_VALUE, 0); 354 355 if (defaultSortBufferMax == 0) 360 { 361 userSpecified = false; 362 defaultSortBufferMax = DEFAULT_SORTBUFFERMAX; 363 } 364 else 365 { 366 userSpecified = true; 367 if (defaultSortBufferMax < MINIMUM_SORTBUFFERMAX) 368 defaultSortBufferMax = MINIMUM_SORTBUFFERMAX; 369 } 370 371 } 372 373 public void stop() 374 { 375 } 376 377 } 378 | Popular Tags |