001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: LNFileReader.java,v 1.59.2.5 2008/01/07 15:14:13 cwl Exp $
007: */
008:
009: package com.sleepycat.je.log;
010:
011: import java.io.IOException;
012: import java.nio.ByteBuffer;
013: import java.util.HashMap;
014: import java.util.Map;
015:
016: import javax.transaction.xa.Xid;
017:
018: import com.sleepycat.je.DatabaseException;
019: import com.sleepycat.je.dbi.DatabaseId;
020: import com.sleepycat.je.dbi.EnvironmentImpl;
021: import com.sleepycat.je.log.entry.LNLogEntry;
022: import com.sleepycat.je.log.entry.LogEntry;
023: import com.sleepycat.je.tree.LN;
024: import com.sleepycat.je.txn.TxnAbort;
025: import com.sleepycat.je.txn.TxnCommit;
026: import com.sleepycat.je.txn.TxnPrepare;
027:
028: /**
029: * LNFileReader scans log files for LNs. Also, if it's going backwards for the
030: * undo phase in recovery, it reads transaction commit entries.
031: */
032: public class LNFileReader extends FileReader {
033:
034: /*
035: * targetEntryMap maps DbLogEntryTypes to log entries. We use this
036: * collection to find the right LogEntry instance to read in the current
037: * entry.
038: */
039: protected Map targetEntryMap;
040: protected LogEntry targetLogEntry;
041:
042: /**
043: * Create this reader to start at a given LSN.
044: * @param env The relevant EnvironmentImpl
045: * @param readBufferSize buffer size in bytes for reading in log
046: * @param startLsn where to start in the log
047: * @param redo If true, we're going to go forward from
048: * the start LSN to the end of the log. If false, we're going
049: * backwards from the end of the log to the start LSN.
050: * @param finishLsn the last LSN to read in the log. May be null if we
051: * want to read to the end of the log.
052: * @param endOfFileLsn the virtual LSN that marks the end of the log. (The
053: * one off the end of the log). Only used if we're reading backwards.
054: * Different from the startLsn because the startLsn tells us where the
055: * beginning of the start entry is, but not the length/end of the start
056: * entry. May be null if we're going foward.
057: */
058: public LNFileReader(EnvironmentImpl env, int readBufferSize,
059: long startLsn, boolean redo, long endOfFileLsn,
060: long finishLsn, Long singleFileNum) throws IOException,
061: DatabaseException {
062:
063: super (env, readBufferSize, redo, startLsn, singleFileNum,
064: endOfFileLsn, finishLsn);
065:
066: targetEntryMap = new HashMap();
067: }
068:
069: public void addTargetType(LogEntryType entryType)
070: throws DatabaseException {
071:
072: targetEntryMap.put(entryType, entryType.getNewLogEntry());
073: }
074:
075: /**
076: * @return true if this is a transactional LN or Locker Commit entry.
077: */
078: protected boolean isTargetEntry(byte entryTypeNum,
079: byte entryTypeVersion) {
080:
081: if (LogEntryType.isEntryProvisional(entryTypeVersion)) {
082: /* Skip provisionial entries */
083: targetLogEntry = null;
084: } else {
085: LogEntryType fromLogType = new LogEntryType(entryTypeNum,
086: entryTypeVersion);
087:
088: /* Is it a target entry? */
089: targetLogEntry = (LogEntry) targetEntryMap.get(fromLogType);
090: }
091: return (targetLogEntry != null);
092: }
093:
094: /**
095: * This reader instantiates an LN and key for every LN entry.
096: */
097: protected boolean processEntry(ByteBuffer entryBuffer)
098: throws DatabaseException {
099:
100: readEntry(targetLogEntry, entryBuffer, true); // readFullItem
101: return true;
102: }
103:
104: /**
105: * @return true if the last entry was an LN.
106: */
107: public boolean isLN() {
108: return (targetLogEntry instanceof LNLogEntry);
109: }
110:
111: /**
112: * Get the last LN seen by the reader.
113: */
114: public LN getLN() {
115: return ((LNLogEntry) targetLogEntry).getLN();
116: }
117:
118: /**
119: * Get the last databaseId seen by the reader.
120: */
121: public DatabaseId getDatabaseId() {
122: return ((LNLogEntry) targetLogEntry).getDbId();
123: }
124:
125: /**
126: * Get the last key seen by the reader.
127: */
128: public byte[] getKey() {
129: return ((LNLogEntry) targetLogEntry).getKey();
130: }
131:
132: /**
133: * Get the last key seen by the reader.
134: */
135: public byte[] getDupTreeKey() {
136: return ((LNLogEntry) targetLogEntry).getDupKey();
137: }
138:
139: /**
140: * @return the transaction id of the current entry.
141: */
142: public Long getTxnId() {
143: return ((LNLogEntry) targetLogEntry).getTxnId();
144: }
145:
146: /*
147: * @return true if the last entry was a TxnPrepare record.
148: */
149: public boolean isPrepare() {
150: return (targetLogEntry.getMainItem() instanceof TxnPrepare);
151: }
152:
153: /**
154: * Get the last txn prepare id seen by the reader.
155: */
156: public long getTxnPrepareId() {
157: return ((TxnPrepare) targetLogEntry.getMainItem()).getId();
158: }
159:
160: /**
161: * Get the last txn prepare Xid seen by the reader.
162: */
163: public Xid getTxnPrepareXid() {
164: return ((TxnPrepare) targetLogEntry.getMainItem()).getXid();
165: }
166:
167: /*
168: * @return true if the last entry was a TxnAbort record.
169: */
170: public boolean isAbort() {
171: return (targetLogEntry.getMainItem() instanceof TxnAbort);
172: }
173:
174: /**
175: * Get the last txn abort id seen by the reader.
176: */
177: public long getTxnAbortId() {
178: return ((TxnAbort) targetLogEntry.getMainItem()).getId();
179: }
180:
181: /**
182: * Get the last txn commit id seen by the reader.
183: */
184: public long getTxnCommitId() {
185: return ((TxnCommit) targetLogEntry.getMainItem()).getId();
186: }
187:
188: /**
189: * Get node id of current LN.
190: */
191: public long getNodeId() {
192: return ((LNLogEntry) targetLogEntry).getLN().getNodeId();
193: }
194:
195: /**
196: * Get last abort LSN seen by the reader (may be null).
197: */
198: public long getAbortLsn() {
199: return ((LNLogEntry) targetLogEntry).getAbortLsn();
200: }
201:
202: /**
203: * Get last abort known deleted seen by the reader.
204: */
205: public boolean getAbortKnownDeleted() {
206: return ((LNLogEntry) targetLogEntry).getAbortKnownDeleted();
207: }
208: }
|