1 21 22 package org.apache.derby.impl.store.raw.log; 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 import org.apache.derby.iapi.services.io.Formatable; 30 31 import org.apache.derby.iapi.error.StandardException; 32 33 import org.apache.derby.iapi.store.raw.Loggable; 34 import org.apache.derby.iapi.store.raw.Compensation; 35 import org.apache.derby.iapi.store.raw.RePreparable; 36 import org.apache.derby.iapi.store.raw.Undoable; 37 38 import org.apache.derby.iapi.store.raw.xact.TransactionId; 39 40 import org.apache.derby.iapi.services.io.CompressedNumber; 41 42 import java.io.ObjectOutput ; 43 import java.io.ObjectInput ; 44 import java.io.IOException ; 45 46 47 69 public class LogRecord implements Formatable { 70 71 private TransactionId xactId; private Loggable op; private int group; 75 transient ObjectInput input; 81 82 private static final int formatLength = FormatIdUtil.getFormatIdByteLength(StoredFormatIds.LOG_RECORD); 83 84 public LogRecord() { 85 } 86 87 90 91 95 public void writeExternal(ObjectOutput out) throws IOException 96 { 97 CompressedNumber.writeInt(out, group); 98 out.writeObject(xactId); 99 out.writeObject(op); 100 } 101 102 107 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException 108 { 109 group = CompressedNumber.readInt(in); 110 input = in; 112 xactId = null; op = null; 114 } 115 116 119 public int getTypeFormatId() { 120 return StoredFormatIds.LOG_RECORD; 121 } 122 123 126 public void setValue(TransactionId xactId, Loggable op) 127 { 128 this.xactId = xactId; 129 this.op = op; 130 131 this.group = op.group(); 132 } 133 134 public static int formatOverhead() 135 { 136 return formatLength; 137 } 138 139 public static int maxGroupStoredSize() 140 { 141 return CompressedNumber.MAX_INT_STORED_SIZE; 142 } 143 144 public static int maxTransactionIdStoredSize(TransactionId tranId) 145 { 146 return tranId.getMaxStoredSize(); 147 } 148 149 150 public static int getStoredSize(int group, TransactionId xactId) 151 { 152 153 if (SanityManager.DEBUG) 154 { 155 SanityManager.ASSERT(xactId == null, 156 "size calculation are based on xactId being null"); 157 } 158 159 return formatLength + CompressedNumber.sizeInt(group) + 160 FormatIdUtil.getFormatIdByteLength(StoredFormatIds.NULL_FORMAT_ID); 161 } 162 163 164 public TransactionId getTransactionId() 165 throws IOException , ClassNotFoundException 166 { 167 if (xactId != null) 168 return xactId; 169 170 if (SanityManager.DEBUG) 171 SanityManager.ASSERT(input != null, 172 "xactId not objectified but object input is not set"); 173 174 Object obj = input.readObject(); 175 if (SanityManager.DEBUG) 176 { 177 SanityManager.ASSERT(obj instanceof TransactionId, 178 "log record not getting expected TransactionId"); 179 } 180 xactId = (TransactionId)obj; 181 182 return xactId; 183 } 184 185 public Loggable getLoggable() throws IOException , ClassNotFoundException { 186 187 if (op != null) return op; 190 if (SanityManager.DEBUG) 191 SanityManager.ASSERT(input != null, 192 "logop not objectified but object input is not set"); 193 194 if (xactId == null) { 196 xactId = (TransactionId)input.readObject(); 197 } 198 199 Object obj = input.readObject(); 200 201 if (SanityManager.DEBUG) { 202 if ( ! (obj instanceof Loggable)) 203 SanityManager.THROWASSERT( 204 "log record not getting expected Loggable: got : " + 205 obj.getClass().getName()); 206 } 207 op = (Loggable)obj; 208 209 input = null; 210 211 return op; 212 } 213 214 public RePreparable getRePreparable() 215 throws IOException , ClassNotFoundException 216 { 217 return((RePreparable) getLoggable()); 218 } 219 220 227 public void skipLoggable() throws StandardException 228 { 229 if (op != null) return; 231 232 try 233 { 234 if (xactId == null) 235 xactId = (TransactionId)input.readObject(); 237 if (op == null) 238 op = (Loggable)input.readObject(); } 240 catch(ClassNotFoundException cnfe) 241 { 242 throw StandardException.newException(SQLState.LOG_CORRUPTED, cnfe); 243 } 244 catch(IOException ioe) 245 { 246 throw StandardException.newException(SQLState.LOG_CORRUPTED, ioe); 247 } 248 } 249 250 public Undoable getUndoable() throws IOException , ClassNotFoundException 251 { 252 if (op == null) 253 getLoggable(); 255 if (op instanceof Undoable) 256 return (Undoable) op; 257 else 258 return null; 259 } 260 261 public boolean isCLR() { 262 return ((group & Loggable.COMPENSATION) != 0); 263 } 264 265 public boolean isFirst() { 266 return ((group & Loggable.FIRST) != 0); 267 } 268 269 public boolean isComplete() { 270 return ((group & Loggable.LAST) != 0); 271 } 272 273 public boolean isPrepare() { 274 return ((group & Loggable.PREPARE) != 0); 275 } 276 277 public boolean requiresPrepareLocks() { 278 return ((group & Loggable.XA_NEEDLOCK) != 0); 279 } 280 281 public boolean isCommit() 282 { 283 if (SanityManager.DEBUG) 284 { 285 SanityManager.ASSERT((group & Loggable.LAST) == Loggable.LAST, 286 "calling isCommit on log record that is not last"); 287 SanityManager.ASSERT((group & (Loggable.COMMIT | Loggable.ABORT)) != 0, 288 "calling isCommit on log record before commit status is recorded"); 289 } 290 return ((group & Loggable.COMMIT) != 0); 291 } 292 293 public boolean isAbort() 294 { 295 if (SanityManager.DEBUG) 296 { 297 SanityManager.ASSERT((group & Loggable.LAST) == Loggable.LAST, 298 "calling isAbort on log record that is not last"); 299 SanityManager.ASSERT((group & (Loggable.COMMIT | Loggable.ABORT)) != 0, 300 "calling isAbort on log record before abort status is recorded"); 301 } 302 return ((group & Loggable.ABORT) != 0); 303 } 304 305 public int group() 306 { 307 return group; 308 } 309 310 311 public boolean isChecksum() { 312 return ((group & Loggable.CHECKSUM) != 0); 313 } 314 } 315 | Popular Tags |