KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > log > LNFileReader


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: LNFileReader.java,v 1.58 2006/10/30 21:14:20 bostic Exp $
7  */

8
9 package com.sleepycat.je.log;
10
11 import java.io.IOException JavaDoc;
12 import java.nio.ByteBuffer JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import javax.transaction.xa.Xid JavaDoc;
17
18 import com.sleepycat.je.DatabaseException;
19 import com.sleepycat.je.dbi.DatabaseId;
20 import com.sleepycat.je.dbi.EnvironmentImpl;
21 import com.sleepycat.je.log.entry.LNLogEntry;
22 import com.sleepycat.je.log.entry.LogEntry;
23 import com.sleepycat.je.tree.LN;
24 import com.sleepycat.je.txn.TxnAbort;
25 import com.sleepycat.je.txn.TxnCommit;
26 import com.sleepycat.je.txn.TxnPrepare;
27
28 /**
29  * LNFileReader scans log files for LNs. Also, if it's going backwards for the
30  * undo phase in recovery, it reads transaction commit entries.
31  */

32 public class LNFileReader extends FileReader {
33
34     /*
35      * targetEntryMap maps DbLogEntryTypes to log entries. We use this
36      * collection to find the right LogEntry instance to read in the current
37      * entry.
38      */

39     protected Map JavaDoc targetEntryMap;
40     protected LogEntry targetLogEntry;
41         
42     /**
43      * Create this reader to start at a given LSN.
44      * @param env The relevant EnvironmentImpl
45      * @param readBufferSize buffer size in bytes for reading in log
46      * @param startLsn where to start in the log
47      * @param redo If false, we're going to go forward from
48      * the start LSN to the end of the log. If true, we're going
49      * backwards from the end of the log to the start LSN.
50      * @param endOfFileLsn the virtual LSN that marks the end of the log. (The
51      * one off the end of the log). Only used if we're reading backwards.
52      * Different from the startLsn because the startLsn tells us where the
53      * beginning of the start entry is, but not the length/end of the start
54      * entry. May be null if we're going foward.
55      * @param finishLsn the last LSN to read in the log. May be null if we
56      * want to read to the end of the log.
57      */

58     public LNFileReader(EnvironmentImpl env,
59                         int readBufferSize,
60                         long startLsn,
61                         boolean redo,
62                         long endOfFileLsn,
63                         long finishLsn,
64             Long JavaDoc singleFileNum)
65         throws IOException JavaDoc, DatabaseException {
66
67         super(env, readBufferSize, redo, startLsn,
68               singleFileNum, endOfFileLsn, finishLsn);
69
70         targetEntryMap = new HashMap JavaDoc();
71     }
72
73     public void addTargetType(LogEntryType entryType)
74         throws DatabaseException {
75
76         targetEntryMap.put(entryType, entryType.getNewLogEntry());
77     }
78
79     /**
80      * @return true if this is a transactional LN or Locker Commit entry.
81      */

82     protected boolean isTargetEntry(byte entryTypeNum,
83                                     byte entryTypeVersion) {
84
85         if (LogEntryType.isProvisional(entryTypeVersion)) {
86             /* Skip provisionial entries */
87             targetLogEntry = null;
88         } else {
89             LogEntryType fromLogType =
90                 new LogEntryType(entryTypeNum, entryTypeVersion);
91                                                             
92             /* Is it a target entry? */
93             targetLogEntry = (LogEntry) targetEntryMap.get(fromLogType);
94         }
95         return (targetLogEntry != null);
96     }
97
98     /**
99      * This reader instantiates an LN and key for every LN entry.
100      */

101     protected boolean processEntry(ByteBuffer JavaDoc entryBuffer)
102         throws DatabaseException {
103
104         targetLogEntry.readEntry(entryBuffer, currentEntrySize,
105                                  currentEntryTypeVersion, true);
106         return true;
107     }
108
109     /**
110      * @return true if the last entry was an LN.
111      */

112     public boolean isLN() {
113         return (targetLogEntry instanceof LNLogEntry);
114     }
115
116     /**
117      * Get the last LN seen by the reader.
118      */

119     public LN getLN() {
120         return ((LNLogEntry) targetLogEntry).getLN();
121     }
122
123     /**
124      * Get the last databaseId seen by the reader.
125      */

126     public DatabaseId getDatabaseId() {
127         return ((LNLogEntry) targetLogEntry).getDbId();
128     }
129
130     /**
131      * Get the last key seen by the reader.
132      */

133     public byte[] getKey() {
134         return ((LNLogEntry) targetLogEntry).getKey();
135     }
136
137     /**
138      * Get the last key seen by the reader.
139      */

140     public byte[] getDupTreeKey() {
141         return ((LNLogEntry) targetLogEntry).getDupKey();
142     }
143
144     /**
145      * @return the transaction id of the current entry.
146      */

147     public Long JavaDoc getTxnId() {
148         return ((LNLogEntry) targetLogEntry).getTxnId();
149     }
150
151     /*
152      * @return true if the last entry was a TxnPrepare record.
153      */

154     public boolean isPrepare() {
155     return (targetLogEntry.getMainItem() instanceof TxnPrepare);
156     }
157
158     /**
159      * Get the last txn prepare id seen by the reader.
160      */

161     public long getTxnPrepareId() {
162         return ((TxnPrepare) targetLogEntry.getMainItem()).getId();
163     }
164
165     /**
166      * Get the last txn prepare Xid seen by the reader.
167      */

168     public Xid JavaDoc getTxnPrepareXid() {
169         return ((TxnPrepare) targetLogEntry.getMainItem()).getXid();
170     }
171
172     /*
173      * @return true if the last entry was a TxnAbort record.
174      */

175     public boolean isAbort() {
176     return (targetLogEntry.getMainItem() instanceof TxnAbort);
177     }
178
179     /**
180      * Get the last txn abort id seen by the reader.
181      */

182     public long getTxnAbortId() {
183         return ((TxnAbort) targetLogEntry.getMainItem()).getId();
184     }
185
186     /**
187      * Get the last txn commit id seen by the reader.
188      */

189     public long getTxnCommitId() {
190         return ((TxnCommit) targetLogEntry.getMainItem()).getId();
191     }
192
193     /**
194      * Get node id of current LN.
195      */

196     public long getNodeId() {
197         return ((LNLogEntry) targetLogEntry).getLN().getNodeId();
198     }
199
200     /**
201      * Get last abort LSN seen by the reader (may be null).
202      */

203     public long getAbortLsn() {
204         return ((LNLogEntry) targetLogEntry).getAbortLsn();
205     }
206
207     /**
208      * Get last abort known deleted seen by the reader.
209      */

210     public boolean getAbortKnownDeleted() {
211         return ((LNLogEntry) targetLogEntry).getAbortKnownDeleted();
212     }
213 }
214
Popular Tags