1 8 9 package com.sleepycat.je.log.entry; 10 11 import java.nio.ByteBuffer ; 12 13 import com.sleepycat.je.DatabaseException; 14 import com.sleepycat.je.dbi.DatabaseId; 15 import com.sleepycat.je.log.LogEntryType; 16 import com.sleepycat.je.log.LogUtils; 17 import com.sleepycat.je.log.LoggableObject; 18 import com.sleepycat.je.tree.Key; 19 import com.sleepycat.je.tree.LN; 20 import com.sleepycat.je.txn.Txn; 21 import com.sleepycat.je.utilint.DbLsn; 22 23 35 public class LNLogEntry implements LogEntry, LoggableObject, NodeLogEntry { 36 37 38 private LN ln; 39 private DatabaseId dbId; 40 private byte[] key; 41 private long abortLsn = DbLsn.NULL_LSN; 42 private boolean abortKnownDeleted; 43 private Txn txn; 44 45 private static final byte ABORT_KNOWN_DELETED_MASK = (byte) 1; 46 47 48 private Class logClass; private LogEntryType entryType; private long nodeId; 51 52 57 private boolean isTransactional; 58 59 60 public LNLogEntry(Class logClass, boolean isTransactional) { 61 this.logClass = logClass; 62 this.isTransactional = isTransactional; 63 } 64 65 66 public LNLogEntry(LogEntryType entryType, 67 LN ln, 68 DatabaseId dbId, 69 byte[] key, 70 long abortLsn, 71 boolean abortKnownDeleted, 72 Txn txn) { 73 this.entryType = entryType; 74 this.ln = ln; 75 this.dbId = dbId; 76 this.key = key; 77 this.abortLsn = abortLsn; 78 this.abortKnownDeleted = abortKnownDeleted; 79 this.txn = txn; 80 this.isTransactional = (txn != null); 81 this.logClass = ln.getClass(); 82 this.nodeId = ln.getNodeId(); 83 } 84 85 88 public void readEntry(ByteBuffer entryBuffer, 89 int entrySize, 90 byte entryTypeVersion, 91 boolean readFullItem) 92 throws DatabaseException { 93 94 try { 95 if (readFullItem) { 96 97 ln = (LN) logClass.newInstance(); 98 ln.readFromLog(entryBuffer, entryTypeVersion); 99 nodeId = ln.getNodeId(); 100 101 102 dbId = new DatabaseId(); 103 dbId.readFromLog(entryBuffer, entryTypeVersion); 104 105 106 key = LogUtils.readByteArray(entryBuffer); 107 108 if (isTransactional) { 109 110 114 abortLsn = LogUtils.readLong(entryBuffer); 115 if (DbLsn.getFileNumber(abortLsn) == 116 DbLsn.getFileNumber(DbLsn.NULL_LSN)) { 117 abortLsn = DbLsn.NULL_LSN; 118 } 119 120 abortKnownDeleted = 121 ((entryBuffer.get() & ABORT_KNOWN_DELETED_MASK) != 0) ? 122 true : false; 123 124 125 txn = new Txn(); 126 txn.readFromLog(entryBuffer, entryTypeVersion); 127 128 ln.setLastLoggedTransactionally(); 129 } else { 130 ln.clearLastLoggedTransactionally(); 131 } 132 } else { 133 134 140 int endPosition = entryBuffer.position() + entrySize; 141 nodeId = LogUtils.readLong(entryBuffer); 142 entryBuffer.position(endPosition); 143 ln = null; 144 } 145 } catch (IllegalAccessException e) { 146 throw new DatabaseException(e); 147 } catch (InstantiationException e) { 148 throw new DatabaseException(e); 149 } 150 } 151 152 155 public StringBuffer dumpEntry(StringBuffer sb, boolean verbose) { 156 ln.dumpLog(sb, verbose); 157 dbId.dumpLog(sb, verbose); 158 sb.append(Key.dumpString(key, 0)); 159 if (isTransactional) { 160 if (abortLsn != DbLsn.NULL_LSN) { 161 sb.append(DbLsn.toString(abortLsn)); 162 } 163 sb.append("<knownDeleted val=\""); 164 sb.append(abortKnownDeleted ? "true" : "false"); 165 sb.append("\"/>"); 166 txn.dumpLog(sb, verbose); 167 } 168 return sb; 169 } 170 171 174 public Object getMainItem() { 175 return ln; 176 } 177 178 181 public Object clone() throws CloneNotSupportedException { 182 return super.clone(); 183 } 184 185 188 public boolean isTransactional() { 189 return isTransactional; 190 } 191 192 195 public long getTransactionId() { 196 if (isTransactional) { 197 return txn.getId(); 198 } else { 199 return 0; 200 } 201 } 202 203 206 public long getNodeId() { 207 return nodeId; 208 } 209 210 213 214 217 public LogEntryType getLogType() { 218 return entryType; 219 } 220 221 225 public boolean marshallOutsideWriteLatch() { 226 return ln.marshallOutsideWriteLatch(); 227 } 228 229 233 public boolean countAsObsoleteWhenLogged() { 234 return ln.isDeleted(); 235 } 236 237 244 public void postLogWork(long justLoggedLsn) 245 throws DatabaseException { 246 247 if (isTransactional) { 248 txn.addLogInfo(justLoggedLsn); 249 } 250 } 251 252 258 public static int getStaticLogSize(int lnLogSize, 259 byte[] key, 260 boolean transactional) { 261 int size = lnLogSize + 262 DatabaseId.LOG_SIZE + 263 LogUtils.getByteArrayLogSize(key); 264 if (transactional) { 265 size += LogUtils.getLongLogSize(); 266 size++; size += Txn.LOG_SIZE; 268 } 269 return size; 270 } 271 272 275 public int getLogSize() { 276 return getStaticLogSize(ln.getLogSize(), key, isTransactional); 277 } 278 279 282 public void writeToLog(ByteBuffer destBuffer) { 283 ln.writeToLog(destBuffer); 284 dbId.writeToLog(destBuffer); 285 LogUtils.writeByteArray(destBuffer, key); 286 287 if (isTransactional) { 288 LogUtils.writeLong(destBuffer, abortLsn); 289 byte aKD = 0; 290 if (abortKnownDeleted) { 291 aKD |= ABORT_KNOWN_DELETED_MASK; 292 } 293 destBuffer.put(aKD); 294 txn.writeToLog(destBuffer); 295 ln.setLastLoggedTransactionally(); 296 } else { 297 ln.clearLastLoggedTransactionally(); 298 } 299 } 300 301 304 public LN getLN() { 305 return ln; 306 } 307 308 public DatabaseId getDbId() { 309 return dbId; 310 } 311 312 public byte[] getKey() { 313 return key; 314 } 315 316 public byte[] getDupKey() { 317 if (ln.isDeleted()) { 318 return null; 319 } else { 320 return ln.getData(); 321 } 322 } 323 324 public long getAbortLsn() { 325 return abortLsn; 326 } 327 328 public boolean getAbortKnownDeleted() { 329 return abortKnownDeleted; 330 } 331 332 public Long getTxnId() { 333 if (isTransactional) { 334 return new Long (txn.getId()); 335 } else { 336 return null; 337 } 338 } 339 340 public Txn getUserTxn() { 341 if (isTransactional) { 342 return txn; 343 } else { 344 return null; 345 } 346 } 347 } 348 | Popular Tags |