1 21 22 package org.apache.derby.impl.store.raw.data; 23 24 import org.apache.derby.iapi.services.io.FormatIdUtil; 25 import org.apache.derby.iapi.services.io.StoredFormatIds; 26 import org.apache.derby.iapi.services.sanity.SanityManager; 27 28 import org.apache.derby.iapi.store.raw.Compensation; 29 import org.apache.derby.iapi.store.raw.ContainerHandle; 30 import org.apache.derby.iapi.store.raw.LockingPolicy; 31 import org.apache.derby.iapi.store.raw.Transaction; 32 import org.apache.derby.iapi.store.raw.Undoable; 33 34 import org.apache.derby.iapi.store.raw.xact.RawTransaction; 35 import org.apache.derby.iapi.store.raw.data.RawContainerHandle; 36 import org.apache.derby.iapi.store.raw.log.LogInstant; 37 38 import org.apache.derby.iapi.error.StandardException; 39 40 import org.apache.derby.iapi.util.ByteArray; 41 42 import java.io.ObjectOutput ; 43 import java.io.InputStream ; 44 import java.io.ObjectInput ; 45 import java.io.IOException ; 46 import org.apache.derby.iapi.services.io.LimitObjectInput; 47 48 70 public class ContainerOperation extends ContainerBasicOperation implements Undoable 71 { 72 protected byte operation; 74 transient protected boolean hasCreateByteArray = true; 77 78 protected ByteArray createByteArray; 81 protected static final byte CREATE = (byte)1; 82 protected static final byte DROP = (byte)2; 83 protected static final byte REMOVE = (byte)4; 84 85 protected ContainerOperation(RawContainerHandle hdl, byte operation) 86 throws StandardException 87 { 88 super(hdl); 89 this.operation = operation; 90 } 91 92 95 96 public ContainerOperation() { super(); } 98 99 public void writeExternal(ObjectOutput out) throws IOException 100 { 101 super.writeExternal(out); 102 out.writeByte(operation); 103 104 if (operation == CREATE) 105 { 106 try 107 { 108 createByteArray = containerHdl.logCreateContainerInfo(); 109 } 110 catch (StandardException se) 111 { 112 throw new IOException (se.toString()); 113 } 114 115 createByteArray.writeExternal(out); 116 } 117 } 118 119 123 public void readExternal(ObjectInput in) 124 throws IOException , ClassNotFoundException 125 { 126 super.readExternal(in); 127 operation = in.readByte(); 128 129 if (operation == CREATE && hasCreateByteArray) 130 { 131 createByteArray = new ByteArray(); 132 createByteArray.readExternal(in); 133 } 134 } 135 136 139 public int getTypeFormatId() { 140 return StoredFormatIds.LOGOP_CONTAINER; 141 } 142 143 144 147 155 protected RawContainerHandle findContainerForRedoRecovery( 156 RawTransaction xact) 157 throws StandardException 158 { 159 if (SanityManager.DEBUG) 160 SanityManager.ASSERT(createByteArray != null, 161 "cannot reCreate container in load tran, createByteArray is null"); 162 163 long sid = containerId.getSegmentId(); 164 long cid = containerId.getContainerId(); 165 166 xact.reCreateContainerForRedoRecovery(sid, cid, createByteArray); 167 168 return xact.openDroppedContainer(containerId, (LockingPolicy)null); 170 } 171 172 175 public final void doMe(Transaction tran, LogInstant instant, 176 LimitObjectInput in) 177 throws StandardException 178 { 179 180 switch (operation) 181 { 182 case DROP: 183 containerHdl.dropContainer(instant, true); 184 break; 190 191 case REMOVE: 192 containerHdl.removeContainer(instant); 193 break; 194 195 case CREATE: 196 break; 197 } 201 202 releaseResource(tran); 203 } 204 205 206 220 public void undoMe(Transaction tran, RawContainerHandle hdl, 221 LogInstant CLRInstant, LimitObjectInput in) 222 throws StandardException 223 { 224 switch(operation) 225 { 226 case DROP: 227 if (SanityManager.DEBUG) { 228 SanityManager.ASSERT(hdl != null, "container handle is null"); 229 SanityManager.ASSERT(hdl.getContainerStatus() != RawContainerHandle.COMMITTED_DROP, 230 "Undoing a drop but the container status is not dropped"); 231 } 232 hdl.dropContainer(CLRInstant, false); break; 234 235 case CREATE: 236 hdl.removeContainer(CLRInstant); 238 break; 239 240 case REMOVE: 241 if (SanityManager.DEBUG) { 242 SanityManager.THROWASSERT("cannot undo REMOVE, should not have generated a CLR in the first place"); 243 } 244 break; 245 } 246 releaseResource(tran); 247 248 } 249 250 254 public Compensation generateUndo(Transaction tran, LimitObjectInput in) 255 throws StandardException 256 { 257 if (operation == REMOVE) 258 return null; else 260 { 261 RawContainerHandle undoContainerHandle = findContainer(tran); 262 263 273 return new ContainerUndoOperation(undoContainerHandle, this); 274 } 275 } 276 277 278 public String toString() 279 { 280 if (SanityManager.DEBUG) 281 { 282 String str = super.toString(); 283 switch(operation) 284 { 285 case CREATE: str += " CREATE container " + containerId; 286 break; 287 case DROP: str += " DROP container " + containerId; 288 break; 289 case REMOVE: str += " REMOVE container " + containerId; 290 break; 291 } 292 return str; 293 } 294 else 295 return null; 296 } 297 298 299 } 300 | Popular Tags |