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.sanity.SanityManager; 27 import org.apache.derby.iapi.services.io.FormatIdUtil; 28 import org.apache.derby.iapi.services.io.StoredFormatIds; 29 30 import org.apache.derby.impl.store.raw.data.BasePage; 31 32 import org.apache.derby.iapi.store.access.conglomerate.LogicalUndo; 33 34 import org.apache.derby.iapi.store.raw.Compensation; 35 import org.apache.derby.iapi.store.raw.Page; 36 import org.apache.derby.iapi.store.raw.RecordHandle; 37 import org.apache.derby.iapi.store.raw.Transaction; 38 import org.apache.derby.iapi.store.raw.xact.RawTransaction; 39 40 import org.apache.derby.iapi.store.raw.log.LogInstant; 41 42 import org.apache.derby.iapi.error.StandardException; 43 44 import org.apache.derby.iapi.types.DataValueDescriptor; 45 46 import org.apache.derby.iapi.services.io.CompressedNumber; 47 import org.apache.derby.iapi.services.io.FormatableBitSet; 48 import org.apache.derby.iapi.util.ByteArray; 49 import org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream; 50 51 import java.io.OutputStream ; 52 import java.io.ObjectOutput ; 53 import java.io.ObjectInput ; 54 import java.io.IOException ; 55 import org.apache.derby.iapi.services.io.LimitObjectInput; 56 57 58 78 public final class DeleteOperation extends LogicalPageOperation 79 { 80 protected int doMeSlot; protected boolean delete; 83 transient protected ByteArray preparedLog; 84 85 public DeleteOperation(RawTransaction t, BasePage page, int slot, int recordId, 86 boolean delete, LogicalUndo undo) 87 throws StandardException 88 { 89 super(page, undo, recordId); 90 91 doMeSlot = slot; 92 this.delete = delete; 93 94 try { 95 writeOptionalDataToBuffer(t); 96 } catch (IOException ioe) { 97 throw StandardException.newException( 98 SQLState.DATA_UNEXPECTED_EXCEPTION, ioe); 99 } 100 } 101 102 105 106 public DeleteOperation() { super(); } 108 109 113 public void writeExternal(ObjectOutput out) throws IOException 114 { 115 super.writeExternal(out); 116 CompressedNumber.writeInt(out, doMeSlot); 117 out.writeBoolean(delete); 118 } 119 120 125 public void readExternal(ObjectInput in) 126 throws IOException , ClassNotFoundException 127 { 128 super.readExternal(in); 129 doMeSlot = CompressedNumber.readInt(in); 130 delete = in.readBoolean(); 131 } 132 133 136 public int getTypeFormatId() { 137 return StoredFormatIds.LOGOP_DELETE; 138 } 139 140 143 151 public void doMe(Transaction xact, LogInstant instant, LimitObjectInput in) 152 throws StandardException, IOException 153 { 154 this.page.setDeleteStatus(instant, doMeSlot, delete); 155 } 156 157 160 161 171 public void undoMe(Transaction xact, BasePage undoPage, int undoRecordId, 172 LogInstant CLRInstant, LimitObjectInput in) 173 throws StandardException, IOException 174 { 175 176 int slot = 177 undoPage.findRecordById(undoRecordId, Page.FIRST_SLOT_NUMBER); 178 179 if (SanityManager.DEBUG) 180 { 181 if (undoRecordId != this.recordId) 186 if (undoPage.getPageNumber() == getPageId().getPageNumber()) 187 SanityManager.THROWASSERT( 188 "recordId changed from " + this.recordId + 189 " to " + undoRecordId + 190 " but page number did not change " + 191 undoPage.getPageNumber()); 192 193 if (slot == -1) 194 SanityManager.THROWASSERT( 195 "recordId " + 196 undoRecordId + 197 " not found on page " + 198 undoPage.getPageNumber() + 199 undoPage); 200 } 201 202 undoPage.setDeleteStatus(CLRInstant, slot, !delete); 203 204 undoPage.setAuxObject(null); 205 } 206 207 210 211 212 218 public void restoreLoggedRow( 219 Object [] row, 220 LimitObjectInput in) 221 throws StandardException, IOException 222 { 223 Page p = null; 224 225 try { 226 p = getContainer().getPage(getPageId().getPageNumber()); 232 233 234 ((BasePage)p).restoreRecordFromStream(in, row); 235 236 } finally { 237 238 if (p != null) { 239 p.unlatch(); 240 p = null; 241 } 242 } 243 } 244 245 248 249 256 public void restoreMe(Transaction xact, BasePage undoPage, LogInstant CLRinstant, LimitObjectInput in) 257 throws StandardException, IOException 258 { 259 int slot = undoPage.findRecordById(recordId, Page.FIRST_SLOT_NUMBER); 260 if (SanityManager.DEBUG) 261 { 262 if ( ! getPageId().equals(undoPage.getPageId())) 263 SanityManager.THROWASSERT( 264 "restoreMe cannot restore to a different page. " 265 + "doMe page:" + getPageId() + " undoPage:" + 266 undoPage.getPageId()); 267 if (slot != doMeSlot) 268 SanityManager.THROWASSERT( 269 "restoreMe cannot restore to a different slot. " 270 + "doMe slot:" + doMeSlot + " undoMe slot: " + 271 slot + " recordId:" + recordId); 272 } 273 undoPage.setDeleteStatus(CLRinstant, slot, !delete); 274 undoPage.setAuxObject(null); 275 } 276 277 282 283 public ByteArray getPreparedLog() 284 { 285 return (this.preparedLog); 286 } 287 288 294 private void writeOptionalDataToBuffer(RawTransaction t) 295 throws StandardException, IOException 296 { 297 298 if (SanityManager.DEBUG) { 299 SanityManager.ASSERT(this.page != null); 300 } 301 302 DynamicByteArrayOutputStream logBuffer = t.getLogBuffer(); 303 int optionalDataStart = logBuffer.getPosition(); 304 305 if (SanityManager.DEBUG) { 306 SanityManager.ASSERT(optionalDataStart == 0, 307 "Buffer for writing the optional data should start at position 0"); 308 } 309 310 if (undo != null) 311 this.page.logRecord(doMeSlot, BasePage.LOG_RECORD_DEFAULT, 312 recordId, (FormatableBitSet) null, logBuffer, 313 (RecordHandle)null); 314 315 int optionalDataLength = logBuffer.getPosition() - optionalDataStart; 316 317 if (SanityManager.DEBUG) { 318 if (optionalDataLength != logBuffer.getUsed()) 319 SanityManager.THROWASSERT("wrong optional data length, optionalDataLength = " 320 + optionalDataLength + ", logBuffer.getUsed() = " + logBuffer.getUsed()); 321 } 322 323 logBuffer.setPosition(optionalDataStart); 325 326 this.preparedLog = new ByteArray(logBuffer.getByteArray(), optionalDataStart, 327 optionalDataLength); 328 329 } 330 331 public String toString() 332 { 333 if (SanityManager.DEBUG) 334 { 335 return super.toString() + 336 " Delete :" + 337 " Slot=" + doMeSlot + 338 " recordId=" + recordId + 339 " delete=" + delete; 340 } 341 else 342 return null; 343 } 344 } 345 | Popular Tags |