1 21 22 package org.apache.derby.impl.store.access.sort; 23 24 import java.util.Enumeration ; 25 import java.util.Vector ; 26 27 import org.apache.derby.iapi.services.sanity.SanityManager; 28 import org.apache.derby.iapi.services.io.Storable; 29 import org.apache.derby.iapi.error.StandardException; 30 import org.apache.derby.iapi.store.access.conglomerate.TransactionManager; 31 import org.apache.derby.iapi.store.access.conglomerate.ScanManager; 32 import org.apache.derby.iapi.store.access.ScanController; 33 import org.apache.derby.iapi.store.access.SortObserver; 34 import org.apache.derby.iapi.store.access.TransactionController; 35 import org.apache.derby.iapi.store.raw.StreamContainerHandle; 36 import org.apache.derby.iapi.store.raw.Transaction; 37 38 import org.apache.derby.iapi.types.DataValueDescriptor; 39 40 44 45 public class MergeScan extends SortScan 46 { 47 50 protected SortBuffer sortBuffer; 51 52 55 protected Vector mergeRuns; 56 57 62 protected StreamContainerHandle openScans[]; 63 64 private SortObserver sortObserver; 65 66 69 70 MergeScan( 71 MergeSort sort, 72 TransactionManager tran, 73 SortBuffer sortBuffer, 74 Vector mergeRuns, 75 SortObserver sortObserver, 76 boolean hold) 77 { 78 super(sort, tran, hold); 79 this.sortBuffer = sortBuffer; 80 this.mergeRuns = mergeRuns; 81 this.tran = tran; 82 this.sortObserver = sortObserver; 83 } 84 85 88 89 93 public boolean next() 94 throws StandardException 95 { 96 current = sortBuffer.removeFirst(); 97 if (current != null) 98 mergeARow(sortBuffer.getLastAux()); 99 return (current != null); 100 } 101 102 106 public void close() 107 { 108 if (openScans != null) 109 { 110 for (int i = 0; i < openScans.length; i++) 111 { 112 if (openScans[i] != null) 113 { 114 openScans[i].close(); 115 } 116 openScans[i] = null; 117 } 118 openScans = null; 119 } 120 121 if (super.sort != null) 123 { 124 sort.doneScanning(this, sortBuffer, mergeRuns); 125 sortBuffer = null; 126 mergeRuns = null; 127 } 128 129 super.close(); 131 } 132 133 137 public boolean closeForEndTransaction(boolean closeHeldScan) 138 { 139 if (!hold || closeHeldScan) 140 { 141 close(); 142 return(true); 143 } 144 else 145 { 146 return(false); 147 } 148 } 149 150 153 154 158 public boolean init(TransactionManager tran) 159 throws StandardException 160 { 161 if (SanityManager.DEBUG) 162 { 163 SanityManager.ASSERT(mergeRuns != null); 166 SanityManager.ASSERT(mergeRuns.size() > 0); 167 168 SanityManager.ASSERT(sortBuffer.capacity() >= mergeRuns.size()); 173 } 174 175 sortBuffer.reset(); 177 178 openScans = new StreamContainerHandle[mergeRuns.size()]; 181 if (openScans == null) 182 return false; 183 184 int scanindex = 0; 186 Enumeration e = mergeRuns.elements(); 187 while (e.hasMoreElements()) 188 { 189 long id = ((Long ) e.nextElement()).longValue(); 191 192 Transaction rawTran = tran.getRawStoreXact(); int segmentId = StreamContainerHandle.TEMPORARY_SEGMENT; 194 openScans[scanindex++] = 195 rawTran.openStreamContainer(segmentId, id, hold); 196 } 197 198 for (scanindex = 0; scanindex < openScans.length; scanindex++) 200 mergeARow(scanindex); 201 202 return true; 204 } 205 206 211 void mergeARow(int scanindex) 212 throws StandardException 213 { 214 if (SanityManager.DEBUG) 215 { 216 SanityManager.ASSERT(openScans[scanindex] != null); 221 } 222 223 DataValueDescriptor[] row; 224 225 do 228 { 229 row = sortObserver.getArrayClone(); 230 231 if (!openScans[scanindex].fetchNext(row)) 233 { 234 236 openScans[scanindex].close(); 237 openScans[scanindex] = null; 238 return; 239 } 240 241 sortBuffer.setNextAux(scanindex); 244 } 245 while (sortBuffer.insert(row) == SortBuffer.INSERT_DUPLICATE); 246 } 247 } 248 | Popular Tags |