1 21 22 package org.apache.derby.impl.store.access.sort; 23 24 import java.util.Vector ; 25 import org.apache.derby.iapi.services.sanity.SanityManager; 26 import org.apache.derby.iapi.services.io.Storable; 27 import org.apache.derby.iapi.error.StandardException; 28 import org.apache.derby.iapi.store.access.conglomerate.TransactionManager; 29 import org.apache.derby.iapi.store.access.ColumnOrdering; 30 import org.apache.derby.iapi.store.access.ConglomerateController; 31 import org.apache.derby.iapi.store.access.Qualifier; 32 import org.apache.derby.iapi.store.access.ScanController; 33 import org.apache.derby.iapi.store.access.SortController; 34 import org.apache.derby.iapi.store.access.SortInfo; 35 import org.apache.derby.iapi.store.access.TransactionController; 36 37 import org.apache.derby.iapi.types.DataValueDescriptor; 38 39 import org.apache.derby.iapi.types.RowLocation; 40 41 45 46 public final class MergeInserter implements SortController 47 { 48 51 protected MergeSort sort = null; 52 53 56 protected TransactionManager tran; 57 58 61 Vector mergeRuns = null; 62 63 67 SortBuffer sortBuffer = null; 68 69 73 long beginFreeMemory; 74 long beginTotalMemory; 75 long estimatedMemoryUsed; 76 boolean avoidMergeRun; int runSize; 78 int totalRunSize; 79 80 protected String stat_sortType; 81 protected int stat_numRowsInput; 82 protected int stat_numRowsOutput; 83 protected int stat_numMergeRuns; 84 protected Vector stat_mergeRunsSize; 85 86 87 90 91 95 public void insert(DataValueDescriptor[] row) 96 throws StandardException 97 { 98 if (SanityManager.DEBUG) 99 { 100 SanityManager.ASSERT(sort != null); 103 } 104 105 sort.checkColumnTypes(row); 107 108 int insertResult = sortBuffer.insert(row); 112 stat_numRowsInput++; 113 if (insertResult != SortBuffer.INSERT_DUPLICATE) 114 stat_numRowsOutput++; 115 if (insertResult == SortBuffer.INSERT_FULL) 116 { 117 if (avoidMergeRun) 118 { 119 Runtime jvm = Runtime.getRuntime(); 120 if (SanityManager.DEBUG) 121 { 122 if (SanityManager.DEBUG_ON("SortTuning")) 123 { 124 jvm.gc(); 125 jvm.gc(); 126 jvm.gc(); 127 } 128 } 129 130 long currentFreeMemory = jvm.freeMemory(); 131 long currentTotalMemory = jvm.totalMemory(); 132 133 estimatedMemoryUsed = (currentTotalMemory-currentFreeMemory) - 140 (beginTotalMemory-beginFreeMemory); 141 142 if (SanityManager.DEBUG) 143 { 144 if (SanityManager.DEBUG_ON("SortTuning")) 145 { 146 SanityManager.DEBUG("SortTuning", 147 "Growing sortBuffer dynamically, " + 148 "current sortBuffer capacity= " + 149 sortBuffer.capacity() + 150 " estimatedMemoryUsed = " + estimatedMemoryUsed + 151 " currentTotalMemory = " + currentTotalMemory + 152 " currentFreeMemory = " + currentFreeMemory + 153 " numcolumn = " + row.length + 154 " real per row memory = " + 155 (estimatedMemoryUsed / sortBuffer.capacity())); 156 } 157 } 158 159 if (estimatedMemoryUsed < 0 || 167 ((2*estimatedMemoryUsed) < (estimatedMemoryUsed+currentFreeMemory)/2) || 168 (2*estimatedMemoryUsed < ExternalSortFactory.DEFAULT_MEM_USE && 169 currentTotalMemory < (5*1024*1024))) 170 { 171 sortBuffer.grow(100); 173 174 if (sortBuffer.insert(row) != SortBuffer.INSERT_FULL) 175 return; 176 } 177 178 avoidMergeRun = false; } 181 182 stat_sortType = "external"; 186 long conglomid = sort.createMergeRun(tran, sortBuffer); 187 if (mergeRuns == null) 188 mergeRuns = new Vector (); 189 mergeRuns.addElement(new Long (conglomid)); 190 191 stat_numMergeRuns++; 192 runSize = stat_numRowsInput - totalRunSize - 1; 195 totalRunSize += runSize; 196 stat_mergeRunsSize.addElement(new Integer (runSize)); 197 198 sortBuffer.insert(row); 202 } 203 } 204 205 213 214 public void close() 215 { 216 if (sort != null) 219 sort.doneInserting(this, sortBuffer, mergeRuns); 220 221 if (stat_sortType == "external") 226 { 227 stat_numMergeRuns++; 228 stat_mergeRunsSize.addElement(new Integer (stat_numRowsInput - totalRunSize)); 229 } 230 231 tran.closeMe(this); 233 234 sort = null; 236 tran = null; 237 mergeRuns = null; 238 sortBuffer = null; 239 } 240 241 244 245 256 public SortInfo getSortInfo() 257 throws StandardException 258 { 259 return(new MergeSortInfo(this)); 260 } 261 262 263 267 boolean initialize(MergeSort sort, TransactionManager tran) 268 { 269 Runtime jvm = Runtime.getRuntime(); 270 if (SanityManager.DEBUG) 271 { 272 if (SanityManager.DEBUG_ON("SortTuning")) 273 { 274 jvm.gc(); 275 jvm.gc(); 276 jvm.gc(); 277 } 278 } 279 280 beginFreeMemory = jvm.freeMemory(); 281 beginTotalMemory = jvm.totalMemory(); 282 estimatedMemoryUsed = 0; 283 avoidMergeRun = true; stat_sortType = "internal"; 285 stat_numMergeRuns = 0; 286 stat_numRowsInput = 0; 287 stat_numRowsOutput = 0; 288 stat_mergeRunsSize = new Vector (); 289 runSize = 0; 290 totalRunSize = 0; 291 292 293 if (SanityManager.DEBUG) 294 { 295 if (SanityManager.DEBUG_ON("testSort")) 296 { 297 avoidMergeRun = false; 298 } 299 } 300 301 this.sort = sort; 302 this.tran = tran; 303 sortBuffer = new SortBuffer(sort); 304 if (sortBuffer.init() == false) 305 return false; 306 return true; 307 } 308 309 } 310 | Popular Tags |