1 21 22 package org.apache.derby.impl.store.raw.data; 23 24 import org.apache.derby.iapi.reference.SQLState; 25 26 import org.apache.derby.iapi.services.io.ArrayInputStream; 27 import org.apache.derby.iapi.services.io.FormatableBitSet; 28 import org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream; 29 import org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream; 30 31 import org.apache.derby.iapi.services.sanity.SanityManager; 32 33 import org.apache.derby.iapi.error.StandardException; 34 35 import org.apache.derby.iapi.store.access.conglomerate.LogicalUndo; 36 37 import org.apache.derby.iapi.store.raw.Page; 38 import org.apache.derby.iapi.store.raw.RecordHandle; 39 import org.apache.derby.iapi.store.raw.xact.RawTransaction; 40 import org.apache.derby.iapi.store.raw.log.LogInstant; 41 42 import org.apache.derby.iapi.types.DataValueDescriptor; 43 44 45 import java.io.ByteArrayOutputStream ; 46 import java.io.IOException ; 47 48 49 public class DirectActions implements PageActions { 50 51 protected DynamicByteArrayOutputStream outBytes; 52 protected ArrayInputStream limitIn; 53 54 public DirectActions() { 55 outBytes = new DynamicByteArrayOutputStream(); 56 limitIn = new ArrayInputStream(); 57 } 58 59 public void actionDelete( 60 RawTransaction t, 61 BasePage page, 62 int slot, 63 int recordId, 64 boolean delete, 65 LogicalUndo undo) 66 throws StandardException 67 { 68 try { 69 70 page.setDeleteStatus((LogInstant)null, slot, delete); 71 72 } catch (IOException ioe) { 73 74 throw StandardException.newException( 75 SQLState.DATA_UNEXPECTED_EXCEPTION, ioe); 76 } 77 78 } 79 80 public int actionUpdate( 81 RawTransaction t, 82 BasePage page, 83 int slot, 84 int recordId, 85 Object [] row, 86 FormatableBitSet validColumns, 87 int realStartColumn, 88 DynamicByteArrayOutputStream logBuffer, 89 int realSpaceOnPage, 90 RecordHandle headRowHandle) 91 throws StandardException 92 { 93 if (logBuffer == null) 94 outBytes.reset(); 95 else 96 outBytes = (DynamicByteArrayOutputStream) logBuffer; 97 98 try { 99 100 int nextColumn = 102 page.logRow( 103 slot, false, recordId, row, validColumns, outBytes, 0, 104 Page.INSERT_OVERFLOW, realStartColumn, 105 realSpaceOnPage, 100); 106 107 limitIn.setData(outBytes.getByteArray()); 108 limitIn.setPosition(outBytes.getBeginPosition()); 109 limitIn.setLimit(outBytes.getPosition() - outBytes.getBeginPosition()); 110 111 page.storeRecord((LogInstant) null, slot, false, limitIn); 113 114 return nextColumn; 115 116 } catch (IOException ioe) { 117 118 throw StandardException.newException( 119 SQLState.DATA_UNEXPECTED_EXCEPTION, ioe); 120 } 121 122 } 123 124 public void actionPurge(RawTransaction t, BasePage page, int slot, int 125 num_rows, int[] recordIds, boolean needDataLogged) 126 throws StandardException 127 { 128 132 135 try { 136 for (int i = num_rows-1; i >= 0; i--) 137 { 138 page.purgeRecord((LogInstant) null, slot+i, recordIds[i]); 139 } 140 } catch (IOException ioe) { 141 142 throw StandardException.newException( 143 SQLState.DATA_UNEXPECTED_EXCEPTION, ioe); 144 } 145 146 } 147 148 public void actionUpdateField( 149 RawTransaction t, 150 BasePage page, 151 int slot, 152 int recordId, 153 int fieldId, 154 Object newValue, 155 LogicalUndo undo) 156 throws StandardException 157 { 158 outBytes.reset(); 159 160 try { 161 162 page.logColumn(slot, fieldId, newValue, (DynamicByteArrayOutputStream) outBytes, 100); 163 164 limitIn.setData(outBytes.getByteArray()); 165 limitIn.setPosition(outBytes.getBeginPosition()); 166 limitIn.setLimit(outBytes.getPosition() - outBytes.getBeginPosition()); 167 168 page.storeField((LogInstant) null, slot, fieldId, limitIn); 169 170 } catch (IOException ioe) { 171 172 throw StandardException.newException( 173 SQLState.DATA_UNEXPECTED_EXCEPTION, ioe); 174 } 175 176 } 177 178 public int actionInsert( 179 RawTransaction t, 180 BasePage page, 181 int slot, 182 int recordId, 183 Object [] row, 184 FormatableBitSet validColumns, 185 LogicalUndo undo, 186 byte insertFlag, 187 int startColumn, 188 boolean isLongColumn, 189 int realStartColumn, 190 DynamicByteArrayOutputStream logBuffer, 191 int realSpaceOnPage, 192 int overflowThreshold) 193 throws StandardException 194 { 195 if (logBuffer == null) 196 outBytes.reset(); 197 else 198 outBytes = (DynamicByteArrayOutputStream) logBuffer; 199 200 try { 201 if (isLongColumn) { 202 startColumn = page.logLongColumn(slot, recordId, 203 row[0], (DynamicByteArrayOutputStream) outBytes); 204 } else { 205 startColumn = page.logRow(slot, true, recordId, row, validColumns, 206 (DynamicByteArrayOutputStream) outBytes, startColumn, insertFlag, realStartColumn, realSpaceOnPage, 207 overflowThreshold); 208 } 209 210 limitIn.setData(outBytes.getByteArray()); 211 limitIn.setPosition(outBytes.getBeginPosition()); 212 limitIn.setLimit(outBytes.getPosition() - outBytes.getBeginPosition()); 213 214 page.storeRecord((LogInstant) null, slot, true, limitIn); 215 return (startColumn); 216 217 } catch (IOException ioe) { 218 219 throw StandardException.newException( 220 SQLState.DATA_UNEXPECTED_EXCEPTION, ioe); 221 } 222 223 } 224 225 public void actionCopyRows(RawTransaction t, BasePage destPage, BasePage srcPage, int destSlot, int numRows, int srcSlot, int[] recordIds) 226 throws StandardException 227 { 228 try { 229 230 234 int[] spaceNeeded = new int[numRows]; 236 for (int i = 0; i < numRows; i++) 237 { 238 outBytes.reset(); 239 srcPage.logRecord(srcSlot + i, BasePage.LOG_RECORD_DEFAULT, 240 recordIds[i], (FormatableBitSet) null, 241 outBytes, (RecordHandle)null); 242 spaceNeeded[i] = outBytes.getUsed(); 243 244 } 246 247 if (!destPage.spaceForCopy(numRows, spaceNeeded)) 248 { 249 throw StandardException.newException( 250 SQLState.DATA_NO_SPACE_FOR_RECORD); 251 } 252 253 for (int i = 0; i < numRows; i++) 255 { 256 outBytes.reset(); 260 srcPage.logRecord(srcSlot + i, BasePage.LOG_RECORD_DEFAULT, 261 recordIds[i], (FormatableBitSet) null, 262 outBytes, (RecordHandle)null); 263 264 limitIn.setData(outBytes.getByteArray()); 265 limitIn.setPosition(outBytes.getBeginPosition()); 266 limitIn.setLimit(outBytes.getPosition() - outBytes.getBeginPosition()); 267 268 destPage.storeRecord((LogInstant) null, destSlot+i, true, limitIn); 269 } 270 } catch (IOException ioe) { 271 272 throw StandardException.newException( 273 SQLState.DATA_UNEXPECTED_EXCEPTION, ioe); 274 } 275 276 } 277 278 public void actionInvalidatePage(RawTransaction t, BasePage page) 279 throws StandardException 280 { 281 page.setPageStatus((LogInstant)null, BasePage.INVALID_PAGE); 282 } 283 284 public void actionInitPage(RawTransaction t, BasePage page, int initFlag, 285 int pageFormatId, long pageOffset) 286 throws StandardException 287 { 288 boolean overflowPage = ((initFlag & BasePage.INIT_PAGE_OVERFLOW) != 0); 289 boolean reuse = ((initFlag & BasePage.INIT_PAGE_REUSE) != 0); 290 291 int nextRecordId = ((initFlag & BasePage.INIT_PAGE_REUSE_RECORDID) == 0) ? 292 page.newRecordId() : RecordHandle.FIRST_RECORD_ID; 293 294 if (SanityManager.DEBUG) 295 SanityManager.ASSERT(page.getTypeFormatId() == pageFormatId, 296 "Direct initPage somehow got the wrong page formatId"); 297 298 page.initPage((LogInstant)null, BasePage.VALID_PAGE, 299 nextRecordId, overflowPage, reuse); 300 } 301 302 public void actionShrinkReservedSpace(RawTransaction t, BasePage page, 303 int slot, int recordId, int newValue, int oldValue) 304 throws StandardException 305 { 306 try 307 { 308 page.setReservedSpace((LogInstant)null, slot, newValue); 309 } 310 catch (IOException ioe) 311 { 312 throw StandardException.newException( 313 SQLState.DATA_UNEXPECTED_EXCEPTION, ioe); 314 } 315 } 316 317 } 318 | Popular Tags |