1 /*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002, 2014 Oracle and/or its affiliates. All rights reserved. 5 * 6 */ 7 8 package com.sleepycat.je.txn; 9 10 import java.nio.ByteBuffer; 11 12 import com.sleepycat.je.log.LogUtils; 13 import com.sleepycat.je.log.Loggable; 14 import com.sleepycat.je.utilint.DbLsn; 15 import com.sleepycat.je.utilint.Timestamp; 16 17 /** 18 * This class writes out a transaction commit or transaction end record. 19 */ 20 public abstract class TxnEnd implements Loggable { 21 22 protected long id; 23 protected Timestamp time; 24 private long lastLsn; 25 26 /* For replication - master node which wrote this record. */ 27 int repMasterNodeId; 28 TxnEnd(long id, long lastLsn, int repMasterNodeId)29 TxnEnd(long id, long lastLsn, int repMasterNodeId) { 30 this.id = id; 31 time = new Timestamp(System.currentTimeMillis()); 32 this.lastLsn = lastLsn; 33 this.repMasterNodeId = repMasterNodeId; 34 } 35 36 /** 37 * For constructing from the log 38 */ TxnEnd()39 public TxnEnd() { 40 lastLsn = DbLsn.NULL_LSN; 41 } 42 43 /* 44 * Accessors. 45 */ getId()46 public long getId() { 47 return id; 48 } 49 getTime()50 public Timestamp getTime() { 51 return time; 52 } 53 getLastLsn()54 long getLastLsn() { 55 return lastLsn; 56 } 57 getMasterNodeId()58 public int getMasterNodeId() { 59 return repMasterNodeId; 60 } 61 getTagName()62 protected abstract String getTagName(); 63 64 /* 65 * Log support for writing. 66 */ 67 68 /** 69 * @see Loggable#getLogSize 70 */ getLogSize()71 public int getLogSize() { 72 return LogUtils.getPackedLongLogSize(id) + 73 LogUtils.getTimestampLogSize(time) + 74 LogUtils.getPackedLongLogSize(lastLsn) + 75 LogUtils.getPackedIntLogSize(repMasterNodeId); 76 77 } 78 79 /** 80 * @see Loggable#writeToLog 81 */ writeToLog(ByteBuffer logBuffer)82 public void writeToLog(ByteBuffer logBuffer) { 83 LogUtils.writePackedLong(logBuffer, id); 84 LogUtils.writeTimestamp(logBuffer, time); 85 LogUtils.writePackedLong(logBuffer, lastLsn); 86 LogUtils.writePackedInt(logBuffer, repMasterNodeId); 87 } 88 89 /** 90 * @see Loggable#readFromLog 91 */ readFromLog(ByteBuffer logBuffer, int entryVersion)92 public void readFromLog(ByteBuffer logBuffer, int entryVersion) { 93 94 /* The versions < 6 are unpacked. */ 95 boolean isUnpacked = (entryVersion < 6); 96 id = LogUtils.readLong(logBuffer, isUnpacked); 97 time = LogUtils.readTimestamp(logBuffer, isUnpacked); 98 lastLsn = LogUtils.readLong(logBuffer, isUnpacked); 99 100 if (entryVersion >= 6) { 101 repMasterNodeId = LogUtils.readInt(logBuffer, 102 false /* unpacked */); 103 } 104 } 105 106 /** 107 * @see Loggable#dumpLog 108 */ dumpLog(StringBuilder sb, boolean verbose)109 public void dumpLog(StringBuilder sb, boolean verbose) { 110 sb.append("<").append(getTagName()); 111 sb.append(" id=\"").append(id); 112 sb.append("\" time=\"").append(time); 113 sb.append("\" master=\"").append(repMasterNodeId); 114 sb.append("\">"); 115 sb.append(DbLsn.toString(lastLsn)); 116 sb.append("</").append(getTagName()).append(">"); 117 } 118 119 /** 120 * @see Loggable#getTransactionId 121 */ getTransactionId()122 public long getTransactionId() { 123 return id; 124 } 125 } 126