KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: CleanerFileReader.java,v 1.33 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 com.sleepycat.je.DatabaseException;
17 import com.sleepycat.je.dbi.DatabaseId;
18 import com.sleepycat.je.dbi.EnvironmentImpl;
19 import com.sleepycat.je.log.entry.INLogEntry;
20 import com.sleepycat.je.log.entry.LNLogEntry;
21 import com.sleepycat.je.log.entry.LogEntry;
22 import com.sleepycat.je.tree.IN;
23 import com.sleepycat.je.tree.LN;
24 import com.sleepycat.je.utilint.DbLsn;
25
26 /**
27  * CleanerFileReader scans log files for INs and LNs.
28  */

29 public class CleanerFileReader extends FileReader {
30     private static final byte IS_IN = 0;
31     private static final byte IS_LN = 1;
32     private static final byte IS_ROOT = 2;
33     private static final byte IS_FILEHEADER = 3;
34
35     private Map JavaDoc targetEntryMap;
36     private LogEntry targetLogEntry;
37     private byte targetCategory;
38
39     /**
40      * Create this reader to start at a given LSN.
41      * @param env The relevant EnvironmentImpl.
42      * @param readBufferSize buffer size in bytes for reading in log.
43      * @param startLsn where to start in the log, or null for the beginning.
44      * @param fileNum single file number.
45      */

46     public CleanerFileReader(EnvironmentImpl env,
47                              int readBufferSize,
48                              long startLsn,
49                              Long JavaDoc fileNum)
50         throws IOException JavaDoc, DatabaseException {
51
52         super(env,
53               readBufferSize,
54               true, // forward
55
startLsn,
56               fileNum, // single file number
57
DbLsn.NULL_LSN, // endOfFileLsn
58
DbLsn.NULL_LSN); // finishLsn
59

60         targetEntryMap = new HashMap JavaDoc();
61
62         addTargetType(IS_LN, LogEntryType.LOG_LN_TRANSACTIONAL);
63         addTargetType(IS_LN, LogEntryType.LOG_LN);
64         addTargetType(IS_LN, LogEntryType.LOG_NAMELN_TRANSACTIONAL);
65         addTargetType(IS_LN, LogEntryType.LOG_NAMELN);
66         addTargetType(IS_LN, LogEntryType.LOG_MAPLN_TRANSACTIONAL);
67         addTargetType(IS_LN, LogEntryType.LOG_MAPLN);
68         addTargetType(IS_LN, LogEntryType.LOG_DEL_DUPLN_TRANSACTIONAL);
69         addTargetType(IS_LN, LogEntryType.LOG_DEL_DUPLN);
70         addTargetType(IS_LN, LogEntryType.LOG_DUPCOUNTLN_TRANSACTIONAL);
71         addTargetType(IS_LN, LogEntryType.LOG_DUPCOUNTLN);
72         addTargetType(IS_LN, LogEntryType.LOG_FILESUMMARYLN);
73         addTargetType(IS_IN, LogEntryType.LOG_IN);
74         addTargetType(IS_IN, LogEntryType.LOG_BIN);
75         addTargetType(IS_IN, LogEntryType.LOG_DIN);
76         addTargetType(IS_IN, LogEntryType.LOG_DBIN);
77         addTargetType(IS_ROOT, LogEntryType.LOG_ROOT);
78         addTargetType(IS_FILEHEADER, LogEntryType.LOG_FILE_HEADER);
79     }
80
81     private void addTargetType(byte category, LogEntryType entryType)
82         throws DatabaseException {
83
84         targetEntryMap.put(entryType,
85                            new EntryInfo(entryType.getNewLogEntry(),
86                      category));
87     }
88
89     /**
90      * Helper for determining the starting position and opening
91      * up a file at the desired location.
92      */

93     protected void initStartingPosition(long endOfFileLsn,
94                                         Long JavaDoc fileNum)
95         throws IOException JavaDoc, DatabaseException {
96
97         eof = false;
98
99         /*
100          * Start off at the startLsn. If that's null, start at the
101          * beginning of the log. If there are no log files, set
102          * eof.
103          */

104         readBufferFileNum = fileNum.longValue();
105         readBufferFileEnd = 0;
106
107         /*
108          * After we read the first entry, the currentEntry will
109          * point here.
110          */

111         nextEntryOffset = readBufferFileEnd;
112     }
113
114     /**
115      * @return true if this is a type we're interested in.
116      */

117     protected boolean isTargetEntry(byte entryTypeNum,
118                                     byte entryTypeVersion) {
119
120         LogEntryType fromLogType = new LogEntryType(entryTypeNum,
121                             entryTypeVersion);
122                                                             
123         /* Is it a target entry? */
124         EntryInfo info = (EntryInfo) targetEntryMap.get(fromLogType);
125         if (info == null) {
126             return false;
127         } else {
128             targetCategory = info.targetCategory;
129             targetLogEntry = info.targetLogEntry;
130             return true;
131         }
132     }
133
134     /**
135      * This reader instantiates an LN and key for every LN entry.
136      */

137     protected boolean processEntry(ByteBuffer JavaDoc entryBuffer)
138         throws DatabaseException {
139
140         targetLogEntry.readEntry(entryBuffer, currentEntrySize,
141                                  currentEntryTypeVersion, true);
142         return true;
143     }
144
145     /**
146      * @return true if the last entry was an IN.
147      */

148     public boolean isIN() {
149         return (targetCategory == IS_IN);
150     }
151
152     /**
153      * @return true if the last entry was a LN.
154      */

155     public boolean isLN() {
156         return (targetCategory == IS_LN);
157     }
158
159     /**
160      * @return true if the last entry was a root
161      */

162     public boolean isRoot() {
163         return (targetCategory == IS_ROOT);
164     }
165
166     public boolean isFileHeader() {
167         return (targetCategory == IS_FILEHEADER);
168     }
169
170     /**
171      * Get the last LN seen by the reader.
172      */

173     public LN getLN() {
174         return ((LNLogEntry) targetLogEntry).getLN();
175     }
176
177     /**
178      * Get the last entry seen by the reader as an IN.
179      */

180     public IN getIN()
181         throws DatabaseException {
182
183         return ((INLogEntry) targetLogEntry).getIN(env);
184     }
185
186     public FileHeader getFileHeader()
187         throws DatabaseException {
188
189         return (FileHeader) (targetLogEntry.getMainItem());
190     }
191
192     /**
193      * Get the last databaseId seen by the reader.
194      */

195     public DatabaseId getDatabaseId() {
196         if (targetCategory == IS_LN) {
197             return ((LNLogEntry) targetLogEntry).getDbId();
198         } else if (targetCategory == IS_IN) {
199             return ((INLogEntry) targetLogEntry).getDbId();
200         } else {
201         return null;
202         }
203     }
204
205     /**
206      * Get the last key seen by the reader.
207      */

208     public byte[] getKey() {
209         return ((LNLogEntry) targetLogEntry).getKey();
210     }
211
212     /**
213      * Get the last key seen by the reader.
214      */

215     public byte[] getDupTreeKey() {
216         return ((LNLogEntry) targetLogEntry).getDupKey();
217     }
218
219     private static class EntryInfo {
220         public LogEntry targetLogEntry;
221         public byte targetCategory;
222
223         EntryInfo(LogEntry targetLogEntry, byte targetCategory) {
224             this.targetLogEntry = targetLogEntry;
225             this.targetCategory = targetCategory;
226         }
227     }
228 }
229
Popular Tags