KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > recovery > stepwise > EntryTrackerReader


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

8
9 package com.sleepycat.je.recovery.stepwise;
10
11 import java.io.IOException JavaDoc;
12 import java.nio.ByteBuffer JavaDoc;
13 import java.util.List JavaDoc;
14
15 import com.sleepycat.bind.tuple.IntegerBinding;
16 import com.sleepycat.je.DatabaseEntry;
17 import com.sleepycat.je.DatabaseException;
18 import com.sleepycat.je.dbi.EnvironmentImpl;
19 import com.sleepycat.je.log.FileReader;
20 import com.sleepycat.je.log.LogEntryType;
21 import com.sleepycat.je.log.entry.DeletedDupLNLogEntry;
22 import com.sleepycat.je.log.entry.LNLogEntry;
23 import com.sleepycat.je.log.entry.LogEntry;
24 import com.sleepycat.je.log.entry.SingleItemLogEntry;
25 import com.sleepycat.je.txn.TxnCommit;
26 import com.sleepycat.je.utilint.DbLsn;
27
28 /**
29  * EntryTrackerReader collects a list of EntryInfo describing all log entries
30  * in the truncated portion of a log. It lets the test know where to do a log
31  * truncation and remembers whether an inserted or deleted record was seen, in
32  * order to update the test's set of expected records.
33  */

34 public class EntryTrackerReader extends FileReader {
35
36     /*
37      * EntryInfo is a list that corresponds to each entry in the truncated
38      * area of the log.
39      */

40     private List JavaDoc entryInfo;
41     private DatabaseEntry dbt = new DatabaseEntry();
42     private LogEntry useLogEntry;
43     private boolean isCommit;
44
45     /**
46      * Create this reader to start at a given LSN.
47      */

48     public EntryTrackerReader(EnvironmentImpl env,
49                               long startLsn,
50                               List JavaDoc entryInfo) // EntryInfo
51
throws IOException JavaDoc, DatabaseException {
52
53         super(env, 2000, true, startLsn, null,
54           -1, DbLsn.NULL_LSN);
55
56         this.entryInfo = entryInfo;
57     }
58
59     /**
60      * @return true if this is a targeted entry that should be processed.
61      */

62     protected boolean isTargetEntry(byte logEntryTypeNumber,
63                                     byte logEntryTypeVersion) {
64         isCommit = false;
65         
66         if (LogEntryType.LOG_LN.equalsType(logEntryTypeNumber)) {
67             useLogEntry = LogEntryType.LOG_LN.getSharedLogEntry();
68             return true;
69         } else if (LogEntryType.LOG_LN_TRANSACTIONAL.equalsType(
70                                                         logEntryTypeNumber)) {
71             useLogEntry =
72                 LogEntryType.LOG_LN_TRANSACTIONAL.getSharedLogEntry();
73             return true;
74         } else if (LogEntryType.LOG_DEL_DUPLN.equalsType(logEntryTypeNumber)) {
75             useLogEntry = LogEntryType.LOG_DEL_DUPLN.getSharedLogEntry();
76             return true;
77         } else if (LogEntryType.LOG_DEL_DUPLN_TRANSACTIONAL.equalsType(
78                                                      logEntryTypeNumber)) {
79             useLogEntry =
80                 LogEntryType.LOG_DEL_DUPLN_TRANSACTIONAL.getSharedLogEntry();
81             return true;
82         } else if (LogEntryType.LOG_TXN_COMMIT.equalsType(logEntryTypeNumber)) {
83             useLogEntry = LogEntryType.LOG_TXN_COMMIT.getSharedLogEntry();
84             isCommit = true;
85             return true;
86         } else {
87             /*
88              * Just make note, no need to process the entry, nothing to record
89              * besides the LSN. Note that the offset has not been bumped by
90              * the FileReader, so use nextEntryOffset.
91              */

92             entryInfo.add(new LogEntryInfo(DbLsn.makeLsn(readBufferFileNum,
93                                                          nextEntryOffset),
94                                            0, 0));
95             return false;
96         }
97     }
98
99
100     /**
101      * This log entry has data which affects the expected set of records.
102      * We need to save each lsn and determine whether the value of the
103      * log entry should affect the expected set of records. For
104      * non-transactional entries, the expected set is affected right away.
105      * For transactional entries, we defer updates of the expected set until
106      * a commit is seen.
107      */

108     protected boolean processEntry(ByteBuffer JavaDoc entryBuffer)
109         throws DatabaseException {
110
111         /*
112          * Note that the offset has been bumped, so use currentEntryOffset
113          * for the LSN.
114          */

115         long lsn = DbLsn.makeLsn(readBufferFileNum, currentEntryOffset);
116         useLogEntry.readEntry(entryBuffer, currentEntrySize,
117                               currentEntryTypeVersion, true);
118
119         boolean isTxnal = useLogEntry.isTransactional();
120         long txnId = useLogEntry.getTransactionId();
121
122         if (isCommit) {
123             SingleItemLogEntry singleEntry = (SingleItemLogEntry) useLogEntry;
124             /*
125              * The txn id in a single item log entry is embedded within
126              * the item.
127              */

128             txnId = ((TxnCommit) singleEntry.getMainItem()).getId();
129             entryInfo.add(new CommitEntry(lsn, txnId));
130         } else if (useLogEntry instanceof DeletedDupLNLogEntry) {
131
132             /* This log entry is a deleted dup LN. */
133             DeletedDupLNLogEntry delDupLogEntry =
134                 (DeletedDupLNLogEntry) useLogEntry;
135             dbt.setData(delDupLogEntry.getKey());
136             int keyValue = IntegerBinding.entryToInt(dbt);
137             dbt.setData(delDupLogEntry.getDupKey());
138             int dataValue = IntegerBinding.entryToInt(dbt);
139
140             if (isTxnal) {
141                 entryInfo.add(new TxnalDeletedEntry(lsn, keyValue,
142                                                     dataValue, txnId));
143             } else {
144                 entryInfo.add(new NonTxnalDeletedEntry(lsn, keyValue,
145                                                        dataValue));
146             }
147         } else {
148             LNLogEntry lnLogEntry = (LNLogEntry) useLogEntry;
149             byte [] keyArray = lnLogEntry.getKey();
150             dbt.setData(keyArray);
151             int keyValue = IntegerBinding.entryToInt(dbt);
152             byte [] dataArray = lnLogEntry.getLN().getData();
153
154             if (dataArray == null) {
155                 /* This log entry is a deleted, non-dup LN. */
156                 if (isTxnal) {
157                     entryInfo.add(new TxnalDeletedEntry(lsn, keyValue, -1,
158                                                         txnId));
159                 } else {
160                     entryInfo.add(new NonTxnalDeletedEntry(lsn, keyValue, -1));
161                 }
162             } else {
163                 /* This log entry is new LN. */
164                 dbt.setData(dataArray);
165                 int dataValue = IntegerBinding.entryToInt(dbt);
166                 if (isTxnal) {
167                     entryInfo.add(new TxnalEntry(lsn, keyValue, dataValue,
168                                                  txnId));
169                 } else {
170                     entryInfo.add(new NonTxnalEntry(lsn, keyValue, dataValue));
171                 }
172             }
173         }
174
175         return true;
176     }
177 }
178
Popular Tags